Enable searching for builtin types

This commit is contained in:
Lukas Wirth 2021-03-15 09:32:06 +01:00
parent c0459c5357
commit 9763d9e8c4
5 changed files with 46 additions and 21 deletions

View File

@ -29,7 +29,7 @@
#[derive(Debug, Clone)]
pub struct ReferenceSearchResult {
pub declaration: Declaration,
pub declaration: Option<Declaration>,
pub references: FxHashMap<FileId, Vec<(TextRange, Option<ReferenceAccess>)>>,
}
@ -91,10 +91,10 @@ pub(crate) fn find_all_refs(
_ => {}
}
}
let nav = def.try_to_nav(sema.db)?;
let decl_range = nav.focus_or_full_range();
let declaration = Declaration { nav, access: decl_access(&def, &syntax, decl_range) };
let declaration = def.try_to_nav(sema.db).map(|nav| {
let decl_range = nav.focus_or_full_range();
Declaration { nav, access: decl_access(&def, &syntax, decl_range) }
});
let references = usages
.into_iter()
.map(|(file_id, refs)| {
@ -1004,8 +1004,7 @@ fn check_with_scope(ra_fixture: &str, search_scope: Option<SearchScope>, expect:
let refs = analysis.find_all_refs(pos, search_scope).unwrap().unwrap();
let mut actual = String::new();
{
let decl = refs.declaration;
if let Some(decl) = refs.declaration {
format_to!(actual, "{}", decl.nav.debug_render());
if let Some(access) = decl.access {
format_to!(actual, " {:?}", access)
@ -1258,4 +1257,17 @@ fn main() {
"#]],
);
}
#[test]
fn test_primitives() {
check(
r#"
fn foo(_: bool) -> bo$0ol { true }
"#,
expect![[r#"
FileId(0) 10..14
FileId(0) 19..23
"#]],
);
}
}

View File

@ -510,7 +510,8 @@ fn source_edit_from_def(
def: Definition,
new_name: &str,
) -> RenameResult<(FileId, TextEdit)> {
let nav = def.try_to_nav(sema.db).unwrap();
let nav =
def.try_to_nav(sema.db).ok_or_else(|| format_err!("No references found at position"))?;
let mut replacement_text = String::new();
let mut repl_range = nav.focus_or_full_range();

View File

@ -70,7 +70,7 @@ pub fn name(&self, db: &RootDatabase) -> Option<Name> {
hir::ModuleDef::Static(it) => it.name(db)?,
hir::ModuleDef::Trait(it) => it.name(db),
hir::ModuleDef::TypeAlias(it) => it.name(db),
hir::ModuleDef::BuiltinType(_) => return None,
hir::ModuleDef::BuiltinType(it) => it.name(),
},
Definition::SelfType(_) => return None,
Definition::Local(it) => it.name(db)?,

View File

@ -6,7 +6,7 @@
use std::{convert::TryInto, mem};
use base_db::{FileId, FileRange, SourceDatabaseExt};
use base_db::{FileId, FileRange, SourceDatabase, SourceDatabaseExt};
use hir::{DefWithBody, HasSource, Module, ModuleSource, Semantics, Visibility};
use once_cell::unsync::Lazy;
use rustc_hash::FxHashMap;
@ -134,6 +134,20 @@ fn into_iter(self) -> Self::IntoIter {
impl Definition {
fn search_scope(&self, db: &RootDatabase) -> SearchScope {
let _p = profile::span("search_scope");
if let Definition::ModuleDef(hir::ModuleDef::BuiltinType(_)) = self {
let mut res = FxHashMap::default();
let graph = db.crate_graph();
for krate in graph.iter() {
let root_file = graph[krate].root_file_id;
let source_root_id = db.file_source_root(root_file);
let source_root = db.source_root(source_root_id);
res.extend(source_root.iter().map(|id| (id, None)));
}
return SearchScope::new(res);
}
let module = match self.module(db) {
Some(it) => it,
None => return SearchScope::empty(),

View File

@ -828,9 +828,9 @@ pub(crate) fn handle_references(
};
let decl = if params.context.include_declaration {
Some(FileRange {
file_id: refs.declaration.nav.file_id,
range: refs.declaration.nav.focus_or_full_range(),
refs.declaration.map(|decl| FileRange {
file_id: decl.nav.file_id,
range: decl.nav.focus_or_full_range(),
})
} else {
None
@ -1135,14 +1135,12 @@ pub(crate) fn handle_document_highlight(
Some(refs) => refs,
};
let decl = if refs.declaration.nav.file_id == position.file_id {
Some(DocumentHighlight {
range: to_proto::range(&line_index, refs.declaration.nav.focus_or_full_range()),
kind: refs.declaration.access.map(to_proto::document_highlight_kind),
})
} else {
None
};
let decl = refs.declaration.filter(|decl| decl.nav.file_id == position.file_id).map(|decl| {
DocumentHighlight {
range: to_proto::range(&line_index, decl.nav.focus_or_full_range()),
kind: decl.access.map(to_proto::document_highlight_kind),
}
});
let file_refs = refs.references.get(&position.file_id).map_or(&[][..], Vec::as_slice);
let mut res = Vec::with_capacity(file_refs.len() + 1);