Fully remove old macro descension API

This commit is contained in:
Lukas Wirth 2024-08-22 16:18:01 +02:00
parent 495118015e
commit 64064907ce
13 changed files with 66 additions and 103 deletions

View file

@ -2,7 +2,7 @@
use std::iter;
use hir::{DescendPreference, Semantics};
use hir::Semantics;
use ide_db::{
defs::{Definition, NameClass, NameRefClass},
helpers::pick_best_token,
@ -86,7 +86,7 @@ pub(crate) fn outgoing_calls(
})?;
let mut calls = CallLocations::default();
sema.descend_into_macros(DescendPreference::None, token)
sema.descend_into_macros_exact(token)
.into_iter()
.filter_map(|it| it.parent_ancestors().nth(1).and_then(ast::Item::cast))
.filter_map(|item| match item {

View file

@ -10,10 +10,7 @@ use pulldown_cmark_to_cmark::{cmark_resume_with_options, Options as CMarkOptions
use stdx::format_to;
use url::Url;
use hir::{
db::HirDatabase, sym, Adt, AsAssocItem, AssocItem, AssocItemContainer, DescendPreference,
HasAttrs,
};
use hir::{db::HirDatabase, sym, Adt, AsAssocItem, AssocItem, AssocItemContainer, HasAttrs};
use ide_db::{
base_db::{CrateOrigin, LangCrateOrigin, ReleaseChannel, SourceDatabase},
defs::{Definition, NameClass, NameRefClass},
@ -289,7 +286,7 @@ impl DocCommentToken {
let original_start = doc_token.text_range().start();
let relative_comment_offset = offset - original_start - prefix_len;
sema.descend_into_macros(DescendPreference::None, doc_token).into_iter().find_map(|t| {
sema.descend_into_macros_ng_v(doc_token).into_iter().find_map(|t| {
let (node, descended_prefix_len) = match_ast! {
match t {
ast::Comment(comment) => (t.parent()?, TextSize::try_from(comment.prefix().len()).ok()?),

View file

@ -1,4 +1,4 @@
use hir::{DescendPreference, InFile, MacroFileIdExt, Semantics};
use hir::{InFile, MacroFileIdExt, Semantics};
use ide_db::{
helpers::pick_best_token, syntax_helpers::insert_whitespace_into_node::insert_ws_into, FileId,
RootDatabase,
@ -41,37 +41,30 @@ pub(crate) fn expand_macro(db: &RootDatabase, position: FilePosition) -> Option<
// struct Bar;
// ```
let derive = sema
.descend_into_macros(DescendPreference::None, tok.clone())
.into_iter()
.find_map(|descended| {
let macro_file = sema.hir_file_for(&descended.parent()?).macro_file()?;
if !macro_file.is_derive_attr_pseudo_expansion(db) {
return None;
}
let derive = sema.descend_into_macros_exact(tok.clone()).into_iter().find_map(|descended| {
let macro_file = sema.hir_file_for(&descended.parent()?).macro_file()?;
if !macro_file.is_derive_attr_pseudo_expansion(db) {
return None;
}
let name = descended.parent_ancestors().filter_map(ast::Path::cast).last()?.to_string();
// up map out of the #[derive] expansion
let InFile { file_id, value: tokens } =
hir::InMacroFile::new(macro_file, descended).upmap_once(db);
let token = sema.parse_or_expand(file_id).covering_element(tokens[0]).into_token()?;
let attr = token.parent_ancestors().find_map(ast::Attr::cast)?;
let expansions = sema.expand_derive_macro(&attr)?;
let idx = attr
.token_tree()?
.token_trees_and_tokens()
.filter_map(NodeOrToken::into_token)
.take_while(|it| it != &token)
.filter(|it| it.kind() == T![,])
.count();
let expansion = format(
db,
SyntaxKind::MACRO_ITEMS,
position.file_id,
expansions.get(idx).cloned()?,
);
Some(ExpandedMacro { name, expansion })
});
let name = descended.parent_ancestors().filter_map(ast::Path::cast).last()?.to_string();
// up map out of the #[derive] expansion
let InFile { file_id, value: tokens } =
hir::InMacroFile::new(macro_file, descended).upmap_once(db);
let token = sema.parse_or_expand(file_id).covering_element(tokens[0]).into_token()?;
let attr = token.parent_ancestors().find_map(ast::Attr::cast)?;
let expansions = sema.expand_derive_macro(&attr)?;
let idx = attr
.token_tree()?
.token_trees_and_tokens()
.filter_map(NodeOrToken::into_token)
.take_while(|it| it != &token)
.filter(|it| it.kind() == T![,])
.count();
let expansion =
format(db, SyntaxKind::MACRO_ITEMS, position.file_id, expansions.get(idx).cloned()?);
Some(ExpandedMacro { name, expansion })
});
if derive.is_some() {
return derive;

View file

@ -1,4 +1,4 @@
use hir::{AsAssocItem, DescendPreference, Semantics};
use hir::{AsAssocItem, Semantics};
use ide_db::{
defs::{Definition, NameClass, NameRefClass},
RootDatabase,
@ -29,7 +29,7 @@ pub(crate) fn goto_declaration(
.find(|it| matches!(it.kind(), IDENT | T![self] | T![super] | T![crate] | T![Self]))?;
let range = original_token.text_range();
let info: Vec<NavigationTarget> = sema
.descend_into_macros(DescendPreference::None, original_token)
.descend_into_macros_ng_v(original_token)
.iter()
.filter_map(|token| {
let parent = token.parent()?;

View file

@ -5,10 +5,7 @@ use crate::{
navigation_target::{self, ToNav},
FilePosition, NavigationTarget, RangeInfo, TryToNav, UpmappingResult,
};
use hir::{
AsAssocItem, AssocItem, DescendPreference, FileRange, InFile, MacroFileIdExt, ModuleDef,
Semantics,
};
use hir::{AsAssocItem, AssocItem, FileRange, InFile, MacroFileIdExt, ModuleDef, Semantics};
use ide_db::{
base_db::{AnchoredPath, FileLoader, SourceDatabase},
defs::{Definition, IdentClass},
@ -86,7 +83,7 @@ pub(crate) fn goto_definition(
}
let navs = sema
.descend_into_macros(DescendPreference::None, original_token.clone())
.descend_into_macros_ng_v(original_token.clone())
.into_iter()
.filter_map(|token| {
let parent = token.parent()?;
@ -251,7 +248,7 @@ pub(crate) fn find_fn_or_blocks(
None
};
sema.descend_into_macros(DescendPreference::None, token.clone())
sema.descend_into_macros_ng_v(token.clone())
.into_iter()
.filter_map(find_ancestors)
.collect_vec()
@ -369,7 +366,7 @@ pub(crate) fn find_loops(
None
};
sema.descend_into_macros(DescendPreference::None, token.clone())
sema.descend_into_macros_ng_v(token.clone())
.into_iter()
.filter_map(find_ancestors)
.collect_vec()

View file

@ -1,4 +1,4 @@
use hir::{DescendPreference, GenericParam};
use hir::GenericParam;
use ide_db::{base_db::Upcast, defs::Definition, helpers::pick_best_token, RootDatabase};
use syntax::{ast, match_ast, AstNode, SyntaxKind::*, SyntaxToken, T};
@ -69,7 +69,7 @@ pub(crate) fn goto_type_definition(
}
let range = token.text_range();
sema.descend_into_macros(DescendPreference::None, token)
sema.descend_into_macros_ng_v(token)
.into_iter()
.filter_map(|token| {
let ty = sema

View file

@ -1,6 +1,6 @@
use std::iter;
use hir::{db, DescendPreference, FilePosition, FileRange, HirFileId, InFile, Semantics};
use hir::{db, FilePosition, FileRange, HirFileId, InFile, Semantics};
use ide_db::{
defs::{Definition, IdentClass},
helpers::pick_best_token,
@ -542,7 +542,7 @@ fn cover_range(r0: Option<TextRange>, r1: Option<TextRange>) -> Option<TextRange
}
fn find_defs(sema: &Semantics<'_, RootDatabase>, token: SyntaxToken) -> FxHashSet<Definition> {
sema.descend_into_macros(DescendPreference::None, token)
sema.descend_into_macros_exact(token)
.into_iter()
.filter_map(|token| IdentClass::classify_token(sema, &token))
.flat_map(IdentClass::definitions_no_ops)

View file

@ -3,7 +3,7 @@
use core::fmt;
use hir::{Adt, AsAssocItem, AssocItemContainer, Crate, DescendPreference, MacroKind, Semantics};
use hir::{Adt, AsAssocItem, AssocItemContainer, Crate, MacroKind, Semantics};
use ide_db::{
base_db::{CrateOrigin, LangCrateOrigin},
defs::{Definition, IdentClass},
@ -154,7 +154,7 @@ pub(crate) fn moniker(
});
}
let navs = sema
.descend_into_macros(DescendPreference::None, original_token.clone())
.descend_into_macros_exact(original_token.clone())
.into_iter()
.filter_map(|token| {
IdentClass::classify_token(sema, &token).map(IdentClass::definitions_no_ops).map(|it| {