Skip to content

Commit f39a250

Browse files
bors[bot]Veykril
andauthored
Merge #9607
9607: minor: Simplify r=Veykril a=Veykril bors r+ Co-authored-by: Lukas Wirth <[email protected]>
2 parents c18cba7 + 4450365 commit f39a250

File tree

4 files changed

+111
-112
lines changed

4 files changed

+111
-112
lines changed

crates/ide/src/goto_implementation.rs

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -105,31 +105,24 @@ fn impls_for_trait_item(
105105
#[cfg(test)]
106106
mod tests {
107107
use ide_db::base_db::FileRange;
108+
use itertools::Itertools;
108109

109110
use crate::fixture;
110111

111112
fn check(ra_fixture: &str) {
112-
let (analysis, position, annotations) = fixture::annotations(ra_fixture);
113+
let (analysis, position, expected) = fixture::annotations(ra_fixture);
113114

114115
let navs = analysis.goto_implementation(position).unwrap().unwrap().info;
115116

116-
let key = |frange: &FileRange| (frange.file_id, frange.range.start());
117+
let cmp = |frange: &FileRange| (frange.file_id, frange.range.start());
117118

118-
let mut expected = annotations
119-
.into_iter()
120-
.map(|(range, data)| {
121-
assert!(data.is_empty());
122-
range
123-
})
124-
.collect::<Vec<_>>();
125-
expected.sort_by_key(key);
126-
127-
let mut actual = navs
119+
let actual = navs
128120
.into_iter()
129121
.map(|nav| FileRange { file_id: nav.file_id, range: nav.focus_or_full_range() })
122+
.sorted_by_key(cmp)
130123
.collect::<Vec<_>>();
131-
actual.sort_by_key(key);
132-
124+
let expected =
125+
expected.into_iter().map(|(range, _)| range).sorted_by_key(cmp).collect::<Vec<_>>();
133126
assert_eq!(expected, actual);
134127
}
135128

crates/ide/src/prime_caches.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
//! rust-analyzer is lazy and doesn't not compute anything unless asked. This
1+
//! rust-analyzer is lazy and doesn't compute anything unless asked. This
22
//! sometimes is counter productive when, for example, the first goto definition
3-
//! request takes longer to compute. This modules implemented prepopulating of
3+
//! request takes longer to compute. This modules implemented prepopulation of
44
//! various caches, it's not really advanced at the moment.
55
66
use hir::db::DefDatabase;
@@ -27,7 +27,7 @@ pub(crate) fn prime_caches(db: &RootDatabase, cb: &(dyn Fn(PrimeCachesProgress)
2727
let topo = &graph.crates_in_topological_order();
2828

2929
cb(PrimeCachesProgress::Started);
30-
// Take care to emit the finish signal even when the computation is canceled.
30+
// Take care to emit the finish signal even when the computation is canceled.
3131
let _d = stdx::defer(|| cb(PrimeCachesProgress::Finished));
3232

3333
// FIXME: This would be easy to parallelize, since it's in the ideal ordering for that.

crates/ide/src/references.rs

Lines changed: 48 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use hir::{PathResolution, Semantics};
1313
use ide_db::{
1414
base_db::FileId,
1515
defs::{Definition, NameClass, NameRefClass},
16-
search::{ReferenceAccess, SearchScope},
16+
search::{ReferenceAccess, SearchScope, UsageSearchResult},
1717
RootDatabase,
1818
};
1919
use rustc_hash::FxHashMap;
@@ -56,48 +56,20 @@ pub(crate) fn find_all_refs(
5656
let _p = profile::span("find_all_refs");
5757
let syntax = sema.parse(position.file_id).syntax().clone();
5858

59-
let (def, is_literal_search) =
60-
if let Some(name) = get_name_of_item_declaration(&syntax, position) {
61-
(
62-
match NameClass::classify(sema, &name)? {
63-
NameClass::Definition(it) | NameClass::ConstReference(it) => it,
64-
NameClass::PatFieldShorthand { local_def: _, field_ref } => {
65-
Definition::Field(field_ref)
66-
}
67-
},
68-
true,
69-
)
70-
} else {
71-
(find_def(sema, &syntax, position.offset)?, false)
72-
};
73-
74-
let mut usages = def.usages(sema).set_scope(search_scope).include_self_refs().all();
75-
if is_literal_search {
76-
// filter for constructor-literals
77-
let refs = usages.references.values_mut();
78-
match def {
79-
Definition::ModuleDef(hir::ModuleDef::Adt(hir::Adt::Enum(enum_))) => {
80-
refs.for_each(|it| {
81-
it.retain(|reference| {
82-
reference
83-
.name
84-
.as_name_ref()
85-
.map_or(false, |name_ref| is_enum_lit_name_ref(sema, enum_, name_ref))
86-
})
87-
});
88-
usages.references.retain(|_, it| !it.is_empty());
89-
}
90-
Definition::ModuleDef(hir::ModuleDef::Adt(_) | hir::ModuleDef::Variant(_)) => {
91-
refs.for_each(|it| {
92-
it.retain(|reference| {
93-
reference.name.as_name_ref().map_or(false, is_lit_name_ref)
94-
})
95-
});
96-
usages.references.retain(|_, it| !it.is_empty());
59+
let mut is_literal_search = false;
60+
let def = if let Some(name) = name_for_constructor_search(&syntax, position) {
61+
is_literal_search = true;
62+
match NameClass::classify(sema, &name)? {
63+
NameClass::Definition(it) | NameClass::ConstReference(it) => it,
64+
NameClass::PatFieldShorthand { local_def: _, field_ref } => {
65+
Definition::Field(field_ref)
9766
}
98-
_ => {}
9967
}
100-
}
68+
} else {
69+
find_def(sema, &syntax, position.offset)?
70+
};
71+
72+
let mut usages = def.usages(sema).set_scope(search_scope).include_self_refs().all();
10173
let declaration = match def {
10274
Definition::ModuleDef(hir::ModuleDef::Module(module)) => {
10375
Some(NavigationTarget::from_module_to_decl(sema.db, module))
@@ -108,6 +80,10 @@ pub(crate) fn find_all_refs(
10880
let decl_range = nav.focus_or_full_range();
10981
Declaration { nav, access: decl_access(&def, &syntax, decl_range) }
11082
});
83+
if is_literal_search {
84+
retain_adt_literal_usages(&mut usages, def, sema);
85+
}
86+
11187
let references = usages
11288
.into_iter()
11389
.map(|(file_id, refs)| {
@@ -174,7 +150,37 @@ pub(crate) fn decl_access(
174150
None
175151
}
176152

177-
fn get_name_of_item_declaration(syntax: &SyntaxNode, position: FilePosition) -> Option<ast::Name> {
153+
/// Filter out all non-literal usages for adt-defs
154+
fn retain_adt_literal_usages(
155+
usages: &mut UsageSearchResult,
156+
def: Definition,
157+
sema: &Semantics<RootDatabase>,
158+
) {
159+
let refs = usages.references.values_mut();
160+
match def {
161+
Definition::ModuleDef(hir::ModuleDef::Adt(hir::Adt::Enum(enum_))) => {
162+
refs.for_each(|it| {
163+
it.retain(|reference| {
164+
reference
165+
.name
166+
.as_name_ref()
167+
.map_or(false, |name_ref| is_enum_lit_name_ref(sema, enum_, name_ref))
168+
})
169+
});
170+
usages.references.retain(|_, it| !it.is_empty());
171+
}
172+
Definition::ModuleDef(hir::ModuleDef::Adt(_) | hir::ModuleDef::Variant(_)) => {
173+
refs.for_each(|it| {
174+
it.retain(|reference| reference.name.as_name_ref().map_or(false, is_lit_name_ref))
175+
});
176+
usages.references.retain(|_, it| !it.is_empty());
177+
}
178+
_ => {}
179+
}
180+
}
181+
182+
/// Returns `Some` if the cursor is at a position for an item to search for all its constructor/literal usages
183+
fn name_for_constructor_search(syntax: &SyntaxNode, position: FilePosition) -> Option<ast::Name> {
178184
let token = syntax.token_at_offset(position.offset).right_biased()?;
179185
let token_parent = token.parent()?;
180186
let kind = token.kind();

crates/ide/src/syntax_highlighting.rs

Lines changed: 53 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use ide_db::{RootDatabase, SymbolKind};
1717
use rustc_hash::FxHashMap;
1818
use syntax::{
1919
ast::{self, HasFormatSpecifier},
20-
AstNode, AstToken, Direction, NodeOrToken,
20+
match_ast, AstNode, AstToken, Direction, NodeOrToken,
2121
SyntaxKind::*,
2222
SyntaxNode, TextRange, WalkEvent, T,
2323
};
@@ -159,15 +159,16 @@ pub(crate) fn highlight(
159159
// Determine the root based on the given range.
160160
let (root, range_to_highlight) = {
161161
let source_file = sema.parse(file_id);
162+
let source_file = source_file.syntax();
162163
match range_to_highlight {
163164
Some(range) => {
164-
let node = match source_file.syntax().covering_element(range) {
165+
let node = match source_file.covering_element(range) {
165166
NodeOrToken::Node(it) => it,
166-
NodeOrToken::Token(it) => it.parent().unwrap(),
167+
NodeOrToken::Token(it) => it.parent().unwrap_or_else(|| source_file.clone()),
167168
};
168169
(node, range)
169170
}
170-
None => (source_file.syntax().clone(), source_file.syntax().text_range()),
171+
None => (source_file.clone(), source_file.text_range()),
171172
}
172173
};
173174

@@ -211,62 +212,61 @@ fn traverse(
211212
continue;
212213
}
213214

214-
// Track "inside macro" state
215-
match event.clone().map(|it| it.into_node().and_then(ast::MacroCall::cast)) {
216-
WalkEvent::Enter(Some(mc)) => {
217-
if let Some(range) = macro_call_range(&mc) {
218-
hl.add(HlRange {
219-
range,
220-
highlight: HlTag::Symbol(SymbolKind::Macro).into(),
221-
binding_hash: None,
222-
});
223-
}
224-
current_macro_call = Some(mc.clone());
225-
continue;
226-
}
227-
WalkEvent::Leave(Some(mc)) => {
228-
assert_eq!(current_macro_call, Some(mc));
229-
current_macro_call = None;
230-
}
231-
_ => (),
232-
}
233-
match event.clone().map(|it| it.into_node().and_then(ast::Item::cast)) {
234-
WalkEvent::Enter(Some(item)) => {
235-
if sema.is_attr_macro_call(&item) {
236-
current_attr_macro_call = Some(item);
215+
match event.clone() {
216+
WalkEvent::Enter(NodeOrToken::Node(node)) => {
217+
match_ast! {
218+
match node {
219+
ast::MacroCall(mcall) => {
220+
if let Some(range) = macro_call_range(&mcall) {
221+
hl.add(HlRange {
222+
range,
223+
highlight: HlTag::Symbol(SymbolKind::Macro).into(),
224+
binding_hash: None,
225+
});
226+
}
227+
current_macro_call = Some(mcall);
228+
continue;
229+
},
230+
ast::Macro(mac) => {
231+
macro_highlighter.init();
232+
current_macro = Some(mac);
233+
continue;
234+
},
235+
ast::Item(item) => {
236+
if sema.is_attr_macro_call(&item) {
237+
current_attr_macro_call = Some(item);
238+
}
239+
},
240+
ast::Attr(__) => inside_attribute = true,
241+
_ => ()
242+
}
237243
}
238244
}
239-
WalkEvent::Leave(Some(item)) => {
240-
if current_attr_macro_call == Some(item) {
241-
current_attr_macro_call = None;
245+
WalkEvent::Leave(NodeOrToken::Node(node)) => {
246+
match_ast! {
247+
match node {
248+
ast::MacroCall(mcall) => {
249+
assert_eq!(current_macro_call, Some(mcall));
250+
current_macro_call = None;
251+
},
252+
ast::Macro(mac) => {
253+
assert_eq!(current_macro, Some(mac));
254+
current_macro = None;
255+
macro_highlighter = MacroHighlighter::default();
256+
},
257+
ast::Item(item) => {
258+
if current_attr_macro_call == Some(item) {
259+
current_attr_macro_call = None;
260+
}
261+
},
262+
ast::Attr(__) => inside_attribute = false,
263+
_ => ()
264+
}
242265
}
243266
}
244267
_ => (),
245268
}
246269

247-
match event.clone().map(|it| it.into_node().and_then(ast::Macro::cast)) {
248-
WalkEvent::Enter(Some(mac)) => {
249-
macro_highlighter.init();
250-
current_macro = Some(mac);
251-
continue;
252-
}
253-
WalkEvent::Leave(Some(mac)) => {
254-
assert_eq!(current_macro, Some(mac));
255-
current_macro = None;
256-
macro_highlighter = MacroHighlighter::default();
257-
}
258-
_ => (),
259-
}
260-
match &event {
261-
WalkEvent::Enter(NodeOrToken::Node(node)) if ast::Attr::can_cast(node.kind()) => {
262-
inside_attribute = true
263-
}
264-
WalkEvent::Leave(NodeOrToken::Node(node)) if ast::Attr::can_cast(node.kind()) => {
265-
inside_attribute = false
266-
}
267-
_ => (),
268-
}
269-
270270
let element = match event {
271271
WalkEvent::Enter(it) => it,
272272
WalkEvent::Leave(it) => {

0 commit comments

Comments
 (0)