-
-
Notifications
You must be signed in to change notification settings - Fork 905
/
Copy pathlsp-metals.el
179 lines (146 loc) · 6.84 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
;;; 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--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-goto-location" #'lsp-metals--goto-location)
(`"metals-echo-command" #'lsp-metals--echo-command)
(c (ignore (lsp-warn "Unknown metals client command: %s" c))))))
(apply command (append (list workspace) (ht-get params "arguments") nil))))
(lsp-register-client
(make-lsp-client :new-connection (lsp-stdio-connection 'lsp-metals--server-command)
:major-modes '(scala-mode)
:priority -1
:notification-handlers (ht ("metals/executeClientCommand" #'lsp-metals--execute-client-command)
("metals/treeViewDidChange" #'ignore))
:server-id 'metals
:initialized-fn (lambda (workspace)
(with-lsp-workspace workspace
(lsp--set-configuration
(lsp-configuration-section "metals"))))))
(provide 'lsp-metals)
;;; lsp-metals.el ends here
;; Local Variables:
;; flycheck-disabled-checkers: (emacs-lisp-checkdoc)
;; End: