-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathlspce-langs.el
268 lines (244 loc) · 11.6 KB
/
lspce-langs.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
;;; lspce.el --- LSP Client for Emacs -*- lexical-binding: t; -*-
(require 'f)
(eval-and-compile
(require 'lspce-util))
(eval-when-compile
(require 'cl-macs))
(declare-function lspce--request "lspce")
;;; rust rust-analyzer
(defun lspce-ra-initializationOptions ()
(let ((options (make-hash-table :test #'equal)))
(lspce--add-option "diagnostics.enable" t options)
(lspce--add-option "enableExperimental" :json-false options)
(lspce--add-option "cargo.features" "all" options)
(lspce--add-option "cargo.noDefaultFeatures" :json-false options)
(lspce--add-option "cargo.runBuildScripts" t options)
(lspce--add-option "cargo.loadOutDirsFromCheck" t options)
(lspce--add-option "cargo.autoreload" t options)
(lspce--add-option "cargo.useRustcWrapperForBuildScripts" t options)
(lspce--add-option "completion.addCallParenthesis" t options)
(lspce--add-option "completion.addCallArgumentSnippets" t options)
(lspce--add-option "completion.postfix.enable" :json-false options)
(lspce--add-option "completion.autoimport.enable" t options)
(lspce--add-option "procMacro.enable" t options)
(lspce--add-option "lens.enable" :json-false options)))
;;; java jdtls
(defcustom lspce-jdtls-workspace-dir (expand-file-name "~/.jdtls/workspace/")
"jdtls workspace directory."
:group 'lspce
:type 'directory)
(defcustom lspce-java-vmargs '("--add-modules=ALL-SYSTEM"
"--add-opens java.base/java.util=ALL-UNNAMED"
"--add-opens java.base/java.lang=ALL-UNNAMED"
"-XX:+UseParallelGC"
"-XX:GCTimeRatio=4"
"-XX:AdaptiveSizePolicyWeight=90"
"-Dsun.zip.disableMemoryMapping=true"
"-Xmx1536m" )
"Specifies extra VM arguments used to launch the Java Language Server."
:group 'lspce
:risky t
:type '(repeat string))
;;;###autoload
(defcustom lspce-java-path "java"
"Path of the java executable."
:group 'lspce
:type 'string)
(defcustom lspce-jdtls-download-url
"https://download.eclipse.org/jdtls/snapshots/jdt-language-server-latest.tar.gz"
"URL to download the Eclipse JDT language server."
:type 'string
:group 'lspce)
(defcustom lspce-jdtls-install-dir nil
"Install directory for eclipse.jdt.ls-server."
:group 'lspce
:type 'directory)
(defcustom lspce-jdtls-launch-mode "Hybrid"
"The launch mode for the Java extension"
:group 'lspce
:type '(choice (:tag "Standard" "LightWeight" "Hybrid")))
(defcustom lspce-jdtls-completion-max-results 30
"The max number of completion results jdtls can return."
:type 'integer
:group 'lspce)
(declare-function tar-untar-buffer "tar-mode" ())
;;;###autoload
(defun lspce-install-jdtls-server ()
"Install the Eclipse JDT LSP server."
(interactive)
(let* ((dest-dir (expand-file-name lspce-jdtls-install-dir))
(dest-dir-bak (concat (string-trim-right dest-dir (f-path-separator))
".bak"
(format ".%s" (format-time-string "%Y%m%d%H%M%S"))))
(download-url lspce-jdtls-download-url)
(dest-filename (file-name-nondirectory download-url))
(dest-abspath (expand-file-name dest-filename dest-dir))
(large-file-warning-threshold nil))
(when (file-exists-p dest-dir)
(f-move dest-dir dest-dir-bak))
(f-mkdir-full-path dest-dir)
(lspce--message "Installing Eclipse JDT LSP server, please wait...")
(lspce--download-file download-url dest-abspath)
(lspce--message "Extracting Eclipse JDT LSP archive, please wait...")
(with-temp-buffer
(let ((temporary-buffer (find-file dest-abspath)))
(goto-char (point-min))
(tar-untar-buffer)
(kill-buffer temporary-buffer)))
(lspce--message "Eclipse JDT LSP server installed in folder \n\"%s\"." dest-dir)))
(defun lspce--jdtls-workspace-dir ()
(let ((proj (project-current))
workspace)
(lspce--ensure-dir lspce-jdtls-workspace-dir)
(setq workspace (if proj
(file-truename (project-root proj))
"DEFAULT"))
(f-join lspce-jdtls-workspace-dir (string-replace "/" "!" workspace))))
(defun lspce--jdtls-workspace-cache-dir ()
(let ((cache-dir (f-join (lspce--jdtls-workspace-dir) ".cache/")))
(lspce--ensure-dir cache-dir)
cache-dir))
(defun lspce--jdtls-locate-server-jar ()
"Return the jar file location of the language server.
The entry point of the language server is in
`lspce-jdtls-install-dir'/plugins/org.eclipse.equinox.launcher_`version'.jar."
(pcase (f-glob "org.eclipse.equinox.launcher_*.jar"
(expand-file-name "plugins" lspce-jdtls-install-dir))
(`(,single-entry) single-entry)
(`nil nil)
(server-jar-filenames
(error "Unable to find single point of entry %s" server-jar-filenames))))
(defun lspce--jdtls-locate-server-config ()
"Return the server config based on OS."
(let ((config (cond
((string-equal system-type "windows-nt") ; Microsoft Windows
"config_win")
((string-equal system-type "darwin") ; Mac OS X
"config_mac")
(t "config_linux"))))
(let ((inhibit-message t))
(message (format "using config for %s" config)))
(expand-file-name config lspce-jdtls-install-dir)))
(defun lspce-jdtls-cmd-args ()
(let ((server-jar (lspce--jdtls-locate-server-jar))
(server-config (lspce--jdtls-locate-server-config))
(data (lspce--jdtls-workspace-dir)))
(mapconcat #'identity `("-Declipse.application=org.eclipse.jdt.ls.core.id1"
"-Dosgi.bundles.defaultStartLevel=4"
"-Declipse.product=org.eclipse.jdt.ls.core.product"
"-Dlog.protocol=true"
"-Dlog.level=ALL"
,@lspce-java-vmargs
"-jar"
,server-jar
"-configuration"
,server-config
"-data"
,data
) " ")))
(defvar lspce--jdt-link-pattern "jdt://contents/\\(.*?\\)/\\(.*\\)\.class\\?")
(defun lspce--jdtls-get-jdt-filename (uri)
"Get the name of the buffer calculating it based on URL."
(or (save-match-data
(when (string-match lspce--jdt-link-pattern uri)
(format "%s.java"
(replace-regexp-in-string "/" "." (match-string 2 uri) t t))))
(save-match-data
(when (string-match
"jdt://.*?/\\(.*?\\)\\?=\\(.*?\\)/.*/\\(.*\\)"
(url-unhex-string uri))
(format "%s(%s)" (match-string 2 uri)
(string-replace "\\" ""
(string-replace "/" "" (match-string 4 uri))))))
(save-match-data
(when (string-match "chelib://\\(.*\\)" uri)
(let ((matched (match-string 1 uri)))
(replace-regexp-in-string (regexp-quote ".jar") "jar" matched t t))))
(error "Unable to match %s" uri)))
(defun lspce--jdtls-open-jdt-link (uri)
(let ((filename (lspce--jdtls-get-jdt-filename uri))
fullname content)
(when filename
(setq fullname (f-join (lspce--jdtls-workspace-cache-dir) filename))
;; FIXME use a cache to reduce requesting
(lspce--debug "jdt link uri %s" uri)
(setq content (lspce--request "java/classFileContents" (list :uri uri)))
(when content
(with-temp-file fullname
(erase-buffer)
(insert content))))
fullname))
(defun lspce-jdtls-initializationOptions ()
(let ((options (make-hash-table :test #'equal)))
(lspce--add-option "settings.java.server.launchMode"
lspce-jdtls-launch-mode options)
(lspce--add-option "settings.java.completion.enabled" t options)
(lspce--add-option "settings.java.completion.maxResults"
(if (> lspce-jdtls-completion-max-results 0)
lspce-jdtls-completion-max-results
30)
options)
(lspce--add-option "settings.java.completion.importOrder"
(vector "java" "javax" "com" "org") options)
(lspce--add-option "settings.java.completion.guessMethodArguments"
t options)
(lspce--add-option "settings.java.signatureHelp.enabled" t options)
(lspce--add-option "settings.java.progressReports.enabled" t options)
(lspce--add-option "settings.java.foldingRange.enabled" :json-false options)
(lspce--add-option "settings.java.maxConcurrentBuilds" 1 options)
(lspce--add-option "settings.java.autobuild.enabled" t options)
(lspce--add-option "settings.java.import.maven.enabled" t options)
(lspce--add-option "settings.java.import.gradle.enabled" t options)
(lspce--add-option "settings.java.import.gradle.wrapper.enabled" t options)
(lspce--add-option "settings.java.import.gradle.offline.enabled" t options)
(lspce--add-option "settings.java.maven.downloadSources" t options)
(lspce--add-option "settings.java.maven.updateSnapshots" t options)
(lspce--add-option "settings.java.project.importHint" t options)
(lspce--add-option "settings.java.project.importOnFirstTimeStartup"
"automatic" options)
(lspce--add-option "settings.java.project.referecedLibraries"
(vector "lib/**/*.jar") options)
(lspce--add-option "settings.java.trace.server" "off" options)
(lspce--add-option "settings.java.configuration.updateBuildConfiguration"
"automatic" options)
(lspce--add-option "settings.java.configuration.checkProjectSettingsExclusions"
t options)
(lspce--add-option "settings.java.showBuildStatusOnStart.enabled" t options)
(lspce--add-option "extendedClientCapabilities.classFileContentsSupport"
t options)))
;;; python pylsp
(defun lspce-pylsp-initializationOptions ()
(let ((options (make-hash-table :test #'equal)))
(lspce--add-option "pylsp.plugins.jedi_completion.include_params" t options)
(lspce--add-option "pylsp.plugins.jedi_completion.fuzzy" t options)
(when (boundp 'lspce-jedi-environment)
(lspce--add-option "pylsp.plugins.jedi.environment"
lspce-jedi-environment options))
(lspce--add-option "pylsp.plugins.pylint.enabled" :json-false options)
options))
;;; python jedi-language-server
(defun lspce-jedi-initializationOptions ()
(let ((options (make-hash-table :test #'equal)))
(when (boundp 'lspce-jedi-environment)
(lspce--add-option "workspace.environmentPath"
lspce-jedi-environment options))
(lspce--add-option "completion.disableSnippets" :json-false options)
(lspce--add-option "completion.resolveEagerly" :json-false options)
(lspce--add-option "diagnostics.enable" t options)
(lspce--add-option "hover.enable" t options)
(lspce--add-option "jediSettings.caseInsensitiveCompletion" t options)
(lspce--add-option "jediSettings.debug" :json-false options)
(lspce--add-option "markupKindPreferred" "markdown" options)
options))
;;; go gopls
(defun lspce-gopls-initializationOptions ()
(let ((options (make-hash-table :test #'equal)))
(lspce--add-option "settings.gopls.usePlaceholders" t options)))
;;; typescript deno
(defun lspce-deno-initializationOptions ()
(let ((options (make-hash-table :test #'equal)))
(lspce--add-option "enable" t options)
(lspce--add-option "lint" t options)
(lspce--add-option "unstable" :json-false options)
options))
(provide 'lspce-langs)