69
69
(defvar company-backends)
70
70
(defvar yas-inhibit-overlay-modification-protection)
71
71
(defvar yas-indent-line)
72
+ (defvar yas-also-auto-indent-first-line)
72
73
(defvar dap-auto-configure-mode)
73
74
(defvar dap-ui-menu-items)
74
75
@@ -622,6 +623,7 @@ If this is set to nil, `eldoc' will show only the symbol information."
622
623
:type 'boolean
623
624
:group 'lsp-mode)
624
625
626
+
625
627
(defcustom lsp-before-save-edits t
626
628
"If non-nil, `lsp-mode' will apply edits suggested by the language server before saving a document."
627
629
:type 'boolean
@@ -3470,10 +3472,14 @@ in that particular folder."
3470
3472
(lsp-managed-mode 1)
3471
3473
3472
3474
(run-hooks 'lsp-after-open-hook)
3473
- (-some-> lsp--cur-workspace
3475
+ (-some->> lsp--cur-workspace
3474
3476
(lsp--workspace-client)
3475
- (lsp--client-after-open-fn)
3476
- (funcall)))
3477
+ (funcall (lambda (client)
3478
+ (-some-> (lsp--client-after-open-fn client)
3479
+ (funcall))
3480
+ (-some-> (format "lsp-%s-after-open-hook" (lsp--client-server-id client))
3481
+ (intern-soft)
3482
+ (run-hooks))))))
3477
3483
3478
3484
(defun lsp--text-document-identifier ()
3479
3485
"Make TextDocumentIdentifier."
@@ -3676,16 +3682,42 @@ The method uses `replace-buffer-contents'."
3676
3682
beg (+ beg (length new-text))
3677
3683
length)))))))))
3678
3684
3679
- (defun lsp--indent-snippets? ()
3680
- "Enable indenting of snippets for everything but `org-mode'.
3685
+ (defun lsp--to-yasnippet-snippet (snippet)
3686
+ "Convert LSP SNIPPET to yasnippet snippet."
3687
+ ;; LSP snippet doesn't escape "{", but yasnippet requires escaping it.
3688
+ (replace-regexp-in-string (rx (or bos (not (any "$" "\\"))) (group "{"))
3689
+ (rx "\\" (backref 1))
3690
+ snippet
3691
+ nil nil 1))
3681
3692
3682
- Indending snippets is extremely slow in `org-mode' buffers since
3683
- it has to calculate indentation based on SRC block position."
3684
- (unless (derived-mode-p 'org-mode)
3685
- 'auto))
3693
+ (defvar-local lsp-enable-relative-indentation nil
3694
+ "Enable relative indentation when insert texts, snippets ... from language server.")
3695
+
3696
+ (defun lsp--expand-snippet (snippet &optional start end expand-env keep-whitespace)
3697
+ "Wrapper of `yas-expand-snippet' with all of it arguments.
3698
+ The snippet will be convert to LSP style and indent according to
3699
+ LSP server result."
3700
+ (let* ((inhibit-field-text-motion t)
3701
+ (offset (save-excursion
3702
+ (goto-char start)
3703
+ (back-to-indentation)
3704
+ (buffer-substring-no-properties
3705
+ (line-beginning-position)
3706
+ (point))))
3707
+ (yas-indent-line (unless keep-whitespace 'auto))
3708
+ (yas-also-auto-indent-first-line nil)
3709
+ (indent-line-function (if (or lsp-enable-relative-indentation
3710
+ (derived-mode-p 'org-mode))
3711
+ (lambda () (save-excursion
3712
+ (forward-line 0)
3713
+ (insert offset)))
3714
+ indent-line-function)))
3715
+ (yas-expand-snippet
3716
+ (lsp--to-yasnippet-snippet snippet)
3717
+ start end expand-env)))
3686
3718
3687
3719
(defun lsp--apply-text-edits (edits)
3688
- "Apply the edits described in the TextEdit[] object."
3720
+ "Apply the EDITS described in the TextEdit[] object."
3689
3721
(unless (seq-empty-p edits)
3690
3722
(atomic-change-group
3691
3723
(run-hooks 'lsp-before-apply-edits-hook)
@@ -3712,9 +3744,7 @@ it has to calculate indentation based on SRC block position."
3712
3744
(-when-let ((&SnippetTextEdit :range (&RangeToPoint :start)
3713
3745
:insert-text-format? :new-text) edit)
3714
3746
(when (eq insert-text-format? lsp/insert-text-format-snippet)
3715
- (let ((yas-indent-line (lsp--indent-snippets?)))
3716
- (yas-expand-snippet (lsp--to-yasnippet-snippet new-text)
3717
- start (+ start (length new-text))))))))))
3747
+ (lsp--expand-snippet new-text start (+ start (length new-text)))))))))
3718
3748
(undo-amalgamate-change-group change-group)
3719
3749
(progress-reporter-done reporter))))))
3720
3750
@@ -4190,14 +4220,6 @@ and the position respectively."
4190
4220
4191
4221
(defalias 'lsp--cur-line-diagnotics 'lsp-cur-line-diagnostics)
4192
4222
4193
- (defun lsp--to-yasnippet-snippet (text)
4194
- "Convert LSP snippet TEXT to yasnippet snippet."
4195
- ;; LSP snippet doesn't escape "{", but yasnippet requires escaping it.
4196
- (replace-regexp-in-string (rx (or bos (not (any "$" "\\"))) (group "{"))
4197
- (rx "\\" (backref 1))
4198
- text
4199
- nil nil 1))
4200
-
4201
4223
(defun lsp--extract-line-from-buffer (pos)
4202
4224
"Return the line pointed to by POS (a Position object) in the current buffer."
4203
4225
(let* ((point (lsp--position-to-point pos))
@@ -6745,7 +6767,13 @@ remote machine and vice versa."
6745
6767
(seq-every-p (apply-partially #'symbolp)
6746
6768
(lsp--client-major-modes client))))
6747
6769
nil "Invalid activation-fn and/or major-modes.")
6748
- (puthash (lsp--client-server-id client) client lsp-clients))
6770
+ (let ((client-id (lsp--client-server-id client)))
6771
+ (puthash client-id client lsp-clients)
6772
+ (setplist (intern (format "lsp-%s-after-open-hook" client-id))
6773
+ `(standard-value (nil) custom-type hook
6774
+ custom-package-version (lsp-mode . "7.0.1")
6775
+ variable-documentation ,(format "Hooks to run after `%s' server is run." client-id)
6776
+ custom-requests nil))))
6749
6777
6750
6778
(defun lsp--create-initialization-options (_session client)
6751
6779
"Create initialization-options from SESSION and CLIENT.
0 commit comments