Skip to content

Commit

Permalink
Add a test for finding references across a workspace
Browse files Browse the repository at this point in the history
  • Loading branch information
asterite committed Jul 10, 2024
1 parent 04d1c17 commit c66b652
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 4 deletions.
9 changes: 6 additions & 3 deletions tooling/lsp/src/notifications/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub(super) fn on_did_open_text_document(

let document_uri = params.text_document.uri;

match process_noir_document(document_uri, state) {
match process_workspace_for_noir_document(document_uri, state) {
Ok(_) => {
state.open_documents_count += 1;
ControlFlow::Continue(())
Expand Down Expand Up @@ -114,13 +114,16 @@ pub(super) fn on_did_save_text_document(
) -> ControlFlow<Result<(), async_lsp::Error>> {
let document_uri = params.text_document.uri;

match process_noir_document(document_uri, state) {
match process_workspace_for_noir_document(document_uri, state) {
Ok(_) => ControlFlow::Continue(()),
Err(err) => ControlFlow::Break(Err(err)),
}
}

fn process_noir_document(
// Given a Noir document, find the workspace it's contained in (an assumed workspace is created if
// it's only contained in a package), then type-checks the workspace's packages,
// caching code lenses and type definitions, and notifying about compilation errors.
pub(crate) fn process_workspace_for_noir_document(
document_uri: lsp_types::Url,
state: &mut LspState,
) -> Result<(), async_lsp::Error> {
Expand Down
70 changes: 69 additions & 1 deletion tooling/lsp/src/requests/references.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@ pub(crate) fn on_references_request(
#[cfg(test)]
mod references_tests {
use super::*;
use crate::notifications;
use crate::test_utils::{self, search_in_file};
use lsp_types::{
PartialResultParams, ReferenceContext, TextDocumentPositionParams, WorkDoneProgressParams,
PartialResultParams, Position, Range, ReferenceContext, TextDocumentPositionParams, Url,
WorkDoneProgressParams,
};
use tokio::test;

Expand Down Expand Up @@ -95,4 +97,70 @@ mod references_tests {
async fn test_on_references_request_without_including_declaration() {
check_references_succeeds("rename_function", "another_function", 0, false).await;
}

#[test]
async fn test_on_references_request_works_accross_workspace_packages() {
let (mut state, noir_text_document) = test_utils::init_lsp_server("workspace").await;

// noir_text_document is always `src/main.nr` in the workspace directory, so let's go to the workspace dir
let noir_text_document = noir_text_document.to_file_path().unwrap();
let workspace_dir = noir_text_document.parent().unwrap().parent().unwrap();

// Let's check that we can find references to `function_one` by doing that in the package "one"
// and getting results in the package "two" too.
let one_lib = Url::from_file_path(workspace_dir.join("one/src/lib.nr")).unwrap();
let two_lib = Url::from_file_path(workspace_dir.join("two/src/lib.nr")).unwrap();

// We call this to open the document, so that the entire workspace is analyzed
notifications::process_workspace_for_noir_document(one_lib.clone(), &mut state).unwrap();

let params = ReferenceParams {
text_document_position: TextDocumentPositionParams {
text_document: lsp_types::TextDocumentIdentifier { uri: one_lib.clone() },
position: Position { line: 0, character: 7 },
},
work_done_progress_params: WorkDoneProgressParams { work_done_token: None },
partial_result_params: PartialResultParams { partial_result_token: None },
context: ReferenceContext { include_declaration: true },
};

let mut locations = on_references_request(&mut state, params)
.await
.expect("Could not execute on_references_request")
.unwrap();

// The definition, a use in "two", and a call in "two"
assert_eq!(locations.len(), 3);

locations.sort_by_cached_key(|location| {
(location.uri.to_file_path().unwrap(), location.range.start.line)
});

assert_eq!(locations[0].uri, one_lib);
assert_eq!(
locations[0].range,
Range {
start: Position { line: 0, character: 7 },
end: Position { line: 0, character: 19 },
}
);

assert_eq!(locations[1].uri, two_lib);
assert_eq!(
locations[1].range,
Range {
start: Position { line: 0, character: 9 },
end: Position { line: 0, character: 21 },
}
);

assert_eq!(locations[2].uri, two_lib);
assert_eq!(
locations[2].range,
Range {
start: Position { line: 3, character: 4 },
end: Position { line: 3, character: 16 },
}
);
}
}
2 changes: 2 additions & 0 deletions tooling/lsp/test_programs/workspace/Nargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[workspace]
members = ["one", "two"]
4 changes: 4 additions & 0 deletions tooling/lsp/test_programs/workspace/one/Nargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[package]
name = "one"
authors = []
type = "lib"
1 change: 1 addition & 0 deletions tooling/lsp/test_programs/workspace/one/src/lib.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub fn function_one() {}
7 changes: 7 additions & 0 deletions tooling/lsp/test_programs/workspace/two/Nargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "two"
authors = []
type = "lib"

[dependencies]
one = { path = "../one" }
5 changes: 5 additions & 0 deletions tooling/lsp/test_programs/workspace/two/src/lib.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
use one::function_one;

pub fn function_two() {
function_one()
}

0 comments on commit c66b652

Please sign in to comment.