Skip to content

Commit 6406490

Browse files
committed
Fully remove old macro descension API
1 parent 4951180 commit 6406490

File tree

13 files changed

+66
-103
lines changed

13 files changed

+66
-103
lines changed

crates/hir/src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,7 @@ pub use crate::{
9393
diagnostics::*,
9494
has_source::HasSource,
9595
semantics::{
96-
DescendPreference, PathResolution, Semantics, SemanticsImpl, SemanticsScope, TypeInfo,
97-
VisibleTraits,
96+
PathResolution, Semantics, SemanticsImpl, SemanticsScope, TypeInfo, VisibleTraits,
9897
},
9998
};
10099
pub use hir_ty::method_resolution::TyFingerprint;

crates/hir/src/semantics.rs

Lines changed: 16 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,6 @@ use crate::{
5151

5252
const CONTINUE_NO_BREAKS: ControlFlow<Infallible, ()> = ControlFlow::Continue(());
5353

54-
pub enum DescendPreference {
55-
None,
56-
}
57-
5854
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
5955
pub enum PathResolution {
6056
/// An item
@@ -183,6 +179,7 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> {
183179

184180
/// Find an AstNode by offset inside SyntaxNode, if it is inside *MacroCall*,
185181
/// descend it and find again
182+
// FIXME: Rethink this API
186183
pub fn find_node_at_offset_with_descend<N: AstNode>(
187184
&self,
188185
node: &SyntaxNode,
@@ -191,8 +188,9 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> {
191188
self.imp.descend_node_at_offset(node, offset).flatten().find_map(N::cast)
192189
}
193190

194-
/// Find an AstNode by offset inside SyntaxNode, if it is inside *MacroCall*,
191+
/// Find an AstNode by offset inside SyntaxNode, if it is inside an attribte macro call,
195192
/// descend it and find again
193+
// FIXME: Rethink this API
196194
pub fn find_nodes_at_offset_with_descend<'slf, N: AstNode + 'slf>(
197195
&'slf self,
198196
node: &SyntaxNode,
@@ -666,38 +664,6 @@ impl<'db> SemanticsImpl<'db> {
666664
res
667665
}
668666

669-
/// Descend the token into its macro call if it is part of one, returning the tokens in the
670-
/// expansion that it is associated with.
671-
pub fn descend_into_macros(
672-
&self,
673-
mode: DescendPreference,
674-
token: SyntaxToken,
675-
) -> SmallVec<[SyntaxToken; 1]> {
676-
enum Dp {
677-
None,
678-
}
679-
let mode = match mode {
680-
DescendPreference::None => Dp::None,
681-
};
682-
let mut res = smallvec![];
683-
self.descend_into_macros_impl::<Infallible>(
684-
token.clone(),
685-
&mut |InFile { value, .. }| {
686-
let is_a_match = match mode {
687-
Dp::None => true,
688-
};
689-
if is_a_match {
690-
res.push(value);
691-
}
692-
ControlFlow::Continue(())
693-
},
694-
);
695-
if res.is_empty() {
696-
res.push(token);
697-
}
698-
res
699-
}
700-
701667
pub fn descend_into_macros_ng(
702668
&self,
703669
token: SyntaxToken,
@@ -709,6 +675,18 @@ impl<'db> SemanticsImpl<'db> {
709675
});
710676
}
711677

678+
pub fn descend_into_macros_ng_v(&self, token: SyntaxToken) -> SmallVec<[SyntaxToken; 1]> {
679+
let mut res = smallvec![];
680+
self.descend_into_macros_impl(token.clone(), &mut |t| {
681+
res.push(t.value);
682+
CONTINUE_NO_BREAKS
683+
});
684+
if res.is_empty() {
685+
res.push(token);
686+
}
687+
res
688+
}
689+
712690
pub fn descend_into_macros_ng_b<T>(
713691
&self,
714692
token: SyntaxToken,
@@ -1003,7 +981,7 @@ impl<'db> SemanticsImpl<'db> {
1003981
offset: TextSize,
1004982
) -> impl Iterator<Item = impl Iterator<Item = SyntaxNode> + '_> + '_ {
1005983
node.token_at_offset(offset)
1006-
.map(move |token| self.descend_into_macros(DescendPreference::None, token))
984+
.map(move |token| self.descend_into_macros_exact(token))
1007985
.map(|descendants| {
1008986
descendants.into_iter().map(move |it| self.token_ancestors_with_macros(it))
1009987
})

crates/ide-assists/src/handlers/extract_function.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ use std::{iter, ops::RangeInclusive};
33
use ast::make;
44
use either::Either;
55
use hir::{
6-
DescendPreference, HasSource, HirDisplay, InFile, Local, LocalSource, ModuleDef,
7-
PathResolution, Semantics, TypeInfo, TypeParam,
6+
HasSource, HirDisplay, InFile, Local, LocalSource, ModuleDef, PathResolution, Semantics,
7+
TypeInfo, TypeParam,
88
};
99
use ide_db::{
1010
defs::{Definition, NameRefClass},
@@ -834,7 +834,7 @@ impl FunctionBody {
834834
.descendants_with_tokens()
835835
.filter_map(SyntaxElement::into_token)
836836
.filter(|it| matches!(it.kind(), SyntaxKind::IDENT | T![self]))
837-
.flat_map(|t| sema.descend_into_macros(DescendPreference::None, t))
837+
.flat_map(|t| sema.descend_into_macros_exact(t))
838838
.for_each(|t| add_name_if_local(t.parent().and_then(ast::NameRef::cast)));
839839
}
840840
}

crates/ide-db/src/helpers.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use std::collections::VecDeque;
44

55
use base_db::SourceRootDatabase;
6-
use hir::{Crate, DescendPreference, ItemInNs, ModuleDef, Name, Semantics};
6+
use hir::{Crate, ItemInNs, ModuleDef, Name, Semantics};
77
use span::{Edition, FileId};
88
use syntax::{
99
ast::{self, make},
@@ -112,11 +112,12 @@ pub fn is_editable_crate(krate: Crate, db: &RootDatabase) -> bool {
112112
!db.source_root(source_root_id).is_library
113113
}
114114

115+
// FIXME: This is a weird function
115116
pub fn get_definition(
116117
sema: &Semantics<'_, RootDatabase>,
117118
token: SyntaxToken,
118119
) -> Option<Definition> {
119-
for token in sema.descend_into_macros(DescendPreference::None, token) {
120+
for token in sema.descend_into_macros_exact(token) {
120121
let def = IdentClass::classify_token(sema, &token).map(IdentClass::definitions_no_ops);
121122
if let Some(&[x]) = def.as_deref() {
122123
return Some(x);

crates/ide-db/src/search.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ use std::mem;
99

1010
use base_db::{salsa::Database, SourceDatabase, SourceRootDatabase};
1111
use hir::{
12-
sym, AsAssocItem, DefWithBody, DescendPreference, FileRange, HasAttrs, HasSource, HirFileIdExt,
13-
InFile, InRealFile, ModuleSource, PathResolution, Semantics, Visibility,
12+
sym, AsAssocItem, DefWithBody, FileRange, HasAttrs, HasSource, HirFileIdExt, InFile,
13+
InRealFile, ModuleSource, PathResolution, Semantics, Visibility,
1414
};
1515
use memchr::memmem::Finder;
1616
use parser::SyntaxKind;
@@ -549,9 +549,7 @@ impl<'a> FindUsages<'a> {
549549
// every textual hit. That function is notoriously
550550
// expensive even for things that do not get down mapped
551551
// into macros.
552-
sema.descend_into_macros(DescendPreference::None, token)
553-
.into_iter()
554-
.filter_map(|it| it.parent())
552+
sema.descend_into_macros_exact(token).into_iter().filter_map(|it| it.parent())
555553
})
556554
};
557555

crates/ide/src/call_hierarchy.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use std::iter;
44

5-
use hir::{DescendPreference, Semantics};
5+
use hir::Semantics;
66
use ide_db::{
77
defs::{Definition, NameClass, NameRefClass},
88
helpers::pick_best_token,
@@ -86,7 +86,7 @@ pub(crate) fn outgoing_calls(
8686
})?;
8787
let mut calls = CallLocations::default();
8888

89-
sema.descend_into_macros(DescendPreference::None, token)
89+
sema.descend_into_macros_exact(token)
9090
.into_iter()
9191
.filter_map(|it| it.parent_ancestors().nth(1).and_then(ast::Item::cast))
9292
.filter_map(|item| match item {

crates/ide/src/doc_links.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,7 @@ use pulldown_cmark_to_cmark::{cmark_resume_with_options, Options as CMarkOptions
1010
use stdx::format_to;
1111
use url::Url;
1212

13-
use hir::{
14-
db::HirDatabase, sym, Adt, AsAssocItem, AssocItem, AssocItemContainer, DescendPreference,
15-
HasAttrs,
16-
};
13+
use hir::{db::HirDatabase, sym, Adt, AsAssocItem, AssocItem, AssocItemContainer, HasAttrs};
1714
use ide_db::{
1815
base_db::{CrateOrigin, LangCrateOrigin, ReleaseChannel, SourceDatabase},
1916
defs::{Definition, NameClass, NameRefClass},
@@ -289,7 +286,7 @@ impl DocCommentToken {
289286
let original_start = doc_token.text_range().start();
290287
let relative_comment_offset = offset - original_start - prefix_len;
291288

292-
sema.descend_into_macros(DescendPreference::None, doc_token).into_iter().find_map(|t| {
289+
sema.descend_into_macros_ng_v(doc_token).into_iter().find_map(|t| {
293290
let (node, descended_prefix_len) = match_ast! {
294291
match t {
295292
ast::Comment(comment) => (t.parent()?, TextSize::try_from(comment.prefix().len()).ok()?),

crates/ide/src/expand_macro.rs

Lines changed: 24 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use hir::{DescendPreference, InFile, MacroFileIdExt, Semantics};
1+
use hir::{InFile, MacroFileIdExt, Semantics};
22
use ide_db::{
33
helpers::pick_best_token, syntax_helpers::insert_whitespace_into_node::insert_ws_into, FileId,
44
RootDatabase,
@@ -41,37 +41,30 @@ pub(crate) fn expand_macro(db: &RootDatabase, position: FilePosition) -> Option<
4141
// struct Bar;
4242
// ```
4343

44-
let derive = sema
45-
.descend_into_macros(DescendPreference::None, tok.clone())
46-
.into_iter()
47-
.find_map(|descended| {
48-
let macro_file = sema.hir_file_for(&descended.parent()?).macro_file()?;
49-
if !macro_file.is_derive_attr_pseudo_expansion(db) {
50-
return None;
51-
}
44+
let derive = sema.descend_into_macros_exact(tok.clone()).into_iter().find_map(|descended| {
45+
let macro_file = sema.hir_file_for(&descended.parent()?).macro_file()?;
46+
if !macro_file.is_derive_attr_pseudo_expansion(db) {
47+
return None;
48+
}
5249

53-
let name = descended.parent_ancestors().filter_map(ast::Path::cast).last()?.to_string();
54-
// up map out of the #[derive] expansion
55-
let InFile { file_id, value: tokens } =
56-
hir::InMacroFile::new(macro_file, descended).upmap_once(db);
57-
let token = sema.parse_or_expand(file_id).covering_element(tokens[0]).into_token()?;
58-
let attr = token.parent_ancestors().find_map(ast::Attr::cast)?;
59-
let expansions = sema.expand_derive_macro(&attr)?;
60-
let idx = attr
61-
.token_tree()?
62-
.token_trees_and_tokens()
63-
.filter_map(NodeOrToken::into_token)
64-
.take_while(|it| it != &token)
65-
.filter(|it| it.kind() == T![,])
66-
.count();
67-
let expansion = format(
68-
db,
69-
SyntaxKind::MACRO_ITEMS,
70-
position.file_id,
71-
expansions.get(idx).cloned()?,
72-
);
73-
Some(ExpandedMacro { name, expansion })
74-
});
50+
let name = descended.parent_ancestors().filter_map(ast::Path::cast).last()?.to_string();
51+
// up map out of the #[derive] expansion
52+
let InFile { file_id, value: tokens } =
53+
hir::InMacroFile::new(macro_file, descended).upmap_once(db);
54+
let token = sema.parse_or_expand(file_id).covering_element(tokens[0]).into_token()?;
55+
let attr = token.parent_ancestors().find_map(ast::Attr::cast)?;
56+
let expansions = sema.expand_derive_macro(&attr)?;
57+
let idx = attr
58+
.token_tree()?
59+
.token_trees_and_tokens()
60+
.filter_map(NodeOrToken::into_token)
61+
.take_while(|it| it != &token)
62+
.filter(|it| it.kind() == T![,])
63+
.count();
64+
let expansion =
65+
format(db, SyntaxKind::MACRO_ITEMS, position.file_id, expansions.get(idx).cloned()?);
66+
Some(ExpandedMacro { name, expansion })
67+
});
7568

7669
if derive.is_some() {
7770
return derive;

crates/ide/src/goto_declaration.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use hir::{AsAssocItem, DescendPreference, Semantics};
1+
use hir::{AsAssocItem, Semantics};
22
use ide_db::{
33
defs::{Definition, NameClass, NameRefClass},
44
RootDatabase,
@@ -29,7 +29,7 @@ pub(crate) fn goto_declaration(
2929
.find(|it| matches!(it.kind(), IDENT | T![self] | T![super] | T![crate] | T![Self]))?;
3030
let range = original_token.text_range();
3131
let info: Vec<NavigationTarget> = sema
32-
.descend_into_macros(DescendPreference::None, original_token)
32+
.descend_into_macros_ng_v(original_token)
3333
.iter()
3434
.filter_map(|token| {
3535
let parent = token.parent()?;

crates/ide/src/goto_definition.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@ use crate::{
55
navigation_target::{self, ToNav},
66
FilePosition, NavigationTarget, RangeInfo, TryToNav, UpmappingResult,
77
};
8-
use hir::{
9-
AsAssocItem, AssocItem, DescendPreference, FileRange, InFile, MacroFileIdExt, ModuleDef,
10-
Semantics,
11-
};
8+
use hir::{AsAssocItem, AssocItem, FileRange, InFile, MacroFileIdExt, ModuleDef, Semantics};
129
use ide_db::{
1310
base_db::{AnchoredPath, FileLoader, SourceDatabase},
1411
defs::{Definition, IdentClass},
@@ -86,7 +83,7 @@ pub(crate) fn goto_definition(
8683
}
8784

8885
let navs = sema
89-
.descend_into_macros(DescendPreference::None, original_token.clone())
86+
.descend_into_macros_ng_v(original_token.clone())
9087
.into_iter()
9188
.filter_map(|token| {
9289
let parent = token.parent()?;
@@ -251,7 +248,7 @@ pub(crate) fn find_fn_or_blocks(
251248
None
252249
};
253250

254-
sema.descend_into_macros(DescendPreference::None, token.clone())
251+
sema.descend_into_macros_ng_v(token.clone())
255252
.into_iter()
256253
.filter_map(find_ancestors)
257254
.collect_vec()
@@ -369,7 +366,7 @@ pub(crate) fn find_loops(
369366
None
370367
};
371368

372-
sema.descend_into_macros(DescendPreference::None, token.clone())
369+
sema.descend_into_macros_ng_v(token.clone())
373370
.into_iter()
374371
.filter_map(find_ancestors)
375372
.collect_vec()

crates/ide/src/goto_type_definition.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use hir::{DescendPreference, GenericParam};
1+
use hir::GenericParam;
22
use ide_db::{base_db::Upcast, defs::Definition, helpers::pick_best_token, RootDatabase};
33
use syntax::{ast, match_ast, AstNode, SyntaxKind::*, SyntaxToken, T};
44

@@ -69,7 +69,7 @@ pub(crate) fn goto_type_definition(
6969
}
7070

7171
let range = token.text_range();
72-
sema.descend_into_macros(DescendPreference::None, token)
72+
sema.descend_into_macros_ng_v(token)
7373
.into_iter()
7474
.filter_map(|token| {
7575
let ty = sema

crates/ide/src/highlight_related.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::iter;
22

3-
use hir::{db, DescendPreference, FilePosition, FileRange, HirFileId, InFile, Semantics};
3+
use hir::{db, FilePosition, FileRange, HirFileId, InFile, Semantics};
44
use ide_db::{
55
defs::{Definition, IdentClass},
66
helpers::pick_best_token,
@@ -542,7 +542,7 @@ fn cover_range(r0: Option<TextRange>, r1: Option<TextRange>) -> Option<TextRange
542542
}
543543

544544
fn find_defs(sema: &Semantics<'_, RootDatabase>, token: SyntaxToken) -> FxHashSet<Definition> {
545-
sema.descend_into_macros(DescendPreference::None, token)
545+
sema.descend_into_macros_exact(token)
546546
.into_iter()
547547
.filter_map(|token| IdentClass::classify_token(sema, &token))
548548
.flat_map(IdentClass::definitions_no_ops)

crates/ide/src/moniker.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
44
use core::fmt;
55

6-
use hir::{Adt, AsAssocItem, AssocItemContainer, Crate, DescendPreference, MacroKind, Semantics};
6+
use hir::{Adt, AsAssocItem, AssocItemContainer, Crate, MacroKind, Semantics};
77
use ide_db::{
88
base_db::{CrateOrigin, LangCrateOrigin},
99
defs::{Definition, IdentClass},
@@ -154,7 +154,7 @@ pub(crate) fn moniker(
154154
});
155155
}
156156
let navs = sema
157-
.descend_into_macros(DescendPreference::None, original_token.clone())
157+
.descend_into_macros_exact(original_token.clone())
158158
.into_iter()
159159
.filter_map(|token| {
160160
IdentClass::classify_token(sema, &token).map(IdentClass::definitions_no_ops).map(|it| {

0 commit comments

Comments
 (0)