Don't indent first and only line of expanded snippet

Unless yas-also-auto-indent-first-line applies.
* yasnippet.el (yas--indent): Check that forward-line successfully
moved 1 line forward before trying to indent.
* yasnippet-tests.el (yas-indent-first-line)
(yas-indent-first-line-fixed): New tests.
This commit is contained in:
Noam Postavsky 2018-03-08 07:40:46 -05:00
parent 15761e85d6
commit 5170f051ad
2 changed files with 41 additions and 14 deletions

View File

@ -447,6 +447,30 @@ end" (buffer-string)))
(yas-expand-snippet "def foo\n\nend") (yas-expand-snippet "def foo\n\nend")
(should (string= "def foo\n \nend" (buffer-string))))) (should (string= "def foo\n \nend" (buffer-string)))))
(ert-deftest yas-indent-first-line ()
(with-temp-buffer
(ruby-mode)
(yas-minor-mode 1)
(set (make-local-variable 'yas-indent-line) 'auto)
(set (make-local-variable 'yas-also-auto-indent-first-line) nil)
(set (make-local-variable 'yas-also-indent-empty-lines) nil)
(yas-expand-snippet "def foo\n$0\nend\n")
;; First (and only) line should not indent.
(yas-expand-snippet "#not indented")
(should (equal "def foo\n#not indented\nend\n" (buffer-string)))))
(ert-deftest yas-indent-first-line-fixed ()
(with-temp-buffer
(ruby-mode)
(yas-minor-mode 1)
(set (make-local-variable 'yas-indent-line) 'fixed)
(set (make-local-variable 'yas-also-auto-indent-first-line) nil)
(set (make-local-variable 'yas-also-indent-empty-lines) nil)
(yas-expand-snippet " def foo\n $0\n end\n")
;; First (and only) line should not indent.
(yas-expand-snippet "#not more indented")
(should (equal " def foo\n #not more indented\n end\n" (buffer-string)))))
(ert-deftest indentation-markers () (ert-deftest indentation-markers ()
"Test a snippet with indentation markers (`$<')." "Test a snippet with indentation markers (`$<')."
(with-temp-buffer (with-temp-buffer

View File

@ -4302,8 +4302,11 @@ The SNIPPET's markers are preserved."
(setq yas--indent-markers nil)) (setq yas--indent-markers nil))
;; Now do stuff for `fixed' and `auto'. ;; Now do stuff for `fixed' and `auto'.
(save-excursion (save-excursion
;; We need to be at end of line, so that `forward-line' will only
;; report 0 if it actually moves over a newline.
(end-of-line)
(cond ((eq yas-indent-line 'fixed) (cond ((eq yas-indent-line 'fixed)
(forward-line 1) (when (= (forward-line 1) 0)
(let ((indent-line-function (let ((indent-line-function
(lambda () (lambda ()
;; We need to be at beginning of line in order to ;; We need to be at beginning of line in order to
@ -4312,13 +4315,13 @@ The SNIPPET's markers are preserved."
(indent-to-column yas--indent-original-column)))) (indent-to-column yas--indent-original-column))))
(yas--indent-region (line-beginning-position) (yas--indent-region (line-beginning-position)
(point-max) (point-max)
snippet))) snippet))))
((eq yas-indent-line 'auto) ((eq yas-indent-line 'auto)
(unless yas-also-auto-indent-first-line (when (or yas-also-auto-indent-first-line
(forward-line 1)) (= (forward-line 1) 0))
(yas--indent-region (line-beginning-position) (yas--indent-region (line-beginning-position)
(point-max) (point-max)
snippet))))) snippet))))))
(defun yas--collect-snippet-markers (snippet) (defun yas--collect-snippet-markers (snippet)
"Make a list of all the markers used by SNIPPET." "Make a list of all the markers used by SNIPPET."