Skip to content

Commit c4a0c66

Browse files
committed
Take &FilePath in get_document()
1 parent a9b0bf2 commit c4a0c66

6 files changed

Lines changed: 31 additions & 29 deletions

File tree

crates/ark/src/lsp/find_references.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::path::Path;
22

33
use aether_lsp_utils::proto::from_proto;
44
use aether_lsp_utils::proto::to_proto;
5+
use aether_path::FilePath;
56
use anyhow::anyhow;
67
use stdext::result::ResultExt;
78
use stdext::unwrap;
@@ -35,7 +36,7 @@ pub(crate) fn find_references(
3536
let position = params.text_document_position.position;
3637
let include_declaration = params.context.include_declaration;
3738

38-
let document = state.get_document(&uri)?;
39+
let document = state.get_document(&FilePath::from_url(&uri))?;
3940

4041
let mut locations: Vec<Location> = Vec::new();
4142

crates/ark/src/lsp/handlers.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
//
66
//
77

8+
use aether_path::FilePath;
89
use anyhow::anyhow;
910
use serde_json::Value;
1011
use stdext::result::ResultExt;
@@ -184,7 +185,7 @@ pub(crate) fn handle_folding_range(
184185
state: &WorldState,
185186
) -> LspResult<Option<Vec<FoldingRange>>> {
186187
let uri = &params.text_document.uri;
187-
let document = state.get_document(uri)?;
188+
let document = state.get_document(&FilePath::from_url(uri))?;
188189
match folding_range(document) {
189190
Ok(foldings) => Ok(Some(foldings)),
190191
Err(err) => {
@@ -210,7 +211,7 @@ pub(crate) fn handle_completion(
210211
) -> LspResult<Option<CompletionResponse>> {
211212
// Get reference to document.
212213
let uri = params.text_document_position.text_document.uri;
213-
let document = state.get_document(&uri)?;
214+
let document = state.get_document(&FilePath::from_url(&uri))?;
214215

215216
let position = params.text_document_position.position;
216217
let point = document.tree_sitter_point_from_lsp_position(position)?;
@@ -243,7 +244,7 @@ pub(crate) fn handle_completion_resolve(mut item: CompletionItem) -> LspResult<C
243244
#[tracing::instrument(level = "info", skip_all)]
244245
pub(crate) fn handle_hover(params: HoverParams, state: &WorldState) -> LspResult<Option<Hover>> {
245246
let uri = params.text_document_position_params.text_document.uri;
246-
let document = state.get_document(&uri)?;
247+
let document = state.get_document(&FilePath::from_url(&uri))?;
247248

248249
let position = params.text_document_position_params.position;
249250
let point = document.tree_sitter_point_from_lsp_position(position)?;
@@ -278,7 +279,7 @@ pub(crate) fn handle_signature_help(
278279
state: &WorldState,
279280
) -> LspResult<Option<SignatureHelp>> {
280281
let uri = params.text_document_position_params.text_document.uri;
281-
let document = state.get_document(&uri)?;
282+
let document = state.get_document(&FilePath::from_url(&uri))?;
282283

283284
let position = params.text_document_position_params.position;
284285
let point = document.tree_sitter_point_from_lsp_position(position)?;
@@ -308,7 +309,7 @@ pub(crate) fn handle_goto_definition(
308309
state: &WorldState,
309310
) -> LspResult<Option<GotoDefinitionResponse>> {
310311
let uri = &params.text_document_position_params.text_document.uri;
311-
let document = state.get_document(uri)?;
312+
let document = state.get_document(&FilePath::from_url(uri))?;
312313

313314
Ok(goto_definition(document, params).log_err().flatten())
314315
}
@@ -318,7 +319,7 @@ pub(crate) fn handle_selection_range(
318319
params: SelectionRangeParams,
319320
state: &WorldState,
320321
) -> LspResult<Option<Vec<SelectionRange>>> {
321-
let document = state.get_document(&params.text_document.uri)?;
322+
let document = state.get_document(&FilePath::from_url(&params.text_document.uri))?;
322323

323324
// Get tree-sitter points to return selection ranges for
324325
let points = params
@@ -382,7 +383,7 @@ pub(crate) fn handle_statement_range(
382383
params: StatementRangeParams,
383384
state: &WorldState,
384385
) -> LspResult<Option<StatementRangeResponse>> {
385-
let document = state.get_document(&params.text_document.uri)?;
386+
let document = state.get_document(&FilePath::from_url(&params.text_document.uri))?;
386387
let point = document.tree_sitter_point_from_lsp_position(params.position)?;
387388
statement_range(document, point)
388389
}
@@ -392,7 +393,7 @@ pub(crate) fn handle_help_topic(
392393
params: HelpTopicParams,
393394
state: &WorldState,
394395
) -> LspResult<Option<HelpTopicResponse>> {
395-
let document = state.get_document(&params.text_document.uri)?;
396+
let document = state.get_document(&FilePath::from_url(&params.text_document.uri))?;
396397
let point = document.tree_sitter_point_from_lsp_position(params.position)?;
397398
help_topic(point, document)
398399
}
@@ -403,7 +404,7 @@ pub(crate) fn handle_indent(
403404
state: &WorldState,
404405
) -> LspResult<Option<Vec<TextEdit>>> {
405406
let ctxt = params.text_document_position;
406-
let doc = state.get_document(&ctxt.text_document.uri)?;
407+
let doc = state.get_document(&FilePath::from_url(&ctxt.text_document.uri))?;
407408
let point = doc.tree_sitter_point_from_lsp_position(ctxt.position)?;
408409

409410
indent_edit(doc, point.row)
@@ -416,7 +417,7 @@ pub(crate) fn handle_code_action(
416417
state: &WorldState,
417418
) -> LspResult<Option<CodeActionResponse>> {
418419
let uri = params.text_document.uri;
419-
let doc = state.get_document(&uri)?;
420+
let doc = state.get_document(&FilePath::from_url(&uri))?;
420421
let range = doc.tree_sitter_range_from_lsp_range(params.range)?;
421422

422423
let code_actions = code_actions(&uri, doc, range, &lsp_state.capabilities);

crates/ark/src/lsp/main_loop.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1077,7 +1077,7 @@ pub(crate) fn index_update(uris: Vec<Url>, state: WorldState) {
10771077
continue;
10781078
}
10791079

1080-
let document = match state.get_document(&uri) {
1080+
let document = match state.get_document(&FilePath::from_url(&uri)) {
10811081
Ok(doc) => doc.clone(),
10821082
Err(err) => {
10831083
tracing::warn!("Can't get document '{uri}' for indexing: {err:?}");

crates/ark/src/lsp/rename.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::collections::HashMap;
22

33
use aether_lsp_utils::proto::from_proto;
44
use aether_lsp_utils::proto::to_proto;
5+
use aether_path::FilePath;
56
use tower_lsp::lsp_types;
67
use tower_lsp::lsp_types::PrepareRenameResponse;
78
use tower_lsp::lsp_types::RenameParams;
@@ -17,7 +18,7 @@ pub(crate) fn prepare_rename(
1718
) -> anyhow::Result<Option<PrepareRenameResponse>> {
1819
let uri = params.text_document.uri;
1920
let position = params.position;
20-
let document = state.get_document(&uri)?;
21+
let document = state.get_document(&FilePath::from_url(&uri))?;
2122

2223
let offset = from_proto::offset_from_position(
2324
position,
@@ -46,7 +47,7 @@ pub(crate) fn rename(
4647
let uri = params.text_document_position.text_document.uri;
4748
let position = params.text_document_position.position;
4849
let new_name = params.new_name;
49-
let document = state.get_document(&uri)?;
50+
let document = state.get_document(&FilePath::from_url(&uri))?;
5051

5152
let offset = from_proto::offset_from_position(
5253
position,

crates/ark/src/lsp/state.rs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -81,21 +81,19 @@ impl WorldState {
8181
}
8282
}
8383

84-
pub(crate) fn get_document(&self, uri: &Url) -> anyhow::Result<&Document> {
85-
let key = FilePath::from_url(uri);
86-
if let Some(doc) = self.documents.get(&key) {
84+
pub(crate) fn get_document(&self, path: &FilePath) -> anyhow::Result<&Document> {
85+
if let Some(doc) = self.documents.get(path) {
8786
Ok(doc)
8887
} else {
89-
Err(anyhow!("Can't find document for URI {uri}"))
88+
Err(anyhow!("Can't find document for path {path}"))
9089
}
9190
}
9291

93-
pub(crate) fn get_document_mut(&mut self, uri: &Url) -> anyhow::Result<&mut Document> {
94-
let key = FilePath::from_url(uri);
95-
if let Some(doc) = self.documents.get_mut(&key) {
92+
pub(crate) fn get_document_mut(&mut self, path: &FilePath) -> anyhow::Result<&mut Document> {
93+
if let Some(doc) = self.documents.get_mut(path) {
9694
Ok(doc)
9795
} else {
98-
Err(anyhow!("Can't find document for URI {uri}"))
96+
Err(anyhow!("Can't find document for path {path}"))
9997
}
10098
}
10199

@@ -148,16 +146,16 @@ where
148146
// If we have a cached copy of the document (because we're monitoring it)
149147
// then use that; otherwise, try to read the document from the provided
150148
// path and use that instead.
151-
let Ok(uri) = Url::from_file_path(path) else {
149+
let Some(key) = FilePath::from_path_buf(path.to_path_buf()) else {
152150
log::info!(
153-
"couldn't construct uri from {}; reading from disk instead",
151+
"couldn't construct file path from {}; reading from disk instead",
154152
path.display()
155153
);
156154
return fallback();
157155
};
158156

159-
let Ok(document) = state.get_document(&uri) else {
160-
log::info!("no document for uri {uri}; reading from disk instead");
157+
let Ok(document) = state.get_document(&key) else {
158+
log::info!("no document for path {key}; reading from disk instead");
161159
return fallback();
162160
};
163161

crates/ark/src/lsp/state_handlers.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ pub(crate) fn did_change(
292292
state: &mut WorldState,
293293
) -> anyhow::Result<()> {
294294
let uri = &params.text_document.uri;
295-
let document = state.get_document_mut(uri)?;
295+
let document = state.get_document_mut(&FilePath::from_url(uri))?;
296296

297297
let parser = lsp_state
298298
.parsers
@@ -503,7 +503,7 @@ pub(crate) fn did_change_formatting_options(
503503
opts: &FormattingOptions,
504504
state: &mut WorldState,
505505
) {
506-
let Ok(doc) = state.get_document_mut(uri) else {
506+
let Ok(doc) = state.get_document_mut(&FilePath::from_url(uri)) else {
507507
return;
508508
};
509509

@@ -594,8 +594,9 @@ async fn update_config(
594594
let tail = remaining.split_off(DOCUMENT_SETTINGS.len());
595595
let head = std::mem::replace(&mut remaining, tail);
596596

597+
let path = FilePath::from_url(&uri);
597598
for (mapping, value) in DOCUMENT_SETTINGS.iter().zip(head) {
598-
if let Ok(doc) = state.get_document_mut(&uri) {
599+
if let Ok(doc) = state.get_document_mut(&path) {
599600
(mapping.set)(&mut doc.config, value);
600601
}
601602
}

0 commit comments

Comments
 (0)