Rename macro descension functions

This commit is contained in:
Lukas Wirth 2024-08-22 16:18:56 +02:00
parent 64064907ce
commit 354ab7a9e8
7 changed files with 16 additions and 19 deletions

View file

@ -188,7 +188,7 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> {
self.imp.descend_node_at_offset(node, offset).flatten().find_map(N::cast) self.imp.descend_node_at_offset(node, offset).flatten().find_map(N::cast)
} }
/// Find an AstNode by offset inside SyntaxNode, if it is inside an attribte macro call, /// Find an AstNode by offset inside SyntaxNode, if it is inside an attribute macro call,
/// descend it and find again /// descend it and find again
// FIXME: Rethink this API // FIXME: Rethink this API
pub fn find_nodes_at_offset_with_descend<'slf, N: AstNode + 'slf>( pub fn find_nodes_at_offset_with_descend<'slf, N: AstNode + 'slf>(
@ -550,7 +550,7 @@ impl<'db> SemanticsImpl<'db> {
string: &ast::String, string: &ast::String,
) -> Option<Vec<(TextRange, Option<PathResolution>)>> { ) -> Option<Vec<(TextRange, Option<PathResolution>)>> {
let quote = string.open_quote_text_range()?; let quote = string.open_quote_text_range()?;
self.descend_into_macros_ng_b(string.syntax().clone(), |token| { self.descend_into_macros_breakable(string.syntax().clone(), |token| {
(|| { (|| {
let token = token.value; let token = token.value;
let string = ast::String::cast(token)?; let string = ast::String::cast(token)?;
@ -577,7 +577,7 @@ impl<'db> SemanticsImpl<'db> {
) -> Option<(TextRange, Option<PathResolution>)> { ) -> Option<(TextRange, Option<PathResolution>)> {
let original_string = ast::String::cast(original_token.clone())?; let original_string = ast::String::cast(original_token.clone())?;
let quote = original_string.open_quote_text_range()?; let quote = original_string.open_quote_text_range()?;
self.descend_into_macros_ng_b(original_token.clone(), |token| { self.descend_into_macros_breakable(original_token.clone(), |token| {
(|| { (|| {
let token = token.value; let token = token.value;
self.resolve_offset_in_format_args( self.resolve_offset_in_format_args(
@ -664,7 +664,7 @@ impl<'db> SemanticsImpl<'db> {
res res
} }
pub fn descend_into_macros_ng( pub fn descend_into_macros_cb(
&self, &self,
token: SyntaxToken, token: SyntaxToken,
mut cb: impl FnMut(InFile<SyntaxToken>), mut cb: impl FnMut(InFile<SyntaxToken>),
@ -675,7 +675,7 @@ impl<'db> SemanticsImpl<'db> {
}); });
} }
pub fn descend_into_macros_ng_v(&self, token: SyntaxToken) -> SmallVec<[SyntaxToken; 1]> { pub fn descend_into_macros(&self, token: SyntaxToken) -> SmallVec<[SyntaxToken; 1]> {
let mut res = smallvec![]; let mut res = smallvec![];
self.descend_into_macros_impl(token.clone(), &mut |t| { self.descend_into_macros_impl(token.clone(), &mut |t| {
res.push(t.value); res.push(t.value);
@ -687,7 +687,7 @@ impl<'db> SemanticsImpl<'db> {
res res
} }
pub fn descend_into_macros_ng_b<T>( pub fn descend_into_macros_breakable<T>(
&self, &self,
token: SyntaxToken, token: SyntaxToken,
mut cb: impl FnMut(InFile<SyntaxToken>) -> ControlFlow<T>, mut cb: impl FnMut(InFile<SyntaxToken>) -> ControlFlow<T>,
@ -702,7 +702,7 @@ impl<'db> SemanticsImpl<'db> {
let text = token.text(); let text = token.text();
let kind = token.kind(); let kind = token.kind();
self.descend_into_macros_ng(token.clone(), |InFile { value, file_id: _ }| { self.descend_into_macros_cb(token.clone(), |InFile { value, file_id: _ }| {
let mapped_kind = value.kind(); let mapped_kind = value.kind();
let any_ident_match = || kind.is_any_identifier() && value.kind().is_any_identifier(); let any_ident_match = || kind.is_any_identifier() && value.kind().is_any_identifier();
let matches = (kind == mapped_kind || any_ident_match()) && text == value.text(); let matches = (kind == mapped_kind || any_ident_match()) && text == value.text();
@ -722,7 +722,7 @@ impl<'db> SemanticsImpl<'db> {
let text = token.text(); let text = token.text();
let kind = token.kind(); let kind = token.kind();
self.descend_into_macros_ng_b(token.clone(), |InFile { value, file_id: _ }| { self.descend_into_macros_breakable(token.clone(), |InFile { value, file_id: _ }| {
let mapped_kind = value.kind(); let mapped_kind = value.kind();
let any_ident_match = || kind.is_any_identifier() && value.kind().is_any_identifier(); let any_ident_match = || kind.is_any_identifier() && value.kind().is_any_identifier();
let matches = (kind == mapped_kind || any_ident_match()) && text == value.text(); let matches = (kind == mapped_kind || any_ident_match()) && text == value.text();

View file

@ -286,7 +286,7 @@ impl DocCommentToken {
let original_start = doc_token.text_range().start(); let original_start = doc_token.text_range().start();
let relative_comment_offset = offset - original_start - prefix_len; let relative_comment_offset = offset - original_start - prefix_len;
sema.descend_into_macros_ng_v(doc_token).into_iter().find_map(|t| { sema.descend_into_macros(doc_token).into_iter().find_map(|t| {
let (node, descended_prefix_len) = match_ast! { let (node, descended_prefix_len) = match_ast! {
match t { match t {
ast::Comment(comment) => (t.parent()?, TextSize::try_from(comment.prefix().len()).ok()?), ast::Comment(comment) => (t.parent()?, TextSize::try_from(comment.prefix().len()).ok()?),

View file

@ -29,7 +29,7 @@ pub(crate) fn goto_declaration(
.find(|it| matches!(it.kind(), IDENT | T![self] | T![super] | T![crate] | T![Self]))?; .find(|it| matches!(it.kind(), IDENT | T![self] | T![super] | T![crate] | T![Self]))?;
let range = original_token.text_range(); let range = original_token.text_range();
let info: Vec<NavigationTarget> = sema let info: Vec<NavigationTarget> = sema
.descend_into_macros_ng_v(original_token) .descend_into_macros(original_token)
.iter() .iter()
.filter_map(|token| { .filter_map(|token| {
let parent = token.parent()?; let parent = token.parent()?;

View file

@ -83,7 +83,7 @@ pub(crate) fn goto_definition(
} }
let navs = sema let navs = sema
.descend_into_macros_ng_v(original_token.clone()) .descend_into_macros(original_token.clone())
.into_iter() .into_iter()
.filter_map(|token| { .filter_map(|token| {
let parent = token.parent()?; let parent = token.parent()?;
@ -248,10 +248,7 @@ pub(crate) fn find_fn_or_blocks(
None None
}; };
sema.descend_into_macros_ng_v(token.clone()) sema.descend_into_macros(token.clone()).into_iter().filter_map(find_ancestors).collect_vec()
.into_iter()
.filter_map(find_ancestors)
.collect_vec()
} }
fn nav_for_exit_points( fn nav_for_exit_points(
@ -366,7 +363,7 @@ pub(crate) fn find_loops(
None None
}; };
sema.descend_into_macros_ng_v(token.clone()) sema.descend_into_macros(token.clone())
.into_iter() .into_iter()
.filter_map(find_ancestors) .filter_map(find_ancestors)
.collect_vec() .collect_vec()

View file

@ -69,7 +69,7 @@ pub(crate) fn goto_type_definition(
} }
let range = token.text_range(); let range = token.text_range();
sema.descend_into_macros_ng_v(token) sema.descend_into_macros(token)
.into_iter() .into_iter()
.filter_map(|token| { .filter_map(|token| {
let ty = sema let ty = sema

View file

@ -181,7 +181,7 @@ fn hover_simple(
// prefer descending the same token kind in attribute expansions, in normal macros text // prefer descending the same token kind in attribute expansions, in normal macros text
// equivalency is more important // equivalency is more important
let mut descended = vec![]; let mut descended = vec![];
sema.descend_into_macros_ng(original_token.clone(), |token| { sema.descend_into_macros_cb(original_token.clone(), |token| {
descended.push(token.value); descended.push(token.value);
}); });
let descended = || descended.iter(); let descended = || descended.iter();

View file

@ -409,7 +409,7 @@ fn traverse(
let mut r = 0; let mut r = 0;
// FIXME: Add an extra API that takes the file id of this. That is a simple way // FIXME: Add an extra API that takes the file id of this. That is a simple way
// to prevent us constantly walking up the tree to fetch the file // to prevent us constantly walking up the tree to fetch the file
sema.descend_into_macros_ng_b(token.clone(), |tok| { sema.descend_into_macros_breakable(token.clone(), |tok| {
let tok = tok.value; let tok = tok.value;
let tok_kind = tok.kind(); let tok_kind = tok.kind();