Skip to content

Commit f750eeb

Browse files
bors[bot]Veykril
andauthored
Merge #10232
10232: internal: Add more tests for ide functionality in attributed items r=Veykril a=Veykril cc #9868 Co-authored-by: Lukas Wirth <[email protected]>
2 parents e896def + 0f4463e commit f750eeb

File tree

12 files changed

+250
-51
lines changed

12 files changed

+250
-51
lines changed

crates/hir/src/semantics.rs

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ use itertools::Itertools;
1616
use rustc_hash::{FxHashMap, FxHashSet};
1717
use smallvec::{smallvec, SmallVec};
1818
use syntax::{
19-
algo::find_node_at_offset,
2019
ast::{self, GenericParamsOwner, LoopBodyOwner},
2120
match_ast, AstNode, SyntaxNode, SyntaxNodePtr, SyntaxToken, TextRange, TextSize,
2221
};
@@ -241,10 +240,6 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> {
241240
node: &SyntaxNode,
242241
offset: TextSize,
243242
) -> Option<N> {
244-
if let Some(it) = find_node_at_offset(node, offset) {
245-
return Some(it);
246-
}
247-
248243
self.imp.descend_node_at_offset(node, offset).flatten().find_map(N::cast)
249244
}
250245

@@ -567,16 +562,25 @@ impl<'db> SemanticsImpl<'db> {
567562

568563
// Note this return type is deliberate as [`find_nodes_at_offset_with_descend`] wants to stop
569564
// traversing the inner iterator when it finds a node.
565+
// The outer iterator is over the tokens descendants
566+
// The inner iterator is the ancestors of a descendant
570567
fn descend_node_at_offset(
571568
&self,
572569
node: &SyntaxNode,
573570
offset: TextSize,
574571
) -> impl Iterator<Item = impl Iterator<Item = SyntaxNode> + '_> + '_ {
575-
// Handle macro token cases
576572
node.token_at_offset(offset)
577573
.map(move |token| self.descend_into_macros(token))
578-
.map(|it| it.into_iter().map(move |it| self.token_ancestors_with_macros(it)))
579-
.flatten()
574+
.map(|descendants| {
575+
descendants.into_iter().map(move |it| self.token_ancestors_with_macros(it))
576+
})
577+
// re-order the tokens from token_at_offset by returning the ancestors with the smaller first nodes first
578+
// See algo::ancestors_at_offset, which uses the same approach
579+
.kmerge_by(|left, right| {
580+
left.clone()
581+
.map(|node| node.text_range().len())
582+
.lt(right.clone().map(|node| node.text_range().len()))
583+
})
580584
}
581585

582586
fn original_range(&self, node: &SyntaxNode) -> FileRange {
@@ -594,11 +598,14 @@ impl<'db> SemanticsImpl<'db> {
594598
fn token_ancestors_with_macros(
595599
&self,
596600
token: SyntaxToken,
597-
) -> impl Iterator<Item = SyntaxNode> + '_ {
601+
) -> impl Iterator<Item = SyntaxNode> + Clone + '_ {
598602
token.parent().into_iter().flat_map(move |parent| self.ancestors_with_macros(parent))
599603
}
600604

601-
fn ancestors_with_macros(&self, node: SyntaxNode) -> impl Iterator<Item = SyntaxNode> + '_ {
605+
fn ancestors_with_macros(
606+
&self,
607+
node: SyntaxNode,
608+
) -> impl Iterator<Item = SyntaxNode> + Clone + '_ {
602609
let node = self.find_file(node);
603610
node.ancestors_with_macros(self.db.upcast()).map(|it| it.value)
604611
}

crates/hir/src/semantics/source_to_def.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,12 @@ impl SourceToDefCtx<'_, '_> {
131131

132132
pub(super) fn module_to_def(&mut self, src: InFile<ast::Module>) -> Option<ModuleId> {
133133
let _p = profile::span("module_to_def");
134-
let parent_declaration =
135-
src.syntax().cloned().ancestors_with_macros(self.db.upcast()).skip(1).find_map(|it| {
134+
let parent_declaration = src
135+
.syntax()
136+
.cloned()
137+
.ancestors_with_macros_skip_attr_item(self.db.upcast())
138+
.skip(1)
139+
.find_map(|it| {
136140
let m = ast::Module::cast(it.value.clone())?;
137141
Some(it.with_value(m))
138142
});
@@ -306,7 +310,8 @@ impl SourceToDefCtx<'_, '_> {
306310
}
307311

308312
pub(super) fn find_container(&mut self, src: InFile<&SyntaxNode>) -> Option<ChildContainer> {
309-
for container in src.cloned().ancestors_with_macros(self.db.upcast()).skip(1) {
313+
for container in src.cloned().ancestors_with_macros_skip_attr_item(self.db.upcast()).skip(1)
314+
{
310315
if let Some(res) = self.container_to_def(container) {
311316
return Some(res);
312317
}
@@ -370,7 +375,8 @@ impl SourceToDefCtx<'_, '_> {
370375
}
371376

372377
fn find_generic_param_container(&mut self, src: InFile<&SyntaxNode>) -> Option<GenericDefId> {
373-
for container in src.cloned().ancestors_with_macros(self.db.upcast()).skip(1) {
378+
for container in src.cloned().ancestors_with_macros_skip_attr_item(self.db.upcast()).skip(1)
379+
{
374380
let res: GenericDefId = match_ast! {
375381
match (container.value) {
376382
ast::Fn(it) => self.fn_to_def(container.with_value(it))?.into(),
@@ -388,7 +394,8 @@ impl SourceToDefCtx<'_, '_> {
388394
}
389395

390396
fn find_pat_or_label_container(&mut self, src: InFile<&SyntaxNode>) -> Option<DefWithBodyId> {
391-
for container in src.cloned().ancestors_with_macros(self.db.upcast()).skip(1) {
397+
for container in src.cloned().ancestors_with_macros_skip_attr_item(self.db.upcast()).skip(1)
398+
{
392399
let res: DefWithBodyId = match_ast! {
393400
match (container.value) {
394401
ast::Const(it) => self.const_to_def(container.with_value(it))?.into(),

crates/hir_expand/src/lib.rs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,17 @@ impl HirFileId {
186186
}
187187
}
188188

189+
/// Return whether this file is an include macro
190+
pub fn is_attr_macro(&self, db: &dyn db::AstDatabase) -> bool {
191+
match self.0 {
192+
HirFileIdRepr::MacroFile(macro_file) => {
193+
let loc: MacroCallLoc = db.lookup_intern_macro(macro_file.macro_call_id);
194+
matches!(loc.kind, MacroCallKind::Attr { .. })
195+
}
196+
_ => false,
197+
}
198+
}
199+
189200
pub fn is_macro(self) -> bool {
190201
matches!(self.0, HirFileIdRepr::MacroFile(_))
191202
}
@@ -525,7 +536,7 @@ impl InFile<SyntaxNode> {
525536
pub fn ancestors_with_macros(
526537
self,
527538
db: &dyn db::AstDatabase,
528-
) -> impl Iterator<Item = InFile<SyntaxNode>> + '_ {
539+
) -> impl Iterator<Item = InFile<SyntaxNode>> + Clone + '_ {
529540
iter::successors(Some(self), move |node| match node.value.parent() {
530541
Some(parent) => Some(node.with_value(parent)),
531542
None => {
@@ -534,6 +545,26 @@ impl InFile<SyntaxNode> {
534545
}
535546
})
536547
}
548+
549+
/// Skips the attributed item that caused the macro invocation we are climbing up
550+
pub fn ancestors_with_macros_skip_attr_item(
551+
self,
552+
db: &dyn db::AstDatabase,
553+
) -> impl Iterator<Item = InFile<SyntaxNode>> + '_ {
554+
iter::successors(Some(self), move |node| match node.value.parent() {
555+
Some(parent) => Some(node.with_value(parent)),
556+
None => {
557+
let parent_node = node.file_id.call_node(db)?;
558+
if node.file_id.is_attr_macro(db) {
559+
// macro call was an attributed item, skip it
560+
// FIXME: does this fail if this is a direct expansion of another macro?
561+
parent_node.map(|node| node.parent()).transpose()
562+
} else {
563+
Some(parent_node)
564+
}
565+
}
566+
})
567+
}
537568
}
538569

539570
impl<'a> InFile<&'a SyntaxNode> {

crates/ide/src/expand_macro.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ pub(crate) fn expand_macro(db: &RootDatabase, position: FilePosition) -> Option<
3131
SyntaxKind::IDENT => 1,
3232
_ => 0,
3333
})?;
34+
3435
let descended = sema.descend_into_macros(tok.clone());
3536
if let Some(attr) = descended.ancestors().find_map(ast::Attr::cast) {
3637
if let Some((path, tt)) = attr.as_simple_call() {
@@ -45,6 +46,9 @@ pub(crate) fn expand_macro(db: &RootDatabase, position: FilePosition) -> Option<
4546
}
4647
}
4748
}
49+
50+
// FIXME: Intermix attribute and bang! expansions
51+
// currently we only recursively expand one of the two types
4852
let mut expanded = None;
4953
let mut name = None;
5054
for node in tok.ancestors() {

crates/ide/src/highlight_related.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,22 @@ fn foo() {
413413
);
414414
}
415415

416+
#[test]
417+
fn test_hl_local_in_attr() {
418+
check(
419+
r#"
420+
//- proc_macros: identity
421+
#[proc_macros::identity]
422+
fn foo() {
423+
let mut bar = 3;
424+
// ^^^ write
425+
bar$0;
426+
// ^^^ read
427+
}
428+
"#,
429+
);
430+
}
431+
416432
#[test]
417433
fn test_multi_macro_usage() {
418434
check(

crates/ide/src/hover.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1730,6 +1730,28 @@ id! {
17301730
);
17311731
}
17321732

1733+
#[test]
1734+
fn test_hover_through_attr() {
1735+
check(
1736+
r#"
1737+
//- proc_macros: identity
1738+
#[proc_macros::identity]
1739+
fn foo$0() {}
1740+
"#,
1741+
expect![[r#"
1742+
*foo*
1743+
1744+
```rust
1745+
test
1746+
```
1747+
1748+
```rust
1749+
fn foo()
1750+
```
1751+
"#]],
1752+
);
1753+
}
1754+
17331755
#[test]
17341756
fn test_hover_through_expr_in_macro() {
17351757
check(

crates/ide/src/references.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1507,4 +1507,23 @@ fn f() {
15071507
"#]],
15081508
)
15091509
}
1510+
1511+
#[test]
1512+
fn attr_expanded() {
1513+
check(
1514+
r#"
1515+
//- proc_macros: identity
1516+
1517+
#[proc_macros::identity]
1518+
fn func$0() {
1519+
func();
1520+
}
1521+
"#,
1522+
expect![[r#"
1523+
func Function FileId(0) 26..51 29..33
1524+
1525+
FileId(0) 42..46
1526+
"#]],
1527+
)
1528+
}
15101529
}

crates/ide/src/rename.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1880,4 +1880,26 @@ fn main() { f$0() }
18801880
"error: No identifier available to rename",
18811881
)
18821882
}
1883+
1884+
#[test]
1885+
fn attributed_item() {
1886+
check(
1887+
"function",
1888+
r#"
1889+
//- proc_macros: identity
1890+
1891+
#[proc_macros::identity]
1892+
fn func$0() {
1893+
func();
1894+
}
1895+
"#,
1896+
r#"
1897+
1898+
#[proc_macros::identity]
1899+
fn function() {
1900+
function();
1901+
}
1902+
"#,
1903+
)
1904+
}
18831905
}

crates/ide/src/runnables.rs

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1737,6 +1737,88 @@ fn t1() {}
17371737
);
17381738
}
17391739

1740+
#[test]
1741+
fn attributed_module() {
1742+
check(
1743+
r#"
1744+
//- proc_macros: identity
1745+
//- /lib.rs
1746+
$0
1747+
#[proc_macros::identity]
1748+
mod module {
1749+
#[test]
1750+
fn t0() {}
1751+
#[test]
1752+
fn t1() {}
1753+
}
1754+
"#,
1755+
&[TestMod, Test, Test],
1756+
expect![[r#"
1757+
[
1758+
Runnable {
1759+
use_name_in_title: true,
1760+
nav: NavigationTarget {
1761+
file_id: FileId(
1762+
0,
1763+
),
1764+
full_range: 26..94,
1765+
focus_range: 30..36,
1766+
name: "module",
1767+
kind: Module,
1768+
description: "mod module",
1769+
},
1770+
kind: TestMod {
1771+
path: "module",
1772+
},
1773+
cfg: None,
1774+
},
1775+
Runnable {
1776+
use_name_in_title: true,
1777+
nav: NavigationTarget {
1778+
file_id: FileId(
1779+
0,
1780+
),
1781+
full_range: 43..65,
1782+
focus_range: 58..60,
1783+
name: "t0",
1784+
kind: Function,
1785+
},
1786+
kind: Test {
1787+
test_id: Path(
1788+
"module::t0",
1789+
),
1790+
attr: TestAttr {
1791+
ignore: false,
1792+
},
1793+
},
1794+
cfg: None,
1795+
},
1796+
Runnable {
1797+
use_name_in_title: true,
1798+
nav: NavigationTarget {
1799+
file_id: FileId(
1800+
0,
1801+
),
1802+
full_range: 70..92,
1803+
focus_range: 85..87,
1804+
name: "t1",
1805+
kind: Function,
1806+
},
1807+
kind: Test {
1808+
test_id: Path(
1809+
"module::t1",
1810+
),
1811+
attr: TestAttr {
1812+
ignore: false,
1813+
},
1814+
},
1815+
cfg: None,
1816+
},
1817+
]
1818+
"#]],
1819+
);
1820+
}
1821+
17401822
#[test]
17411823
fn find_no_tests() {
17421824
check_tests(

crates/ide/src/syntax_highlighting/test_data/highlighting.html

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,15 @@
5151
<span class="keyword">pub</span> <span class="keyword">trait</span> <span class="trait declaration public">Copy</span> <span class="brace">{</span><span class="brace">}</span>
5252
<span class="brace">}</span>
5353

54+
<span class="attribute attribute">#</span><span class="attribute attribute">[</span><span class="module attribute">proc_macros</span><span class="operator attribute">::</span><span class="builtin_attr attribute">identity</span><span class="attribute attribute">]</span>
5455
<span class="keyword">pub</span> <span class="keyword">mod</span> <span class="module declaration public">ops</span> <span class="brace">{</span>
55-
<span class="attribute attribute">#</span><span class="attribute attribute">[</span><span class="builtin_attr attribute">lang</span><span class="attribute attribute"> </span><span class="operator attribute">=</span><span class="attribute attribute"> </span><span class="string_literal attribute">"fn_once"</span><span class="attribute attribute">]</span>
56+
<span class="attribute attribute">#</span><span class="attribute attribute">[</span><span class="builtin_attr attribute">lang</span> <span class="operator attribute">=</span> <span class="string_literal attribute">"fn_once"</span><span class="attribute attribute">]</span>
5657
<span class="keyword">pub</span> <span class="keyword">trait</span> <span class="trait declaration public">FnOnce</span><span class="angle">&lt;</span><span class="type_param declaration">Args</span><span class="angle">&gt;</span> <span class="brace">{</span><span class="brace">}</span>
5758

58-
<span class="attribute attribute">#</span><span class="attribute attribute">[</span><span class="builtin_attr attribute">lang</span><span class="attribute attribute"> </span><span class="operator attribute">=</span><span class="attribute attribute"> </span><span class="string_literal attribute">"fn_mut"</span><span class="attribute attribute">]</span>
59+
<span class="attribute attribute">#</span><span class="attribute attribute">[</span><span class="builtin_attr attribute">lang</span> <span class="operator attribute">=</span> <span class="string_literal attribute">"fn_mut"</span><span class="attribute attribute">]</span>
5960
<span class="keyword">pub</span> <span class="keyword">trait</span> <span class="trait declaration public">FnMut</span><span class="angle">&lt;</span><span class="type_param declaration">Args</span><span class="angle">&gt;</span><span class="colon">:</span> <span class="trait public">FnOnce</span><span class="angle">&lt;</span><span class="type_param">Args</span><span class="angle">&gt;</span> <span class="brace">{</span><span class="brace">}</span>
6061

61-
<span class="attribute attribute">#</span><span class="attribute attribute">[</span><span class="builtin_attr attribute">lang</span><span class="attribute attribute"> </span><span class="operator attribute">=</span><span class="attribute attribute"> </span><span class="string_literal attribute">"fn"</span><span class="attribute attribute">]</span>
62+
<span class="attribute attribute">#</span><span class="attribute attribute">[</span><span class="builtin_attr attribute">lang</span> <span class="operator attribute">=</span> <span class="string_literal attribute">"fn"</span><span class="attribute attribute">]</span>
6263
<span class="keyword">pub</span> <span class="keyword">trait</span> <span class="trait declaration public">Fn</span><span class="angle">&lt;</span><span class="type_param declaration">Args</span><span class="angle">&gt;</span><span class="colon">:</span> <span class="trait public">FnMut</span><span class="angle">&lt;</span><span class="type_param">Args</span><span class="angle">&gt;</span> <span class="brace">{</span><span class="brace">}</span>
6364
<span class="brace">}</span>
6465

0 commit comments

Comments
 (0)