Skip to content

feat: enable excluding refs search results in test #16441

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 22 additions & 6 deletions crates/ide-db/src/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ pub enum ReferenceCategory {
// FIXME: Some day should be able to search in doc comments. Would probably
// need to switch from enum to bitflags then?
// DocComment
Test,
Copy link
Member

@matklad matklad Apr 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something can be a test and a read at the same time. So, the time has come to use a bitset here! This is precisely the situation the old comment just above this line talks about

}

/// Generally, `search_scope` returns files that might contain references for the element.
Expand Down Expand Up @@ -743,7 +744,7 @@ impl<'a> FindUsages<'a> {
let reference = FileReference {
range,
name: FileReferenceNode::NameRef(name_ref.clone()),
category: ReferenceCategory::new(&def, name_ref),
category: ReferenceCategory::new(self.sema, &def, name_ref),
};
sink(file_id, reference)
}
Expand All @@ -759,7 +760,7 @@ impl<'a> FindUsages<'a> {
let reference = FileReference {
range,
name: FileReferenceNode::NameRef(name_ref.clone()),
category: ReferenceCategory::new(&def, name_ref),
category: ReferenceCategory::new(self.sema, &def, name_ref),
};
sink(file_id, reference)
}
Expand All @@ -769,7 +770,7 @@ impl<'a> FindUsages<'a> {
let reference = FileReference {
range,
name: FileReferenceNode::NameRef(name_ref.clone()),
category: ReferenceCategory::new(&def, name_ref),
category: ReferenceCategory::new(self.sema, &def, name_ref),
};
sink(file_id, reference)
} else {
Expand All @@ -783,10 +784,10 @@ impl<'a> FindUsages<'a> {
let local = Definition::Local(local);
let access = match self.def {
Definition::Field(_) if field == self.def => {
ReferenceCategory::new(&field, name_ref)
ReferenceCategory::new(self.sema, &field, name_ref)
}
Definition::Local(_) if local == self.def => {
ReferenceCategory::new(&local, name_ref)
ReferenceCategory::new(self.sema, &local, name_ref)
}
_ => return false,
};
Expand Down Expand Up @@ -871,7 +872,15 @@ fn def_to_ty(sema: &Semantics<'_, RootDatabase>, def: &Definition) -> Option<hir
}

impl ReferenceCategory {
fn new(def: &Definition, r: &ast::NameRef) -> Option<ReferenceCategory> {
fn new(
sema: &Semantics<'_, RootDatabase>,
def: &Definition,
r: &ast::NameRef,
) -> Option<ReferenceCategory> {
if is_name_ref_in_test(sema, r) {
return Some(ReferenceCategory::Test);
}

// Only Locals and Fields have accesses for now.
if !matches!(def, Definition::Local(_) | Definition::Field(_)) {
return is_name_ref_in_import(r).then_some(ReferenceCategory::Import);
Expand Down Expand Up @@ -910,3 +919,10 @@ fn is_name_ref_in_import(name_ref: &ast::NameRef) -> bool {
.and_then(|it| it.parent_path().top_path().syntax().parent())
.map_or(false, |it| it.kind() == SyntaxKind::USE_TREE)
}

fn is_name_ref_in_test(sema: &Semantics<'_, RootDatabase>, name_ref: &ast::NameRef) -> bool {
name_ref.syntax().ancestors().any(|node| match ast::Fn::cast(node) {
Some(it) => sema.to_def(&it).map_or(false, |func| func.is_test(sema.db)),
None => false,
})
}
1 change: 1 addition & 0 deletions crates/ide/src/highlight_related.rs
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,7 @@ mod tests {
ReferenceCategory::Read => "read",
ReferenceCategory::Write => "write",
ReferenceCategory::Import => "import",
ReferenceCategory::Test => "test",
}
.to_string()
}),
Expand Down
48 changes: 47 additions & 1 deletion crates/ide/src/references.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,51 @@ mod tests {

use crate::{fixture, SearchScope};

#[test]
fn exclude_tests() {
check(
r#"
fn test_func() {}

fn func() {
test_func$0();
}

#[test]
fn test() {
test_func();
}
"#,
expect![[r#"
test_func Function FileId(0) 0..17 3..12

FileId(0) 35..44
FileId(0) 75..84 Test
"#]],
);

check(
r#"
fn test_func() {}

fn func() {
test_func$0();
}

#[::core::prelude::v1::test]
fn test() {
test_func();
}
"#,
expect![[r#"
test_func Function FileId(0) 0..17 3..12

FileId(0) 35..44
FileId(0) 96..105 Test
"#]],
);
}

#[test]
fn test_struct_literal_after_space() {
check(
Expand Down Expand Up @@ -454,6 +499,7 @@ fn main() {
"#]],
);
}

#[test]
fn test_variant_tuple_before_paren() {
check(
Expand Down Expand Up @@ -1435,7 +1481,7 @@ fn test$0() {
expect![[r#"
test Function FileId(0) 0..33 11..15

FileId(0) 24..28
FileId(0) 24..28 Test
"#]],
);
}
Expand Down
7 changes: 7 additions & 0 deletions crates/rust-analyzer/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,9 @@ config_data! {
/// Exclude imports from find-all-references.
references_excludeImports: bool = "false",

/// Exclude tests from find-all-references.
references_excludeTests: bool = "false",

/// Allow renaming of items not belonging to the loaded workspaces.
rename_allowExternalItems: bool = "false",

Expand Down Expand Up @@ -1545,6 +1548,10 @@ impl Config {
self.data.references_excludeImports
}

pub fn find_all_refs_exclude_tests(&self) -> bool {
self.data.references_excludeTests
}

pub fn snippet_cap(&self) -> bool {
self.experimental("snippetTextEdit")
}
Expand Down
4 changes: 3 additions & 1 deletion crates/rust-analyzer/src/handlers/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1055,6 +1055,7 @@ pub(crate) fn handle_references(
let position = from_proto::file_position(&snap, params.text_document_position)?;

let exclude_imports = snap.config.find_all_refs_exclude_imports();
let exclude_tests = snap.config.find_all_refs_exclude_tests();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exclude_tests should be pushed down as a parameter to find_all_refs.

that way, a potentially more optimal implementation would be able to avoid even analyzing the test, which would be faster than filtering out results after the fact.

I think the second parameter we are passing here, which is currently None, is something like a SearchScope. It exists exactly to narrow down search results, and I think could be extended to exclude tests.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point, I had create an issue for this

(sorry for the slow response, kind of busy recently)


let refs = match snap.analysis.find_all_refs(position, None)? {
None => return Ok(None),
Expand All @@ -1078,7 +1079,8 @@ pub(crate) fn handle_references(
.flat_map(|(file_id, refs)| {
refs.into_iter()
.filter(|&(_, category)| {
!exclude_imports || category != Some(ReferenceCategory::Import)
(!exclude_imports || category != Some(ReferenceCategory::Import))
&& (!exclude_tests || category != Some(ReferenceCategory::Test))
})
.map(move |(range, _)| FileRange { file_id, range })
})
Expand Down
1 change: 1 addition & 0 deletions crates/rust-analyzer/src/lsp/to_proto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ pub(crate) fn document_highlight_kind(
ReferenceCategory::Read => Some(lsp_types::DocumentHighlightKind::READ),
ReferenceCategory::Write => Some(lsp_types::DocumentHighlightKind::WRITE),
ReferenceCategory::Import => None,
ReferenceCategory::Test => None,
}
}

Expand Down
5 changes: 5 additions & 0 deletions docs/user/generated_config.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -777,6 +777,11 @@ Internal config, path to proc-macro server executable.
--
Exclude imports from find-all-references.
--
[[rust-analyzer.references.excludeTests]]rust-analyzer.references.excludeTests (default: `false`)::
+
--
Exclude tests from find-all-references.
--
[[rust-analyzer.rename.allowExternalItems]]rust-analyzer.rename.allowExternalItems (default: `false`)::
+
--
Expand Down
5 changes: 5 additions & 0 deletions editors/code/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -1505,6 +1505,11 @@
"default": false,
"type": "boolean"
},
"rust-analyzer.references.excludeTests": {
"markdownDescription": "Exclude tests from find-all-references.",
"default": false,
"type": "boolean"
},
"rust-analyzer.rename.allowExternalItems": {
"markdownDescription": "Allow renaming of items not belonging to the loaded workspaces.",
"default": false,
Expand Down