scip: Rewrite tests to be closer to what we actually do.

It's also less code.
This commit is contained in:
Emilio Cobos Álvarez 2022-10-21 22:04:43 +02:00
parent 7ee72256eb
commit ec6d72baa1
No known key found for this signature in database
GPG Key ID: E1152D0994E4BF8A

View File

@ -8,8 +8,8 @@
use crate::line_index::{LineEndings, LineIndex, OffsetEncoding}; use crate::line_index::{LineEndings, LineIndex, OffsetEncoding};
use hir::Name; use hir::Name;
use ide::{ use ide::{
LineCol, MonikerDescriptorKind, MonikerResult, StaticIndex, StaticIndexedFile, TextRange, LineCol, MonikerDescriptorKind, StaticIndex, StaticIndexedFile, TextRange, TokenId,
TokenId, TokenStaticData,
}; };
use ide_db::LineIndexDatabase; use ide_db::LineIndexDatabase;
use project_model::{CargoConfig, ProjectManifest, ProjectWorkspace}; use project_model::{CargoConfig, ProjectManifest, ProjectWorkspace};
@ -109,10 +109,7 @@ pub fn run(self) -> Result<()> {
occurrence.symbol = tokens_to_symbol occurrence.symbol = tokens_to_symbol
.entry(id) .entry(id)
.or_insert_with(|| { .or_insert_with(|| {
let symbol = match &token.moniker { let symbol = token_to_symbol(&token).unwrap_or_else(&mut new_local_symbol);
Some(moniker) => moniker_to_symbol(&moniker),
None => new_local_symbol(),
};
scip::symbol::format_symbol(symbol) scip::symbol::format_symbol(symbol)
}) })
.clone(); .clone();
@ -201,9 +198,11 @@ fn new_descriptor(name: Name, suffix: scip_types::descriptor::Suffix) -> scip_ty
/// ///
/// Only returns a Symbol when it's a non-local 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 /// So if the visibility isn't outside of a document, then it will return None
fn moniker_to_symbol(moniker: &MonikerResult) -> scip_types::Symbol { 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
@ -227,7 +226,7 @@ fn moniker_to_symbol(moniker: &MonikerResult) -> scip_types::Symbol {
}) })
.collect(); .collect();
scip_types::Symbol { Some(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(),
@ -238,19 +237,15 @@ fn moniker_to_symbol(moniker: &MonikerResult) -> scip_types::Symbol {
.into(), .into(),
descriptors, descriptors,
..Default::default() ..Default::default()
} })
} }
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use super::*; use super::*;
use hir::Semantics; use ide::{AnalysisHost, FilePosition, StaticIndex, TextSize};
use ide::{AnalysisHost, FilePosition}; use ide_db::base_db::fixture::ChangeFixture;
use ide_db::defs::IdentClass;
use ide_db::{base_db::fixture::ChangeFixture, helpers::pick_best_token};
use scip::symbol::format_symbol; use scip::symbol::format_symbol;
use syntax::SyntaxKind::*;
use syntax::{AstNode, T};
fn position(ra_fixture: &str) -> (AnalysisHost, FilePosition) { fn position(ra_fixture: &str) -> (AnalysisHost, FilePosition) {
let mut host = AnalysisHost::default(); let mut host = AnalysisHost::default();
@ -267,53 +262,33 @@ fn position(ra_fixture: &str) -> (AnalysisHost, FilePosition) {
fn check_symbol(ra_fixture: &str, expected: &str) { fn check_symbol(ra_fixture: &str, expected: &str) {
let (host, position) = position(ra_fixture); let (host, position) = position(ra_fixture);
let analysis = host.analysis();
let si = StaticIndex::compute(&analysis);
let FilePosition { file_id, offset } = position; let FilePosition { file_id, offset } = position;
let db = host.raw_database(); let mut found_symbol = None;
let sema = &Semantics::new(db); for file in &si.files {
let file = sema.parse(file_id).syntax().clone(); if file.file_id != file_id {
let original_token = pick_best_token(file.token_at_offset(offset), |kind| match kind { continue;
IDENT }
| INT_NUMBER for &(range, id) in &file.tokens {
| LIFETIME_IDENT if range.contains(offset - TextSize::from(1)) {
| T![self] let token = si.tokens.get(id).unwrap();
| T![super] found_symbol = token_to_symbol(token);
| T![crate] break;
| T![Self] }
| COMMENT => 2, }
kind if kind.is_trivia() => 0, }
_ => 1,
})
.expect("OK OK");
let navs = sema
.descend_into_macros(original_token.clone())
.into_iter()
.filter_map(|token| {
IdentClass::classify_token(sema, &token).map(IdentClass::definitions).map(|it| {
it.into_iter().flat_map(|def| {
let module = def.module(db).unwrap();
let current_crate = module.krate();
match MonikerResult::from_def(sema.db, def, current_crate) {
Some(moniker_result) => Some(moniker_to_symbol(&moniker_result)),
None => None,
}
})
})
})
.flatten()
.collect::<Vec<_>>();
if expected == "" { if expected == "" {
assert_eq!(0, navs.len(), "must have no symbols {:?}", navs); assert!(found_symbol.is_none(), "must have no symbols {:?}", found_symbol);
return; return;
} }
assert_eq!(1, navs.len(), "must have one symbol {:?}", navs); assert!(found_symbol.is_some(), "must have one symbol {:?}", found_symbol);
let res = found_symbol.unwrap();
let res = navs.get(0).unwrap(); let formatted = format_symbol(res);
let formatted = format_symbol(res.clone());
assert_eq!(formatted, expected); assert_eq!(formatted, expected);
} }