Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 495a556

Browse files
scip: Populate SymbolInformation::enclosing_symbol
For local variables, this gets the moniker from the enclosing definition and stores it into the TokenStaticData. Then it builds the scip symbol for that moniker when building the SymbolInformation.
1 parent 62663e6 commit 495a556

File tree

3 files changed

+40
-5
lines changed

3 files changed

+40
-5
lines changed

crates/ide-db/src/defs.rs

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77

88
use arrayvec::ArrayVec;
99
use hir::{
10-
Adt, AsAssocItem, AssocItem, BuiltinAttr, BuiltinType, Const, Crate, DeriveHelper, DocLinkDef,
11-
ExternCrateDecl, Field, Function, GenericParam, HasVisibility, Impl, Label, Local, Macro,
12-
Module, ModuleDef, Name, PathResolution, Semantics, Static, ToolModule, Trait, TraitAlias,
13-
TypeAlias, Variant, Visibility,
10+
Adt, AsAssocItem, AssocItem, BuiltinAttr, BuiltinType, Const, Crate, DefWithBody, DeriveHelper,
11+
DocLinkDef, ExternCrateDecl, Field, Function, GenericParam, HasVisibility, Impl, Label, Local,
12+
Macro, Module, ModuleDef, Name, PathResolution, Semantics, Static, ToolModule, Trait,
13+
TraitAlias, TypeAlias, Variant, Visibility,
1414
};
1515
use stdx::impl_from;
1616
use syntax::{
@@ -83,6 +83,13 @@ impl Definition {
8383
Some(module)
8484
}
8585

86+
pub fn enclosing_definition(&self, db: &RootDatabase) -> Option<Definition> {
87+
match self {
88+
Definition::Local(it) => it.parent(db).try_into().ok(),
89+
_ => None,
90+
}
91+
}
92+
8693
pub fn visibility(&self, db: &RootDatabase) -> Option<Visibility> {
8794
let vis = match self {
8895
Definition::Field(sf) => sf.visibility(db),
@@ -662,3 +669,16 @@ impl From<DocLinkDef> for Definition {
662669
}
663670
}
664671
}
672+
673+
impl TryFrom<DefWithBody> for Definition {
674+
type Error = ();
675+
fn try_from(def: DefWithBody) -> Result<Self, Self::Error> {
676+
match def {
677+
DefWithBody::Function(it) => Ok(it.into()),
678+
DefWithBody::Static(it) => Ok(it.into()),
679+
DefWithBody::Const(it) => Ok(it.into()),
680+
DefWithBody::Variant(it) => Ok(it.into()),
681+
DefWithBody::InTypeConst(_) => Err(()),
682+
}
683+
}
684+
}

crates/ide/src/static_index.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ pub struct TokenStaticData {
4747
pub references: Vec<ReferenceData>,
4848
pub moniker: Option<MonikerResult>,
4949
pub display_name: Option<String>,
50+
pub enclosing_moniker: Option<MonikerResult>,
5051
}
5152

5253
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
@@ -174,6 +175,9 @@ impl StaticIndex<'_> {
174175
references: vec![],
175176
moniker: current_crate.and_then(|cc| def_to_moniker(self.db, def, cc)),
176177
display_name: def.name(self.db).map(|name| name.display(self.db).to_string()),
178+
enclosing_moniker: current_crate
179+
.zip(def.enclosing_definition(self.db))
180+
.and_then(|(cc, enclosing_def)| def_to_moniker(self.db, enclosing_def, cc)),
177181
});
178182
self.def_map.insert(def, it);
179183
it

crates/rust-analyzer/src/cli/scip.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ impl flags::Scip {
7878

7979
let mut symbols_emitted: HashSet<TokenId> = HashSet::default();
8080
let mut tokens_to_symbol: HashMap<TokenId, String> = HashMap::new();
81+
let mut tokens_to_enclosing_symbol: HashMap<TokenId, Option<String>> = HashMap::new();
8182

8283
for StaticIndexedFile { file_id, tokens, .. } in si.files {
8384
let mut local_count = 0;
@@ -117,6 +118,16 @@ impl flags::Scip {
117118
scip::symbol::format_symbol(symbol)
118119
})
119120
.clone();
121+
let enclosing_symbol = tokens_to_enclosing_symbol
122+
.entry(id)
123+
.or_insert_with(|| {
124+
token
125+
.enclosing_moniker
126+
.as_ref()
127+
.map(moniker_to_symbol)
128+
.map(scip::symbol::format_symbol)
129+
})
130+
.clone();
120131

121132
let mut symbol_roles = Default::default();
122133

@@ -140,7 +151,7 @@ impl flags::Scip {
140151
kind: Default::default(),
141152
display_name: token.display_name.clone().unwrap_or_default(),
142153
signature_documentation: Default::default(),
143-
enclosing_symbol: String::new(),
154+
enclosing_symbol: enclosing_symbol.unwrap_or_default(),
144155
};
145156

146157
symbols.push(symbol_info)

0 commit comments

Comments
 (0)