Skip to content

Commit 8c6ad26

Browse files
committed
expand-snippet: indent according to the line where snippet is expanded
1 parent 602934e commit 8c6ad26

File tree

2 files changed

+44
-28
lines changed

2 files changed

+44
-28
lines changed

lsp-mode.el

Lines changed: 43 additions & 27 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
@@ -3759,21 +3760,45 @@ The method uses `replace-buffer-contents'."
37593760
beg (+ beg (length new-text))
37603761
length)))))))))
37613762

3762-
(defun lsp--indent-snippets? ()
3763-
"Enable indenting of snippets for everything but `org-mode'.
3763+
(defun lsp--to-yasnippet-snippet (snippet)
3764+
"Convert LSP SNIPPET to yasnippet snippet."
3765+
;; LSP snippet doesn't escape "{", but yasnippet requires escaping it.
3766+
(replace-regexp-in-string (rx (or bos (not (any "$" "\\"))) (group "{"))
3767+
(rx "\\" (backref 1))
3768+
snippet
3769+
nil nil 1))
37643770

3765-
Indending snippets is extremely slow in `org-mode' buffers since
3766-
it has to calculate identation based on SRC block position."
3767-
(unless (derived-mode-p 'org-mode)
3768-
'auto))
3771+
(defun lsp--expand-snippet (snippet &optional start end expand-env keep-whitespace)
3772+
"Wrapper of `yas-expand-snippet' with all of it arguments.
3773+
The snippet will be convert to LSP style and indent according to
3774+
LSP server result."
3775+
(let* ((inhibit-field-text-motion t)
3776+
(offset (save-excursion
3777+
(goto-char start)
3778+
(back-to-indentation)
3779+
(buffer-substring-no-properties
3780+
(line-beginning-position)
3781+
(point))))
3782+
(yas-indent-line (unless keep-whitespace 'auto))
3783+
(yas-also-auto-indent-first-line nil)
3784+
(indent-line-function (if (or (and lsp-enable-indentation
3785+
(or (lsp-feature? "textDocument/rangeFormatting")
3786+
(lsp-feature? "textDocument/formatting")))
3787+
(derived-mode-p 'org-mode))
3788+
(lambda () (save-excursion
3789+
(forward-line 0)
3790+
(insert offset)))
3791+
indent-line-function)))
3792+
(yas-expand-snippet
3793+
(lsp--to-yasnippet-snippet snippet)
3794+
start end expand-env)))
37693795

37703796
(defun lsp--apply-text-edits (edits)
3771-
"Apply the edits described in the TextEdit[] object."
3797+
"Apply the EDITS described in the TextEdit[] object."
37723798
(unless (seq-empty-p edits)
37733799
(atomic-change-group
37743800
(run-hooks 'lsp-before-apply-edits-hook)
3775-
(let* ((change-group (when (functionp 'undo-amalgamate-change-group)
3776-
(prepare-change-group)))
3801+
(let* ((change-group (prepare-change-group))
37773802
(howmany (length edits))
37783803
(message (format "Applying %s edits to `%s' ..." howmany (current-buffer)))
37793804
(_ (lsp--info message))
@@ -3796,9 +3821,7 @@ it has to calculate identation based on SRC block position."
37963821
(-when-let ((&SnippetTextEdit :range (&RangeToPoint :start)
37973822
:insert-text-format? :new-text) edit)
37983823
(when (eq insert-text-format? lsp/insert-text-format-snippet)
3799-
(let ((yas-indent-line (lsp--indent-snippets?)))
3800-
(yas-expand-snippet (lsp--to-yasnippet-snippet new-text)
3801-
start (+ start (length new-text))))))))))
3824+
(lsp--expand-snippet new-text start (+ start (length new-text)))))))))
38023825
(undo-amalgamate-change-group change-group)
38033826
(progress-reporter-done reporter))))))
38043827

@@ -4574,7 +4597,8 @@ Others: TRIGGER-CHARS"
45744597
'lsp-completion-markers markers
45754598
'lsp-completion-prefix prefix)
45764599
(text-properties-at 0 candidate))
4577-
((&CompletionItem :label :insert-text? :text-edit? :insert-text-format? :additional-text-edits?)
4600+
((&CompletionItem :label :insert-text? :text-edit? :insert-text-format?
4601+
:additional-text-edits? :keep-whitespace?)
45784602
item))
45794603
(cond
45804604
(text-edit?
@@ -4587,12 +4611,12 @@ Others: TRIGGER-CHARS"
45874611
(delete-region start-point (point))
45884612
(insert (or insert-text? label))))
45894613

4590-
(when (eq insert-text-format? 2)
4591-
(let ((yas-indent-line (lsp--indent-snippets?)))
4592-
(yas-expand-snippet
4593-
(lsp--to-yasnippet-snippet (buffer-substring start-point (point)))
4594-
start-point
4595-
(point))))
4614+
(when (equal insert-text-format? lsp/insert-text-format-snippet)
4615+
(lsp--expand-snippet (buffer-substring start-point (point))
4616+
start-point
4617+
(point)
4618+
nil
4619+
keep-whitespace?))
45964620

45974621
(when lsp-completion-enable-additional-text-edit
45984622
(if (or (get-text-property 0 'lsp-completion-resolved candidate)
@@ -4616,14 +4640,6 @@ Others: TRIGGER-CHARS"
46164640
(setq this-command 'self-insert-command)))
46174641
(lsp--capf-clear-cache)))
46184642

4619-
(defun lsp--to-yasnippet-snippet (text)
4620-
"Convert LSP snippet TEXT to yasnippet snippet."
4621-
;; LSP snippet doesn't escape "{", but yasnippet requires escaping it.
4622-
(replace-regexp-in-string (rx (or bos (not (any "$" "\\"))) (group "{"))
4623-
(rx "\\" (backref 1))
4624-
text
4625-
nil nil 1))
4626-
46274643
(defun lsp--regex-fuzzy (str)
46284644
"Build a regex sequence from STR. Insert .* between each char."
46294645
(apply #'concat

lsp-protocol.el

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,7 @@ See `-let' for a description of the destructuring mechanism."
403403
(Command (:title :command) (:arguments))
404404
(CompletionCapabilities nil (:completionItem :completionItemKind :contextSupport :dynamicRegistration))
405405
(CompletionContext (:triggerKind) (:triggerCharacter))
406-
(CompletionItem (:label) (:additionalTextEdits :command :commitCharacters :data :deprecated :detail :documentation :filterText :insertText :insertTextFormat :kind :preselect :sortText :tags :textEdit :score))
406+
(CompletionItem (:label) (:additionalTextEdits :command :commitCharacters :data :deprecated :detail :documentation :filterText :insertText :insertTextFormat :kind :preselect :sortText :tags :textEdit :score :keepWhitespace))
407407
(CompletionItemCapabilities nil (:commitCharactersSupport :deprecatedSupport :documentationFormat :preselectSupport :snippetSupport :tagSupport))
408408
(CompletionItemKindCapabilities nil (:valueSet))
409409
(CompletionItemTagSupportCapabilities (:valueSet) nil)

0 commit comments

Comments
 (0)