about summary refs log tree commit diff
diff options
context:
space:
mode:
authorNicolas Guichard <nicolas.guichard@kdab.com>2023-12-05 18:26:58 +0100
committerNicolas Guichard <nicolas.guichard@kdab.com>2024-01-03 10:54:57 +0100
commit62663e6d4b87e4405a5fe57a43ce7cc59312cb9f (patch)
tree271c7b3c83b671a6c519595afe20db9de1a7741b
parent375f1cca4fe90d8deffd7e84b7cb6594b7ccca3b (diff)
downloadrust-62663e6d4b87e4405a5fe57a43ce7cc59312cb9f.tar.gz
rust-62663e6d4b87e4405a5fe57a43ce7cc59312cb9f.zip
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.
-rw-r--r--crates/rust-analyzer/src/cli/scip.rs24
1 files changed, 11 insertions, 13 deletions
diff --git a/crates/rust-analyzer/src/cli/scip.rs b/crates/rust-analyzer/src/cli/scip.rs
index af2cdcb28ef..02f341c9bd4 100644
--- a/crates/rust-analyzer/src/cli/scip.rs
+++ b/crates/rust-analyzer/src/cli/scip.rs
@@ -7,8 +7,8 @@ use std::{
 };
 
 use ide::{
-    LineCol, MonikerDescriptorKind, StaticIndex, StaticIndexedFile, TextRange, TokenId,
-    TokenStaticData,
+    LineCol, MonikerDescriptorKind, MonikerResult, StaticIndex, StaticIndexedFile, TextRange,
+    TokenId,
 };
 use ide_db::LineIndexDatabase;
 use load_cargo::{load_workspace_at, LoadCargoConfig, ProcMacroServerChoice};
@@ -109,7 +109,11 @@ impl flags::Scip {
                 let symbol = tokens_to_symbol
                     .entry(id)
                     .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)
                     })
                     .clone();
@@ -228,15 +232,9 @@ fn new_descriptor(name: &str, suffix: scip_types::descriptor::Suffix) -> scip_ty
     }
 }
 
-/// Loosely based on `def_to_moniker`
-///
-/// 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> {
+fn moniker_to_symbol(moniker: &MonikerResult) -> scip_types::Symbol {
     use scip_types::descriptor::Suffix::*;
 
-    let moniker = token.moniker.as_ref()?;
-
     let package_name = moniker.package_information.name.clone();
     let version = moniker.package_information.version.clone();
     let descriptors = moniker
@@ -260,7 +258,7 @@ fn token_to_symbol(token: &TokenStaticData) -> Option<scip_types::Symbol> {
         })
         .collect();
 
-    Some(scip_types::Symbol {
+    scip_types::Symbol {
         scheme: "rust-analyzer".into(),
         package: Some(scip_types::Package {
             manager: "cargo".to_string(),
@@ -271,7 +269,7 @@ fn token_to_symbol(token: &TokenStaticData) -> Option<scip_types::Symbol> {
         .into(),
         descriptors,
         special_fields: Default::default(),
-    })
+    }
 }
 
 #[cfg(test)]
@@ -309,7 +307,7 @@ mod test {
             for &(range, id) in &file.tokens {
                 if range.contains(offset - TextSize::from(1)) {
                     let token = si.tokens.get(id).unwrap();
-                    found_symbol = token_to_symbol(token);
+                    found_symbol = token.moniker.as_ref().map(moniker_to_symbol);
                     break;
                 }
             }