Skip to content

Commit

Permalink
feat: Add auto-installaion for Zig's zls (#4339)
Browse files Browse the repository at this point in the history
* feat: Add auto-installaion for Zig's zls

* fix compile error

* fix indentation

* Check with executable
  • Loading branch information
jcs090218 authored Feb 19, 2024
1 parent 1666460 commit 799ca89
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 28 deletions.
47 changes: 23 additions & 24 deletions clients/lsp-json.el
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,8 @@
:package-version '(lsp-mode . "6.3"))

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

(defvar lsp-json--extra-init-params
`(:provideFormatter t
:handledSchemaProtocols ["file" "http" "https"]))
`( :provideFormatter t
:handledSchemaProtocols ["file" "http" "https"]))

(defvar lsp-json--schema-associations
`(:/*.css-data.json ["https://raw.githubusercontent.com/Microsoft/vscode-css-languageservice/master/docs/customData.schema.json"]
:/package.json ["http://json.schemastore.org/package"]
:/*.html-data.json ["https://raw.githubusercontent.com/Microsoft/vscode-html-languageservice/master/docs/customData.schema.json"]
:/*.schema.json ["http://json-schema.org/draft-07/schema#"]
:/bower.json ["http://json.schemastore.org/bower"]
:/composer.json ["http://json.schemastore.org/composer"]
:/tsconfig.json ["http://json.schemastore.org/tsconfig"]
:/tsconfig.*.json ["http://json.schemastore.org/tsconfig"]
:/typings.json ["http://json.schemastore.org/typings"]
:/.bowerrc ["http://json.schemastore.org/bowerrc"]
:/.babelrc ["http://json.schemastore.org/babelrc"]
:/.babelrc.json ["http://json.schemastore.org/babelrc"]
:/babel.config.json ["http://json.schemastore.org/babelrc"]
:/jsconfig.json ["http://json.schemastore.org/jsconfig"]
:/jsconfig.*.json ["http://json.schemastore.org/jsconfig"]
:/project.json ["http://json.schemastore.org/project"]
:/omnisharp.json ["http://json.schemastore.org/omnisharp"]
:/.eslintrc.json ["http://json.schemastore.org/eslintrc"]
:/.eslintrc ["http://json.schemastore.org/eslintrc"])
`( :/*.css-data.json ["https://raw.githubusercontent.com/Microsoft/vscode-css-languageservice/master/docs/customData.schema.json"]
:/package.json ["http://json.schemastore.org/package"]
:/*.html-data.json ["https://raw.githubusercontent.com/Microsoft/vscode-html-languageservice/master/docs/customData.schema.json"]
:/*.schema.json ["http://json-schema.org/draft-07/schema#"]
:/bower.json ["http://json.schemastore.org/bower"]
:/composer.json ["http://json.schemastore.org/composer"]
:/tsconfig.json ["http://json.schemastore.org/tsconfig"]
:/tsconfig.*.json ["http://json.schemastore.org/tsconfig"]
:/typings.json ["http://json.schemastore.org/typings"]
:/.bowerrc ["http://json.schemastore.org/bowerrc"]
:/.babelrc ["http://json.schemastore.org/babelrc"]
:/.babelrc.json ["http://json.schemastore.org/babelrc"]
:/babel.config.json ["http://json.schemastore.org/babelrc"]
:/jsconfig.json ["http://json.schemastore.org/jsconfig"]
:/jsconfig.*.json ["http://json.schemastore.org/jsconfig"]
:/project.json ["http://json.schemastore.org/project"]
:/omnisharp.json ["http://json.schemastore.org/omnisharp"]
:/.eslintrc.json ["http://json.schemastore.org/eslintrc"]
:/.eslintrc ["http://json.schemastore.org/eslintrc"])
"Default json schemas.")

(defun lsp-json--get-content (_workspace uri callback)
Expand Down
103 changes: 99 additions & 4 deletions clients/lsp-zig.el
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,111 @@

(defcustom lsp-zig-zls-executable "zls"
"The zls executable to use.
Leave as just the executable name to use the default behavior of
finding the executable with variable `exec-path'."
Leave as just the executable name to use the default behavior of finding the
executable with variable `exec-path'."
:group 'lsp-zig
:type 'string)

(defcustom lsp-zig-server-store-path
(expand-file-name "zig/" lsp-server-install-dir)
"The path to the file in which zls will be stored."
:type 'file
:group 'lsp-zig)

(defcustom lsp-zig-server-version "0.11.0"
"The zls version to install."
:type 'file
:group 'lsp-zig)

(defconst lsp-zig-download-url-format
"https://github.com/zigtools/zls/releases/download/%s/zls-%s-%s.%s"
"Format to the download url link.")

(defun lsp-zig--zls-url ()
"Return Url points to the zls' zip/tar file."
(let* ((x86 (string-prefix-p "x86_64" system-configuration))
(arch (if x86 "x86_64" "aarch64")))
(cl-case system-type
((cygwin windows-nt ms-dos)
(format lsp-zig-download-url-format
lsp-zig-server-version arch "windows" "zip"))
(darwin
(format lsp-zig-download-url-format
lsp-zig-server-version arch "macos" "tar.gz"))
(gnu/linux
(format lsp-zig-download-url-format
lsp-zig-server-version arch "linux" "tar.gz")))))

(defvar lsp-zig--server-download-url (lsp-zig--zls-url)
"The actual url used to download language server.")

(defvar lsp-zig--downloaded-file (f-join lsp-zig-server-store-path "temp.tar")
"The full file path after downloading the server zipped file.")

;;
;;; Util

(defmacro lsp-zig--mute-apply (&rest body)
"Execute BODY without message."
(declare (indent 0) (debug t))
`(let (message-log-max)
(with-temp-message (or (current-message) nil)
(let ((inhibit-message t)) ,@body))))

(defun lsp-zig--execute (cmd &rest args)
"Return non-nil if CMD executed succesfully with ARGS."
(save-window-excursion
(lsp-zig--mute-apply
(= 0 (shell-command (concat cmd " "
(mapconcat #'shell-quote-argument
(cl-remove-if #'null args)
" ")))))))

;;
;;; Installation

(defun lsp-zig--stored-zls-executable ()
"Return the stored zls executable.
This is differ from the variable `lsp-zig-zls-executable'; this is local storage
and not the global storage."
(executable-find (f-join lsp-zig-server-store-path "bin/zls")))

(defun lsp-zig--extract-compressed-file ()
"Install zls."
(cond ((file-exists-p lsp-zig--downloaded-file)
;; Suprisingly, you can just use `tar' to unzip a zip file on Windows.
;; Therefore, just use the same command.
(lsp-zig--execute "tar" "-xvzf" lsp-zig--downloaded-file "-C" lsp-zig-server-store-path)
;; Delete the zip file.
(ignore-errors (delete-file lsp-zig--downloaded-file)))
(t
(error "Can't extract the downloaded file: %s" lsp-zig--downloaded-file))))

(lsp-dependency
'zls
'(:system "zls")
`(:download :url ,lsp-zig--server-download-url
:store-path ,lsp-zig--downloaded-file))

;;
;;; Core

(lsp-register-client
(make-lsp-client
:new-connection (lsp-stdio-connection (lambda () lsp-zig-zls-executable))
:new-connection (lsp-stdio-connection
(lambda () (or (executable-find lsp-zig-zls-executable)
(lsp-zig--stored-zls-executable)))
(lambda ()
(or (executable-find lsp-zig-zls-executable)
(file-executable-p (lsp-zig--stored-zls-executable)))))
:activation-fn (lsp-activate-on "zig")
:server-id 'zls))
:priority -1
:server-id 'zls
:download-server-fn
(lambda (_client _callback error-callback _update?)
(lsp-package-ensure 'zls #'lsp-zig--extract-compressed-file error-callback))))

(lsp-consistency-check lsp-zig)

Expand Down

0 comments on commit 799ca89

Please sign in to comment.