scip: Refactor token_to_symbol to moniker_to_symbol

This is meant to implement SymbolInformation::enclosing_symbol, so we
can build the enclosing symbol from the enclosing moniker without
having the full enclosing token's TokenStaticData.
This commit is contained in:
Nicolas Guichard 2023-12-05 18:26:58 +01:00
parent 375f1cca4f
commit 62663e6d4b

View File

@ -7,8 +7,8 @@ use std::{
}; };
use ide::{ use ide::{
LineCol, MonikerDescriptorKind, StaticIndex, StaticIndexedFile, TextRange, TokenId, LineCol, MonikerDescriptorKind, MonikerResult, StaticIndex, StaticIndexedFile, TextRange,
TokenStaticData, TokenId,
}; };
use ide_db::LineIndexDatabase; use ide_db::LineIndexDatabase;
use load_cargo::{load_workspace_at, LoadCargoConfig, ProcMacroServerChoice}; use load_cargo::{load_workspace_at, LoadCargoConfig, ProcMacroServerChoice};
@ -109,7 +109,11 @@ impl flags::Scip {
let symbol = tokens_to_symbol let symbol = tokens_to_symbol
.entry(id) .entry(id)
.or_insert_with(|| { .or_insert_with(|| {
let symbol = token_to_symbol(token).unwrap_or_else(&mut new_local_symbol); let symbol = token
.moniker
.as_ref()
.map(moniker_to_symbol)
.unwrap_or_else(&mut new_local_symbol);
scip::symbol::format_symbol(symbol) scip::symbol::format_symbol(symbol)
}) })
.clone(); .clone();
@ -228,15 +232,9 @@ fn new_descriptor(name: &str, suffix: scip_types::descriptor::Suffix) -> scip_ty
} }
} }
/// Loosely based on `def_to_moniker` fn moniker_to_symbol(moniker: &MonikerResult) -> scip_types::Symbol {
///
/// Only returns a Symbol when it's a non-local symbol.
/// So if the visibility isn't outside of a document, then it will return None
fn token_to_symbol(token: &TokenStaticData) -> Option<scip_types::Symbol> {
use scip_types::descriptor::Suffix::*; use scip_types::descriptor::Suffix::*;
let moniker = token.moniker.as_ref()?;
let package_name = moniker.package_information.name.clone(); let package_name = moniker.package_information.name.clone();
let version = moniker.package_information.version.clone(); let version = moniker.package_information.version.clone();
let descriptors = moniker let descriptors = moniker
@ -260,7 +258,7 @@ fn token_to_symbol(token: &TokenStaticData) -> Option<scip_types::Symbol> {
}) })
.collect(); .collect();
Some(scip_types::Symbol { scip_types::Symbol {
scheme: "rust-analyzer".into(), scheme: "rust-analyzer".into(),
package: Some(scip_types::Package { package: Some(scip_types::Package {
manager: "cargo".to_string(), manager: "cargo".to_string(),
@ -271,7 +269,7 @@ fn token_to_symbol(token: &TokenStaticData) -> Option<scip_types::Symbol> {
.into(), .into(),
descriptors, descriptors,
special_fields: Default::default(), special_fields: Default::default(),
}) }
} }
#[cfg(test)] #[cfg(test)]
@ -309,7 +307,7 @@ mod test {
for &(range, id) in &file.tokens { for &(range, id) in &file.tokens {
if range.contains(offset - TextSize::from(1)) { if range.contains(offset - TextSize::from(1)) {
let token = si.tokens.get(id).unwrap(); let token = si.tokens.get(id).unwrap();
found_symbol = token_to_symbol(token); found_symbol = token.moniker.as_ref().map(moniker_to_symbol);
break; break;
} }
} }