From fbdb32adfc49e0d69b7fd8e44135bea59382d2cb Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Tue, 12 Jan 2021 00:05:07 +0100 Subject: Group references by FileId --- crates/ssr/src/search.rs | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) (limited to 'crates/ssr/src') diff --git a/crates/ssr/src/search.rs b/crates/ssr/src/search.rs index 44b5db029..a1d653aff 100644 --- a/crates/ssr/src/search.rs +++ b/crates/ssr/src/search.rs @@ -5,10 +5,10 @@ use crate::{ resolving::{ResolvedPath, ResolvedPattern, ResolvedRule}, Match, MatchFinder, }; -use ide_db::base_db::{FileId, FileRange}; use ide_db::{ + base_db::{FileId, FileRange}, defs::Definition, - search::{Reference, SearchScope}, + search::{FileReferences, SearchScope}, }; use rustc_hash::FxHashSet; use syntax::{ast, AstNode, SyntaxKind, SyntaxNode}; @@ -20,7 +20,7 @@ use test_utils::mark; /// them more than once. #[derive(Default)] pub(crate) struct UsageCache { - usages: Vec<(Definition, Vec)>, + usages: Vec<(Definition, Vec)>, } impl<'db> MatchFinder<'db> { @@ -58,8 +58,12 @@ impl<'db> MatchFinder<'db> { ) { if let Some(resolved_path) = pick_path_for_usages(pattern) { let definition: Definition = resolved_path.resolution.clone().into(); - for reference in self.find_usages(usage_cache, definition) { - if let Some(node_to_match) = self.find_node_to_match(resolved_path, reference) { + for file_range in self + .find_usages(usage_cache, definition) + .iter() + .flat_map(FileReferences::file_ranges) + { + if let Some(node_to_match) = self.find_node_to_match(resolved_path, file_range) { if !is_search_permitted_ancestors(&node_to_match) { mark::hit!(use_declaration_with_braces); continue; @@ -73,11 +77,11 @@ impl<'db> MatchFinder<'db> { fn find_node_to_match( &self, resolved_path: &ResolvedPath, - reference: &Reference, + file_range: FileRange, ) -> Option { - let file = self.sema.parse(reference.file_range.file_id); + let file = self.sema.parse(file_range.file_id); let depth = resolved_path.depth as usize; - let offset = reference.file_range.range.start(); + let offset = file_range.range.start(); if let Some(path) = self.sema.find_node_at_offset_with_descend::(file.syntax(), offset) { @@ -108,7 +112,7 @@ impl<'db> MatchFinder<'db> { &self, usage_cache: &'a mut UsageCache, definition: Definition, - ) -> &'a [Reference] { + ) -> &'a [FileReferences] { // Logically if a lookup succeeds we should just return it. Unfortunately returning it would // extend the lifetime of the borrow, then we wouldn't be able to do the insertion on a // cache miss. This is a limitation of NLL and is fixed with Polonius. For now we do two @@ -250,7 +254,7 @@ fn is_search_permitted(node: &SyntaxNode) -> bool { } impl UsageCache { - fn find(&mut self, definition: &Definition) -> Option<&[Reference]> { + fn find(&mut self, definition: &Definition) -> Option<&[FileReferences]> { // We expect a very small number of cache entries (generally 1), so a linear scan should be // fast enough and avoids the need to implement Hash for Definition. for (d, refs) in &self.usages { -- cgit v1.2.3 From 2c1777a2e264e58fccd5ace94b238c8a497ddbda Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Tue, 12 Jan 2021 15:51:02 +0100 Subject: Ensure uniqueness of file ids in reference search via hashmap --- crates/ssr/src/search.rs | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) (limited to 'crates/ssr/src') diff --git a/crates/ssr/src/search.rs b/crates/ssr/src/search.rs index a1d653aff..a3eb2e800 100644 --- a/crates/ssr/src/search.rs +++ b/crates/ssr/src/search.rs @@ -20,7 +20,7 @@ use test_utils::mark; /// them more than once. #[derive(Default)] pub(crate) struct UsageCache { - usages: Vec<(Definition, Vec)>, + usages: Vec<(Definition, FileReferences)>, } impl<'db> MatchFinder<'db> { @@ -58,11 +58,7 @@ impl<'db> MatchFinder<'db> { ) { if let Some(resolved_path) = pick_path_for_usages(pattern) { let definition: Definition = resolved_path.resolution.clone().into(); - for file_range in self - .find_usages(usage_cache, definition) - .iter() - .flat_map(FileReferences::file_ranges) - { + for file_range in self.find_usages(usage_cache, definition).file_ranges() { if let Some(node_to_match) = self.find_node_to_match(resolved_path, file_range) { if !is_search_permitted_ancestors(&node_to_match) { mark::hit!(use_declaration_with_braces); @@ -112,7 +108,7 @@ impl<'db> MatchFinder<'db> { &self, usage_cache: &'a mut UsageCache, definition: Definition, - ) -> &'a [FileReferences] { + ) -> &'a FileReferences { // Logically if a lookup succeeds we should just return it. Unfortunately returning it would // extend the lifetime of the borrow, then we wouldn't be able to do the insertion on a // cache miss. This is a limitation of NLL and is fixed with Polonius. For now we do two @@ -254,7 +250,7 @@ fn is_search_permitted(node: &SyntaxNode) -> bool { } impl UsageCache { - fn find(&mut self, definition: &Definition) -> Option<&[FileReferences]> { + fn find(&mut self, definition: &Definition) -> Option<&FileReferences> { // We expect a very small number of cache entries (generally 1), so a linear scan should be // fast enough and avoids the need to implement Hash for Definition. for (d, refs) in &self.usages { -- cgit v1.2.3 From aff9102afb2b6756b3935dedb9be30401975b262 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Tue, 12 Jan 2021 15:56:24 +0100 Subject: Rename FileReferences -> UsageSearchResult --- crates/ssr/src/search.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'crates/ssr/src') diff --git a/crates/ssr/src/search.rs b/crates/ssr/src/search.rs index a3eb2e800..836eb94b2 100644 --- a/crates/ssr/src/search.rs +++ b/crates/ssr/src/search.rs @@ -8,7 +8,7 @@ use crate::{ use ide_db::{ base_db::{FileId, FileRange}, defs::Definition, - search::{FileReferences, SearchScope}, + search::{SearchScope, UsageSearchResult}, }; use rustc_hash::FxHashSet; use syntax::{ast, AstNode, SyntaxKind, SyntaxNode}; @@ -20,7 +20,7 @@ use test_utils::mark; /// them more than once. #[derive(Default)] pub(crate) struct UsageCache { - usages: Vec<(Definition, FileReferences)>, + usages: Vec<(Definition, UsageSearchResult)>, } impl<'db> MatchFinder<'db> { @@ -108,7 +108,7 @@ impl<'db> MatchFinder<'db> { &self, usage_cache: &'a mut UsageCache, definition: Definition, - ) -> &'a FileReferences { + ) -> &'a UsageSearchResult { // Logically if a lookup succeeds we should just return it. Unfortunately returning it would // extend the lifetime of the borrow, then we wouldn't be able to do the insertion on a // cache miss. This is a limitation of NLL and is fixed with Polonius. For now we do two @@ -250,7 +250,7 @@ fn is_search_permitted(node: &SyntaxNode) -> bool { } impl UsageCache { - fn find(&mut self, definition: &Definition) -> Option<&FileReferences> { + fn find(&mut self, definition: &Definition) -> Option<&UsageSearchResult> { // We expect a very small number of cache entries (generally 1), so a linear scan should be // fast enough and avoids the need to implement Hash for Definition. for (d, refs) in &self.usages { -- cgit v1.2.3