Skip to content

Commit 88039b7

Browse files
committed
fix(lsp): hack solution for workspace-wide PWD
1 parent 6e04433 commit 88039b7

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

crates/nu-lsp/src/workspace.rs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use nu_protocol::{
1717
use std::{
1818
collections::{BTreeMap, HashMap},
1919
fs,
20-
path::{Path, PathBuf},
20+
path::{Path, PathBuf, MAIN_SEPARATOR},
2121
sync::Arc,
2222
};
2323

@@ -46,6 +46,20 @@ fn find_nu_scripts_in_folder(folder_uri: &Uri) -> Result<nu_glob::Paths> {
4646
nu_glob::glob(&pattern, Uninterruptible).into_diagnostic()
4747
}
4848

49+
/// HACK: when current file is imported (use keyword) by others in the workspace,
50+
/// it will get parsed a second time via `parse_module_block`, so that its definitions'
51+
/// ids are renewed, making it harder to track the references.
52+
/// The fake Uri stops that with `ParseError::ModuleNotFound`,
53+
/// which seems benign for tasks like references/renaming.
54+
///
55+
/// FIXME: cross-file shadowing can still cause false-positive/false-negative cases
56+
fn pseudo_path() -> PathBuf {
57+
let mut pseudo_path = PathBuf::new();
58+
pseudo_path.push(format!("{}non-existing-dir", MAIN_SEPARATOR));
59+
pseudo_path.push("non-existing-file");
60+
pseudo_path
61+
}
62+
4963
impl LanguageServer {
5064
/// Get initial workspace folders from initialization response
5165
pub(crate) fn initialize_workspace_folders(
@@ -138,7 +152,8 @@ impl LanguageServer {
138152
) -> Option<Vec<Location>> {
139153
self.occurrences = BTreeMap::new();
140154
let path_uri = params.text_document_position.text_document.uri.to_owned();
141-
let mut engine_state = self.new_engine_state(None);
155+
let mut engine_state = self.new_engine_state(Some(&path_to_uri(pseudo_path())));
156+
142157
let (_, id, span, _) = self
143158
.parse_and_find(
144159
&mut engine_state,
@@ -199,7 +214,7 @@ impl LanguageServer {
199214
self.occurrences = BTreeMap::new();
200215

201216
let path_uri = params.text_document.uri.to_owned();
202-
let mut engine_state = self.new_engine_state(None);
217+
let mut engine_state = self.new_engine_state(Some(&path_to_uri(pseudo_path())));
203218

204219
let (working_set, id, span, file_offset) =
205220
self.parse_and_find(&mut engine_state, &path_uri, params.position)?;

0 commit comments

Comments
 (0)