Emacs: Format C++ project buffers before saving
This commit is contained in:
@@ -1127,7 +1127,7 @@ Evaluated keybinds.
|
|||||||
(with-eval-after-load 'lsp-mode
|
(with-eval-after-load 'lsp-mode
|
||||||
(space-leader lsp-mode-map
|
(space-leader lsp-mode-map
|
||||||
"l" lsp-command-map
|
"l" lsp-command-map
|
||||||
"l = f" '(lsp-format-buffer-or-region :which-key "format buffer or region")
|
"l = f" '(dot/lsp-format-buffer-or-region :which-key "format buffer or region")
|
||||||
))
|
))
|
||||||
|
|
||||||
(with-eval-after-load 'dap-mode
|
(with-eval-after-load 'dap-mode
|
||||||
|
|||||||
@@ -210,7 +210,7 @@ Language Server Protocol.
|
|||||||
(let ((lsp-keymap-prefix (concat leader-key " l")))
|
(let ((lsp-keymap-prefix (concat leader-key " l")))
|
||||||
(lsp-enable-which-key-integration)))
|
(lsp-enable-which-key-integration)))
|
||||||
|
|
||||||
(defun lsp-format-buffer-or-region ()
|
(defun dot/lsp-format-buffer-or-region ()
|
||||||
"Format the selection (or buffer) with LSP."
|
"Format the selection (or buffer) with LSP."
|
||||||
(interactive)
|
(interactive)
|
||||||
(unless (bound-and-true-p lsp-mode)
|
(unless (bound-and-true-p lsp-mode)
|
||||||
@@ -218,7 +218,23 @@ Language Server Protocol.
|
|||||||
(call-interactively
|
(call-interactively
|
||||||
(if (use-region-p)
|
(if (use-region-p)
|
||||||
#'lsp-format-region
|
#'lsp-format-region
|
||||||
#'lsp-format-buffer))))
|
#'lsp-format-buffer)))
|
||||||
|
|
||||||
|
;; This is cached to prevent unneeded I/O
|
||||||
|
(setq lsp-in-cpp-project-cache nil)
|
||||||
|
(defun dot/lsp-format-cpp-buffer ()
|
||||||
|
"Format buffer in C++ projects."
|
||||||
|
(unless lsp-in-cpp-project-cache
|
||||||
|
(set (make-local-variable 'lsp-in-cpp-project-cache)
|
||||||
|
(list
|
||||||
|
(if (and (eq major-mode 'c++-mode)
|
||||||
|
(bound-and-true-p lsp-mode)
|
||||||
|
(project-current))
|
||||||
|
t
|
||||||
|
nil))))
|
||||||
|
(when (car lsp-in-cpp-project-cache)
|
||||||
|
(lsp-format-buffer)))
|
||||||
|
(add-hook 'before-save-hook #'dot/lsp-format-cpp-buffer))
|
||||||
|
|
||||||
;; TODO: add lsp-signature keybinds
|
;; TODO: add lsp-signature keybinds
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user