From 9763d9e8c4ca01a4df0d70877020ec7351403b75 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Mon, 15 Mar 2021 09:32:06 +0100 Subject: Enable searching for builtin types --- crates/ide/src/references.rs | 26 +++++++++++++++++++------- crates/ide/src/references/rename.rs | 3 ++- 2 files changed, 21 insertions(+), 8 deletions(-) (limited to 'crates/ide') 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}; #[derive(Debug, Clone)] pub struct ReferenceSearchResult { - pub declaration: Declaration, + pub declaration: Option, pub references: FxHashMap)>>, } @@ -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 @@ impl Foo { 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 + "#]], + ); + } } 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( 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(); -- cgit v1.2.3 From cad617bba054334e2172b9ef54f2ed82c6067794 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20Kr=C3=BCger?= Date: Mon, 15 Mar 2021 10:15:08 +0100 Subject: some clippy::performance fixes use vec![] instead of Vec::new() + push() avoid redundant clones use chars instead of &str for single char patterns in ends_with() and starts_with() allocate some Vecs with capacity to avoid unneccessary resizing --- crates/ide/src/join_lines.rs | 4 ++-- crates/ide/src/typing.rs | 3 +-- 2 files changed, 3 insertions(+), 4 deletions(-) (limited to 'crates/ide') diff --git a/crates/ide/src/join_lines.rs b/crates/ide/src/join_lines.rs index 20a920ddb..d571ed559 100644 --- a/crates/ide/src/join_lines.rs +++ b/crates/ide/src/join_lines.rs @@ -218,7 +218,7 @@ mod tests { let result = join_lines(&file, range); let actual = { - let mut actual = before.to_string(); + let mut actual = before; result.apply(&mut actual); actual }; @@ -622,7 +622,7 @@ fn foo() { let parse = SourceFile::parse(&before); let result = join_lines(&parse.tree(), sel); let actual = { - let mut actual = before.to_string(); + let mut actual = before; result.apply(&mut actual); actual }; diff --git a/crates/ide/src/typing.rs b/crates/ide/src/typing.rs index e3c3aebac..a718faf63 100644 --- a/crates/ide/src/typing.rs +++ b/crates/ide/src/typing.rs @@ -145,9 +145,8 @@ mod tests { use super::*; fn do_type_char(char_typed: char, before: &str) -> Option { - let (offset, before) = extract_offset(before); + let (offset, mut before) = extract_offset(before); let edit = TextEdit::insert(offset, char_typed.to_string()); - let mut before = before.to_string(); edit.apply(&mut before); let parse = SourceFile::parse(&before); on_char_typed_inner(&parse.tree(), offset, char_typed).map(|it| { -- cgit v1.2.3