Skip to content

Commit 24a66a3

Browse files
committed
fix: Handle included files better in IDE layer
This does not fully fix things, but it introduces a function that can be used to fix occurences. When using `to_def` functionality, the input node needs to come from the macro expanded include, not the real file that was included. This does unfortunately add more caller burden, but there is not really a way around it.
1 parent 6440fe2 commit 24a66a3

File tree

3 files changed

+91
-31
lines changed

3 files changed

+91
-31
lines changed

crates/hir/src/semantics.rs

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -769,6 +769,26 @@ impl<'db> SemanticsImpl<'db> {
769769
})
770770
}
771771

772+
/// Descends the token into the include expansion, if its file is an included file.
773+
pub fn descend_token_into_include_expansion(
774+
&self,
775+
tok: InRealFile<SyntaxToken>,
776+
) -> Option<SyntaxToken> {
777+
let include =
778+
self.s2d_cache.borrow_mut().get_or_insert_include_for(self.db, tok.file_id)?;
779+
let span = self.db.real_span_map(tok.file_id).span_for_range(tok.value.text_range());
780+
let InMacroFile { file_id: _, value: mut mapped_tokens } = self.with_ctx(|ctx| {
781+
Some(
782+
ctx.cache
783+
.get_or_insert_expansion(ctx.db, include)
784+
.map_range_down(span)?
785+
.map(SmallVec::<[_; 2]>::from_iter),
786+
)
787+
})?;
788+
// We should only get one result at most
789+
mapped_tokens.pop().map(TupleExt::head)
790+
}
791+
772792
/// Maps a node down by mapping its first and last token down.
773793
pub fn descend_node_into_attributes<N: AstNode>(&self, node: N) -> SmallVec<[N; 1]> {
774794
// This might not be the correct way to do this, but it works for now
@@ -1528,11 +1548,9 @@ impl<'db> SemanticsImpl<'db> {
15281548
}
15291549

15301550
pub fn resolve_macro_call2(&self, macro_call: InFile<&ast::MacroCall>) -> Option<Macro> {
1531-
self.with_ctx(|ctx| {
1532-
ctx.macro_call_to_macro_call(macro_call)
1533-
.and_then(|call| macro_call_to_macro_id(ctx, call))
1534-
.map(Into::into)
1535-
})
1551+
self.to_def2(macro_call)
1552+
.and_then(|call| self.with_ctx(|ctx| macro_call_to_macro_id(ctx, call)))
1553+
.map(Into::into)
15361554
}
15371555

15381556
pub fn is_proc_macro_call(&self, macro_call: InFile<&ast::MacroCall>) -> bool {
@@ -1647,6 +1665,10 @@ impl<'db> SemanticsImpl<'db> {
16471665
T::to_def(self, src)
16481666
}
16491667

1668+
pub fn to_def2<T: ToDef>(&self, src: InFile<&T>) -> Option<T::Def> {
1669+
T::to_def(self, src)
1670+
}
1671+
16501672
fn file_to_module_defs(&self, file: FileId) -> impl Iterator<Item = Module> {
16511673
self.with_ctx(|ctx| ctx.file_to_def(file).to_owned()).into_iter().map(Module::from)
16521674
}

crates/hir/src/semantics/source_to_def.rs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -399,19 +399,6 @@ impl SourceToDefCtx<'_, '_> {
399399
Some((container, label?))
400400
}
401401

402-
pub(super) fn item_to_macro_call(&mut self, src: InFile<&ast::Item>) -> Option<MacroCallId> {
403-
let map = self.dyn_map(src)?;
404-
map[keys::ATTR_MACRO_CALL].get(&AstPtr::new(src.value)).copied()
405-
}
406-
407-
pub(super) fn macro_call_to_macro_call(
408-
&mut self,
409-
src: InFile<&ast::MacroCall>,
410-
) -> Option<MacroCallId> {
411-
let map = self.dyn_map(src)?;
412-
map[keys::MACRO_CALL].get(&AstPtr::new(src.value)).copied()
413-
}
414-
415402
/// (AttrId, derive attribute call id, derive call ids)
416403
pub(super) fn attr_to_derive_macro_call(
417404
&mut self,
@@ -449,6 +436,17 @@ impl SourceToDefCtx<'_, '_> {
449436
.or_insert_with(|| container.child_by_source(db, file_id))
450437
}
451438

439+
pub(super) fn item_to_macro_call(&mut self, src: InFile<&ast::Item>) -> Option<MacroCallId> {
440+
self.to_def(src, keys::ATTR_MACRO_CALL)
441+
}
442+
443+
pub(super) fn macro_call_to_macro_call(
444+
&mut self,
445+
src: InFile<&ast::MacroCall>,
446+
) -> Option<MacroCallId> {
447+
Some(self.to_def(src, keys::MACRO_CALL).unwrap())
448+
}
449+
452450
pub(super) fn type_param_to_def(
453451
&mut self,
454452
src: InFile<&ast::TypeParam>,

crates/ide/src/expand_macro.rs

Lines changed: 53 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
use hir::db::ExpandDatabase;
2-
use hir::{ExpandResult, InFile, Semantics};
2+
use hir::{ExpandResult, InFile, InRealFile, Semantics};
33
use ide_db::{
44
FileId, RootDatabase, base_db::Crate, helpers::pick_best_token,
55
syntax_helpers::prettify_macro_expansion,
66
};
7-
use span::{Edition, SpanMap, SyntaxContext, TextRange, TextSize};
7+
use span::{SpanMap, SyntaxContext, TextRange, TextSize};
88
use stdx::format_to;
99
use syntax::{AstNode, NodeOrToken, SyntaxKind, SyntaxNode, T, ast, ted};
1010

@@ -26,8 +26,9 @@ pub struct ExpandedMacro {
2626
// ![Expand Macro Recursively](https://user-images.githubusercontent.com/48062697/113020648-b3973180-917a-11eb-84a9-ecb921293dc5.gif)
2727
pub(crate) fn expand_macro(db: &RootDatabase, position: FilePosition) -> Option<ExpandedMacro> {
2828
let sema = Semantics::new(db);
29-
let file = sema.parse_guess_edition(position.file_id);
30-
let krate = sema.file_to_module_def(position.file_id)?.krate().into();
29+
let file_id = sema.attach_first_edition(position.file_id)?;
30+
let file = sema.parse(file_id);
31+
let krate = sema.file_to_module_def(file_id.file_id(db))?.krate().into();
3132

3233
let tok = pick_best_token(file.syntax().token_at_offset(position.offset), |kind| match kind {
3334
SyntaxKind::IDENT => 1,
@@ -86,7 +87,9 @@ pub(crate) fn expand_macro(db: &RootDatabase, position: FilePosition) -> Option<
8687
return derive;
8788
}
8889

89-
let mut anc = tok.parent_ancestors();
90+
let mut anc = sema
91+
.descend_token_into_include_expansion(InRealFile::new(file_id, tok))?
92+
.parent_ancestors();
9093
let mut span_map = SpanMap::empty();
9194
let mut error = String::new();
9295
let (name, expanded, kind) = loop {
@@ -95,14 +98,7 @@ pub(crate) fn expand_macro(db: &RootDatabase, position: FilePosition) -> Option<
9598
if let Some(item) = ast::Item::cast(node.clone()) {
9699
if let Some(def) = sema.resolve_attr_macro_call(&item) {
97100
break (
98-
def.name(db)
99-
.display(
100-
db,
101-
sema.attach_first_edition(position.file_id)
102-
.map(|it| it.edition(db))
103-
.unwrap_or(Edition::CURRENT),
104-
)
105-
.to_string(),
101+
def.name(db).display(db, file_id.edition(db)).to_string(),
106102
expand_macro_recur(&sema, &item, &mut error, &mut span_map, TextSize::new(0))?,
107103
SyntaxKind::MACRO_ITEMS,
108104
);
@@ -759,4 +755,48 @@ fn test() {
759755
"<>hi""#]],
760756
);
761757
}
758+
759+
#[test]
760+
fn in_included() {
761+
check(
762+
r#"
763+
//- minicore: include
764+
//- /main.rs crate:main
765+
include!("./included.rs");
766+
//- /included.rs
767+
macro_rules! foo {
768+
() => { fn item() {} };
769+
}
770+
foo$0!();
771+
"#,
772+
expect![[r#"
773+
foo!
774+
fn item(){}"#]],
775+
);
776+
}
777+
778+
#[test]
779+
fn include() {
780+
check(
781+
r#"
782+
//- minicore: include
783+
//- /main.rs crate:main
784+
include$0!("./included.rs");
785+
//- /included.rs
786+
macro_rules! foo {
787+
() => { fn item() {} };
788+
}
789+
foo();
790+
"#,
791+
expect![[r#"
792+
include!
793+
macro_rules! foo {
794+
() => {
795+
fn item(){}
796+
797+
};
798+
}
799+
foo();"#]],
800+
);
801+
}
762802
}

0 commit comments

Comments
 (0)