Skip to content

Commit 1222869

Browse files
committed
Fix rust-lang#14557. Docs aliases can now be detected and used in searching for workspace symbols
1 parent a512774 commit 1222869

File tree

9 files changed

+338
-48
lines changed

9 files changed

+338
-48
lines changed

crates/hir/src/symbols.rs

+61-37
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ pub struct FileSymbol {
2020
pub def: ModuleDef,
2121
pub loc: DeclarationLocation,
2222
pub container_name: Option<SmolStr>,
23+
pub is_alias: bool,
2324
}
2425

2526
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
@@ -249,46 +250,69 @@ impl<'a> SymbolCollector<'a> {
249250
<L as Lookup>::Data: HasSource,
250251
<<L as Lookup>::Data as HasSource>::Value: HasName,
251252
{
252-
self.push_file_symbol(|s| {
253-
let loc = id.lookup(s.db.upcast());
254-
let source = loc.source(s.db.upcast());
255-
let name_node = source.value.name()?;
256-
Some(FileSymbol {
257-
name: name_node.text().into(),
258-
def: ModuleDef::from(id.into()),
259-
container_name: s.current_container_name.clone(),
260-
loc: DeclarationLocation {
261-
hir_file_id: source.file_id,
262-
ptr: SyntaxNodePtr::new(source.value.syntax()),
263-
name_ptr: SyntaxNodePtr::new(name_node.syntax()),
264-
},
265-
})
266-
})
267-
}
253+
let loc = id.lookup(self.db.upcast());
254+
let source = loc.source(self.db.upcast());
255+
let Some(name_node) = source.value.name() else { return };
256+
let def = ModuleDef::from(id.into());
257+
let dec_loc = DeclarationLocation {
258+
hir_file_id: source.file_id,
259+
ptr: SyntaxNodePtr::new(source.value.syntax()),
260+
name_ptr: SyntaxNodePtr::new(name_node.syntax()),
261+
};
262+
263+
if let Some(attrs) = def.attrs(self.db) {
264+
for alias in attrs.doc_aliases() {
265+
self.symbols.push(FileSymbol {
266+
name: alias,
267+
def,
268+
loc: dec_loc.clone(),
269+
container_name: self.current_container_name.clone(),
270+
is_alias: true,
271+
});
272+
}
273+
}
268274

269-
fn push_module(&mut self, module_id: ModuleId) {
270-
self.push_file_symbol(|s| {
271-
let def_map = module_id.def_map(s.db.upcast());
272-
let module_data = &def_map[module_id.local_id];
273-
let declaration = module_data.origin.declaration()?;
274-
let module = declaration.to_node(s.db.upcast());
275-
let name_node = module.name()?;
276-
Some(FileSymbol {
277-
name: name_node.text().into(),
278-
def: ModuleDef::Module(module_id.into()),
279-
container_name: s.current_container_name.clone(),
280-
loc: DeclarationLocation {
281-
hir_file_id: declaration.file_id,
282-
ptr: SyntaxNodePtr::new(module.syntax()),
283-
name_ptr: SyntaxNodePtr::new(name_node.syntax()),
284-
},
285-
})
286-
})
275+
self.symbols.push(FileSymbol {
276+
name: name_node.text().into(),
277+
def,
278+
container_name: self.current_container_name.clone(),
279+
loc: dec_loc,
280+
is_alias: false,
281+
});
287282
}
288283

289-
fn push_file_symbol(&mut self, f: impl FnOnce(&Self) -> Option<FileSymbol>) {
290-
if let Some(file_symbol) = f(self) {
291-
self.symbols.push(file_symbol);
284+
fn push_module(&mut self, module_id: ModuleId) {
285+
let def_map = module_id.def_map(self.db.upcast());
286+
let module_data = &def_map[module_id.local_id];
287+
let Some(declaration) = module_data.origin.declaration() else { return };
288+
let module = declaration.to_node(self.db.upcast());
289+
let Some(name_node) = module.name() else { return };
290+
let dec_loc = DeclarationLocation {
291+
hir_file_id: declaration.file_id,
292+
ptr: SyntaxNodePtr::new(module.syntax()),
293+
name_ptr: SyntaxNodePtr::new(name_node.syntax()),
294+
};
295+
296+
let def = ModuleDef::Module(module_id.into());
297+
298+
if let Some(attrs) = def.attrs(self.db) {
299+
for alias in attrs.doc_aliases() {
300+
self.symbols.push(FileSymbol {
301+
name: alias,
302+
def,
303+
loc: dec_loc.clone(),
304+
container_name: self.current_container_name.clone(),
305+
is_alias: true,
306+
});
307+
}
292308
}
309+
310+
self.symbols.push(FileSymbol {
311+
name: name_node.text().into(),
312+
def: ModuleDef::Module(module_id.into()),
313+
container_name: self.current_container_name.clone(),
314+
loc: dec_loc,
315+
is_alias: false,
316+
});
293317
}
294318
}

crates/ide-db/src/symbol_index.rs

+27
Original file line numberDiff line numberDiff line change
@@ -434,4 +434,31 @@ struct StructInModB;
434434

435435
expect_file!["./test_data/test_symbol_index_collection.txt"].assert_debug_eq(&symbols);
436436
}
437+
438+
#[test]
439+
fn test_doc_alias() {
440+
let (db, _) = RootDatabase::with_single_file(
441+
r#"
442+
#[doc(alias="s1")]
443+
#[doc(alias="s2")]
444+
#[doc(alias("mul1","mul2"))]
445+
struct Struct;
446+
447+
#[doc(alias="s1")]
448+
struct Duplicate;
449+
"#,
450+
);
451+
452+
let symbols: Vec<_> = Crate::from(db.test_crate())
453+
.modules(&db)
454+
.into_iter()
455+
.map(|module_id| {
456+
let mut symbols = SymbolCollector::collect_module(&db, module_id);
457+
symbols.sort_by_key(|it| it.name.clone());
458+
(module_id, symbols)
459+
})
460+
.collect();
461+
462+
expect_file!["./test_data/test_doc_alias.txt"].assert_debug_eq(&symbols);
463+
}
437464
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
1+
[
2+
(
3+
Module {
4+
id: ModuleId {
5+
krate: Idx::<CrateData>(0),
6+
block: None,
7+
local_id: Idx::<ModuleData>(0),
8+
},
9+
},
10+
[
11+
FileSymbol {
12+
name: "Duplicate",
13+
def: Adt(
14+
Struct(
15+
Struct {
16+
id: StructId(
17+
1,
18+
),
19+
},
20+
),
21+
),
22+
loc: DeclarationLocation {
23+
hir_file_id: HirFileId(
24+
0,
25+
),
26+
ptr: SyntaxNodePtr {
27+
kind: STRUCT,
28+
range: 83..119,
29+
},
30+
name_ptr: SyntaxNodePtr {
31+
kind: NAME,
32+
range: 109..118,
33+
},
34+
},
35+
container_name: None,
36+
is_alias: false,
37+
},
38+
FileSymbol {
39+
name: "Struct",
40+
def: Adt(
41+
Struct(
42+
Struct {
43+
id: StructId(
44+
0,
45+
),
46+
},
47+
),
48+
),
49+
loc: DeclarationLocation {
50+
hir_file_id: HirFileId(
51+
0,
52+
),
53+
ptr: SyntaxNodePtr {
54+
kind: STRUCT,
55+
range: 0..81,
56+
},
57+
name_ptr: SyntaxNodePtr {
58+
kind: NAME,
59+
range: 74..80,
60+
},
61+
},
62+
container_name: None,
63+
is_alias: false,
64+
},
65+
FileSymbol {
66+
name: "mul1",
67+
def: Adt(
68+
Struct(
69+
Struct {
70+
id: StructId(
71+
0,
72+
),
73+
},
74+
),
75+
),
76+
loc: DeclarationLocation {
77+
hir_file_id: HirFileId(
78+
0,
79+
),
80+
ptr: SyntaxNodePtr {
81+
kind: STRUCT,
82+
range: 0..81,
83+
},
84+
name_ptr: SyntaxNodePtr {
85+
kind: NAME,
86+
range: 74..80,
87+
},
88+
},
89+
container_name: None,
90+
is_alias: true,
91+
},
92+
FileSymbol {
93+
name: "mul2",
94+
def: Adt(
95+
Struct(
96+
Struct {
97+
id: StructId(
98+
0,
99+
),
100+
},
101+
),
102+
),
103+
loc: DeclarationLocation {
104+
hir_file_id: HirFileId(
105+
0,
106+
),
107+
ptr: SyntaxNodePtr {
108+
kind: STRUCT,
109+
range: 0..81,
110+
},
111+
name_ptr: SyntaxNodePtr {
112+
kind: NAME,
113+
range: 74..80,
114+
},
115+
},
116+
container_name: None,
117+
is_alias: true,
118+
},
119+
FileSymbol {
120+
name: "s1",
121+
def: Adt(
122+
Struct(
123+
Struct {
124+
id: StructId(
125+
0,
126+
),
127+
},
128+
),
129+
),
130+
loc: DeclarationLocation {
131+
hir_file_id: HirFileId(
132+
0,
133+
),
134+
ptr: SyntaxNodePtr {
135+
kind: STRUCT,
136+
range: 0..81,
137+
},
138+
name_ptr: SyntaxNodePtr {
139+
kind: NAME,
140+
range: 74..80,
141+
},
142+
},
143+
container_name: None,
144+
is_alias: true,
145+
},
146+
FileSymbol {
147+
name: "s1",
148+
def: Adt(
149+
Struct(
150+
Struct {
151+
id: StructId(
152+
1,
153+
),
154+
},
155+
),
156+
),
157+
loc: DeclarationLocation {
158+
hir_file_id: HirFileId(
159+
0,
160+
),
161+
ptr: SyntaxNodePtr {
162+
kind: STRUCT,
163+
range: 83..119,
164+
},
165+
name_ptr: SyntaxNodePtr {
166+
kind: NAME,
167+
range: 109..118,
168+
},
169+
},
170+
container_name: None,
171+
is_alias: true,
172+
},
173+
FileSymbol {
174+
name: "s2",
175+
def: Adt(
176+
Struct(
177+
Struct {
178+
id: StructId(
179+
0,
180+
),
181+
},
182+
),
183+
),
184+
loc: DeclarationLocation {
185+
hir_file_id: HirFileId(
186+
0,
187+
),
188+
ptr: SyntaxNodePtr {
189+
kind: STRUCT,
190+
range: 0..81,
191+
},
192+
name_ptr: SyntaxNodePtr {
193+
kind: NAME,
194+
range: 74..80,
195+
},
196+
},
197+
container_name: None,
198+
is_alias: true,
199+
},
200+
],
201+
),
202+
]

0 commit comments

Comments
 (0)