Skip to content

Commit e257b98

Browse files
committed
Add scip/lsif flag to exclude vendored libaries
1 parent 91aa3f4 commit e257b98

File tree

5 files changed

+95
-19
lines changed

5 files changed

+95
-19
lines changed

crates/ide/src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,9 @@ pub use crate::{
104104
rename::RenameError,
105105
runnables::{Runnable, RunnableKind, TestId},
106106
signature_help::SignatureHelp,
107-
static_index::{StaticIndex, StaticIndexedFile, TokenId, TokenStaticData},
107+
static_index::{
108+
StaticIndex, StaticIndexedFile, TokenId, TokenStaticData, VendoredLibrariesConfig,
109+
},
108110
syntax_highlighting::{
109111
tags::{Highlight, HlMod, HlMods, HlOperator, HlPunct, HlTag},
110112
HighlightConfig, HlRange,

crates/ide/src/static_index.rs

Lines changed: 55 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,11 @@ fn documentation_for_definition(
119119
def.docs(sema.db, famous_defs.as_ref())
120120
}
121121

122+
pub enum VendoredLibrariesConfig<'a> {
123+
Included { workspace_root: &'a VfsPath },
124+
Excluded,
125+
}
126+
122127
impl StaticIndex<'_> {
123128
fn add_file(&mut self, file_id: FileId) {
124129
let current_crate = crates_for(self.db, file_id).pop().map(Into::into);
@@ -230,15 +235,22 @@ impl StaticIndex<'_> {
230235
self.files.push(result);
231236
}
232237

233-
pub fn compute<'a>(analysis: &'a Analysis, workspace_root: &VfsPath) -> StaticIndex<'a> {
238+
pub fn compute<'a>(
239+
analysis: &'a Analysis,
240+
vendored_libs_config: VendoredLibrariesConfig<'_>,
241+
) -> StaticIndex<'a> {
234242
let db = &*analysis.db;
235243
let work = all_modules(db).into_iter().filter(|module| {
236244
let file_id = module.definition_source_file_id(db).original_file(db);
237245
let source_root = db.file_source_root(file_id.into());
238246
let source_root = db.source_root(source_root);
239-
let is_vendored = source_root
240-
.path_for_file(&file_id.into())
241-
.is_some_and(|module_path| module_path.starts_with(workspace_root));
247+
let is_vendored = match vendored_libs_config {
248+
VendoredLibrariesConfig::Included { workspace_root } => source_root
249+
.path_for_file(&file_id.into())
250+
.is_some_and(|module_path| module_path.starts_with(workspace_root)),
251+
VendoredLibrariesConfig::Excluded => false,
252+
};
253+
242254
!source_root.is_library || is_vendored
243255
});
244256
let mut this = StaticIndex {
@@ -268,10 +280,11 @@ mod tests {
268280
use ide_db::{base_db::VfsPath, FileRange, FxHashSet};
269281
use syntax::TextSize;
270282

271-
fn check_all_ranges(ra_fixture: &str) {
283+
use super::VendoredLibrariesConfig;
284+
285+
fn check_all_ranges(ra_fixture: &str, vendored_libs_config: VendoredLibrariesConfig<'_>) {
272286
let (analysis, ranges) = fixture::annotations_without_marker(ra_fixture);
273-
let s =
274-
StaticIndex::compute(&analysis, &VfsPath::new_virtual_path("/workspace".to_owned()));
287+
let s = StaticIndex::compute(&analysis, vendored_libs_config);
275288
let mut range_set: FxHashSet<_> = ranges.iter().map(|it| it.0).collect();
276289
for f in s.files {
277290
for (range, _) in f.tokens {
@@ -288,10 +301,9 @@ mod tests {
288301
}
289302

290303
#[track_caller]
291-
fn check_definitions(ra_fixture: &str) {
304+
fn check_definitions(ra_fixture: &str, vendored_libs_config: VendoredLibrariesConfig<'_>) {
292305
let (analysis, ranges) = fixture::annotations_without_marker(ra_fixture);
293-
let s =
294-
StaticIndex::compute(&analysis, &VfsPath::new_virtual_path("/workspace".to_owned()));
306+
let s = StaticIndex::compute(&analysis, vendored_libs_config);
295307
let mut range_set: FxHashSet<_> = ranges.iter().map(|it| it.0).collect();
296308
for (_, t) in s.tokens.iter() {
297309
if let Some(t) = t.definition {
@@ -319,6 +331,9 @@ struct Foo;
319331
enum E { X(Foo) }
320332
//^ ^ ^^^
321333
"#,
334+
VendoredLibrariesConfig::Included {
335+
workspace_root: &VfsPath::new_virtual_path("/workspace".to_owned()),
336+
},
322337
);
323338
check_definitions(
324339
r#"
@@ -327,6 +342,9 @@ struct Foo;
327342
enum E { X(Foo) }
328343
//^ ^
329344
"#,
345+
VendoredLibrariesConfig::Included {
346+
workspace_root: &VfsPath::new_virtual_path("/workspace".to_owned()),
347+
},
330348
);
331349
}
332350

@@ -349,6 +367,9 @@ pub func() {
349367
350368
}
351369
"#,
370+
VendoredLibrariesConfig::Included {
371+
workspace_root: &VfsPath::new_virtual_path("/workspace".to_owned()),
372+
},
352373
);
353374
}
354375

@@ -367,9 +388,30 @@ struct ExternalLibrary(i32);
367388
struct VendoredLibrary(i32);
368389
//^^^^^^^^^^^^^^^ ^^^
369390
"#,
391+
VendoredLibrariesConfig::Included {
392+
workspace_root: &VfsPath::new_virtual_path("/workspace".to_owned()),
393+
},
370394
);
371395
}
372396

397+
#[test]
398+
fn vendored_crate_excluded() {
399+
check_all_ranges(
400+
r#"
401+
//- /workspace/main.rs crate:main deps:external,vendored
402+
struct Main(i32);
403+
//^^^^ ^^^
404+
405+
//- /external/lib.rs new_source_root:library crate:[email protected],https://a.b/foo.git library
406+
struct ExternalLibrary(i32);
407+
408+
//- /workspace/vendored/lib.rs new_source_root:library crate:[email protected],https://a.b/bar.git library
409+
struct VendoredLibrary(i32);
410+
"#,
411+
VendoredLibrariesConfig::Excluded,
412+
)
413+
}
414+
373415
#[test]
374416
fn derives() {
375417
check_all_ranges(
@@ -384,6 +426,9 @@ pub macro Copy {}
384426
struct Hello(i32);
385427
//^^^^^ ^^^
386428
"#,
429+
VendoredLibrariesConfig::Included {
430+
workspace_root: &VfsPath::new_virtual_path("/workspace".to_owned()),
431+
},
387432
);
388433
}
389434
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,9 @@ xflags::xflags! {
138138

139139
cmd lsif {
140140
required path: PathBuf
141+
142+
/// Exclude code from vendored libraries from the resulting index.
143+
optional --exclude-vendored-libraries
141144
}
142145

143146
cmd scip {
@@ -148,6 +151,9 @@ xflags::xflags! {
148151

149152
/// A path to an json configuration file that can be used to customize cargo behavior.
150153
optional --config-path config_path: PathBuf
154+
155+
/// Exclude code from vendored libraries from the resulting index.
156+
optional --exclude-vendored-libraries
151157
}
152158
}
153159
}
@@ -259,6 +265,8 @@ pub struct Search {
259265
#[derive(Debug)]
260266
pub struct Lsif {
261267
pub path: PathBuf,
268+
269+
pub exclude_vendored_libraries: bool,
262270
}
263271

264272
#[derive(Debug)]
@@ -267,6 +275,7 @@ pub struct Scip {
267275

268276
pub output: Option<PathBuf>,
269277
pub config_path: Option<PathBuf>,
278+
pub exclude_vendored_libraries: bool,
270279
}
271280

272281
impl RustAnalyzer {

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::time::Instant;
55

66
use ide::{
77
Analysis, AnalysisHost, FileId, FileRange, MonikerKind, PackageInformation, RootDatabase,
8-
StaticIndex, StaticIndexedFile, TokenId, TokenStaticData,
8+
StaticIndex, StaticIndexedFile, TokenId, TokenStaticData, VendoredLibrariesConfig,
99
};
1010
use ide_db::{line_index::WideEncoding, LineIndexDatabase};
1111
use load_cargo::{load_workspace, LoadCargoConfig, ProcMacroServerChoice};
@@ -296,7 +296,13 @@ impl flags::Lsif {
296296
let db = host.raw_database();
297297
let analysis = host.analysis();
298298

299-
let si = StaticIndex::compute(&analysis, &path.clone().into());
299+
let vendored_libs_config = if self.exclude_vendored_libraries {
300+
VendoredLibrariesConfig::Excluded
301+
} else {
302+
VendoredLibrariesConfig::Included { workspace_root: &path.clone().into() }
303+
};
304+
305+
let si = StaticIndex::compute(&analysis, vendored_libs_config);
300306

301307
let mut lsif = LsifManager::new(&analysis, db, &vfs);
302308
lsif.add_vertex(lsif::Vertex::MetaData(lsif::MetaData {

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

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::{path::PathBuf, time::Instant};
44

55
use ide::{
66
AnalysisHost, LineCol, MonikerDescriptorKind, MonikerResult, StaticIndex, StaticIndexedFile,
7-
SymbolInformationKind, TextRange, TokenId,
7+
SymbolInformationKind, TextRange, TokenId, VendoredLibrariesConfig,
88
};
99
use ide_db::LineIndexDatabase;
1010
use load_cargo::{load_workspace_at, LoadCargoConfig, ProcMacroServerChoice};
@@ -63,7 +63,13 @@ impl flags::Scip {
6363
let db = host.raw_database();
6464
let analysis = host.analysis();
6565

66-
let si = StaticIndex::compute(&analysis, &root.clone().into());
66+
let vendored_libs_config = if self.exclude_vendored_libraries {
67+
VendoredLibrariesConfig::Excluded
68+
} else {
69+
VendoredLibrariesConfig::Included { workspace_root: &root.clone().into() }
70+
};
71+
72+
let si = StaticIndex::compute(&analysis, vendored_libs_config);
6773

6874
let metadata = scip_types::Metadata {
6975
version: scip_types::ProtocolVersion::UnspecifiedProtocolVersion.into(),
@@ -352,8 +358,12 @@ mod test {
352358
let (host, position) = position(ra_fixture);
353359

354360
let analysis = host.analysis();
355-
let si =
356-
StaticIndex::compute(&analysis, &VfsPath::new_virtual_path("/workspace".to_owned()));
361+
let si = StaticIndex::compute(
362+
&analysis,
363+
VendoredLibrariesConfig::Included {
364+
workspace_root: &VfsPath::new_virtual_path("/workspace".to_owned()),
365+
},
366+
);
357367

358368
let FilePosition { file_id, offset } = position;
359369

@@ -617,8 +627,12 @@ pub mod example_mod {
617627
host.raw_database_mut().apply_change(change_fixture.change);
618628

619629
let analysis = host.analysis();
620-
let si =
621-
StaticIndex::compute(&analysis, &VfsPath::new_virtual_path("/workspace".to_owned()));
630+
let si = StaticIndex::compute(
631+
&analysis,
632+
VendoredLibrariesConfig::Included {
633+
workspace_root: &VfsPath::new_virtual_path("/workspace".to_owned()),
634+
},
635+
);
622636

623637
let file = si.files.first().unwrap();
624638
let (_, token_id) = file.tokens.first().unwrap();

0 commit comments

Comments
 (0)