Skip to content

Commit e1d5649

Browse files
authored
Add `lsp-use-workspace-root-for-server-default-directory' (#3327)
This is a buffer-local variable to enable temporarily binding `default-directory' to `lsp-workspace-root' when running an LSP server, allowing for project-local LSP server installations (e.g. as with `dotnet tool` for F#).
1 parent e4cc46d commit e1d5649

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

CHANGELOG.org

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
* Drop deprecated rust-analyzer variable lsp-rust-analyzer-import-merge-behaviour
3737
* Added run and debug code lenses to ~lsp-kotlin~
3838
* Add setting UPDATE_EXPECT=1 when running `expect!` tests ~lsp-rust~
39+
* Add ~lsp-use-workspace-root-for-server-default-directory~.
3940
** Release 8.0.0
4041
* Add ~lsp-clients-angular-node-get-prefix-command~ to get the Angular server from another location which is still has ~/lib/node_modules~ in it.
4142
* Set ~lsp-clients-angular-language-server-command~ after the first connection to speed up subsequent connections.

clients/lsp-fsharp.el

+18-2
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,20 @@ UPDATE? is t."
167167
error-callback
168168
"dotnet" "tool" (if update? "update" "install") "-g" "fsautocomplete"))
169169

170+
(defcustom lsp-fsharp-use-dotnet-tool-for-fsac t
171+
"Run FsAutoComplete as a dotnet tool.
172+
173+
The binary will be invoked via \"dotnet fsautocomplete\" in the
174+
project's root directory, which will run a project-local tool if
175+
available, else the globally installed tool."
176+
:group 'lsp-fsharp
177+
:type 'boolean
178+
:risky t)
179+
180+
(defun lsp-fsharp--fsac-cmd ()
181+
"The location of fsautocomplete executable."
182+
(expand-file-name "fsautocomplete.dll" lsp-fsharp-server-install-dir))
183+
170184
(defun lsp-fsharp--make-launch-cmd ()
171185
"Build the command required to launch fsautocomplete."
172186

@@ -199,8 +213,10 @@ UPDATE? is t."
199213

200214
(defun lsp-fsharp--test-fsautocomplete-present ()
201215
"Return non-nil if dotnet tool fsautocomplete is installed globally."
202-
(string-match-p "fsautocomplete"
203-
(shell-command-to-string "dotnet tool list -g")))
216+
(if lsp-fsharp-use-dotnet-tool-for-fsac
217+
(string-match-p "fsautocomplete"
218+
(shell-command-to-string "dotnet tool list -g"))
219+
(f-exists? (lsp-fsharp--fsac-cmd))))
204220

205221
(defun lsp-fsharp--project-list ()
206222
"Get the list of files we need to send to fsharp/workspaceLoad."

lsp-mode.el

+14
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,9 @@ Takes a value accepted by `spinner-start'."
200200
,@(mapcar (lambda (c) (list 'const (car c)))
201201
spinner-types)))
202202

203+
(defvar-local lsp-use-workspace-root-for-server-default-directory nil
204+
"Use `lsp-workspace-root' for `default-directory' when starting LSP process.")
205+
203206
(defvar-local lsp--cur-workspace nil)
204207

205208
(defvar-local lsp--cur-version 0)
@@ -7021,6 +7024,16 @@ Ignore non-boolean keys whose value is nil."
70217024
(eval value))))
70227025
process-environment))))
70237026

7027+
(defun lsp--default-directory-for-connection (&optional path)
7028+
"Return path to be used for the working directory of a LSP process.
7029+
7030+
If `lsp-use-workspace-root-for-server-default-directory' is
7031+
non-nil, uses `lsp-workspace-root' to find the directory
7032+
corresponding to PATH, else returns `default-directory'."
7033+
(if lsp-use-workspace-root-for-server-default-directory
7034+
(lsp-workspace-root path)
7035+
default-directory))
7036+
70247037
(defun lsp-stdio-connection (command &optional test-command)
70257038
"Returns a connection property list using COMMAND.
70267039
COMMAND can be: A string, denoting the command to launch the
@@ -7045,6 +7058,7 @@ returned by COMMAND is available via `executable-find'"
70457058
(process-environment
70467059
(lsp--compute-process-environment environment-fn)))
70477060
(let* ((stderr-buf (format "*%s::stderr*" process-name))
7061+
(default-directory (lsp--default-directory-for-connection))
70487062
(proc (make-process
70497063
:name process-name
70507064
:connection-type 'pipe

0 commit comments

Comments
 (0)