72
72
(defvar c-basic-offset)
73
73
(defvar yas-inhibit-overlay-modification-protection)
74
74
(defvar yas-indent-line)
75
+ (defvar yas-also-auto-indent-first-line)
75
76
(defvar dap-auto-configure-mode)
76
77
77
78
(defconst lsp--message-type-face
@@ -3759,21 +3760,45 @@ The method uses `replace-buffer-contents'."
3759
3760
beg (+ beg (length new-text))
3760
3761
length)))))))))
3761
3762
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))
3764
3770
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)))
3769
3795
3770
3796
(defun lsp--apply-text-edits (edits)
3771
- "Apply the edits described in the TextEdit[] object."
3797
+ "Apply the EDITS described in the TextEdit[] object."
3772
3798
(unless (seq-empty-p edits)
3773
3799
(atomic-change-group
3774
3800
(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))
3777
3802
(howmany (length edits))
3778
3803
(message (format "Applying %s edits to `%s' ..." howmany (current-buffer)))
3779
3804
(_ (lsp--info message))
@@ -3796,9 +3821,7 @@ it has to calculate identation based on SRC block position."
3796
3821
(-when-let ((&SnippetTextEdit :range (&RangeToPoint :start)
3797
3822
:insert-text-format? :new-text) edit)
3798
3823
(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)))))))))
3802
3825
(undo-amalgamate-change-group change-group)
3803
3826
(progress-reporter-done reporter))))))
3804
3827
@@ -4574,7 +4597,8 @@ Others: TRIGGER-CHARS"
4574
4597
'lsp-completion-markers markers
4575
4598
'lsp-completion-prefix prefix)
4576
4599
(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?)
4578
4602
item))
4579
4603
(cond
4580
4604
(text-edit?
@@ -4587,12 +4611,12 @@ Others: TRIGGER-CHARS"
4587
4611
(delete-region start-point (point))
4588
4612
(insert (or insert-text? label))))
4589
4613
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? ))
4596
4620
4597
4621
(when lsp-completion-enable-additional-text-edit
4598
4622
(if (or (get-text-property 0 'lsp-completion-resolved candidate)
@@ -4616,14 +4640,6 @@ Others: TRIGGER-CHARS"
4616
4640
(setq this-command 'self-insert-command)))
4617
4641
(lsp--capf-clear-cache)))
4618
4642
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
-
4627
4643
(defun lsp--regex-fuzzy (str)
4628
4644
"Build a regex sequence from STR. Insert .* between each char."
4629
4645
(apply #'concat
0 commit comments