Skip to content

Commit abd0419

Browse files
committed
fixes 14557 v2
1 parent fe79529 commit abd0419

File tree

2 files changed

+78
-77
lines changed

2 files changed

+78
-77
lines changed

crates/hir/src/symbols.rs

Lines changed: 62 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -249,85 +249,74 @@ impl<'a> SymbolCollector<'a> {
249249
<L as Lookup>::Data: HasSource,
250250
<<L as Lookup>::Data as HasSource>::Value: HasName,
251251
{
252-
self.push_file_symbol(|s| {
253-
let mut symbols = Vec::new();
254-
let loc = id.lookup(s.db.upcast());
255-
let source = loc.source(s.db.upcast());
256-
let name_node = source.value.name()?;
257-
let def = ModuleDef::from(id.into());
258-
let dec_loc = DeclarationLocation {
259-
hir_file_id: source.file_id,
260-
ptr: SyntaxNodePtr::new(source.value.syntax()),
261-
name_ptr: SyntaxNodePtr::new(name_node.syntax()),
262-
};
263-
264-
if let Some(attrs) = def.attrs(s.db) {
265-
for alias in attrs.doc_aliases() {
266-
eprintln!("Adding alias {}", alias);
267-
symbols.push(FileSymbol {
268-
name: alias,
269-
def: def.clone(),
270-
loc: dec_loc.clone(),
271-
container_name: s.current_container_name.clone(),
272-
});
273-
}
252+
let loc = id.lookup(self.db.upcast());
253+
let source = loc.source(self.db.upcast());
254+
let name_node = match source.value.name() {
255+
Some(n) => n,
256+
None => return,
257+
};
258+
let def = ModuleDef::from(id.into());
259+
let dec_loc = DeclarationLocation {
260+
hir_file_id: source.file_id,
261+
ptr: SyntaxNodePtr::new(source.value.syntax()),
262+
name_ptr: SyntaxNodePtr::new(name_node.syntax()),
263+
};
264+
265+
if let Some(attrs) = def.attrs(self.db) {
266+
for alias in attrs.doc_aliases() {
267+
self.symbols.push(FileSymbol {
268+
name: alias,
269+
def,
270+
loc: dec_loc.clone(),
271+
container_name: self.current_container_name.clone(),
272+
});
274273
}
274+
}
275275

276-
symbols.push(FileSymbol {
277-
name: name_node.text().into(),
278-
def,
279-
container_name: s.current_container_name.clone(),
280-
loc: dec_loc,
281-
});
282-
283-
Some(symbols)
284-
})
276+
self.symbols.push(FileSymbol {
277+
name: name_node.text().into(),
278+
def,
279+
container_name: self.current_container_name.clone(),
280+
loc: dec_loc,
281+
});
285282
}
286283

287284
fn push_module(&mut self, module_id: ModuleId) {
288-
self.push_file_symbol(|s| {
289-
let mut symbols = Vec::new();
290-
let def_map = module_id.def_map(s.db.upcast());
291-
let module_data = &def_map[module_id.local_id];
292-
let declaration = module_data.origin.declaration()?;
293-
let module = declaration.to_node(s.db.upcast());
294-
let name_node = module.name()?;
295-
let dec_loc = DeclarationLocation {
296-
hir_file_id: declaration.file_id,
297-
ptr: SyntaxNodePtr::new(module.syntax()),
298-
name_ptr: SyntaxNodePtr::new(name_node.syntax()),
299-
};
300-
301-
let def = ModuleDef::Module(module_id.into());
302-
303-
if let Some(attrs) = def.attrs(s.db) {
304-
for alias in attrs.doc_aliases() {
305-
eprintln!("Adding alias for module. {}", alias);
306-
symbols.push(FileSymbol {
307-
name: alias,
308-
def: def.clone(),
309-
loc: dec_loc.clone(),
310-
container_name: s.current_container_name.clone(),
311-
});
312-
}
313-
}
314-
315-
symbols.push(FileSymbol {
316-
name: name_node.text().into(),
317-
def: ModuleDef::Module(module_id.into()),
318-
container_name: s.current_container_name.clone(),
319-
loc: dec_loc,
320-
});
321-
322-
Some(symbols)
323-
})
324-
}
325-
326-
fn push_file_symbol(&mut self, f: impl FnOnce(&Self) -> Option<Vec<FileSymbol>>) {
327-
if let Some(file_symbols) = f(self) {
328-
for file_symbol in file_symbols {
329-
self.symbols.push(file_symbol);
285+
let def_map = module_id.def_map(self.db.upcast());
286+
let module_data = &def_map[module_id.local_id];
287+
let declaration = match module_data.origin.declaration() {
288+
Some(d) => d,
289+
None => return,
290+
};
291+
let module = declaration.to_node(self.db.upcast());
292+
let name_node = match module.name() {
293+
Some(n) => n,
294+
None => return,
295+
};
296+
let dec_loc = DeclarationLocation {
297+
hir_file_id: declaration.file_id,
298+
ptr: SyntaxNodePtr::new(module.syntax()),
299+
name_ptr: SyntaxNodePtr::new(name_node.syntax()),
300+
};
301+
302+
let def = ModuleDef::Module(module_id.into());
303+
304+
if let Some(attrs) = def.attrs(self.db) {
305+
for alias in attrs.doc_aliases() {
306+
self.symbols.push(FileSymbol {
307+
name: alias,
308+
def,
309+
loc: dec_loc.clone(),
310+
container_name: self.current_container_name.clone(),
311+
});
330312
}
331313
}
314+
315+
self.symbols.push(FileSymbol {
316+
name: name_node.text().into(),
317+
def: ModuleDef::Module(module_id.into()),
318+
container_name: self.current_container_name.clone(),
319+
loc: dec_loc,
320+
});
332321
}
333322
}

crates/ide/src/lib.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ use ide_db::{
7272
},
7373
symbol_index, FxHashMap, FxIndexSet, LineIndexDatabase,
7474
};
75-
use syntax::SourceFile;
75+
use syntax::{SmolStr, SourceFile};
7676
use triomphe::Arc;
7777

7878
use crate::navigation_target::{ToNav, TryToNav};
@@ -407,11 +407,23 @@ impl Analysis {
407407
.into_iter() // xx: should we make this a par iter?
408408
.filter_map(|s| {
409409
let nav = s.def.try_to_nav(db);
410-
// With the use of doc aliases, we should adopt `FileSymbol`s name.
411-
// Otherwise aliases would be ignored.
410+
412411
match nav {
413412
Some(mut inner) => {
414-
inner.name = s.name;
413+
let canpath = s.def.canonical_path(db);
414+
// With the use of doc aliases, we should adopt `FileSymbol`s name.
415+
// Otherwise aliases would be ignored.
416+
if inner.name != s.name {
417+
inner.name = SmolStr::from(format!(
418+
"{} ( see `{}` )",
419+
s.name,
420+
if canpath.is_some() {
421+
canpath.unwrap()
422+
} else {
423+
inner.name.to_string()
424+
}
425+
));
426+
}
415427
Some(inner)
416428
}
417429
None => None,

0 commit comments

Comments
 (0)