config formatter when eglot enabled

This commit is contained in:
zedhugh committed 2023-10-10 22:14:17 +08:00
1 parent 64ef980ff2
commit 487b9670dd
2 files changed
+17 -8

No files matched your search

+7 -8
View File
@@ -29,12 +29,11 @@
(eglot--highlight-piggyback cb) (eglot--highlight-piggyback cb)
t))) t)))
(defun creature/eglot-c-mode-keybinding () (defun creature/eglot-format ()
(when (derived-mode-p 'c-mode 'c++-mode 'c-ts-mode 'c++-ts-mode) (interactive)
(make-local-variable 'eglot-mode-map) (if creature/formatter
(lazy-load-set-keys (funcall creature/formatter)
'(("C-M-\\" . eglot-format)) (eglot-format)))
eglot-mode-map)))
(with-eval-after-load 'eglot (with-eval-after-load 'eglot
(setq eglot-events-buffer-size 0) (setq eglot-events-buffer-size 0)
@@ -43,7 +42,6 @@
(add-to-list 'eglot-ignored-server-capabilities :inlayHintProvider) (add-to-list 'eglot-ignored-server-capabilities :inlayHintProvider)
(add-hook 'eglot-managed-mode-hook #'creature/eglot-load-markdown-for-doc) (add-hook 'eglot-managed-mode-hook #'creature/eglot-load-markdown-for-doc)
(add-hook 'eglot-managed-mode-hook #'creature/eglot-c-mode-keybinding)
;; variable `eglot-prefer-plaintext' invoked in eglot-1.14 ;; variable `eglot-prefer-plaintext' invoked in eglot-1.14
;; and document only show one line by eldoc since eglot-1.14 ;; and document only show one line by eldoc since eglot-1.14
@@ -54,7 +52,8 @@
'(("M-." . xref-find-definitions) '(("M-." . xref-find-definitions)
("M-?" . xref-find-references) ("M-?" . xref-find-references)
("C-c r" . eglot-rename) ("C-c r" . eglot-rename)
("C-c o" . eglot-code-actions)) ("C-c o" . eglot-code-actions)
("C-M-\\" . creature/eglot-format))
eglot-mode-map)) eglot-mode-map))
+10
View File
@@ -2,6 +2,9 @@
(autoload 'prettier-mode "prettier" "" t) (autoload 'prettier-mode "prettier" "" t)
(defvar-local creature/formatter nil
"Formatter for buffer.")
(defun creature/prettier-setup () (defun creature/prettier-setup ()
"Enable `prettier-mode' selectively." "Enable `prettier-mode' selectively."
(when (and prettier-mode (when (and prettier-mode
@@ -16,8 +19,15 @@
'(json-stringify json json5) '(json-stringify json json5)
'(json json5 json-stringify))) '(json json5 json-stringify)))
(defun creature/prettier-set-formatter ()
(setq creature/formatter
(if (bound-and-true-p prettier-mode)
'creature/indent-region-or-buffer
nil)))
(with-eval-after-load 'prettier (with-eval-after-load 'prettier
(add-hook 'prettier-mode-hook #'creature/prettier-setup) (add-hook 'prettier-mode-hook #'creature/prettier-setup)
(add-hook 'prettier-mode-hook #'creature/prettier-set-formatter)
(add-to-list 'prettier-major-mode-parsers (add-to-list 'prettier-major-mode-parsers
'(js-json-mode . creature/prettier-json-parser))) '(js-json-mode . creature/prettier-json-parser)))