Lot of optimisation, replace eglot with lsp-mode

This commit is contained in:
Zelong Kuang
2026-03-30 11:49:18 +11:00
parent e1374b0e88
commit a3468b833e
24 changed files with 311 additions and 179 deletions

View File

@@ -1,8 +1,14 @@
;; -*- lexical-binding: t; -*-
;; LSP booster
(use-package lsp-mode
:diminish
:defines (lsp-diagnostics-disabled-modes lsp-clients-python-library-directories)
:autoload lsp-enable-which-key-integration
:commands (lsp-format-buffer lsp-organize-imports lsp lsp-deferred)
:hook ((prog-mode . (lambda ()
(unless (derived-mode-p
'emacs-lisp-mode 'lisp-mode
@@ -12,7 +18,9 @@
((markdown-mode yaml-mode yaml-ts-mode) . lsp-deferred)
(lsp-mode . (lambda ()
;; Integrate `which-key'
(lsp-enable-which-key-integration))))
(lsp-enable-which-key-integration)
(add-hook 'before-save-hook #'lsp-format-buffer t t)
(add-hook 'before-save-hook #'lsp-organize-imports t t))))
:bind (:map lsp-mode-map
("C-c c d" . lsp-describe-thing-at-point)
([remap xref-find-definitions] . lsp-find-definition)
@@ -20,6 +28,9 @@
:init (setq lsp-use-plists t
lsp-log-io nil
lsp-enable-suggest-server-download t
;; lsp-auto-configure t
lsp-keymap-prefix "C-c c"
lsp-keep-workspace-alive nil
lsp-signature-auto-activate nil
@@ -27,6 +38,12 @@
lsp-modeline-diagnostics-enable nil
lsp-modeline-workspace-status-enable nil
lsp-completion-enable t
lsp-completion-provider :none ;; using corfu
lsp-completion-enable-additional-text-edit t
lsp-enable-snippet t
lsp-completion-show-kind t
lsp-semantic-tokens-enable t
lsp-progress-spinner-type 'progress-bar-filled
@@ -34,15 +51,33 @@
lsp-enable-folding nil
lsp-enable-symbol-highlighting nil
lsp-enable-text-document-color nil
lsp-enable-imenu t
lsp-enable-indentation nil
lsp-enable-on-type-formatting nil)
lsp-enable-on-type-formatting nil
;; For diagnostics
lsp-diagnostics-disabled-modes '(markdown-mode gfm-mode))
:config
(use-package consult-lsp
:bind (:map lsp-mode-map
("C-M-." . consult-lsp-symbols)))
(with-no-warnings
;; Disable `lsp-mode' in `git-timemachine-mode'
(defun my/lsp--init-if-visible (fn &rest args)
(unless (bound-and-true-p git-timemachine-mode)
(apply fn args)))
(advice-add #'lsp--init-if-visible :around #'my/lsp--init-if-visible))
;; Enable `lsp-mode' in sh/bash/zsh
(defun my/lsp-bash-check-sh-shell (&rest _)
(and (memq major-mode '(sh-mode bash-ts-mode))
(memq sh-shell '(sh bash zsh))))
(advice-add #'lsp-bash-check-sh-shell :override #'my/lsp-bash-check-sh-shell)
(add-to-list 'lsp-language-id-configuration '(bash-ts-mode . "shellscript"))
(use-package lsp-ui
:custom-face
(lsp-ui-sideline-code-action ((t (:inherit warning))))
@@ -114,34 +149,40 @@
,(face-foreground 'font-lock-string-face)
,(face-foreground 'font-lock-constant-face)
,(face-foreground 'font-lock-variable-name-face))))
(use-package lsp-pyright
:functions lsp-pyright-format-buffer
:hook (((python-mode python-ts-mode) . (lambda ()
(require 'lsp-pyright)
(add-hook 'after-save-hook #'lsp-pyright-format-buffer t t))))
:init
;; (when (executable-find "python3")
;; (setq lsp-pyright-python-executable-cmd "python3"))
(defun lsp-pyright-format-buffer ()
"Use `yapf' to format the buffer."
(interactive)
(when (and (executable-find "yapf") buffer-file-name)
(call-process "yapf" nil nil nil "-i" buffer-file-name))))
(use-package ccls
:hook ((c-mode c++-mode objc-mode cuda-mode) . (lambda () (require 'ccls)))
:config
(with-no-warnings
;; FIXME: fail to call ccls.xref
;; @see https://github.com/emacs-lsp/emacs-ccls/issues/109
(cl-defmethod my-lsp-execute-command
((_server (eql ccls)) (command (eql ccls.xref)) arguments)
(when-let* ((xrefs (lsp--locations-to-xref-items
(lsp--send-execute-command (symbol-name command) arguments))))
(xref--show-xrefs xrefs nil)))
(advice-add #'lsp-execute-command :override #'my-lsp-execute-command))))
:preface
(defun lsp-booster--advice-json-parse (old-fn &rest args)
"Try to parse bytecode instead of json."
(or
(when (equal (following-char) ?#)
(let ((bytecode (read (current-buffer))))
(when (byte-code-function-p bytecode)
(funcall bytecode))))
(apply old-fn args)))
(advice-add (if (progn (require 'json)
(fboundp 'json-parse-buffer))
'json-parse-buffer
'json-read)
:around
#'lsp-booster--advice-json-parse)
(defun lsp-booster--advice-final-command (old-fn cmd &optional test?)
"Prepend emacs-lsp-booster command to lsp CMD."
(let ((orig-result (funcall old-fn cmd test?)))
(if (and (not test?) ;; for check lsp-server-present?
(not (file-remote-p default-directory)) ;; see lsp-resolve-final-command, it would add extra shell wrapper
lsp-use-plists
(not (functionp 'json-rpc-connection)) ;; native json-rpc
(executable-find "emacs-lsp-booster"))
(progn
(when-let ((command-from-exec-path (executable-find (car orig-result)))) ;; resolve command from exec-path (in case not found in $PATH)
(setcar orig-result command-from-exec-path))
(message "Using emacs-lsp-booster for %s!" orig-result)
(cons "emacs-lsp-booster" orig-result))
orig-result)))
(advice-add 'lsp-resolve-final-command :around #'lsp-booster--advice-final-command)
)
(provide 'init-lsp)