Skip to content

Commit 03daeeb

Browse files
ath3pathwave
authored andcommitted
Find workspace from document path (helix-editor#3553)
1 parent f16fc4c commit 03daeeb

File tree

4 files changed

+19
-6
lines changed

4 files changed

+19
-6
lines changed

helix-lsp/src/client.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ impl Client {
4949
root_markers: &[String],
5050
id: usize,
5151
req_timeout: u64,
52+
doc_path: Option<&std::path::PathBuf>,
5253
) -> Result<(Self, UnboundedReceiver<(usize, Call)>, Arc<Notify>)> {
5354
// Resolve path to the binary
5455
let cmd = which::which(cmd).map_err(|err| anyhow::anyhow!(err))?;
@@ -72,7 +73,10 @@ impl Client {
7273
let (server_rx, server_tx, initialize_notify) =
7374
Transport::start(reader, writer, stderr, id);
7475

75-
let root_path = find_root(None, root_markers);
76+
let root_path = find_root(
77+
doc_path.and_then(|x| x.parent().and_then(|x| x.to_str())),
78+
root_markers,
79+
);
7680

7781
let root_uri = lsp::Url::from_file_path(root_path.clone()).ok();
7882

helix-lsp/src/lib.rs

+12-3
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,7 @@ impl Registry {
353353
pub fn restart(
354354
&mut self,
355355
language_config: &LanguageConfiguration,
356+
doc_path: Option<&std::path::PathBuf>,
356357
) -> Result<Option<Arc<Client>>> {
357358
let config = match &language_config.language_server {
358359
Some(config) => config,
@@ -367,7 +368,8 @@ impl Registry {
367368
// initialize a new client
368369
let id = self.counter.fetch_add(1, Ordering::Relaxed);
369370

370-
let NewClientResult(client, incoming) = start_client(id, language_config, config)?;
371+
let NewClientResult(client, incoming) =
372+
start_client(id, language_config, config, doc_path)?;
371373
self.incoming.push(UnboundedReceiverStream::new(incoming));
372374

373375
let (_, old_client) = entry.insert((id, client.clone()));
@@ -381,7 +383,11 @@ impl Registry {
381383
}
382384
}
383385

384-
pub fn get(&mut self, language_config: &LanguageConfiguration) -> Result<Option<Arc<Client>>> {
386+
pub fn get(
387+
&mut self,
388+
language_config: &LanguageConfiguration,
389+
doc_path: Option<&std::path::PathBuf>,
390+
) -> Result<Option<Arc<Client>>> {
385391
let config = match &language_config.language_server {
386392
Some(config) => config,
387393
None => return Ok(None),
@@ -393,7 +399,8 @@ impl Registry {
393399
// initialize a new client
394400
let id = self.counter.fetch_add(1, Ordering::Relaxed);
395401

396-
let NewClientResult(client, incoming) = start_client(id, language_config, config)?;
402+
let NewClientResult(client, incoming) =
403+
start_client(id, language_config, config, doc_path)?;
397404
self.incoming.push(UnboundedReceiverStream::new(incoming));
398405

399406
entry.insert((id, client.clone()));
@@ -493,6 +500,7 @@ fn start_client(
493500
id: usize,
494501
config: &LanguageConfiguration,
495502
ls_config: &LanguageServerConfiguration,
503+
doc_path: Option<&std::path::PathBuf>,
496504
) -> Result<NewClientResult> {
497505
let (client, incoming, initialize_notify) = Client::start(
498506
&ls_config.command,
@@ -501,6 +509,7 @@ fn start_client(
501509
&config.roots,
502510
id,
503511
ls_config.timeout,
512+
doc_path,
504513
)?;
505514

506515
let client = Arc::new(client);

helix-term/src/commands/typed.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1000,7 +1000,7 @@ fn lsp_restart(
10001000
.context("LSP not defined for the current document")?;
10011001

10021002
let scope = config.scope.clone();
1003-
cx.editor.language_servers.restart(config)?;
1003+
cx.editor.language_servers.restart(config, doc.path())?;
10041004

10051005
// This collect is needed because refresh_language_server would need to re-borrow editor.
10061006
let document_ids_to_refresh: Vec<DocumentId> = cx

helix-view/src/editor.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -868,7 +868,7 @@ impl Editor {
868868

869869
// try to find a language server based on the language name
870870
let language_server = doc.language.as_ref().and_then(|language| {
871-
ls.get(language)
871+
ls.get(language, doc.path())
872872
.map_err(|e| {
873873
log::error!(
874874
"Failed to initialize the LSP for `{}` {{ {} }}",

0 commit comments

Comments
 (0)