Skip to content

feat: using doc aliases to search workspace symbols #14849

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 61 additions & 37 deletions crates/hir/src/symbols.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub struct FileSymbol {
pub def: ModuleDef,
pub loc: DeclarationLocation,
pub container_name: Option<SmolStr>,
pub is_alias: bool,
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
Expand Down Expand Up @@ -249,46 +250,69 @@ impl<'a> SymbolCollector<'a> {
<L as Lookup>::Data: HasSource,
<<L as Lookup>::Data as HasSource>::Value: HasName,
{
self.push_file_symbol(|s| {
let loc = id.lookup(s.db.upcast());
let source = loc.source(s.db.upcast());
let name_node = source.value.name()?;
Some(FileSymbol {
name: name_node.text().into(),
def: ModuleDef::from(id.into()),
container_name: s.current_container_name.clone(),
loc: DeclarationLocation {
hir_file_id: source.file_id,
ptr: SyntaxNodePtr::new(source.value.syntax()),
name_ptr: SyntaxNodePtr::new(name_node.syntax()),
},
})
})
}
let loc = id.lookup(self.db.upcast());
let source = loc.source(self.db.upcast());
let Some(name_node) = source.value.name() else { return };
let def = ModuleDef::from(id.into());
let dec_loc = DeclarationLocation {
hir_file_id: source.file_id,
ptr: SyntaxNodePtr::new(source.value.syntax()),
name_ptr: SyntaxNodePtr::new(name_node.syntax()),
};

if let Some(attrs) = def.attrs(self.db) {
for alias in attrs.doc_aliases() {
self.symbols.push(FileSymbol {
name: alias,
def,
loc: dec_loc.clone(),
container_name: self.current_container_name.clone(),
is_alias: true,
});
}
}

fn push_module(&mut self, module_id: ModuleId) {
self.push_file_symbol(|s| {
let def_map = module_id.def_map(s.db.upcast());
let module_data = &def_map[module_id.local_id];
let declaration = module_data.origin.declaration()?;
let module = declaration.to_node(s.db.upcast());
let name_node = module.name()?;
Some(FileSymbol {
name: name_node.text().into(),
def: ModuleDef::Module(module_id.into()),
container_name: s.current_container_name.clone(),
loc: DeclarationLocation {
hir_file_id: declaration.file_id,
ptr: SyntaxNodePtr::new(module.syntax()),
name_ptr: SyntaxNodePtr::new(name_node.syntax()),
},
})
})
self.symbols.push(FileSymbol {
name: name_node.text().into(),
def,
container_name: self.current_container_name.clone(),
loc: dec_loc,
is_alias: false,
});
}

fn push_file_symbol(&mut self, f: impl FnOnce(&Self) -> Option<FileSymbol>) {
if let Some(file_symbol) = f(self) {
self.symbols.push(file_symbol);
fn push_module(&mut self, module_id: ModuleId) {
let def_map = module_id.def_map(self.db.upcast());
let module_data = &def_map[module_id.local_id];
let Some(declaration) = module_data.origin.declaration() else { return };
let module = declaration.to_node(self.db.upcast());
let Some(name_node) = module.name() else { return };
let dec_loc = DeclarationLocation {
hir_file_id: declaration.file_id,
ptr: SyntaxNodePtr::new(module.syntax()),
name_ptr: SyntaxNodePtr::new(name_node.syntax()),
};

let def = ModuleDef::Module(module_id.into());

if let Some(attrs) = def.attrs(self.db) {
for alias in attrs.doc_aliases() {
self.symbols.push(FileSymbol {
name: alias,
def,
loc: dec_loc.clone(),
container_name: self.current_container_name.clone(),
is_alias: true,
});
}
}

self.symbols.push(FileSymbol {
name: name_node.text().into(),
def: ModuleDef::Module(module_id.into()),
container_name: self.current_container_name.clone(),
loc: dec_loc,
is_alias: false,
});
}
}
27 changes: 27 additions & 0 deletions crates/ide-db/src/symbol_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -434,4 +434,31 @@ struct StructInModB;

expect_file!["./test_data/test_symbol_index_collection.txt"].assert_debug_eq(&symbols);
}

#[test]
fn test_doc_alias() {
let (db, _) = RootDatabase::with_single_file(
r#"
#[doc(alias="s1")]
#[doc(alias="s2")]
#[doc(alias("mul1","mul2"))]
struct Struct;

#[doc(alias="s1")]
struct Duplicate;
"#,
);

let symbols: Vec<_> = Crate::from(db.test_crate())
.modules(&db)
.into_iter()
.map(|module_id| {
let mut symbols = SymbolCollector::collect_module(&db, module_id);
symbols.sort_by_key(|it| it.name.clone());
(module_id, symbols)
})
.collect();

expect_file!["./test_data/test_doc_alias.txt"].assert_debug_eq(&symbols);
}
}
202 changes: 202 additions & 0 deletions crates/ide-db/src/test_data/test_doc_alias.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
[
(
Module {
id: ModuleId {
krate: Idx::<CrateData>(0),
block: None,
local_id: Idx::<ModuleData>(0),
},
},
[
FileSymbol {
name: "Duplicate",
def: Adt(
Struct(
Struct {
id: StructId(
1,
),
},
),
),
loc: DeclarationLocation {
hir_file_id: HirFileId(
0,
),
ptr: SyntaxNodePtr {
kind: STRUCT,
range: 83..119,
},
name_ptr: SyntaxNodePtr {
kind: NAME,
range: 109..118,
},
},
container_name: None,
is_alias: false,
},
FileSymbol {
name: "Struct",
def: Adt(
Struct(
Struct {
id: StructId(
0,
),
},
),
),
loc: DeclarationLocation {
hir_file_id: HirFileId(
0,
),
ptr: SyntaxNodePtr {
kind: STRUCT,
range: 0..81,
},
name_ptr: SyntaxNodePtr {
kind: NAME,
range: 74..80,
},
},
container_name: None,
is_alias: false,
},
FileSymbol {
name: "mul1",
def: Adt(
Struct(
Struct {
id: StructId(
0,
),
},
),
),
loc: DeclarationLocation {
hir_file_id: HirFileId(
0,
),
ptr: SyntaxNodePtr {
kind: STRUCT,
range: 0..81,
},
name_ptr: SyntaxNodePtr {
kind: NAME,
range: 74..80,
},
},
container_name: None,
is_alias: true,
},
FileSymbol {
name: "mul2",
def: Adt(
Struct(
Struct {
id: StructId(
0,
),
},
),
),
loc: DeclarationLocation {
hir_file_id: HirFileId(
0,
),
ptr: SyntaxNodePtr {
kind: STRUCT,
range: 0..81,
},
name_ptr: SyntaxNodePtr {
kind: NAME,
range: 74..80,
},
},
container_name: None,
is_alias: true,
},
FileSymbol {
name: "s1",
def: Adt(
Struct(
Struct {
id: StructId(
0,
),
},
),
),
loc: DeclarationLocation {
hir_file_id: HirFileId(
0,
),
ptr: SyntaxNodePtr {
kind: STRUCT,
range: 0..81,
},
name_ptr: SyntaxNodePtr {
kind: NAME,
range: 74..80,
},
},
container_name: None,
is_alias: true,
},
FileSymbol {
name: "s1",
def: Adt(
Struct(
Struct {
id: StructId(
1,
),
},
),
),
loc: DeclarationLocation {
hir_file_id: HirFileId(
0,
),
ptr: SyntaxNodePtr {
kind: STRUCT,
range: 83..119,
},
name_ptr: SyntaxNodePtr {
kind: NAME,
range: 109..118,
},
},
container_name: None,
is_alias: true,
},
FileSymbol {
name: "s2",
def: Adt(
Struct(
Struct {
id: StructId(
0,
),
},
),
),
loc: DeclarationLocation {
hir_file_id: HirFileId(
0,
),
ptr: SyntaxNodePtr {
kind: STRUCT,
range: 0..81,
},
name_ptr: SyntaxNodePtr {
kind: NAME,
range: 74..80,
},
},
container_name: None,
is_alias: true,
},
],
),
]
Loading