Skip to content

Commit 1fb656e

Browse files
authored
Add format on save option (issue #3944) (#4753)
1 parent 7c0df12 commit 1fb656e

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

CHANGELOG.org

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
* Changelog
22
** Unreleased 9.0.1
3+
* Add format on save support
34
* Fix beancount journal file init option
45
* Add support for [[https://github.com/glehmann/earthlyls][earthlyls]]
56
* Add support for GNAT Project (~gpr-mode~, ~gpr-ts-mode~).

docs/page/main-features.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ the formatting is triggered when the corresponding character is
9292
pressed(typically, `}`, `RET`). This behaviour is controlled via
9393
`lsp-enable-on-type-formatting` and it is enabled by default.
9494

95+
You can also trigger format on save by setting the variable `lsp-format-buffer-on-save` to a non-nil value. To select what major modes to format use `lsp-format-buffer-on-save-list`.
96+
9597
## Debugger
9698

9799
`lsp-mode` integrates with [dap-mode](https://emacs-lsp.github.io/dap-mode/) with implements the DAP(Debugger Adapter Protocol), for more information check the [`dap-mode` documentation](https://emacs-lsp.github.io/dap-mode/).

lsp-mode.el

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,18 @@ before saving a document."
575575
:type 'boolean
576576
:group 'lsp-mode)
577577

578+
(defcustom lsp-format-buffer-on-save nil
579+
"If non-nil format buffer on save.
580+
To only format specific major-mode buffers see `lsp-format-buffer-on-save-list'."
581+
:type 'boolean
582+
:group 'lsp-mode)
583+
584+
(defcustom lsp-format-buffer-on-save-list '()
585+
"If the list is empty format all buffer on save. Else only format buffers
586+
if their major-mode is in the list."
587+
:type '(repeat symbol)
588+
:group 'lsp-mode)
589+
578590
(defcustom lsp-after-apply-edits-hook nil
579591
"Hooks to run when text edit is applied.
580592
It contains the operation source."
@@ -5201,6 +5213,13 @@ if it's closing the last buffer in the workspace."
52015213
'before-save)
52025214
(error)))))))
52035215

5216+
(defun lsp--format-buffer-before-save ()
5217+
(when lsp-format-buffer-on-save
5218+
(if (not lsp-format-buffer-on-save-list)
5219+
(lsp-format-buffer)
5220+
(when (member major-mode lsp-format-buffer-on-save-list)
5221+
(lsp-format-buffer)))))
5222+
52045223
(defun lsp--on-auto-save ()
52055224
"Handler for auto-save."
52065225
(when (lsp--send-will-save-p)
@@ -9397,6 +9416,7 @@ Errors if there are none."
93979416
(lsp--text-document-did-close t)
93989417
(lsp-managed-mode -1)
93999418
(lsp-mode -1)
9419+
(remove-hook 'before-save-hook #'lsp--format-buffer-before-save t)
94009420
(setq lsp--buffer-workspaces nil)
94019421
(lsp--info "Disconnected"))
94029422

@@ -9445,6 +9465,7 @@ argument ask the user to select which language server to start."
94459465
(lsp--try-project-root-workspaces (equal arg '(4))
94469466
(and arg (not (equal arg 1))))))
94479467
(lsp-mode 1)
9468+
(add-hook 'before-save-hook #'lsp--format-buffer-before-save nil t)
94489469
(when lsp-auto-configure (lsp--auto-configure))
94499470
(setq lsp-buffer-uri (lsp--buffer-uri))
94509471
(lsp--info "Connected to %s."

0 commit comments

Comments
 (0)