Skip to content

Commit 799ca89

Browse files
authored
feat: Add auto-installaion for Zig's zls (#4339)
* feat: Add auto-installaion for Zig's zls * fix compile error * fix indentation * Check with executable
1 parent 1666460 commit 799ca89

File tree

2 files changed

+122
-28
lines changed

2 files changed

+122
-28
lines changed

clients/lsp-json.el

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,8 @@
3636
:package-version '(lsp-mode . "6.3"))
3737

3838
(defcustom lsp-json-schemas nil
39-
"Associate schemas to JSON files in the current project example can be found \
40-
[here]\
41-
(https://github.com/emacs-lsp/lsp-mode/issues/3368#issuecomment-1049635155)."
39+
"Associate schemas to JSON files in the current project example can be found
40+
here, https://github.com/emacs-lsp/lsp-mode/issues/3368#issuecomment-1049635155."
4241
:type '(repeat alist)
4342
:group 'lsp-json
4443
:package-version '(lsp-mode . "6.3"))
@@ -61,29 +60,29 @@
6160
("http.proxyStrictSSL" lsp-http-proxyStrictSSL)))
6261

6362
(defvar lsp-json--extra-init-params
64-
`(:provideFormatter t
65-
:handledSchemaProtocols ["file" "http" "https"]))
63+
`( :provideFormatter t
64+
:handledSchemaProtocols ["file" "http" "https"]))
6665

6766
(defvar lsp-json--schema-associations
68-
`(:/*.css-data.json ["https://raw.githubusercontent.com/Microsoft/vscode-css-languageservice/master/docs/customData.schema.json"]
69-
:/package.json ["http://json.schemastore.org/package"]
70-
:/*.html-data.json ["https://raw.githubusercontent.com/Microsoft/vscode-html-languageservice/master/docs/customData.schema.json"]
71-
:/*.schema.json ["http://json-schema.org/draft-07/schema#"]
72-
:/bower.json ["http://json.schemastore.org/bower"]
73-
:/composer.json ["http://json.schemastore.org/composer"]
74-
:/tsconfig.json ["http://json.schemastore.org/tsconfig"]
75-
:/tsconfig.*.json ["http://json.schemastore.org/tsconfig"]
76-
:/typings.json ["http://json.schemastore.org/typings"]
77-
:/.bowerrc ["http://json.schemastore.org/bowerrc"]
78-
:/.babelrc ["http://json.schemastore.org/babelrc"]
79-
:/.babelrc.json ["http://json.schemastore.org/babelrc"]
80-
:/babel.config.json ["http://json.schemastore.org/babelrc"]
81-
:/jsconfig.json ["http://json.schemastore.org/jsconfig"]
82-
:/jsconfig.*.json ["http://json.schemastore.org/jsconfig"]
83-
:/project.json ["http://json.schemastore.org/project"]
84-
:/omnisharp.json ["http://json.schemastore.org/omnisharp"]
85-
:/.eslintrc.json ["http://json.schemastore.org/eslintrc"]
86-
:/.eslintrc ["http://json.schemastore.org/eslintrc"])
67+
`( :/*.css-data.json ["https://raw.githubusercontent.com/Microsoft/vscode-css-languageservice/master/docs/customData.schema.json"]
68+
:/package.json ["http://json.schemastore.org/package"]
69+
:/*.html-data.json ["https://raw.githubusercontent.com/Microsoft/vscode-html-languageservice/master/docs/customData.schema.json"]
70+
:/*.schema.json ["http://json-schema.org/draft-07/schema#"]
71+
:/bower.json ["http://json.schemastore.org/bower"]
72+
:/composer.json ["http://json.schemastore.org/composer"]
73+
:/tsconfig.json ["http://json.schemastore.org/tsconfig"]
74+
:/tsconfig.*.json ["http://json.schemastore.org/tsconfig"]
75+
:/typings.json ["http://json.schemastore.org/typings"]
76+
:/.bowerrc ["http://json.schemastore.org/bowerrc"]
77+
:/.babelrc ["http://json.schemastore.org/babelrc"]
78+
:/.babelrc.json ["http://json.schemastore.org/babelrc"]
79+
:/babel.config.json ["http://json.schemastore.org/babelrc"]
80+
:/jsconfig.json ["http://json.schemastore.org/jsconfig"]
81+
:/jsconfig.*.json ["http://json.schemastore.org/jsconfig"]
82+
:/project.json ["http://json.schemastore.org/project"]
83+
:/omnisharp.json ["http://json.schemastore.org/omnisharp"]
84+
:/.eslintrc.json ["http://json.schemastore.org/eslintrc"]
85+
:/.eslintrc ["http://json.schemastore.org/eslintrc"])
8786
"Default json schemas.")
8887

8988
(defun lsp-json--get-content (_workspace uri callback)

clients/lsp-zig.el

Lines changed: 99 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,111 @@
3333

3434
(defcustom lsp-zig-zls-executable "zls"
3535
"The zls executable to use.
36-
Leave as just the executable name to use the default behavior of
37-
finding the executable with variable `exec-path'."
36+
37+
Leave as just the executable name to use the default behavior of finding the
38+
executable with variable `exec-path'."
3839
:group 'lsp-zig
3940
:type 'string)
4041

42+
(defcustom lsp-zig-server-store-path
43+
(expand-file-name "zig/" lsp-server-install-dir)
44+
"The path to the file in which zls will be stored."
45+
:type 'file
46+
:group 'lsp-zig)
47+
48+
(defcustom lsp-zig-server-version "0.11.0"
49+
"The zls version to install."
50+
:type 'file
51+
:group 'lsp-zig)
52+
53+
(defconst lsp-zig-download-url-format
54+
"https://github.com/zigtools/zls/releases/download/%s/zls-%s-%s.%s"
55+
"Format to the download url link.")
56+
57+
(defun lsp-zig--zls-url ()
58+
"Return Url points to the zls' zip/tar file."
59+
(let* ((x86 (string-prefix-p "x86_64" system-configuration))
60+
(arch (if x86 "x86_64" "aarch64")))
61+
(cl-case system-type
62+
((cygwin windows-nt ms-dos)
63+
(format lsp-zig-download-url-format
64+
lsp-zig-server-version arch "windows" "zip"))
65+
(darwin
66+
(format lsp-zig-download-url-format
67+
lsp-zig-server-version arch "macos" "tar.gz"))
68+
(gnu/linux
69+
(format lsp-zig-download-url-format
70+
lsp-zig-server-version arch "linux" "tar.gz")))))
71+
72+
(defvar lsp-zig--server-download-url (lsp-zig--zls-url)
73+
"The actual url used to download language server.")
74+
75+
(defvar lsp-zig--downloaded-file (f-join lsp-zig-server-store-path "temp.tar")
76+
"The full file path after downloading the server zipped file.")
77+
78+
;;
79+
;;; Util
80+
81+
(defmacro lsp-zig--mute-apply (&rest body)
82+
"Execute BODY without message."
83+
(declare (indent 0) (debug t))
84+
`(let (message-log-max)
85+
(with-temp-message (or (current-message) nil)
86+
(let ((inhibit-message t)) ,@body))))
87+
88+
(defun lsp-zig--execute (cmd &rest args)
89+
"Return non-nil if CMD executed succesfully with ARGS."
90+
(save-window-excursion
91+
(lsp-zig--mute-apply
92+
(= 0 (shell-command (concat cmd " "
93+
(mapconcat #'shell-quote-argument
94+
(cl-remove-if #'null args)
95+
" ")))))))
96+
97+
;;
98+
;;; Installation
99+
100+
(defun lsp-zig--stored-zls-executable ()
101+
"Return the stored zls executable.
102+
103+
This is differ from the variable `lsp-zig-zls-executable'; this is local storage
104+
and not the global storage."
105+
(executable-find (f-join lsp-zig-server-store-path "bin/zls")))
106+
107+
(defun lsp-zig--extract-compressed-file ()
108+
"Install zls."
109+
(cond ((file-exists-p lsp-zig--downloaded-file)
110+
;; Suprisingly, you can just use `tar' to unzip a zip file on Windows.
111+
;; Therefore, just use the same command.
112+
(lsp-zig--execute "tar" "-xvzf" lsp-zig--downloaded-file "-C" lsp-zig-server-store-path)
113+
;; Delete the zip file.
114+
(ignore-errors (delete-file lsp-zig--downloaded-file)))
115+
(t
116+
(error "Can't extract the downloaded file: %s" lsp-zig--downloaded-file))))
117+
118+
(lsp-dependency
119+
'zls
120+
'(:system "zls")
121+
`(:download :url ,lsp-zig--server-download-url
122+
:store-path ,lsp-zig--downloaded-file))
123+
124+
;;
125+
;;; Core
126+
41127
(lsp-register-client
42128
(make-lsp-client
43-
:new-connection (lsp-stdio-connection (lambda () lsp-zig-zls-executable))
129+
:new-connection (lsp-stdio-connection
130+
(lambda () (or (executable-find lsp-zig-zls-executable)
131+
(lsp-zig--stored-zls-executable)))
132+
(lambda ()
133+
(or (executable-find lsp-zig-zls-executable)
134+
(file-executable-p (lsp-zig--stored-zls-executable)))))
44135
:activation-fn (lsp-activate-on "zig")
45-
:server-id 'zls))
136+
:priority -1
137+
:server-id 'zls
138+
:download-server-fn
139+
(lambda (_client _callback error-callback _update?)
140+
(lsp-package-ensure 'zls #'lsp-zig--extract-compressed-file error-callback))))
46141

47142
(lsp-consistency-check lsp-zig)
48143

0 commit comments

Comments
 (0)