From 8a1634a9e1b1bedcc7e3152354cda531cf63a39a Mon Sep 17 00:00:00 2001 From: Joseph Ferano Date: Sat, 28 Sep 2024 21:11:48 +0700 Subject: [PATCH] Emacs: Switching to lsp-mode, lsp-ui, and flycheck, added stuff to csharp and odin --- .config/emacs/init.org | 161 ++++++++++++++++++++++++++--------------- 1 file changed, 103 insertions(+), 58 deletions(-) diff --git a/.config/emacs/init.org b/.config/emacs/init.org index 32cad0f..28280b8 100644 --- a/.config/emacs/init.org +++ b/.config/emacs/init.org @@ -460,7 +460,7 @@ Setup other stuff (defun joe/disable-line-numbers () (display-line-numbers-mode 0)) (dolist (mode '( dashboard-mode-hook org-mode-hook term-mode-hook eww-mode-hook eat-mode-hook - vterm-mode-hook dirvish-directory-view-mode-hook eshell-mode-hook + vterm-mode-hook dirvish-directory-view-mode-hook eshell-mode-hook lsp-ui-doc-frame-mode-hook dired-mode-hook org-agenda-mode-hook shell-mode-hook magit-mode-hook compilation-mode-hook mu4e-headers-mode-hook mu4e-main-mode-hook)) (add-hook mode #'joe/disable-line-numbers)) @@ -880,7 +880,6 @@ Fill region is great, except when you don't need it... state)) (evil-define-key 'normal joe/evil-space-mode-map - (kbd "K") #'eldoc (kbd "SPC t") tab-prefix-map (kbd "SPC p") project-prefix-map (kbd "SPC q") 'kill-buffer-and-window @@ -1177,6 +1176,7 @@ Ace Window will show a hint if there are more than 2 windows, but I don't really "^\\*eldoc\\*" special-mode "^\\*godot.*" godot-mode "^\\*ert\\*" ert-results-mode + "^\\*xref\\*" xref-mode "^\\*HTTP Response\\*" javascript-mode "^\\*SQL.*" sql-interactive-mode "^\\*cargo-test\\*" cargo-test-mode @@ -1793,6 +1793,7 @@ odd looking ~'(t .t)~ is for specifying a default for all other actions. (setq dirvish-quick-access-entries '(("h" "~/" "Home") + ("c" "~/.config" "Config") ("d" "~/Downloads/" "Downloads") ("D" "~/Documents/" "Documents") ("b" "~/Documents/Books/" "Books") @@ -2178,13 +2179,7 @@ close and remove the hook #'completion--in-region) args))) #+end_src -*** Eldoc -#+begin_src emacs-lisp -(evil-global-set-key 'normal (kbd "K") #'eldoc) -(global-eldoc-mode -1) - -#+end_src -*** Eglot +*** COMMENT Eglot #+begin_src emacs-lisp (with-eval-after-load 'eglot ;; (flymake-mode -1) @@ -2213,58 +2208,66 @@ These help speed eglot up apparently [[https://www.reddit.com/r/emacs/comments/1 (setq eglot-events-buffer-size 0) #+end_src -*** COMMENT LSP +*** LSP #+begin_src emacs-lisp (require 'lsp-mode) - (setq lsp-enable-which-key-integration t) - (setq lsp-keymap-prefix "C-c c") +(setq lsp-enable-which-key-integration t) +(setq lsp-headerline-breadcrumb-enable nil) +(setq lsp-modeline-diagnostics-enable nil) +(setq lsp-modeline-code-actions-enable nil) +(setq lsp-lens-enable nil) +(setq lsp-eldoc-enable-hover nil) +(setq eldoc-echo-area-use-multiline-p 'truncate-sym-name-if-fit) +(setq eldoc-idle-delay 0) +(setq lsp-eldoc-render-all t) +;; (setq lsp-ui-doc-enable t) +(defun joe/lsp-mode-hook () + (flymake-mode -1)) +(add-hook 'lsp-mode-hook #'joe/lsp-mode-hook) - ;; (setq lsp-ui-peek-always-show t) - ;; (setq lsp-ui-doc-enable t) - ;; TODO: Disable lsp sideline - (setq lsp-headerline-breadcrumb-enable nil) - (setq lsp-eldoc-enable-hover t) - (setq max-mini-window-height 0.25) - (setq lsp-eldoc-enable-hover t) - (setq lsp-ui-doc-enable nil) - (setq eldoc-echo-area-use-multiline-p 'truncate-sym-name-if-fit) - (setq eldoc-idle-delay 0) - (setq lsp-idle-delay 0.2) +(define-key global-map (kbd "C-c l l") #'lsp) +(define-key lsp-command-map (kbd "c") #'flymake-mode) - ;; Signatures - (setq lsp-signature-doc-lines t) - (setq lsp-signature-render-documentation nil) - (setq lsp-eldoc-render-all nil) +(setq lsp-ui-doc-position 'at-point) +(setq lsp-ui-doc-show-with-mouse nil) + + +(require 'flycheck) +(setq flycheck-check-syntax-automatically '()) +(setq flycheck-keymap-prefix (kbd "C-c e")) + +(add-hook 'after-save-hook #'flycheck-clear) ;; All this changes because we are using eglot now - (when (boundp 'evil-mode) - (evil-global-set-key 'normal (kbd "M-d") #'lsp-describe-thing-at-point) - (evil-global-set-key 'normal (kbd "lh") 'lsp-headerline-breadcrumb-mode) - (evil-global-set-key 'normal (kbd "SPC li") 'lsp-rust-analyzer-inlay-hints-mode) - (evil-global-set-key 'normal (kbd "cs") 'consult-lsp-symbols) - (evil-global-set-key 'normal (kbd "cf") 'consult-lsp-file-symbols)) +(when (boundp 'evil-mode) + (evil-global-set-key 'normal (kbd "M-e") #'flycheck-buffer) + (evil-global-set-key 'normal (kbd "M-n") #'flycheck-next-error) + (evil-global-set-key 'normal (kbd "M-p") #'flycheck-previous-error) + (evil-global-set-key 'normal (kbd "M-d") #'lsp-ui-doc-glance) + (evil-global-set-key 'normal (kbd "SPC cs") 'consult-lsp-symbols) + (evil-global-set-key 'normal (kbd "SPC cd") 'joe/consult-lsp-diagnostics) + (evil-global-set-key 'normal (kbd "SPC cf") 'consult-lsp-file-symbols)) ;; This function allows us to extract Rust's true function signature - (cl-defmethod lsp-clients-extract-signature-on-hover (contents (_server-id (eql rust-analyzer))) - (-let* (((&hash "value") contents) - (groups (--partition-by (s-blank? it) (s-lines (s-trim value)))) - (sig_group (if (s-equals? "```rust" (car (-third-item groups))) - (-third-item groups) - (car groups))) - (sig (--> sig_group - (--drop-while (s-equals? "```rust" it) it) - (--take-while (not (s-equals? "```" it)) it) - (--map (s-trim it) it) - (s-join " " it)))) - (lsp--render-element (concat "```rust\n" sig "\n```")))) + ;; (cl-defmethod lsp-clients-extract-signature-on-hover (contents (_server-id (eql rust-analyzer))) + ;; (-let* (((&hash "value") contents) + ;; (groups (--partition-by (s-blank? it) (s-lines (s-trim value)))) + ;; (sig_group (if (s-equals? "```rust" (car (-third-item groups))) + ;; (-third-item groups) + ;; (car groups))) + ;; (sig (--> sig_group + ;; (--drop-while (s-equals? "```rust" it) it) + ;; (--take-while (not (s-equals? "```" it)) it) + ;; (--map (s-trim it) it) + ;; (s-join " " it)))) + ;; (lsp--render-element (concat "```rust\n" sig "\n```"))) -(require 'lsp-ui) +;; (require 'lsp-ui) #+end_src *** COMMENT Flycheck #+begin_src emacs-lisp -(require 'flycheck) ;; TODO Add wrapping to these functions (when (boundp 'evil-mode) (evil-global-set-key 'normal (kbd "M-e") #'flycheck-next-error) @@ -2405,6 +2408,59 @@ file." #+end_src ** Programming Languages +*** CSharp + +Omnisharp is very slow to start, so I want to start it manually, but not if the +buffer already belongs to the current lsp workspace. This function checks if a +newly opened buffer belongs to an LSP session and if it does, start lsp + +#+begin_src emacs-lisp +(defun lsp-mode-server-exists-p+ (mode &optional all-workspaces) + (when (buffer-file-name) ; Needed for lsp--matching-clients? + (let ((major-mode mode)) + (seq-intersection + (lsp--filter-clients (-andfn #'lsp--supports-buffer? + #'lsp--server-binary-present?)) + (when-let ((workspaces + (if all-workspaces + (-flatten (hash-table-values (lsp-session-folder->servers (lsp-session)))) + (gethash (lsp-workspace-root) (lsp-session-folder->servers (lsp-session)))))) + (mapcar #'lsp--workspace-client workspaces)) + #'equal)))) + +;; (setq treesit-load-name-override-list '((csharp "libtree-sitter-c-sharp.so"))) +#+end_src + +And we do the rest here, including a macro + +#+begin_src emacs-lisp +(defun joe/csharp-mode-hook () + (yas-minor-mode t) + (defalias 'joe/serialized-private-public-getter + (kmacro "^ W W Y o C-y ^ i p u b l i c SPC l W y i w P a SPC = > SPC B B ~")) + (evil-set-register ?g (lambda nil "Unity/C#" + (call-interactively 'joe/serialized-private-public-getter))) + ;; (schmo/reapply-csharp-ts-mode-font-lock-settings) + + (electric-pair-local-mode t) + (when (lsp-mode-server-exists-p+ 'csharp-mode) + (lsp))) +(add-hook 'csharp-mode-hook #'joe/csharp-mode-hook) +#+end_src +*** Odin +#+begin_src emacs-lisp +(with-eval-after-load 'lsp-mode + (setq-default lsp-auto-guess-root t) ;; Helps find the ols.json file with Projectile or project.el + (setq lsp-language-id-configuration (cons '(odin-mode . "odin") lsp-language-id-configuration)) + + (lsp-register-client + (make-lsp-client :new-connection (lsp-stdio-connection "~/.local/bin/ols") ;; Adjust the path here + :major-modes '(odin-mode) + :server-id 'ols + :multi-root t))) ;; Ensures lsp-mode sends "workspaceFolders" to the server + +(add-hook 'odin-mode-hook #'lsp) +#+end_src *** C Design some basic functions for compiling. There's also a hook to close the popper window if there @@ -2422,17 +2478,6 @@ it doesn't close it. (c-toggle-comment-style -1)) (add-hook 'c-mode-hook #'joe/c-mode-hook) #+end_src -*** CSharp -#+begin_src emacs-lisp -(defun joe/csharp-mode-hook () - (yas-minor-mode t) - (defalias 'joe/serialized-private-public-getter - (kmacro "^ W W Y o C-y ^ i p u b l i c SPC l W y i w P a SPC = > SPC B B ~")) - (evil-set-register ?g (lambda nil "Unity/C#" - (call-interactively 'joe/serialized-private-public-getter))) - (electric-pair-local-mode t)) -(add-hook 'csharp-mode-hook #'joe/csharp-mode-hook) -#+end_src *** Python #+begin_src emacs-lisp (require 'python)