Skip to content

Commit

Permalink
feat: remove 'multi-server' key, implement list of server config inst…
Browse files Browse the repository at this point in the history
…ead (update doc)
  • Loading branch information
sfavazza committed Dec 9, 2023
1 parent 8461f24 commit e6dd277
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 41 deletions.
25 changes: 19 additions & 6 deletions README.org
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,11 @@
#+begin_src bash
EMACS_D_VOLUME=/path/to/spacemacs bash start-spacemacs.sh
#+end_src
** Custom language server containers

** Custom language server containers
You can use manually built language containers or images hosting language server(s), just follow a few simple rules (shown below).
The docker images may feature an optional tag, if omitted _latest_ will be assumed.

*** Building a container (or an image) manually:
You have 2 constraints:
- A language server must be launched in =stdio= mode (other types of communication are yet to be supported)
Expand All @@ -147,6 +147,7 @@
It is structured in the following way:

#+begin_src yaml
# signle server configuration
lsp:
server:
type: docker
Expand All @@ -156,7 +157,7 @@ It is structured in the following way:
# (see Automatic image building). An image might feature an optional tag, i.e. '<image>:<tag>'. If a
# tagless image is indicated 'latest' will be assumed.
subtype: container
# image/container name to use for this language server
# Image/container name to use for this language server.
name: image-container-name
# server id of a registered LSP server. You can find the list of registered servers evaluating:
#
Expand All @@ -173,14 +174,26 @@ It is structured in the following way:
# NOTE: the paths must be within the project this server is being build for
- source: "/your/host/source/path"
destination: "/your/path/inside/the/container"

# multiple server configuration
lsp:
server:
- type: ...
subtype: ...
... # keys as in the classic single server case, e.g. type, subtype, etc...
- ... # other single server configuration(s)
mappings:
- source: <path-on-host>
destination: <path-on-lang-server>
... # other mappings
#+end_src

*** Registering a language server using a =.dir-locals= file:
Just refer to the source code and general conventions of using =.dir-locals=. The variable you need is =lsp-docker-persistent-default-config=, its content is merged with the =lsp= section from a configuration file (if present).

*** Automatic image building:
You can also build an image automatically (currently supported only for =image= subtype): just drop the corresponding =Dockerfile= into the =.lsp-docker= folder in the project root (=Dockerfile= may be named as =Dockerfile= or =Dockerfile.lsp=). Building process is triggered by the =lsp-docker-register= call (you will be prompted whether you want to build the image). Image building *takes place in the project root* (*not* in the =.lsp-docker= subfolder)! In case of an automatic build the image will be registered automatically (based on the values from the config or =.dir-locals= file).

You can also troubleshoot any issues with supplemental docker calls (checking whether the required image already exists, building a new image) using the supplemental logging functionality: there are 2 variables: first you have to set =lsp-docker-log-docker-supplemental-calls= to true-like value (by default it is =nil=) and then specify the log buffer in the =lsp-docker-log-docker-supplemental-calls-buffer-name= variable (by default it is set to =*lsp-docker-supplemental-calls*=)

** Docker over TRAMP (TBD)
Expand Down
56 changes: 21 additions & 35 deletions lsp-docker.el
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,6 @@ registered servers evaluating: `(ht-keys lsp-clients)'.")
not used when the `lsp-docker--srv-cfg-subtype-key' is set to
container, as the server command shall be the entrypoint.")

(defconst lsp-docker--multi-server-yml-key 'multi-server
"multi-server YAML key, if matched it is assumed multiple language servers are being configured")

(defun lsp-docker--log-docker-supplemental-calls-p ()
"Return non-nil if should log docker invocation commands"
lsp-docker-log-docker-supplemental-calls)
Expand Down Expand Up @@ -304,12 +301,10 @@ be bigger than default servers in order to override them)")
(if (f-exists? project-config-file-path)
(if-let* ((whole-config (yaml-parse-string (f-read project-config-file-path)))
(lsp-config (gethash lsp-docker--lsp-key whole-config)))
(cond
;; DO NOT merge to the persistent configuration when a multi-server one is detected
((gethash lsp-docker--multi-server-yml-key lsp-config)
lsp-config)
(t ; use default values for missing fields in the provided configuration
(ht-merge (ht-copy lsp-docker-persistent-default-config) lsp-config))))))
;; use default values for missing fields in the provided configuration
(if (vectorp (gethash lsp-docker--server-key lsp-config))
lsp-config ; DO NOT merge to the persistent configuration when a multi-server one is detected
(ht-merge (ht-copy lsp-docker-persistent-default-config) lsp-config)))))

(defun lsp-docker--find-project-config-file-from-lsp ()
"Get the LSP configuration file path (project-local configuration, using lsp-mode)"
Expand Down Expand Up @@ -367,9 +362,7 @@ be bigger than default servers in order to override them)")

(defun lsp-docker-get-server-id (server-config)
"Get the server id from the SERVER-CONFIG hash-table"
(if (stringp (gethash lsp-docker--srv-cfg-server-key server-config))
(intern (gethash lsp-docker--srv-cfg-server-key server-config))
(gethash lsp-docker--srv-cfg-server-key server-config)))
(gethash lsp-docker--srv-cfg-server-key server-config))

(defun lsp-docker--get-base-client (base-server-id)
"Get the base lsp client associated to BASE-SERVER-ID key for
Expand Down Expand Up @@ -732,36 +725,29 @@ dockerized server."
"Register one or more dockerized language servers for the current project"
(interactive)
(if (lsp-workspace-root)
(let* (
(config (lsp-docker-get-config-from-lsp))
(let* ((config (lsp-docker-get-config-from-lsp))
(project-root (lsp-workspace-root))
(path-mappings (lsp-docker-get-path-mappings config (lsp-workspace-root)))
(single-server-config (gethash lsp-docker--server-key config))
(multi-server-config (gethash lsp-docker--multi-server-yml-key config)))
(server-config (gethash lsp-docker--server-key config)))

;; check whether a single or multiple servers are described in the configuration
(cond
(single-server-config
(if (and single-server-config multi-server-config)
(display-warning "lsp-docker"
(concat "both single/multiple server configuration detected, "
"ignoring multi-server configuration")
:warning
"*Warnings lsp-docker*"))
(message "registering a single server")
(lsp-docker--register-single-server single-server-config project-root path-mappings))

(multi-server-config
((vectorp server-config)
(message "registering multiple servers")
(--map (lsp-docker--register-single-server
it
project-root
path-mappings)
multi-server-config))
;; NOTE: if multiple language server descriptions share the same name "server" field, the latest entry
;; will be enforced.
(--map (lsp-docker--register-single-server it
project-root
path-mappings)
server-config))
(server-config
(message "registering a single server")
(lsp-docker--register-single-server server-config
project-root
path-mappings))
(t
(user-error "no '%s' neither '%s' keywords found in configuration file"
lsp-docker--server-key
lsp-docker--multi-server-yml-key))))
(user-error "no `%s' node found in configuration, see README for reference"
lsp-docker--server-key))))
(user-error
(format (concat "Current file: %s is not in a registered project! "
"Try adding your project with `lsp-workspace-folders-add'")
Expand Down

0 comments on commit e6dd277

Please sign in to comment.