diff options
author | Lukas Wirth <[email protected]> | 2021-03-15 08:32:06 +0000 |
---|---|---|
committer | Lukas Wirth <[email protected]> | 2021-03-15 08:32:06 +0000 |
commit | 9763d9e8c4ca01a4df0d70877020ec7351403b75 (patch) | |
tree | b007710dcc6152b5c0fedd1c0bedf8c96811652d /crates | |
parent | c0459c53572f90fa9134192e432562af3daba5fa (diff) |
Enable searching for builtin types
Diffstat (limited to 'crates')
-rw-r--r-- | crates/ide/src/references.rs | 26 | ||||
-rw-r--r-- | crates/ide/src/references/rename.rs | 3 | ||||
-rw-r--r-- | crates/ide_db/src/defs.rs | 2 | ||||
-rw-r--r-- | crates/ide_db/src/search.rs | 16 | ||||
-rw-r--r-- | crates/rust-analyzer/src/handlers.rs | 20 |
5 files changed, 46 insertions, 21 deletions
diff --git a/crates/ide/src/references.rs b/crates/ide/src/references.rs index fef70533d..ec7c7686d 100644 --- a/crates/ide/src/references.rs +++ b/crates/ide/src/references.rs | |||
@@ -29,7 +29,7 @@ use crate::{display::TryToNav, FilePosition, NavigationTarget}; | |||
29 | 29 | ||
30 | #[derive(Debug, Clone)] | 30 | #[derive(Debug, Clone)] |
31 | pub struct ReferenceSearchResult { | 31 | pub struct ReferenceSearchResult { |
32 | pub declaration: Declaration, | 32 | pub declaration: Option<Declaration>, |
33 | pub references: FxHashMap<FileId, Vec<(TextRange, Option<ReferenceAccess>)>>, | 33 | pub references: FxHashMap<FileId, Vec<(TextRange, Option<ReferenceAccess>)>>, |
34 | } | 34 | } |
35 | 35 | ||
@@ -91,10 +91,10 @@ pub(crate) fn find_all_refs( | |||
91 | _ => {} | 91 | _ => {} |
92 | } | 92 | } |
93 | } | 93 | } |
94 | let nav = def.try_to_nav(sema.db)?; | 94 | let declaration = def.try_to_nav(sema.db).map(|nav| { |
95 | let decl_range = nav.focus_or_full_range(); | 95 | let decl_range = nav.focus_or_full_range(); |
96 | 96 | Declaration { nav, access: decl_access(&def, &syntax, decl_range) } | |
97 | let declaration = Declaration { nav, access: decl_access(&def, &syntax, decl_range) }; | 97 | }); |
98 | let references = usages | 98 | let references = usages |
99 | .into_iter() | 99 | .into_iter() |
100 | .map(|(file_id, refs)| { | 100 | .map(|(file_id, refs)| { |
@@ -1004,8 +1004,7 @@ impl Foo { | |||
1004 | let refs = analysis.find_all_refs(pos, search_scope).unwrap().unwrap(); | 1004 | let refs = analysis.find_all_refs(pos, search_scope).unwrap().unwrap(); |
1005 | 1005 | ||
1006 | let mut actual = String::new(); | 1006 | let mut actual = String::new(); |
1007 | { | 1007 | if let Some(decl) = refs.declaration { |
1008 | let decl = refs.declaration; | ||
1009 | format_to!(actual, "{}", decl.nav.debug_render()); | 1008 | format_to!(actual, "{}", decl.nav.debug_render()); |
1010 | if let Some(access) = decl.access { | 1009 | if let Some(access) = decl.access { |
1011 | format_to!(actual, " {:?}", access) | 1010 | format_to!(actual, " {:?}", access) |
@@ -1258,4 +1257,17 @@ fn main() { | |||
1258 | "#]], | 1257 | "#]], |
1259 | ); | 1258 | ); |
1260 | } | 1259 | } |
1260 | |||
1261 | #[test] | ||
1262 | fn test_primitives() { | ||
1263 | check( | ||
1264 | r#" | ||
1265 | fn foo(_: bool) -> bo$0ol { true } | ||
1266 | "#, | ||
1267 | expect![[r#" | ||
1268 | FileId(0) 10..14 | ||
1269 | FileId(0) 19..23 | ||
1270 | "#]], | ||
1271 | ); | ||
1272 | } | ||
1261 | } | 1273 | } |
diff --git a/crates/ide/src/references/rename.rs b/crates/ide/src/references/rename.rs index 1e378279d..5340b638a 100644 --- a/crates/ide/src/references/rename.rs +++ b/crates/ide/src/references/rename.rs | |||
@@ -510,7 +510,8 @@ fn source_edit_from_def( | |||
510 | def: Definition, | 510 | def: Definition, |
511 | new_name: &str, | 511 | new_name: &str, |
512 | ) -> RenameResult<(FileId, TextEdit)> { | 512 | ) -> RenameResult<(FileId, TextEdit)> { |
513 | let nav = def.try_to_nav(sema.db).unwrap(); | 513 | let nav = |
514 | def.try_to_nav(sema.db).ok_or_else(|| format_err!("No references found at position"))?; | ||
514 | 515 | ||
515 | let mut replacement_text = String::new(); | 516 | let mut replacement_text = String::new(); |
516 | let mut repl_range = nav.focus_or_full_range(); | 517 | let mut repl_range = nav.focus_or_full_range(); |
diff --git a/crates/ide_db/src/defs.rs b/crates/ide_db/src/defs.rs index ff612b7d0..f86e5ce93 100644 --- a/crates/ide_db/src/defs.rs +++ b/crates/ide_db/src/defs.rs | |||
@@ -70,7 +70,7 @@ impl Definition { | |||
70 | hir::ModuleDef::Static(it) => it.name(db)?, | 70 | hir::ModuleDef::Static(it) => it.name(db)?, |
71 | hir::ModuleDef::Trait(it) => it.name(db), | 71 | hir::ModuleDef::Trait(it) => it.name(db), |
72 | hir::ModuleDef::TypeAlias(it) => it.name(db), | 72 | hir::ModuleDef::TypeAlias(it) => it.name(db), |
73 | hir::ModuleDef::BuiltinType(_) => return None, | 73 | hir::ModuleDef::BuiltinType(it) => it.name(), |
74 | }, | 74 | }, |
75 | Definition::SelfType(_) => return None, | 75 | Definition::SelfType(_) => return None, |
76 | Definition::Local(it) => it.name(db)?, | 76 | Definition::Local(it) => it.name(db)?, |
diff --git a/crates/ide_db/src/search.rs b/crates/ide_db/src/search.rs index ddcfbd3f3..1615d407e 100644 --- a/crates/ide_db/src/search.rs +++ b/crates/ide_db/src/search.rs | |||
@@ -6,7 +6,7 @@ | |||
6 | 6 | ||
7 | use std::{convert::TryInto, mem}; | 7 | use std::{convert::TryInto, mem}; |
8 | 8 | ||
9 | use base_db::{FileId, FileRange, SourceDatabaseExt}; | 9 | use base_db::{FileId, FileRange, SourceDatabase, SourceDatabaseExt}; |
10 | use hir::{DefWithBody, HasSource, Module, ModuleSource, Semantics, Visibility}; | 10 | use hir::{DefWithBody, HasSource, Module, ModuleSource, Semantics, Visibility}; |
11 | use once_cell::unsync::Lazy; | 11 | use once_cell::unsync::Lazy; |
12 | use rustc_hash::FxHashMap; | 12 | use rustc_hash::FxHashMap; |
@@ -134,6 +134,20 @@ impl IntoIterator for SearchScope { | |||
134 | impl Definition { | 134 | impl Definition { |
135 | fn search_scope(&self, db: &RootDatabase) -> SearchScope { | 135 | fn search_scope(&self, db: &RootDatabase) -> SearchScope { |
136 | let _p = profile::span("search_scope"); | 136 | let _p = profile::span("search_scope"); |
137 | |||
138 | if let Definition::ModuleDef(hir::ModuleDef::BuiltinType(_)) = self { | ||
139 | let mut res = FxHashMap::default(); | ||
140 | |||
141 | let graph = db.crate_graph(); | ||
142 | for krate in graph.iter() { | ||
143 | let root_file = graph[krate].root_file_id; | ||
144 | let source_root_id = db.file_source_root(root_file); | ||
145 | let source_root = db.source_root(source_root_id); | ||
146 | res.extend(source_root.iter().map(|id| (id, None))); | ||
147 | } | ||
148 | return SearchScope::new(res); | ||
149 | } | ||
150 | |||
137 | let module = match self.module(db) { | 151 | let module = match self.module(db) { |
138 | Some(it) => it, | 152 | Some(it) => it, |
139 | None => return SearchScope::empty(), | 153 | None => return SearchScope::empty(), |
diff --git a/crates/rust-analyzer/src/handlers.rs b/crates/rust-analyzer/src/handlers.rs index 6cc433cb8..9bb82164d 100644 --- a/crates/rust-analyzer/src/handlers.rs +++ b/crates/rust-analyzer/src/handlers.rs | |||
@@ -828,9 +828,9 @@ pub(crate) fn handle_references( | |||
828 | }; | 828 | }; |
829 | 829 | ||
830 | let decl = if params.context.include_declaration { | 830 | let decl = if params.context.include_declaration { |
831 | Some(FileRange { | 831 | refs.declaration.map(|decl| FileRange { |
832 | file_id: refs.declaration.nav.file_id, | 832 | file_id: decl.nav.file_id, |
833 | range: refs.declaration.nav.focus_or_full_range(), | 833 | range: decl.nav.focus_or_full_range(), |
834 | }) | 834 | }) |
835 | } else { | 835 | } else { |
836 | None | 836 | None |
@@ -1135,14 +1135,12 @@ pub(crate) fn handle_document_highlight( | |||
1135 | Some(refs) => refs, | 1135 | Some(refs) => refs, |
1136 | }; | 1136 | }; |
1137 | 1137 | ||
1138 | let decl = if refs.declaration.nav.file_id == position.file_id { | 1138 | let decl = refs.declaration.filter(|decl| decl.nav.file_id == position.file_id).map(|decl| { |
1139 | Some(DocumentHighlight { | 1139 | DocumentHighlight { |
1140 | range: to_proto::range(&line_index, refs.declaration.nav.focus_or_full_range()), | 1140 | range: to_proto::range(&line_index, decl.nav.focus_or_full_range()), |
1141 | kind: refs.declaration.access.map(to_proto::document_highlight_kind), | 1141 | kind: decl.access.map(to_proto::document_highlight_kind), |
1142 | }) | 1142 | } |
1143 | } else { | 1143 | }); |
1144 | None | ||
1145 | }; | ||
1146 | 1144 | ||
1147 | let file_refs = refs.references.get(&position.file_id).map_or(&[][..], Vec::as_slice); | 1145 | let file_refs = refs.references.get(&position.file_id).map_or(&[][..], Vec::as_slice); |
1148 | let mut res = Vec::with_capacity(file_refs.len() + 1); | 1146 | let mut res = Vec::with_capacity(file_refs.len() + 1); |