|
33 | 33 |
|
34 | 34 | (defcustom lsp-zig-zls-executable "zls"
|
35 | 35 | "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'." |
38 | 39 | :group 'lsp-zig
|
39 | 40 | :type 'string)
|
40 | 41 |
|
| 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 | + |
41 | 127 | (lsp-register-client
|
42 | 128 | (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))))) |
44 | 135 | :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)))) |
46 | 141 |
|
47 | 142 | (lsp-consistency-check lsp-zig)
|
48 | 143 |
|
|
0 commit comments