Skip to content

Commit 1aa4740

Browse files
authored
fix: defer onTypeFormatting to after running other hooks (#4842)
If other functions on post-self-insert-hook (e.g. smart-parens, electric-indent) run after LSP's on-type-formatting, they may modify the buffer and therefore cancel the async reques and prevent on-type-formatting from working as expected. This commit updates the hook to run in a timer (after 0 seconds), so it lets all other functions perform their changes before asking the server for a formatting action.
1 parent 95cf850 commit 1aa4740

File tree

1 file changed

+25
-13
lines changed

1 file changed

+25
-13
lines changed

lsp-mode.el

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5036,26 +5036,38 @@ movements may have changed the position")
50365036
:group 'lsp-mode
50375037
:type 'boolean)
50385038

5039+
(defun lsp--on-type-formatting-do-apply (data)
5040+
(lsp--apply-text-edits data 'format))
50395041

50405042
(defun lsp--on-type-formatting (first-trigger-characters more-trigger-characters)
50415043
"Self insert handling.
50425044
Applies on type formatting."
50435045
(let ((ch last-command-event))
50445046
(when (or (eq (string-to-char first-trigger-characters) ch)
50455047
(cl-find ch more-trigger-characters :key #'string-to-char))
5046-
(lsp-request-async "textDocument/onTypeFormatting"
5047-
(lsp-make-document-on-type-formatting-params
5048-
:text-document (lsp--text-document-identifier)
5049-
:options (lsp-make-formatting-options
5050-
:tab-size (symbol-value (lsp--get-indent-width major-mode))
5051-
:insert-spaces (lsp-json-bool (not indent-tabs-mode))
5052-
:trim-trailing-whitespace? (lsp-json-bool lsp-trim-trailing-whitespace)
5053-
:insert-final-newline? (lsp-json-bool lsp-insert-final-newline)
5054-
:trim-final-newlines? (lsp-json-bool lsp-trim-final-newlines))
5055-
:ch (char-to-string ch)
5056-
:position (lsp--cur-position))
5057-
(lambda (data) (lsp--apply-text-edits data 'format))
5058-
:mode 'tick))))
5048+
5049+
;; defer to after hooks -- avoids the request being cancelled by smart
5050+
;; parens, electric indent or other hooks
5051+
(run-at-time
5052+
0 nil
5053+
(lambda (pos-marker)
5054+
(lsp-request-async "textDocument/onTypeFormatting"
5055+
(lsp-make-document-on-type-formatting-params
5056+
:text-document (lsp--text-document-identifier)
5057+
:options (lsp-make-formatting-options
5058+
:tab-size (symbol-value (lsp--get-indent-width major-mode))
5059+
:insert-spaces (lsp-json-bool (not indent-tabs-mode))
5060+
:trim-trailing-whitespace? (lsp-json-bool lsp-trim-trailing-whitespace)
5061+
:insert-final-newline? (lsp-json-bool lsp-insert-final-newline)
5062+
:trim-final-newlines? (lsp-json-bool lsp-trim-final-newlines))
5063+
:ch (char-to-string ch)
5064+
:position (lsp--point-to-position (marker-position pos-marker)))
5065+
#'lsp--on-type-formatting-do-apply
5066+
:mode 'tick)
5067+
(set-marker pos-marker nil))
5068+
5069+
;; argument to lambda:
5070+
(point-marker)))))
50595071

50605072

50615073
;; links

0 commit comments

Comments
 (0)