1 ;;; ra-emacs-lsp.el --- Rust analyzer emacs bindings for emacs-lsp -*- lexical-binding: t; -*-
9 ;; - sets up rust-analyzer with emacs-lsp, giving
11 ;; - completion (use company-lsp for proper snippet support)
13 ;; - on-type formatting
14 ;; - 'hover' type information & documentation (with lsp-ui)
15 ;; - implements source changes (for code actions etc.), except for file system changes
16 ;; - implements joinLines (you need to bind rust-analyzer-join-lines to a key)
17 ;; - implements extendSelection (either bind rust-analyzer-extend-selection to a key, or use expand-region)
20 ;; - file system changes in apply-source-change
21 ;; - semantic highlighting
22 ;; - onEnter, parentModule, findMatchingBrace
24 ;; - the debugging commands (syntaxTree and analyzerStatus)
25 ;; - lsp-ui doesn't interpret the markdown we return currently and instead displays it raw (https://github.com/emacs-lsp/lsp-ui/issues/220 )
28 ;; Also, there's a problem with company-lsp's caching being too eager, sometimes
29 ;; resulting in outdated completions.
31 (defcustom rust-analyzer-command '("ra_lsp_server")
33 :type '(repeat (string)))
35 (defconst rust-analyzer--notification-handlers
36 '(("rust-analyzer/publishDecorations" . (lambda (_w _p)))))
38 (defconst rust-analyzer--action-handlers
39 '(("rust-analyzer.applySourceChange" .
40 (lambda (p) (rust-analyzer--apply-source-change-command p)))))
42 (defun rust-analyzer--uri-filename (text-document)
43 (lsp--uri-to-path (gethash "uri" text-document)))
45 (defun rust-analyzer--goto-lsp-loc (loc)
46 (-let (((&hash "line" "character") loc))
48 (move-to-column character)))
50 (defun rust-analyzer--apply-text-document-edit (edit)
51 "Like lsp--apply-text-document-edit, but it allows nil version."
52 (let* ((ident (gethash "textDocument" edit))
53 (filename (rust-analyzer--uri-filename ident))
54 (version (gethash "version" ident)))
55 (with-current-buffer (find-file-noselect filename)
56 (when (or (not version) (= version (lsp--cur-file-version)))
57 (lsp--apply-text-edits (gethash "edits" edit))))))
59 (defun rust-analyzer--apply-source-change (data)
60 ;; TODO fileSystemEdits
61 (--each (-> data (ht-get "workspaceEdit") (ht-get "documentChanges"))
62 (rust-analyzer--apply-text-document-edit it))
63 (-when-let (cursor-position (ht-get data "cursorPosition"))
64 (let ((filename (rust-analyzer--uri-filename (ht-get cursor-position "textDocument")))
65 (position (ht-get cursor-position "position")))
67 (rust-analyzer--goto-lsp-loc position)
70 (defun rust-analyzer--apply-source-change-command (p)
71 (let ((data (-> p (ht-get "arguments") (car))))
72 (rust-analyzer--apply-source-change data)))
76 :new-connection (lsp-stdio-connection (lambda () rust-analyzer-command))
77 :notification-handlers (ht<-alist rust-analyzer--notification-handlers)
78 :action-handlers (ht<-alist rust-analyzer--action-handlers)
79 :major-modes '(rust-mode)
81 :server-id 'rust-analyzer))
83 (with-eval-after-load 'company-lsp
84 ;; company-lsp provides a snippet handler for rust by default that adds () after function calls, which RA does better
85 (setq company-lsp--snippet-functions (assq-delete-all "rust" company-lsp--snippet-functions)))
89 (defun rust-analyzer--join-lines-params ()
91 (list :textDocument (lsp--text-document-identifier)
92 :range (if (use-region-p)
93 (lsp--region-to-range (region-beginning) (region-end))
94 (lsp--region-to-range (point) (point)))))
96 (defun rust-analyzer-join-lines ()
99 (lsp-send-request (lsp-make-request "rust-analyzer/joinLines"
100 (rust-analyzer--join-lines-params)))
101 (rust-analyzer--apply-source-change)))
105 (defun rust-analyzer-extend-selection ()
107 (-let (((&hash "start" "end") (rust-analyzer--extend-selection)))
108 (rust-analyzer--goto-lsp-loc start)
110 (rust-analyzer--goto-lsp-loc end)
111 (exchange-point-and-mark)))
113 (defun rust-analyzer--extend-selection-params ()
114 "Extend selection params."
115 (list :textDocument (lsp--text-document-identifier)
119 (lsp--region-to-range (region-beginning) (region-end))
120 (lsp--region-to-range (point) (point))))))
122 (defun rust-analyzer--extend-selection ()
126 "rust-analyzer/extendSelection"
127 (rust-analyzer--extend-selection-params)))
128 (ht-get "selections")
131 (defun rust-analyzer--add-er-expansion ()
132 (make-variable-buffer-local 'er/try-expand-list)
133 (setq er/try-expand-list (append
135 '(rust-analyzer-extend-selection))))
137 (with-eval-after-load 'expand-region
138 (add-hook 'rust-mode-hook 'rust-analyzer--add-er-expansion))
141 (defvar rust-analyzer--last-runnable)
143 (defun rust-analyzer--runnables-params ()
144 (list :textDocument (lsp--text-document-identifier)
145 :position (lsp--cur-position)))
147 (defun rust-analyzer--runnables ()
148 (lsp-send-request (lsp-make-request "rust-analyzer/runnables"
149 (rust-analyzer--runnables-params))))
151 (defun rust-analyzer--select-runnable ()
152 (lsp--completing-read
154 (if rust-analyzer--last-runnable
155 (cons rust-analyzer--last-runnable (rust-analyzer--runnables))
156 (rust-analyzer--runnables))
157 (-lambda ((&hash "label")) label)))
159 (defun rust-analyzer-run (runnable)
160 (interactive (list (rust-analyzer--select-runnable)))
161 (-let (((&hash "env" "bin" "args" "label") runnable))
163 (string-join (cons bin args) " ")
164 ;; cargo-process-mode is nice, but try to work without it...
165 (if (functionp 'cargo-process-mode) 'cargo-process-mode nil)
166 (lambda (_) (concat "*" label "*")))
167 (setq rust-analyzer--last-runnable runnable)))
169 (defun rust-analyzer-rerun (&optional runnable)
170 (interactive (list (or rust-analyzer--last-runnable
171 (rust-analyzer--select-runnable))))
172 (rust-analyzer-run (or runnable rust-analyzer--last-runnable)))
174 (provide 'ra-emacs-lsp)
175 ;;; ra-emacs-lsp.el ends here