Skip to content

Commit ec6d72b

Browse files
committed
scip: Rewrite tests to be closer to what we actually do.
It's also less code.
1 parent 7ee7225 commit ec6d72b

File tree

1 file changed

+30
-55
lines changed
  • crates/rust-analyzer/src/cli

1 file changed

+30
-55
lines changed

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

Lines changed: 30 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ use std::{
88
use crate::line_index::{LineEndings, LineIndex, OffsetEncoding};
99
use hir::Name;
1010
use ide::{
11-
LineCol, MonikerDescriptorKind, MonikerResult, StaticIndex, StaticIndexedFile, TextRange,
12-
TokenId,
11+
LineCol, MonikerDescriptorKind, StaticIndex, StaticIndexedFile, TextRange, TokenId,
12+
TokenStaticData,
1313
};
1414
use ide_db::LineIndexDatabase;
1515
use project_model::{CargoConfig, ProjectManifest, ProjectWorkspace};
@@ -109,10 +109,7 @@ impl flags::Scip {
109109
occurrence.symbol = tokens_to_symbol
110110
.entry(id)
111111
.or_insert_with(|| {
112-
let symbol = match &token.moniker {
113-
Some(moniker) => moniker_to_symbol(&moniker),
114-
None => new_local_symbol(),
115-
};
112+
let symbol = token_to_symbol(&token).unwrap_or_else(&mut new_local_symbol);
116113
scip::symbol::format_symbol(symbol)
117114
})
118115
.clone();
@@ -201,9 +198,11 @@ fn new_descriptor(name: Name, suffix: scip_types::descriptor::Suffix) -> scip_ty
201198
///
202199
/// Only returns a Symbol when it's a non-local symbol.
203200
/// So if the visibility isn't outside of a document, then it will return None
204-
fn moniker_to_symbol(moniker: &MonikerResult) -> scip_types::Symbol {
201+
fn token_to_symbol(token: &TokenStaticData) -> Option<scip_types::Symbol> {
205202
use scip_types::descriptor::Suffix::*;
206203

204+
let moniker = token.moniker.as_ref()?;
205+
207206
let package_name = moniker.package_information.name.clone();
208207
let version = moniker.package_information.version.clone();
209208
let descriptors = moniker
@@ -227,7 +226,7 @@ fn moniker_to_symbol(moniker: &MonikerResult) -> scip_types::Symbol {
227226
})
228227
.collect();
229228

230-
scip_types::Symbol {
229+
Some(scip_types::Symbol {
231230
scheme: "rust-analyzer".into(),
232231
package: Some(scip_types::Package {
233232
manager: "cargo".to_string(),
@@ -238,19 +237,15 @@ fn moniker_to_symbol(moniker: &MonikerResult) -> scip_types::Symbol {
238237
.into(),
239238
descriptors,
240239
..Default::default()
241-
}
240+
})
242241
}
243242

244243
#[cfg(test)]
245244
mod test {
246245
use super::*;
247-
use hir::Semantics;
248-
use ide::{AnalysisHost, FilePosition};
249-
use ide_db::defs::IdentClass;
250-
use ide_db::{base_db::fixture::ChangeFixture, helpers::pick_best_token};
246+
use ide::{AnalysisHost, FilePosition, StaticIndex, TextSize};
247+
use ide_db::base_db::fixture::ChangeFixture;
251248
use scip::symbol::format_symbol;
252-
use syntax::SyntaxKind::*;
253-
use syntax::{AstNode, T};
254249

255250
fn position(ra_fixture: &str) -> (AnalysisHost, FilePosition) {
256251
let mut host = AnalysisHost::default();
@@ -267,53 +262,33 @@ mod test {
267262
fn check_symbol(ra_fixture: &str, expected: &str) {
268263
let (host, position) = position(ra_fixture);
269264

265+
let analysis = host.analysis();
266+
let si = StaticIndex::compute(&analysis);
267+
270268
let FilePosition { file_id, offset } = position;
271269

272-
let db = host.raw_database();
273-
let sema = &Semantics::new(db);
274-
let file = sema.parse(file_id).syntax().clone();
275-
let original_token = pick_best_token(file.token_at_offset(offset), |kind| match kind {
276-
IDENT
277-
| INT_NUMBER
278-
| LIFETIME_IDENT
279-
| T![self]
280-
| T![super]
281-
| T![crate]
282-
| T![Self]
283-
| COMMENT => 2,
284-
kind if kind.is_trivia() => 0,
285-
_ => 1,
286-
})
287-
.expect("OK OK");
288-
289-
let navs = sema
290-
.descend_into_macros(original_token.clone())
291-
.into_iter()
292-
.filter_map(|token| {
293-
IdentClass::classify_token(sema, &token).map(IdentClass::definitions).map(|it| {
294-
it.into_iter().flat_map(|def| {
295-
let module = def.module(db).unwrap();
296-
let current_crate = module.krate();
297-
298-
match MonikerResult::from_def(sema.db, def, current_crate) {
299-
Some(moniker_result) => Some(moniker_to_symbol(&moniker_result)),
300-
None => None,
301-
}
302-
})
303-
})
304-
})
305-
.flatten()
306-
.collect::<Vec<_>>();
270+
let mut found_symbol = None;
271+
for file in &si.files {
272+
if file.file_id != file_id {
273+
continue;
274+
}
275+
for &(range, id) in &file.tokens {
276+
if range.contains(offset - TextSize::from(1)) {
277+
let token = si.tokens.get(id).unwrap();
278+
found_symbol = token_to_symbol(token);
279+
break;
280+
}
281+
}
282+
}
307283

308284
if expected == "" {
309-
assert_eq!(0, navs.len(), "must have no symbols {:?}", navs);
285+
assert!(found_symbol.is_none(), "must have no symbols {:?}", found_symbol);
310286
return;
311287
}
312288

313-
assert_eq!(1, navs.len(), "must have one symbol {:?}", navs);
314-
315-
let res = navs.get(0).unwrap();
316-
let formatted = format_symbol(res.clone());
289+
assert!(found_symbol.is_some(), "must have one symbol {:?}", found_symbol);
290+
let res = found_symbol.unwrap();
291+
let formatted = format_symbol(res);
317292
assert_eq!(formatted, expected);
318293
}
319294

0 commit comments

Comments
 (0)