forked from emacs-lsp/lsp-mode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lsp-metals.el
279 lines (235 loc) · 11.7 KB
/
lsp-metals.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
;;; lsp-metals.el --- Scala Client settings -*- lexical-binding: t; -*-
;; Copyright (C) 2018-2019 Ross A. Baker <[email protected]>, Evgeny Kurnevsky <[email protected]>
;; Author: Ross A. Baker <[email protected]>, Evgeny Kurnevsky <[email protected]>
;; Keywords:
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;; lsp-metals client
;;; Code:
(require 'lsp-mode)
(defgroup lsp-metals nil
"LSP support for Scala, using Metals."
:group 'lsp-mode
:link '(url-link "https://scalameta.org/metals")
:package-version '(lsp-mode . "6.1"))
(defcustom lsp-metals-server-command "metals-emacs"
"The command to launch the Scala language server."
:group 'lsp-metals
:type 'file
:package-version '(lsp-mode . "6.1"))
(defcustom lsp-metals-server-args '()
"Extra arguments for the Scala language server."
:group 'lsp-metals
:type '(repeat string)
:package-version '(lsp-mode . "6.1"))
(defcustom lsp-metals-java-home ""
"The Java Home directory used for indexing JDK sources and locating
the `java` binary."
:type '(string)
:group 'lsp-metals
:package-version '(lsp-mode . "6.1"))
(defcustom lsp-metals-scalafmt-config-path ""
"Optional custom path to the .scalafmt.conf file. Should be relative
to the workspace root directory and use forward slashes / for file
separators (even on Windows)."
:type '(string)
:group 'lsp-metals
:package-version '(lsp-mode . "6.1"))
(defcustom lsp-metals-sbt-script ""
"Optional absolute path to an `sbt` executable to use for running
`sbt bloopInstall`. By default, Metals uses `java -jar sbt-launch.jar`
with an embedded launcher while respecting `.jvmopts` and `.sbtopts`.
Update this setting if your `sbt` script requires more customizations
like using environment variables."
:type '(string)
:group 'lsp-metals
:package-version '(lsp-mode . "6.1"))
(defcustom lsp-metals-gradle-script ""
"Optional absolute path to a `gradle` executable to use for running
`gradle bloopInstall`. By default, Metals uses gradlew with 5.3.1
gradle version. Update this setting if your `gradle` script requires
more customizations like using environment variables."
:type '(string)
:group 'lsp-metals
:package-version '(lsp-mode . "6.1"))
(lsp-register-custom-settings
'(("metals.java-home" lsp-metals-java-home)
("metals.scalafmt-config-path" lsp-metals-scalafmt-config-path)
("metals.sbt-script" lsp-metals-sbt-script)
("metals.gradle-script" lsp-metals-gradle-script)))
(defun lsp-metals--server-command ()
"Generate the Scala language server startup command."
`(,lsp-metals-server-command ,@lsp-metals-server-args))
(defun lsp-metals-build-import ()
"Unconditionally run `sbt bloopInstall` and re-connect to the build server."
(interactive)
(lsp-send-execute-command "build-import" ()))
(defun lsp-metals-build-connect ()
"Unconditionally cancel existing build server connection and re-connect."
(interactive)
(lsp-send-execute-command "build-connect" ()))
(defun lsp-metals-doctor-run ()
"Open the Metals doctor to troubleshoot potential build problems."
(interactive)
(lsp-send-execute-command "doctor-run" ()))
(defun lsp-metals-sources-scan ()
"Walk all files in the workspace and index where symbols are defined."
(interactive)
(lsp-send-execute-command "sources-scan" ()))
(defun lsp-metals--doctor-render (html)
"Render the Metals doctor html in the current buffer."
(require 'shr)
(setq-local show-trailing-whitespace nil)
(setq-local buffer-read-only nil)
(erase-buffer)
(insert html)
(shr-render-region (point-min) (point-max))
(setq-local buffer-read-only t))
(defun lsp-metals--generate-doctor-buffer-name (workspace)
(format "*Metals Doctor: %s*" (process-id (lsp--workspace-cmd-proc workspace))))
(defun lsp-metals--doctor-run (workspace html)
"Focus on a window displaying troubleshooting help from the Metals doctor."
(pop-to-buffer (lsp-metals--generate-doctor-buffer-name workspace))
(lsp-metals--doctor-render html))
(defun lsp-metals--doctor-reload (workspace html)
"Reload the HTML contents of an open Doctor window, if any.
Should be ignored if there is no open doctor window."
(when-let ((buffer (get-buffer (lsp-metals--generate-doctor-buffer-name workspace))))
(with-current-buffer buffer
(lsp-metals--doctor-render html))))
(defun lsp-metals--goto-location (_workspace location)
"Move the cursor focus to the provided location."
(let ((xrefs (lsp--locations-to-xref-items (list location))))
(if (boundp 'xref-show-definitions-function)
(with-no-warnings
(funcall xref-show-definitions-function
(-const xrefs)
`((window . ,(selected-window)))))
(xref--show-xrefs xrefs nil))))
(defun lsp-metals--echo-command (workspace command)
"A client command that should be forwarded back to the Metals server."
(with-lsp-workspace workspace
(lsp-send-execute-command command)))
(defun lsp-metals--publish-decorations (workspace params)
"Handle the metals/publishDecorations extension notification."
(with-lsp-workspace workspace
(let* ((file (lsp--uri-to-path (ht-get params "uri")))
(buffer (lsp--buffer-for-file file)))
(when buffer
(with-current-buffer buffer
(lsp--remove-overlays 'metals-decoration)
(mapc #'lsp-metals--make-overlay (ht-get params "options")))))))
(defun lsp-metals--make-overlay (decoration)
"Create overlay from metals decoration."
(let* ((region (lsp--range-to-region (ht-get decoration "range")))
(content (ht-get (ht-get (ht-get decoration "renderOptions") "after") "contentText"))
(hover (ht-get (ht-get decoration "hoverMessage") "value"))
(ov (make-overlay (car region) (cdr region) nil t t)))
(overlay-put ov 'after-string (propertize content 'cursor t 'font-lock-face 'font-lock-comment-face))
(overlay-put ov 'help-echo hover)
(overlay-put ov 'metals-decoration t)))
(defun lsp-metals--logs-toggle (_workspace)
"Focus or remove focus on the output logs reported by the
server via `window/logMessage'."
(switch-to-buffer (get-buffer-create "*lsp-log*")))
(defun lsp-metals--diagnostics-focus (_workspace)
"Focus on the window that lists all published diagnostics."
(defvar flymake-mode)
(defvar flycheck-mode)
(cond ((fboundp 'flymake-show-diagnostics-buffer) (flymake-show-diagnostics-buffer))
((and flymake-mode (fboundp 'flymake-show-diagnostics-buffer)) (flymake-show-diagnostics-buffer))
((and flycheck-mode (fboundp 'flycheck-list-errors)) (flycheck-list-errors))))
(defun lsp-metals--execute-client-command (workspace params)
"Handle the metals/executeClientCommand extension notification."
(when-let ((command (pcase (ht-get params "command")
(`"metals-doctor-run" #'lsp-metals--doctor-run)
(`"metals-doctor-reload" #'lsp-metals--doctor-reload)
(`"metals-logs-toggle" #'lsp-metals--logs-toggle)
(`"metals-diagnostics-focus" #'lsp-metals--diagnostics-focus)
(`"metals-goto-location" #'lsp-metals--goto-location)
(`"metals-echo-command" #'lsp-metals--echo-command)
(`"metals-model-refresh" #'lsp-metals--model-refresh)
(c (ignore (lsp-warn "Unknown metals client command: %s" c))))))
(apply command (append (list workspace) (ht-get params "arguments") nil))))
(defvar lsp-metals--current-buffer nil
"Current buffer used to send `metals/didFocusTextDocument' notification.")
(defun lsp-metals--did-focus ()
"Send `metals/didFocusTextDocument' when current buffer changes."
(unless (eq lsp-metals--current-buffer (current-buffer))
(setq lsp-metals--current-buffer (current-buffer))
(lsp-notify "metals/didFocusTextDocument" (lsp--buffer-uri))))
(declare-function dap-debug "ext:dap-mode" (conf))
(declare-function dap-register-debug-provider "ext:dap-mode" (name conf))
(defun lsp-metals--debug-start (no-debug params)
(dap-register-debug-provider "scala" #'identity)
(-let [session-params (lsp-send-execute-command
"debug-adapter-start"
(gethash "arguments" params))]
(dap-debug
(list :debugServer (-> (gethash "uri" session-params)
(split-string ":")
cl-third
string-to-number)
:type "scala"
:name (gethash "name" session-params)
:host "localhost"
:request "launch"
:noDebug no-debug))))
(defun lsp-metals--model-refresh (workspace)
(->> workspace
(lsp--workspace-buffers)
(mapc (lambda (buffer)
(with-current-buffer buffer
(when (bound-and-true-p lsp-lens-mode)
(lsp--lens-schedule-refresh t)))))))
(defun lsp-metals--status-string-keymap (workspace command)
"Keymap for `metals/status' notification."
(when command
(-doto (make-sparse-keymap)
(define-key [mode-line mouse-1]
(lambda ()
(interactive)
(lsp-metals--execute-client-command workspace (ht ("command" command))))))))
(defun lsp-metals--status-string (workspace params)
"Handle `metals/status' notification."
(-let (((&hash "text" "hide" "tooltip" "command") params))
(if (or hide (s-blank-str? text))
(lsp-workspace-status nil workspace)
(lsp-workspace-status (propertize text
'help-echo tooltip
'local-map (lsp-metals--status-string-keymap workspace command))
workspace))))
(lsp-register-client
(make-lsp-client :new-connection (lsp-stdio-connection 'lsp-metals--server-command)
:major-modes '(scala-mode)
:priority -1
:custom-capabilities `((experimental (decorationProvider . t)
(debuggingProvider . ,(fboundp 'dap-mode))))
:notification-handlers (ht ("metals/executeClientCommand" #'lsp-metals--execute-client-command)
("metals/publishDecorations" #'lsp-metals--publish-decorations)
("metals/treeViewDidChange" #'ignore)
("metals-model-refresh" #'lsp-metals--model-refresh)
("metals/status" #'lsp-metals--status-string))
:action-handlers (ht ("metals-debug-session-start" (-partial #'lsp-metals--debug-start :json-false))
("metals-run-session-start" (-partial #'lsp-metals--debug-start t)))
:server-id 'metals
:initialized-fn (lambda (workspace)
(with-lsp-workspace workspace
(lsp--set-configuration
(lsp-configuration-section "metals"))))
:after-open-fn (lambda ()
(add-hook 'lsp-on-idle-hook #'lsp-metals--did-focus nil t))))
(provide 'lsp-metals)
;;; lsp-metals.el ends here
;; Local Variables:
;; flycheck-disabled-checkers: (emacs-lisp-checkdoc)
;; End: