From 78dde6efdd81e4d11a21ba87a7f9b141d274e947 Mon Sep 17 00:00:00 2001 From: Henrik Lissner Date: Fri, 4 Jun 2021 22:22:57 -0400 Subject: [PATCH] Fix #5133: don't kill LSP after reformatting This happened because LSP hooks on kill-buffer-hook (and possibly others) caused our temporary formatting buffer to talk to LSP as if it were the original buffer. When the temp buffer was cleaned up, LSP assumed the original buffer had been closed. No more! In fact, to avoid similar issues, let's avoid any complex functionality in hooks in this temp buffer. --- modules/editor/format/autoload/format.el | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/modules/editor/format/autoload/format.el b/modules/editor/format/autoload/format.el index 4740b60789e..6015cc7d72d 100644 --- a/modules/editor/format/autoload/format.el +++ b/modules/editor/format/autoload/format.el @@ -148,7 +148,15 @@ See `+format/buffer' for the interactive version of this function, and ;; `format-all' (and various formatting functions, like `gofmt') widen ;; the buffer, we must copy the region first. (let ((output (buffer-substring-no-properties (point-min) (point-max))) - (origin-buffer (or (buffer-base-buffer) (current-buffer)))) + (origin-buffer (or (buffer-base-buffer) (current-buffer))) + ;; Fixes #5133: some packages (like lsp-mode) can do a bunch + ;; of complicated stuff in these hooks. Better to not have to + ;; deal with any of them at all. + write-file-functions + before-save-hook + after-save-hook + kill-buffer-query-functions + kill-buffer-hook) (with-temp-buffer (with-silent-modifications (insert output) @@ -156,10 +164,13 @@ See `+format/buffer' for the interactive version of this function, and ;; buffer as possible, in case the formatter is an elisp ;; function, like `gofmt'. (cl-loop for (var . val) - in (cl-remove-if-not #'listp (buffer-local-variables origin-buffer)) + in (cl-remove-if-not #'listp (buffer-local-variables (current-buffer))) ;; Making enable-multibyte-characters buffer-local ;; causes an error. - unless (eq var 'enable-multibyte-characters) + unless (eq var enable-multibyte-characters) + ;; Fixes #5133: don't deal with complicated hook + ;; functionality! This isn't a real buffer anyway. + unless (string-match-p (symbol-name var) "-\\(hook\\|functions\\)$") ;; Using setq-local would quote var. do (set (make-local-variable var) val)) ;; Since we're piping a region of text to the formatter, remove