Skip to content

Commit d06390a

Browse files
authored
Key by Url instead of Path in workspace indexer (#910)
1 parent ee70100 commit d06390a

File tree

7 files changed

+139
-125
lines changed

7 files changed

+139
-125
lines changed

crates/ark/src/lsp/completions/sources/composite/workspace.rs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ fn completions_from_workspace(
7171
let token = token.as_str();
7272

7373
// get entries from the index
74-
indexer::map(|path, symbol, entry| {
74+
indexer::map(|uri, symbol, entry| {
7575
if !symbol.fuzzy_matches(token) {
7676
return;
7777
}
@@ -87,17 +87,21 @@ fn completions_from_workspace(
8787
return;
8888
});
8989

90-
// add some metadata about where the completion was found
91-
let mut path = path.to_str().unwrap_or_default();
92-
for folder in &state.workspace.folders {
93-
if let Ok(folder) = folder.to_file_path() {
94-
if let Some(folder) = folder.to_str() {
95-
if path.starts_with(folder) {
96-
path = &path[folder.len() + 1..];
90+
// Add some metadata about where the completion was found
91+
let mut path = uri.as_str().to_owned();
92+
93+
if uri.scheme() == "file" {
94+
if let Ok(file_path) = uri.to_file_path() {
95+
for folder in &state.workspace.folders {
96+
let Ok(folder_path) = folder.to_file_path() else {
97+
continue;
98+
};
99+
if let Ok(relative_path) = file_path.strip_prefix(&folder_path) {
100+
path = relative_path.to_string_lossy().to_string();
97101
break;
98102
}
99103
}
100-
}
104+
};
101105
}
102106

103107
let value = format!(

crates/ark/src/lsp/definitions.rs

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ use tower_lsp::lsp_types::GotoDefinitionParams;
1010
use tower_lsp::lsp_types::GotoDefinitionResponse;
1111
use tower_lsp::lsp_types::LocationLink;
1212
use tower_lsp::lsp_types::Range;
13-
use tower_lsp::lsp_types::Url;
1413

1514
use crate::lsp::documents::Document;
1615
use crate::lsp::encoding::convert_point_to_position;
@@ -46,19 +45,16 @@ pub fn goto_definition<'a>(
4645
if node.is_identifier() {
4746
let symbol = document.contents.node_slice(&node)?.to_string();
4847

48+
// First search in current file, then in all files
4949
let uri = &params.text_document_position_params.text_document.uri;
50-
let info = if let Ok(preferred_path) = uri.to_file_path() {
51-
// First search in current file, then in all files
52-
indexer::find_in_file(symbol.as_str(), &preferred_path)
53-
.or_else(|| indexer::find(symbol.as_str()))
54-
} else {
55-
indexer::find(symbol.as_str())
56-
};
50+
let info =
51+
indexer::find_in_file(symbol.as_str(), uri).or_else(|| indexer::find(symbol.as_str()));
5752

58-
if let Some((path, entry)) = info {
53+
if let Some((file_id, entry)) = info {
54+
let target_uri = file_id.as_uri().clone();
5955
let link = LocationLink {
6056
origin_selection_range: None,
61-
target_uri: Url::from_file_path(path).unwrap(),
57+
target_uri,
6258
target_range: entry.range,
6359
target_selection_range: entry.range,
6460
};
@@ -105,9 +101,9 @@ foo <- 42
105101
print(foo)
106102
"#;
107103
let doc = Document::new(code, None);
108-
let (path, uri) = test_path("test.R");
104+
let uri = test_path("test.R");
109105

110-
indexer::update(&doc, &path).unwrap();
106+
indexer::update(&doc, &uri).unwrap();
111107

112108
let params = GotoDefinitionParams {
113109
text_document_position_params: lsp_types::TextDocumentPositionParams {
@@ -142,9 +138,9 @@ foo <- 1
142138
print(foo)
143139
"#;
144140
let doc = Document::new(code, None);
145-
let (path, uri) = test_path("test.R");
141+
let uri = test_path("test.R");
146142

147-
indexer::update(&doc, &path).unwrap();
143+
indexer::update(&doc, &uri).unwrap();
148144

149145
let params = lsp_types::GotoDefinitionParams {
150146
text_document_position_params: lsp_types::TextDocumentPositionParams {
@@ -187,11 +183,11 @@ foo
187183
let doc1 = Document::new(code1, None);
188184
let doc2 = Document::new(code2, None);
189185

190-
let (path1, uri1) = test_path("file1.R");
191-
let (path2, uri2) = test_path("file2.R");
186+
let uri1 = test_path("file1.R");
187+
let uri2 = test_path("file2.R");
192188

193-
indexer::update(&doc1, &path1).unwrap();
194-
indexer::update(&doc2, &path2).unwrap();
189+
indexer::update(&doc1, &uri1).unwrap();
190+
indexer::update(&doc2, &uri2).unwrap();
195191

196192
// Go to definition for foo in file1
197193
let params1 = GotoDefinitionParams {
@@ -244,11 +240,11 @@ foo
244240
let doc2 = Document::new(code2, None);
245241

246242
// Use test_path for cross-platform compatibility
247-
let (path1, uri1) = crate::lsp::util::test_path("file1.R");
248-
let (path2, uri2) = crate::lsp::util::test_path("file2.R");
243+
let uri1 = test_path("file1.R");
244+
let uri2 = test_path("file2.R");
249245

250-
indexer::update(&doc1, &path1).unwrap();
251-
indexer::update(&doc2, &path2).unwrap();
246+
indexer::update(&doc1, &uri1).unwrap();
247+
indexer::update(&doc2, &uri2).unwrap();
252248

253249
// Go to definition for foo in file2 (should jump to file1)
254250
let params2 = GotoDefinitionParams {

crates/ark/src/lsp/diagnostics.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ pub(crate) fn generate_diagnostics(
159159
context.document_symbols.push(HashMap::new());
160160

161161
// Add the current workspace symbols.
162-
indexer::map(|_path, _symbol, entry| match &entry.data {
162+
indexer::map(|_uri, _symbol, entry| match &entry.data {
163163
indexer::IndexEntryData::Function { name, arguments: _ } => {
164164
context.workspace_symbols.insert(name.to_string());
165165
},

0 commit comments

Comments
 (0)