Unnamed repository; edit this file 'description' to name the repository.
Only report unique text ranges in highlight_related
Lukas Wirth 2021-08-29
parent 72bfbb0 · commit 99f1e66
-rw-r--r--crates/hir/src/semantics.rs2
-rw-r--r--crates/ide/src/highlight_related.rs14
-rw-r--r--crates/ide_db/src/search.rs2
3 files changed, 8 insertions, 10 deletions
diff --git a/crates/hir/src/semantics.rs b/crates/hir/src/semantics.rs
index cb1efb90ab..a0859dd1ec 100644
--- a/crates/hir/src/semantics.rs
+++ b/crates/hir/src/semantics.rs
@@ -538,6 +538,8 @@ impl<'db> SemanticsImpl<'db> {
res
}
+ // Note this return type is deliberate as [`find_nodes_at_offset_with_descend`] wants to stop
+ // traversing the inner iterator when it finds a node.
fn descend_node_at_offset(
&self,
node: &SyntaxNode,
diff --git a/crates/ide/src/highlight_related.rs b/crates/ide/src/highlight_related.rs
index e442cf7c4f..67ad263fa2 100644
--- a/crates/ide/src/highlight_related.rs
+++ b/crates/ide/src/highlight_related.rs
@@ -6,7 +6,7 @@ use ide_db::{
search::{FileReference, ReferenceAccess, SearchScope},
RootDatabase,
};
-use itertools::Itertools;
+use rustc_hash::FxHashSet;
use syntax::{
ast::{self, LoopBodyOwner},
match_ast, AstNode, SyntaxNode, SyntaxToken, TextRange, TextSize, T,
@@ -14,6 +14,7 @@ use syntax::{
use crate::{display::TryToNav, references, NavigationTarget};
+#[derive(PartialEq, Eq, Hash)]
pub struct HighlightedRange {
pub range: TextRange,
pub access: Option<ReferenceAccess>,
@@ -100,11 +101,11 @@ fn highlight_references(
})
});
- let res: Vec<_> = declarations.chain(usages).collect();
+ let res: FxHashSet<_> = declarations.chain(usages).collect();
if res.is_empty() {
None
} else {
- Some(res)
+ Some(res.into_iter().collect())
}
}
@@ -276,7 +277,7 @@ fn find_defs(
sema: &Semantics<RootDatabase>,
syntax: &SyntaxNode,
offset: TextSize,
-) -> Vec<Definition> {
+) -> FxHashSet<Definition> {
sema.find_nodes_at_offset_with_descend(syntax, offset)
.flat_map(|name_like| {
Some(match name_like {
@@ -309,7 +310,6 @@ fn find_defs(
})
})
.flatten()
- .unique()
.collect()
}
@@ -423,8 +423,6 @@ macro_rules! foo {
foo!(bar$0);
// ^^^
- // ^^^
- // ^^^
fn foo() {
let bar: bar = bar();
// ^^^
@@ -443,7 +441,6 @@ macro_rules! foo {
foo!(bar);
// ^^^
- // ^^^
fn foo() {
let bar: bar$0 = bar();
// ^^^
@@ -873,7 +870,6 @@ fn function(field: u32) {
//^^^^^
Struct { field$0 }
//^^^^^ read
- //^^^^^ read
}
"#,
);
diff --git a/crates/ide_db/src/search.rs b/crates/ide_db/src/search.rs
index b125d1666f..855675be42 100644
--- a/crates/ide_db/src/search.rs
+++ b/crates/ide_db/src/search.rs
@@ -61,7 +61,7 @@ pub struct FileReference {
pub access: Option<ReferenceAccess>,
}
-#[derive(Debug, Copy, Clone, PartialEq)]
+#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum ReferenceAccess {
Read,
Write,