Skip to content

Commit

Permalink
Implement automatic downloading facilities
Browse files Browse the repository at this point in the history
- Fixes emacs-lsp#506

- Implemented base facilities for downloading and installing language servers
- added new field :download-server-fn in lsp--client structure which will be
called with (client callback update?).
- Implemented automatic installation via npm for jsts-ls and ts-ls.
- All servers(when feasible) should be installed under `lsp-server-install-dir`.

@akirak - check the cl-defgenerics in lsp-mode - let me know if they are
sufficient to implement the nix variands.

@razzmatazz - I have ported CSharp implementation but I havent converted it into
async.

@seagle0128 - lsp-python-ms is not ported. In general, it will work as it is but
it will ask to install the server even if you are in different file.

@TOTBWF - F# same as C#.

@kiennq powershell installation is converted to async one.
  • Loading branch information
Ivan authored and yyoncho committed Dec 28, 2019
1 parent 4b22c40 commit 1f670a3
Show file tree
Hide file tree
Showing 8 changed files with 349 additions and 170 deletions.
87 changes: 45 additions & 42 deletions lsp-clients.el
Original file line number Diff line number Diff line change
Expand Up @@ -158,14 +158,6 @@ This directory shoud contain a file matching groovy-language-server-*.jar"
:group 'lsp-mode
:link '(url-link "https://github.com/sourcegraph/javascript-typescript-langserver"))

(defcustom lsp-clients-javascript-typescript-server "javascript-typescript-stdio"
"The javascript-typescript-stdio executable to use.
Leave as just the executable name to use the default behavior of
finding the executable with variable `exec-path'."
:group 'lsp-typescript-javascript
:risky t
:type 'file)

(defcustom lsp-clients-typescript-javascript-server-args '()
"Extra arguments for the typescript-language-server language server."
:group 'lsp-typescript-javascript
Expand All @@ -179,13 +171,18 @@ finding the executable with variable `exec-path'."

(lsp-register-client
(make-lsp-client :new-connection (lsp-stdio-connection (lambda ()
(cons lsp-clients-javascript-typescript-server
(cons (lsp-package-path :npm "javascript-typescript-langserver" ".bin/javascript-typescript-stdio")
lsp-clients-typescript-javascript-server-args)))
:activation-fn 'lsp-typescript-javascript-tsx-jsx-activate-p
:priority -3
:completion-in-comments? t
:ignore-messages '("readFile .*? requested by TypeScript but content not available")
:server-id 'jsts-ls))
:server-id 'jsts-ls
:download-server-fn (lambda (_client callback _update?)
(lsp-package-ensure
:npm
"javascript-typescript-langserver"
(lambda (success? msg)
(funcall callback success? msg))))))


;;; TypeScript
Expand All @@ -194,14 +191,6 @@ finding the executable with variable `exec-path'."
:group 'lsp-mode
:link '(url-link "https://github.com/theia-ide/typescript-language-server"))

(defcustom lsp-clients-typescript-server "typescript-language-server"
"The typescript-language-server executable to use.
Leave as just the executable name to use the default behavior of
finding the executable with variable `exec-path'."
:group 'lsp-typescript
:risky t
:type 'file)

(defcustom lsp-clients-typescript-server-args '("--stdio")
"Extra arguments for the typescript-language-server language server."
:group 'lsp-typescript
Expand Down Expand Up @@ -232,16 +221,31 @@ directory containing the package. Example:

(lsp-register-client
(make-lsp-client :new-connection (lsp-stdio-connection (lambda ()
(cons lsp-clients-typescript-server
(cons (lsp-package-path :npm
"typescript-language-server"
".bin/typescript-language-server")
lsp-clients-typescript-server-args)))
:activation-fn 'lsp-typescript-javascript-tsx-jsx-activate-p
:priority -2
:completion-in-comments? t
:initialization-options (lambda ()
(list :plugins lsp-clients-typescript-plugins
:logVerbosity lsp-clients-typescript-log-verbosity))
:logVerbosity lsp-clients-typescript-log-verbosity
:tsServerPath (lsp-package-path :npm "typescript" ".bin/tsserver")))
:ignore-messages '("readFile .*? requested by TypeScript but content not available")
:server-id 'ts-ls))
:server-id 'ts-ls
:download-server-fn (lambda (_client callback _update?)
(lsp-package-ensure
:npm
"typescript"
(lambda (success? msg)
(if success?
(lsp-package-ensure
:npm
"typescript-language-server"
(lambda (success? msg)
(funcall callback success? msg)))
(funcall success? msg)))))))



Expand Down Expand Up @@ -338,19 +342,18 @@ particular FILE-NAME and MODE."

(defun lsp-php--create-connection ()
"Create lsp connection."
(plist-put
(lsp-stdio-connection
(lambda () lsp-clients-php-server-command))
:test? (lambda ()
(if (and (cdr lsp-clients-php-server-command)
(eq (string-match-p "php[0-9.]*\\'" (car lsp-clients-php-server-command)) 0))
;; Start with the php command and the list has more elems. Test the existence of the PHP script.
(let ((php-file (nth 1 lsp-clients-php-server-command)))
(or (file-exists-p php-file)
(progn
(lsp-log "%s is not present." php-file)
nil)))
t))))
(lsp-stdio-connection
(lambda () lsp-clients-php-server-command)
(lambda ()
(if (and (cdr lsp-clients-php-server-command)
(eq (string-match-p "php[0-9.]*\\'" (car lsp-clients-php-server-command)) 0))
;; Start with the php command and the list has more elems. Test the existence of the PHP script.
(let ((php-file (nth 1 lsp-clients-php-server-command)))
(or (file-exists-p php-file)
(progn
(lsp-log "%s is not present." php-file)
nil)))
t))))

(lsp-register-client
(make-lsp-client :new-connection (lsp-php--create-connection)
Expand Down Expand Up @@ -390,7 +393,9 @@ particular FILE-NAME and MODE."
"LSP support for OCaml, using ocaml-lsp-server."
:group 'lsp-mode
:link '(url-link "https://github.com/ocaml/ocaml-lsp"))

(define-obsolete-variable-alias 'lsp-merlin 'lsp-ocaml-lsp-server)
(define-obsolete-variable-alias 'lsp-merlin-command 'lsp-ocaml-lsp-server-command)

(defcustom lsp-ocaml-lsp-server-command
'("ocamllsp")
Expand All @@ -400,7 +405,6 @@ particular FILE-NAME and MODE."
(string :tag "Single string value")
(repeat :tag "List of string values"
string)))
(define-obsolete-variable-alias 'lsp-merlin-command 'lsp-ocaml-lsp-server-command)

(lsp-register-client
(make-lsp-client
Expand Down Expand Up @@ -734,12 +738,11 @@ responsiveness at the cost of possibile stability issues."

(defun lsp-clients-emmy-lua--create-connection ()
"Create connection to emmy lua language server."
(plist-put
(lsp-stdio-connection
(lambda ()
(list lsp-clients-emmy-lua-java-path "-jar" lsp-clients-emmy-lua-jar-path)))
:test? (lambda ()
(f-exists? lsp-clients-emmy-lua-jar-path))))
(lsp-stdio-connection
(lambda ()
(list lsp-clients-emmy-lua-java-path "-jar" lsp-clients-emmy-lua-jar-path))
(lambda ()
(f-exists? lsp-clients-emmy-lua-jar-path))))

(lsp-register-client
(make-lsp-client :new-connection (lsp-clients-emmy-lua--create-connection)
Expand Down
24 changes: 16 additions & 8 deletions lsp-csharp.el
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Version 1.34.3 minimum is required."
:link '(url-link "https://github.com/OmniSharp/omnisharp-roslyn"))

(defcustom lsp-csharp-server-install-dir
(locate-user-emacs-file ".cache/omnisharp-roslyn/")
(f-join lsp-server-install-dir "omnisharp-roslyn/")
"Installation directory for OmniSharp Roslyn server."
:group 'lsp-csharp
:type 'directory)
Expand Down Expand Up @@ -163,14 +163,12 @@ available on github and if so, downloads and installs a newer version."
(format "can be updated, currently installed version is %s" installed-version)
"is not installed")
target-version)))
(let ((cache-dir (expand-file-name (locate-user-emacs-file ".cache/")))
(o-r-dir (expand-file-name (locate-user-emacs-file ".cache/omnisharp-roslyn/")))
(new-server-dir (lsp-csharp--server-dir target-version))
(let ((new-server-dir (lsp-csharp--server-dir target-version))
(new-server-bin (lsp-csharp--server-bin target-version))
(package-filename (lsp-csharp--server-package-filename))
(package-url (lsp-csharp--server-package-url target-version)))

(f-mkdir cache-dir o-r-dir new-server-dir)
(mkdir new-server-dir t)

(lsp-csharp--extract-server package-url
(f-join new-server-dir package-filename)
Expand Down Expand Up @@ -242,13 +240,23 @@ Will attempt to install the server if it is not installed already for the
current platform."
(if lsp-csharp-server-path
(list lsp-csharp-server-path "-lsp")
(list (lsp-csharp--get-or-install-server) "-lsp")))
(list (lsp-csharp--server-bin (lsp-csharp--latest-installed-version)) "-lsp")))

(lsp-register-client
(make-lsp-client :new-connection (lsp-stdio-connection
#'lsp-csharp--language-server-command)
#'lsp-csharp--language-server-command
(lambda ()
(when-let (binary (lsp-csharp--server-bin (lsp-csharp--latest-installed-version)))
(f-exists? binary))))

:major-modes '(csharp-mode)
:server-id 'csharp))
:server-id 'csharp
:download-server-fn (lambda (_client callback _update?)
(condition-case err
(progn
(lsp-csharp--install-server nil nil)
(funcall callback t nil))
(error (funcall callback nil (error-message-string err)))))))

(provide 'lsp-csharp)
;;; lsp-csharp.el ends here
12 changes: 5 additions & 7 deletions lsp-eslint.el
Original file line number Diff line number Diff line change
Expand Up @@ -198,16 +198,14 @@
(interactive)
(lsp-send-execute-command "eslint.applyAllFixes" (vector (lsp--versioned-text-document-identifier))))



(lsp-register-client
(make-lsp-client
:new-connection
(plist-put (lsp-stdio-connection
(lambda () lsp-eslint-server-command))
:test? (lambda ()
(and (cl-second lsp-eslint-server-command)
(file-exists-p (cl-second lsp-eslint-server-command)))))
(lsp-stdio-connection
(lambda () lsp-eslint-server-command)
(lambda ()
(and (cl-second lsp-eslint-server-command)
(file-exists-p (cl-second lsp-eslint-server-command)))))
:activation-fn (lambda (filename &optional _)
(or (string-match-p (rx (one-or-more anything) "."
(or "ts" "js" "jsx" "tsx" "html" "vue"))
Expand Down
27 changes: 11 additions & 16 deletions lsp-fsharp.el
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
(const :tag "Use .Net Framework" net-framework))
:package-version '(lsp-mode . "6.1"))

(defcustom lsp-fsharp-server-install-dir (locate-user-emacs-file "fsautocomplete/")
(defcustom lsp-fsharp-server-install-dir (f-join lsp-server-install-dir "fsautocomplete/")
"Install directory for fsautocomplete server.
The slash is expected at the end."
:group 'lsp-fsharp
Expand Down Expand Up @@ -179,16 +179,7 @@ disable if `--backgorund-service-enabled' is not used"
".exe")))
(expand-file-name (concat "fsautocomplete" file-ext) lsp-fsharp-server-install-dir)))

(defun lsp-fsharp--fsac-locate ()
"Return the location of the fsautocomplete langauge server."
(let ((fsac (lsp-fsharp--fsac-cmd)))
(unless (file-exists-p fsac)
(if (yes-or-no-p "Server is not installed. Do you want to install it?")
(lsp-fsharp--fsac-install)
(error "LSP F# cannot be started without FsAutoComplete Server")))
fsac))

(defun lsp-fsharp--fsac-install ()
(defun lsp-fsharp--fsac-install (_client callback _update?)
"Download the latest version of fsautocomplete and extract it to `lsp-fsharp-server-install-dir'."
(let* ((temp-file (make-temp-file "fsautocomplete" nil ".zip"))
(install-dir-full (expand-file-name lsp-fsharp-server-install-dir))
Expand All @@ -197,18 +188,19 @@ disable if `--backgorund-service-enabled' is not used"
(t (user-error (format "Unable to unzip server - file %s cannot be extracted, please extract it manually" temp-file))))))
(url-copy-file lsp-fsharp-server-download-url temp-file t)
(shell-command unzip-script)
(shell-command (format "%s %s --version" (lsp-fsharp--fsac-runtime-cmd) (lsp-fsharp--fsac-cmd)))))
(shell-command (format "%s %s --version" (lsp-fsharp--fsac-runtime-cmd) (lsp-fsharp--fsac-cmd)))
(funcall callback t nil)))

(defun lsp-fsharp-update-fsac ()
"Update fsautocomplete to the latest version."
(interactive)
(-let [install-dir (f-expand lsp-fsharp-server-install-dir)]
(f-delete install-dir t)
(lsp-fsharp--fsac-install)))
(lsp-fsharp--fsac-install nil #'ignore t)))

(defun lsp-fsharp--make-launch-cmd ()
"Build the command required to launch fsautocomplete."
(append (list (lsp-fsharp--fsac-runtime-cmd) (lsp-fsharp--fsac-locate) "--background-service-enabled")
(append (list (lsp-fsharp--fsac-runtime-cmd) (lsp-fsharp--fsac-cmd) "--background-service-enabled")
lsp-fsharp-server-args))

(defun lsp-fsharp--project-list ()
Expand Down Expand Up @@ -260,7 +252,9 @@ disable if `--backgorund-service-enabled' is not used"
("FSharp.EnableReferenceCodeLens" lsp-fsharp-enable-reference-code-lens t)))

(lsp-register-client
(make-lsp-client :new-connection (lsp-stdio-connection 'lsp-fsharp--make-launch-cmd)
(make-lsp-client :new-connection (lsp-stdio-connection
#'lsp-fsharp--make-launch-cmd
(lambda () (f-exists? (lsp-fsharp--fsac-cmd))))
:major-modes '(fsharp-mode)
:notification-handlers (ht ("fsharp/notifyCancel" #'ignore)
("fsharp/notifyWorkspace" #'ignore)
Expand All @@ -275,7 +269,8 @@ disable if `--backgorund-service-enabled' is not used"
(lsp-configuration-section "fsharp"))
(lsp-fsharp--workspace-load
(lsp-fsharp--project-list)))))
:server-id 'fsac))
:server-id 'fsac
:download-server-fn #'lsp-fsharp--fsac-install))

(provide 'lsp-fsharp)
;;; lsp-fsharp.el ends here
Loading

0 comments on commit 1f670a3

Please sign in to comment.