-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathlsp-docker.el
471 lines (419 loc) · 23.8 KB
/
lsp-docker.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
;;; lsp-docker.el --- LSP Docker integration -*- lexical-binding: t; -*-
;; Copyright (C) 2019 Ivan Yonchovski
;; Author: Ivan Yonchovski <[email protected]>
;; URL: https://github.com/emacs-lsp/lsp-docker
;; Keywords: languages langserver
;; Version: 1.0.0
;; Package-Requires: ((emacs "25.1") (dash "2.14.1") (lsp-mode "6.2.1") (f "0.20.0") (yaml "0.2.0") (ht "2.0"))
;; 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:
;; Run language servers in containers
;;; Code:
(require 'lsp-mode)
(require 'dash)
(require 'f)
(require 'yaml)
(require 'ht)
(defun lsp-docker--uri->path (path-mappings docker-container-name uri)
"Turn docker URI into host path.
Argument PATH-MAPPINGS dotted pair of (host-path . container-path).
Argument DOCKER-CONTAINER-NAME name to use when running container.
Argument URI the uri to translate."
(let ((path (lsp--uri-to-path-1 uri)))
(-if-let ((local . remote) (-first (-lambda ((_ . docker-path))
(s-contains? docker-path path))
path-mappings))
(s-replace remote local path)
(format "/docker:%s:%s" docker-container-name path))))
(defun lsp-docker--path->uri (path-mappings path)
"Turn host PATH into docker uri.
Argument PATH-MAPPINGS dotted pair of (host-path . container-path).
Argument PATH the path to translate."
(lsp--path-to-uri-1
(-if-let ((local . remote) (-first (-lambda ((local-path . _))
(s-contains? local-path path))
path-mappings))
(s-replace local remote path)
(user-error "The path %s is not under path mappings" path))))
(defvar lsp-docker-container-name-suffix 0
"Used to prevent collision of container names.")
(defvar lsp-docker-command "docker"
"The docker command to use.")
(defun lsp-docker-launch-new-container (docker-container-name path-mappings docker-image-id server-command)
"Return the docker command to be executed on host.
Argument DOCKER-CONTAINER-NAME name to use for container.
Argument PATH-MAPPINGS dotted pair of (host-path . container-path).
Argument DOCKER-IMAGE-ID the docker container to run language servers with.
Argument SERVER-COMMAND the language server command to run inside the container."
(split-string
(--doto (format "%s run --name %s --rm -i %s %s %s"
lsp-docker-command
docker-container-name
(->> path-mappings
(-map (-lambda ((path . docker-path))
(format "-v %s:%s" path docker-path)))
(s-join " "))
docker-image-id
server-command))
" "))
(defun lsp-docker-exec-in-container (docker-container-name server-command)
"Return command to exec into running container.
Argument DOCKER-CONTAINER-NAME name of container to exec into.
Argument SERVER-COMMAND the command to execute inside the running container."
(split-string
(format "docker exec -i %s %s" docker-container-name server-command)))
(cl-defun lsp-docker-register-client (&key server-id
docker-server-id
path-mappings
docker-image-id
docker-container-name
priority
server-command
launch-server-cmd-fn)
"Registers docker clients with lsp"
(if-let ((client (copy-lsp--client (gethash server-id lsp-clients))))
(progn
(let ((docker-container-name-full
(if lsp-docker-container-name-suffix
(format "%s-%d"
docker-container-name
(if (numberp lsp-docker-container-name-suffix)
(cl-incf lsp-docker-container-name-suffix)
lsp-docker-container-name-suffix))
docker-container-name)))
(setf (lsp--client-server-id client) docker-server-id
(lsp--client-uri->path-fn client) (-partial #'lsp-docker--uri->path
path-mappings
docker-container-name-full)
(lsp--client-path->uri-fn client) (-partial #'lsp-docker--path->uri path-mappings)
(lsp--client-new-connection client) (plist-put
(lsp-stdio-connection
(lambda ()
(funcall (or launch-server-cmd-fn #'lsp-docker-launch-new-container)
docker-container-name-full
path-mappings
docker-image-id
server-command)))
:test? (lambda (&rest _)
(-any?
(-lambda ((dir))
(f-ancestor-of? dir (buffer-file-name)))
path-mappings)))
(lsp--client-priority client) (or priority (lsp--client-priority client))))
(lsp-register-client client))
(user-error "No such client %s" server-id)))
(defvar lsp-docker-default-client-packages
'(lsp-bash
lsp-clangd
lsp-css
lsp-dockerfile
lsp-go
lsp-html
lsp-javascript
lsp-pyls)
"Default list of client packages to load.")
(defvar lsp-docker-default-client-configs
(list
(list :server-id 'bash-ls :docker-server-id 'bashls-docker :server-command "bash-language-server start")
(list :server-id 'clangd :docker-server-id 'clangd-docker :server-command "ccls")
(list :server-id 'css-ls :docker-server-id 'cssls-docker :server-command "css-languageserver --stdio")
(list :server-id 'dockerfile-ls :docker-server-id 'dockerfilels-docker :server-command "docker-langserver --stdio")
(list :server-id 'gopls :docker-server-id 'gopls-docker :server-command "gopls")
(list :server-id 'html-ls :docker-server-id 'htmls-docker :server-command "html-languageserver --stdio")
(list :server-id 'pyls :docker-server-id 'pyls-docker :server-command "pyls")
(list :server-id 'ts-ls :docker-server-id 'tsls-docker :server-command "typescript-language-server --stdio"))
"Default list of client configurations.")
(cl-defun lsp-docker-init-clients (&key
path-mappings
(docker-image-id "emacslsp/lsp-docker-langservers")
(docker-container-name "lsp-container")
(priority 10)
(client-packages lsp-docker-default-client-packages)
(client-configs lsp-docker-default-client-configs))
"Loads the required client packages and registers the required clients to run with docker.
:path-mappings is an alist of local paths and their mountpoints
in the docker container.
Example: '((\"/path/to/projects\" . \"/projects\"))
:docker-image-id is the identifier for the docker image to be
used for all clients, as a string.
:docker-container-name is the name to use for the container when
it is started.
:priority is the priority with which to register the docker
clients with lsp. (See the library ‘lsp-clients’ for details.)
:client-packages is a list of libraries to load before registering the clients.
:client-configs is a list of configurations for the various
clients you wish to use with ‘lsp-docker’. Each element takes
the form
'(:server-id 'example-ls
:docker-server-id 'examplels-docker
:docker-image-id \"examplenamespace/examplels-docker:x.y\"
:docker-container-name \"examplels-container\"
:server-command \"run_example_ls.sh\")
where
:server-id is the ID of the language server, as defined in the
library ‘lsp-clients’.
:docker-server-id is any arbitrary unique symbol used internally
by ‘lsp’ to distinguish it from non-docker clients for the same
server.
:docker-image-id is an optional property to override this
function's :docker-image-id argument for just this client. If
you specify this, you MUST also specify :docker-container-name.
:docker-container-name is an optional property to override this
function's :docker-container-name argument for just this client.
This MUST be specified if :docker-image-id is specified, but is
otherwise optional.
:server-command is a string specifying the command to run inside
the docker container to run the language server."
(seq-do (lambda (package) (require package nil t)) client-packages)
(let ((default-docker-image-id docker-image-id)
(default-docker-container-name docker-container-name))
(seq-do (-lambda ((&plist :server-id :docker-server-id :docker-image-id :docker-container-name :server-command))
(when (and docker-image-id (not docker-container-name))
(user-error "Invalid client definition for server ID %S. You must specify a container name when specifying an image ID."
server-id))
(lsp-docker-register-client
:server-id server-id
:priority priority
:docker-server-id docker-server-id
:docker-image-id (or docker-image-id default-docker-image-id)
:docker-container-name (if docker-image-id
docker-container-name
default-docker-container-name)
:server-command server-command
:path-mappings path-mappings
:launch-server-cmd-fn #'lsp-docker-launch-new-container))
client-configs)))
(defvar lsp-docker-default-priority
100
"Default lsp-docker containerized servers priority (it needs to be bigger than default servers in order to override them)")
(defcustom lsp-docker-persistent-default-config
(ht ('server (ht ('type "docker")
('subtype "image")
('name "emacslsp/lsp-docker-langservers")
('server nil)
('launch_command nil)))
('mappings [
(ht ('source ".")
('destination "/projects"))
]))
"Default configuration for all language servers with persistent configurations"
:type 'hash-table
:group 'lsp-docker
)
(defun lsp-docker-get-config-from-project-config-file (project-config-file-path)
"Get the LSP configuration based on a project configuration file"
(if (f-exists? project-config-file-path)
(if-let* ((whole-config (yaml-parse-string (f-read project-config-file-path)))
(lsp-config (gethash 'lsp whole-config)))
(ht-merge (ht-copy lsp-docker-persistent-default-config) lsp-config))))
(defun lsp-docker-get-config-from-lsp ()
"Get the LSP configuration based on a project-local configuration (using lsp-mode)"
(let ((project-config-file-path (if (f-exists? (f-join (lsp-workspace-root) ".lsp-docker.yml"))
(f-join (lsp-workspace-root) ".lsp-docker.yml")
(f-join (lsp-workspace-root) ".lsp-docker.yaml"))))
(or (lsp-docker-get-config-from-project-config-file project-config-file-path)
(ht-copy lsp-docker-persistent-default-config))))
(defvar lsp-docker-supported-server-types-subtypes
(ht ('docker (list 'container 'image)))
"A list of all supported server types and subtypes, currently only docker is supported")
(defun lsp-docker-get-server-type-subtype (config)
"Get the server type"
(let* ((lsp-server-info (gethash 'server config))
(lsp-server-type (gethash 'type lsp-server-info))
(lsp-server-subtype (gethash 'subtype lsp-server-info)))
(cons (if (stringp lsp-server-type)
(intern lsp-server-type)
lsp-server-type)
(if (stringp lsp-server-subtype)
(intern lsp-server-subtype)
lsp-server-subtype))))
(defun lsp-docker-get-server-container-name (config)
"Get the server container name"
(let* ((lsp-server-info (gethash 'server config))
(lsp-server-subtype (gethash 'subtype lsp-server-info)))
(if (equal lsp-server-subtype "container")
(gethash 'name lsp-server-info))))
(defun lsp-docker-get-server-image-name (config)
"Get the server image name"
(let* ((lsp-server-info (gethash 'server config))
(lsp-server-subtype (gethash 'subtype lsp-server-info)))
(if (equal lsp-server-subtype "image")
(gethash 'name lsp-server-info))))
(defun lsp-docker-get-server-id (config)
"Get the server id"
(let ((lsp-server-info (gethash 'server config)))
(if (stringp (gethash 'server lsp-server-info))
(intern (gethash 'server lsp-server-info))
(gethash 'server lsp-server-info))))
(defun lsp-docker-get-path-mappings (config)
"Get the server path mappings"
(if-let ((lsp-mappings-info (gethash 'mappings config)))
(--map (cons (f-canonical (gethash 'source it)) (gethash 'destination it)) lsp-mappings-info)
(user-error "No path mappings specified!")))
(defun lsp-docker-get-launch-command (config)
"Get the server launch command"
(let ((lsp-server-info (gethash 'server config)))
(gethash 'launch_command lsp-server-info)))
(defun lsp-docker-check-server-type-subtype (supported-server-types-subtypes server-type-subtype)
"Verify that the combination of server (type . subtype) is supported by the current implementation"
(if (not server-type-subtype)
(user-error "No server type and subtype specified!"))
(if (ht-find (lambda (type subtypes)
(let ((server-type (car server-type-subtype))
(server-subtype (cdr server-type-subtype)))
(if (and (equal server-type type) (-contains? subtypes server-subtype))
t)))
supported-server-types-subtypes)
server-type-subtype
(user-error "No compatible server type and subtype found!")))
(defun lsp-docker-check-path-mappings (path-mappings)
"Verify that specified path mappings are all inside the project directory"
(--all? (or (f-descendant-of? (f-canonical (car it)) (f-canonical (lsp-workspace-root)))
(f-same? (f-canonical (car it)) (f-canonical (lsp-workspace-root))))
path-mappings))
(defun lsp-docker-verify-path-mappings-against-container (path-mappings container-name)
"Verify that specified path mappings are all included in container's path mappings"
(--all? (let ((source (car it))
(destination (cdr it)))
(-any? (lambda (mapping)
(and (f-same? source (car mapping))
(equal destination (cdr mapping))))
(lsp-docker-get-path-mappings-from-container container-name)))
path-mappings))
(defun lsp-docker-get-path-mappings-from-container (container-name)
"Get path mappings from a container"
(-let ((
(inspection-command-program . inspection-command-arguments)
(--map-when
(equal it "'{{.Mounts}}'")
"'{{json .Mounts}}'"
(s-split " " (format "%s container inspect -f '{{.Mounts}}' %s" lsp-docker-command container-name)))))
(-let (((exit-code . raw-output) (with-temp-buffer
(cons
(apply #'call-process inspection-command-program nil (current-buffer) nil inspection-command-arguments)
(buffer-string)))))
(if (equal exit-code 0)
(let* ((output (s-chop-prefix "'" (s-chop-suffix "'" (s-chomp raw-output))))
(raw-mappings (append (json-parse-string output) nil)) ; using append to convert a vector to a list
(bind-mappings (--filter (and (equal (gethash "Type" it) "bind") (equal (gethash "RW" it) t)) raw-mappings)))
(--map (cons (f-canonical (gethash "Source" it)) (f-canonical (gethash "Destination" it))) bind-mappings))
(user-error "Cannot analyze the following container: %s, exit code: %d" container-name exit-code)))))
(defun lsp-docker-launch-existing-container (docker-container-name &rest _unused)
"Return the docker command to be executed on host.
Argument DOCKER-CONTAINER-NAME name to use for container."
(split-string
(format "%s start -i %s"
lsp-docker-command
docker-container-name)
" "))
(defmacro lsp-docker-create-activation-function-by-project-dir (project-dir)
`(lambda (&rest unused)
(let ((current-project-root (lsp-workspace-root))
(registered-project-root ,project-dir))
(f-same? current-project-root registered-project-root))))
(defun lsp-docker-generate-docker-server-id (config project-root)
"Generate the docker-server-id from the project config"
(let ((original-server-id (symbol-name (lsp-docker-get-server-id config)))
(project-path-server-id-part (s-chop-prefix "-" (s-replace-all '(("/" . "-") ("." . "")) project-root))))
(intern (s-join "-" (list project-path-server-id-part original-server-id "docker")))))
(cl-defun lsp-docker-register-client-with-activation-fn (&key server-id
docker-server-id
path-mappings
docker-image-id
docker-container-name
(docker-container-name-suffix lsp-docker-container-name-suffix)
activation-fn
priority
server-command
launch-server-cmd-fn)
"Registers docker clients with lsp (by persisting configuration)"
(if-let ((client (copy-lsp--client (gethash server-id lsp-clients))))
(let ((docker-container-name-full
(if docker-container-name-suffix
(format "%s-%d"
docker-container-name
(if (numberp docker-container-name-suffix)
(cl-incf docker-container-name-suffix)
docker-container-name-suffix))
docker-container-name)))
(setf (lsp--client-server-id client) docker-server-id
(lsp--client-uri->path-fn client) (-partial #'lsp-docker--uri->path
path-mappings
docker-container-name-full)
(lsp--client-activation-fn client) activation-fn
(lsp--client-path->uri-fn client) (-partial #'lsp-docker--path->uri path-mappings)
(lsp--client-new-connection client) (plist-put
(lsp-stdio-connection
(lambda ()
(funcall (or launch-server-cmd-fn #'lsp-docker-launch-new-container)
docker-container-name-full
path-mappings
docker-image-id
server-command)))
:test? (lambda (&rest _)
t))
(lsp--client-priority client) (or priority (lsp--client-priority client)))
(lsp-register-client client)
(message "Registered a language server with id: %s and container name: %s" docker-server-id docker-container-name-full))
(user-error "No such client %s" server-id)))
(defun lsp-docker-register ()
"Register a server to use LSP mode in a container for the current project"
(interactive)
(if (lsp-workspace-root)
(let* (
(config (lsp-docker-get-config-from-lsp))
(server-type-subtype (lsp-docker-get-server-type-subtype config))
(server-container-name (lsp-docker-get-server-container-name config))
(server-image-name (lsp-docker-get-server-image-name config))
(path-mappings (lsp-docker-get-path-mappings config))
(regular-server-id (lsp-docker-get-server-id config))
(server-id (lsp-docker-generate-docker-server-id config (lsp-workspace-root)))
(server-launch-command (lsp-docker-get-launch-command config)))
(if (and (lsp-docker-check-server-type-subtype lsp-docker-supported-server-types-subtypes server-type-subtype)
(lsp-docker-check-path-mappings path-mappings))
(let ((container-type (car server-type-subtype))
(container-subtype (cdr server-type-subtype)))
(pcase container-type
('docker (pcase container-subtype
('image (lsp-docker-register-client-with-activation-fn
:server-id regular-server-id
:docker-server-id server-id
:path-mappings path-mappings
:docker-image-id server-image-name
:docker-container-name server-container-name
:docker-container-name-suffix nil
:activation-fn (lsp-docker-create-activation-function-by-project-dir (lsp-workspace-root))
:priority lsp-docker-default-priority
:server-command server-launch-command
:launch-server-cmd-fn #'lsp-docker-launch-new-container))
('container (if (lsp-docker-verify-path-mappings-against-container path-mappings server-container-name)
(lsp-docker-register-client-with-activation-fn
:server-id regular-server-id
:docker-server-id server-id
:path-mappings path-mappings
:docker-image-id nil
:docker-container-name server-container-name
:docker-container-name-suffix nil
:activation-fn (lsp-docker-create-activation-function-by-project-dir (lsp-workspace-root))
:priority lsp-docker-default-priority
:server-command server-launch-command
:launch-server-cmd-fn #'lsp-docker-launch-existing-container)
(user-error "Container path mappings don't match the config ones!")))))))
(user-error "Invalid LSP docker config: unsupported server type and/or subtype or malformed mappings!")))
(user-error (format "Current file: %s is not in a registered project!" (buffer-file-name)))))
(defun lsp-docker-start ()
"Register and launch a server to use LSP mode in a container for the current project"
(interactive)
(lsp-docker-register)
(lsp))
(provide 'lsp-docker)
;;; lsp-docker.el ends here