Skip to content

Commit cf9ede9

Browse files
committed
expand-snippet: indent according to the line where snippet is expanded
1 parent 00fcee9 commit cf9ede9

File tree

1 file changed

+35
-17
lines changed

1 file changed

+35
-17
lines changed

lsp-mode.el

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
(defvar c-basic-offset)
7373
(defvar yas-inhibit-overlay-modification-protection)
7474
(defvar yas-indent-line)
75+
(defvar yas-also-auto-indent-first-line)
7576
(defvar dap-auto-configure-mode)
7677

7778
(defconst lsp--message-type-face
@@ -4208,6 +4209,35 @@ it has to calculate identation based on SRC block position."
42084209
(unless (derived-mode-p 'org-mode)
42094210
'auto))
42104211

4212+
4213+
(defun lsp--to-yasnippet-snippet (snippet)
4214+
"Convert LSP SNIPPET to yasnippet snippet."
4215+
;; LSP snippet doesn't escape "{", but yasnippet requires escaping it.
4216+
(replace-regexp-in-string (rx (or bos (not (any "$" "\\"))) (group "{"))
4217+
(rx "\\" (backref 1))
4218+
snippet
4219+
nil nil 1))
4220+
4221+
(defun lsp--expand-snippet (snippet &optional start end expand-env)
4222+
"Wrapper of `yas-expand-snippet' with all of it arguments.
4223+
The snippet will be convert to LSP style and indent according to
4224+
LSP server result."
4225+
(let* ((offset (save-excursion
4226+
(goto-char start)
4227+
(back-to-indentation)
4228+
(let ((inhibit-field-text-motion t))
4229+
(buffer-substring-no-properties
4230+
(line-beginning-position)
4231+
(point)))))
4232+
(yas-indent-line 'auto)
4233+
(yas-also-auto-indent-first-line nil)
4234+
(indent-line-function (lambda () (save-excursion
4235+
(forward-line 0)
4236+
(insert offset)))))
4237+
(yas-expand-snippet
4238+
(lsp--to-yasnippet-snippet snippet)
4239+
start end expand-env)))
4240+
42114241
(defun lsp--apply-text-edits (edits)
42124242
"Apply the edits described in the TextEdit[] object."
42134243
(unless (seq-empty-p edits)
@@ -4238,9 +4268,7 @@ it has to calculate identation based on SRC block position."
42384268
(-when-let ((&SnippetTextEdit :range (&RangeToPoint :start)
42394269
:insert-text-format? :new-text) edit)
42404270
(when (eq insert-text-format? lsp/insert-text-format-snippet)
4241-
(let ((yas-indent-line (lsp--indent-snippets?)))
4242-
(yas-expand-snippet (lsp--to-yasnippet-snippet new-text)
4243-
start (+ start (length new-text))))))))))
4271+
(lsp--expand-snippet new-text start (+ start (length new-text)))))))))
42444272
(when (fboundp 'undo-amalgamate-change-group)
42454273
(with-no-warnings (undo-amalgamate-change-group change-group)))
42464274
(progress-reporter-done reporter))))))
@@ -5011,12 +5039,10 @@ Others: TRIGGER-CHARS"
50115039
(delete-region start-point (point))
50125040
(insert (or insert-text? label))))
50135041

5014-
(when (eq insert-text-format? 2)
5015-
(let ((yas-indent-line (lsp--indent-snippets?)))
5016-
(yas-expand-snippet
5017-
(lsp--to-yasnippet-snippet (buffer-substring start-point (point)))
5018-
start-point
5019-
(point))))
5042+
(when (equal insert-text-format? lsp/insert-text-format-snippet)
5043+
(lsp--expand-snippet (buffer-substring start-point (point))
5044+
start-point
5045+
(point)))
50205046

50215047
(when (and lsp-completion-enable-additional-text-edit additional-text-edits?)
50225048
(lsp--apply-text-edits additional-text-edits?))
@@ -5031,14 +5057,6 @@ Others: TRIGGER-CHARS"
50315057
(setq this-command 'self-insert-command)))
50325058
(lsp--capf-clear-cache)))
50335059

5034-
(defun lsp--to-yasnippet-snippet (text)
5035-
"Convert LSP snippet TEXT to yasnippet snippet."
5036-
;; LSP snippet doesn't escape "{", but yasnippet requires escaping it.
5037-
(replace-regexp-in-string (rx (or bos (not (any "$" "\\"))) (group "{"))
5038-
(rx "\\" (backref 1))
5039-
text
5040-
nil nil 1))
5041-
50425060
(defun lsp--regex-fuzzy (str)
50435061
"Build a regex sequence from STR. Insert .* between each char."
50445062
(apply #'concat

0 commit comments

Comments
 (0)