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
@@ -4208,6 +4209,35 @@ it has to calculate identation based on SRC block position."
4208
4209
(unless (derived-mode-p 'org-mode)
4209
4210
'auto))
4210
4211
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
+
4211
4241
(defun lsp--apply-text-edits (edits)
4212
4242
"Apply the edits described in the TextEdit[] object."
4213
4243
(unless (seq-empty-p edits)
@@ -4238,9 +4268,7 @@ it has to calculate identation based on SRC block position."
4238
4268
(-when-let ((&SnippetTextEdit :range (&RangeToPoint :start)
4239
4269
:insert-text-format? :new-text) edit)
4240
4270
(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)))))))))
4244
4272
(when (fboundp 'undo-amalgamate-change-group)
4245
4273
(with-no-warnings (undo-amalgamate-change-group change-group)))
4246
4274
(progress-reporter-done reporter))))))
@@ -5011,12 +5039,10 @@ Others: TRIGGER-CHARS"
5011
5039
(delete-region start-point (point))
5012
5040
(insert (or insert-text? label))))
5013
5041
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)))
5020
5046
5021
5047
(when (and lsp-completion-enable-additional-text-edit additional-text-edits?)
5022
5048
(lsp--apply-text-edits additional-text-edits?))
@@ -5031,14 +5057,6 @@ Others: TRIGGER-CHARS"
5031
5057
(setq this-command 'self-insert-command)))
5032
5058
(lsp--capf-clear-cache)))
5033
5059
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
-
5042
5060
(defun lsp--regex-fuzzy (str)
5043
5061
"Build a regex sequence from STR. Insert .* between each char."
5044
5062
(apply #'concat
0 commit comments