@@ -8,8 +8,8 @@ use std::{
8
8
use crate :: line_index:: { LineEndings , LineIndex , OffsetEncoding } ;
9
9
use hir:: Name ;
10
10
use ide:: {
11
- LineCol , MonikerDescriptorKind , MonikerResult , StaticIndex , StaticIndexedFile , TextRange ,
12
- TokenId ,
11
+ LineCol , MonikerDescriptorKind , StaticIndex , StaticIndexedFile , TextRange , TokenId ,
12
+ TokenStaticData ,
13
13
} ;
14
14
use ide_db:: LineIndexDatabase ;
15
15
use project_model:: { CargoConfig , ProjectManifest , ProjectWorkspace } ;
@@ -109,10 +109,7 @@ impl flags::Scip {
109
109
occurrence. symbol = tokens_to_symbol
110
110
. entry ( id)
111
111
. 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) ;
116
113
scip:: symbol:: format_symbol ( symbol)
117
114
} )
118
115
. clone ( ) ;
@@ -201,9 +198,11 @@ fn new_descriptor(name: Name, suffix: scip_types::descriptor::Suffix) -> scip_ty
201
198
///
202
199
/// Only returns a Symbol when it's a non-local symbol.
203
200
/// 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 > {
205
202
use scip_types:: descriptor:: Suffix :: * ;
206
203
204
+ let moniker = token. moniker . as_ref ( ) ?;
205
+
207
206
let package_name = moniker. package_information . name . clone ( ) ;
208
207
let version = moniker. package_information . version . clone ( ) ;
209
208
let descriptors = moniker
@@ -227,7 +226,7 @@ fn moniker_to_symbol(moniker: &MonikerResult) -> scip_types::Symbol {
227
226
} )
228
227
. collect ( ) ;
229
228
230
- scip_types:: Symbol {
229
+ Some ( scip_types:: Symbol {
231
230
scheme : "rust-analyzer" . into ( ) ,
232
231
package : Some ( scip_types:: Package {
233
232
manager : "cargo" . to_string ( ) ,
@@ -238,19 +237,15 @@ fn moniker_to_symbol(moniker: &MonikerResult) -> scip_types::Symbol {
238
237
. into ( ) ,
239
238
descriptors,
240
239
..Default :: default ( )
241
- }
240
+ } )
242
241
}
243
242
244
243
#[ cfg( test) ]
245
244
mod test {
246
245
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 ;
251
248
use scip:: symbol:: format_symbol;
252
- use syntax:: SyntaxKind :: * ;
253
- use syntax:: { AstNode , T } ;
254
249
255
250
fn position ( ra_fixture : & str ) -> ( AnalysisHost , FilePosition ) {
256
251
let mut host = AnalysisHost :: default ( ) ;
@@ -267,53 +262,33 @@ mod test {
267
262
fn check_symbol ( ra_fixture : & str , expected : & str ) {
268
263
let ( host, position) = position ( ra_fixture) ;
269
264
265
+ let analysis = host. analysis ( ) ;
266
+ let si = StaticIndex :: compute ( & analysis) ;
267
+
270
268
let FilePosition { file_id, offset } = position;
271
269
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
+ }
307
283
308
284
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 ) ;
310
286
return ;
311
287
}
312
288
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) ;
317
292
assert_eq ! ( formatted, expected) ;
318
293
}
319
294
0 commit comments