-
Notifications
You must be signed in to change notification settings - Fork 244
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: lsp "find all references" (#5395)
# Description ## Problem Resolves #5393 ## Summary Implements "Find all references". The logic for it is the same as the one for "Rename symbol" so it was straight-forward to do. https://github.com/noir-lang/noir/assets/209371/5eef03bf-6170-48f3-beaa-87dcc86532d2 ## Additional Context In the video above, "Find all references" misses a few locations. I captured that as a separate issue ( #5394 ) because since both features rely on the same logic, fixing that would make this one work too... so this PR is mostly about enabling "Find all references" rather than the logic to actually find all references. ## Documentation Check one: - [x] No documentation needed. - [ ] Documentation included in this PR. - [ ] **[For Experimental Features]** Documentation to be submitted in a separate PR. # PR Checklist - [x] I have tested the changes locally. - [ ] I have formatted the changes with [Prettier](https://prettier.io/) and/or `cargo fmt` on default settings. --------- Co-authored-by: Tom French <[email protected]>
- Loading branch information
1 parent
0603bd3
commit ce1994c
Showing
6 changed files
with
131 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
use std::future::{self, Future}; | ||
|
||
use async_lsp::ResponseError; | ||
use lsp_types::{Location, ReferenceParams}; | ||
|
||
use crate::LspState; | ||
|
||
use super::{process_request, to_lsp_location}; | ||
|
||
pub(crate) fn on_references_request( | ||
state: &mut LspState, | ||
params: ReferenceParams, | ||
) -> impl Future<Output = Result<Option<Vec<Location>>, ResponseError>> { | ||
let result = | ||
process_request(state, params.text_document_position, |location, interner, files| { | ||
interner.find_all_references(location, params.context.include_declaration).map( | ||
|locations| { | ||
locations | ||
.iter() | ||
.filter_map(|location| to_lsp_location(files, location.file, location.span)) | ||
.collect() | ||
}, | ||
) | ||
}); | ||
future::ready(result) | ||
} | ||
|
||
#[cfg(test)] | ||
mod references_tests { | ||
use super::*; | ||
use crate::test_utils::{self, search_in_file}; | ||
use lsp_types::{ | ||
PartialResultParams, ReferenceContext, TextDocumentPositionParams, WorkDoneProgressParams, | ||
}; | ||
use tokio::test; | ||
|
||
async fn check_references_succeeds( | ||
directory: &str, | ||
name: &str, | ||
declaration_index: usize, | ||
include_declaration: bool, | ||
) { | ||
let (mut state, noir_text_document) = test_utils::init_lsp_server(directory).await; | ||
|
||
// First we find out all of the occurrences of `name` in the main.nr file. | ||
// Note that this only works if that name doesn't show up in other places where we don't | ||
// expect a rename, but we craft our tests to avoid that. | ||
let ranges = search_in_file(noir_text_document.path(), name); | ||
|
||
// Test getting references works on any instance of the symbol. | ||
for target_range in &ranges { | ||
let target_position = target_range.start; | ||
|
||
let params = ReferenceParams { | ||
text_document_position: TextDocumentPositionParams { | ||
text_document: lsp_types::TextDocumentIdentifier { | ||
uri: noir_text_document.clone(), | ||
}, | ||
position: target_position, | ||
}, | ||
work_done_progress_params: WorkDoneProgressParams { work_done_token: None }, | ||
partial_result_params: PartialResultParams { partial_result_token: None }, | ||
context: ReferenceContext { include_declaration }, | ||
}; | ||
|
||
let locations = on_references_request(&mut state, params) | ||
.await | ||
.expect("Could not execute on_references_request") | ||
.unwrap(); | ||
|
||
let mut references_ranges: Vec<_> = | ||
locations.iter().map(|location| location.range).collect(); | ||
references_ranges.sort_by_key(|range| range.start.line); | ||
|
||
if include_declaration { | ||
assert_eq!(ranges, references_ranges); | ||
} else { | ||
let mut ranges_without_declaration = ranges.clone(); | ||
ranges_without_declaration.remove(declaration_index); | ||
assert_eq!(ranges_without_declaration, references_ranges); | ||
} | ||
} | ||
} | ||
|
||
#[test] | ||
async fn test_on_references_request_including_declaration() { | ||
check_references_succeeds("rename_function", "another_function", 0, true).await; | ||
} | ||
|
||
#[test] | ||
async fn test_on_references_request_without_including_declaration() { | ||
check_references_succeeds("rename_function", "another_function", 0, false).await; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters