Created
November 16, 2022 13:32
-
-
Save ntBre/b0622cc212314d4d2ea795d913980068 to your computer and use it in GitHub Desktop.
rust-mode and lsp-mode setup for emacs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ;;; rust | |
| (use-package rust-mode | |
| :ensure t | |
| :hook ((rust-mode . flycheck-mode) | |
| (rust-mode . hs-minor-mode)) | |
| :bind (:map rust-mode-map | |
| ("<f6>" . my/rust-format-buffer) | |
| ("C-c 6" . my/rust-format-buffer)) | |
| :config | |
| (require 'rust-rustfmt) | |
| (defun my/rust-format-buffer () | |
| (interactive) | |
| (rust-format-buffer) | |
| (save-buffer))) | |
| ;;; lsp | |
| (use-package lsp-mode | |
| :ensure t | |
| :hook ((rust-mode . lsp-deferred) | |
| (go-mode . lsp-deferred) | |
| (f90-mode . lsp-deferred) | |
| (c++-mode . lsp-deferred) | |
| (fortran-mode . lsp-deferred)) | |
| :bind (:map lsp-mode-map | |
| ("C-c d" . lsp-describe-thing-at-point) | |
| ("C-c a" . lsp-execute-code-action) | |
| ("C-c f" . flycheck-list-errors)) | |
| :config | |
| ;; rust | |
| (require 'lsp-rust) | |
| (setq lsp-rust-analyzer-completion-add-call-parenthesis nil | |
| lsp-rust-analyzer-proc-macro-enable t | |
| lsp-rust-analyzer-cargo-watch-command "clippy") | |
| ;; general | |
| (define-key lsp-mode-map (kbd "C-c l") lsp-command-map) | |
| (lsp-enable-which-key-integration t) | |
| (setq lsp-enable-links nil | |
| lsp-keep-workspace-alive nil) | |
| (cl-defmethod lsp-clients-extract-signature-on-hover | |
| (contents (_server-id (eql rust-analyzer))) | |
| "from https://github.com/emacs-lsp/lsp-mode/pull/1740 to extract | |
| signature in rust" | |
| (-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```"))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment