Skip to content

Commit 30e1ece

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

File tree

4 files changed

+64
-14
lines changed

4 files changed

+64
-14
lines changed

crates/ide/src/static_index.rs

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,11 @@ impl StaticIndex<'_> {
230230
self.files.push(result);
231231
}
232232

233-
pub fn compute<'a>(analysis: &'a Analysis, workspace_root: &VfsPath) -> StaticIndex<'a> {
233+
pub fn compute<'a>(
234+
analysis: &'a Analysis,
235+
workspace_root: &VfsPath,
236+
exclude_vendored_libraries: bool,
237+
) -> StaticIndex<'a> {
234238
let db = &*analysis.db;
235239
let work = all_modules(db).into_iter().filter(|module| {
236240
let file_id = module.definition_source_file_id(db).original_file(db);
@@ -239,7 +243,7 @@ impl StaticIndex<'_> {
239243
let is_vendored = source_root
240244
.path_for_file(&file_id.into())
241245
.is_some_and(|module_path| module_path.starts_with(workspace_root));
242-
!source_root.is_library || is_vendored
246+
!source_root.is_library || (!exclude_vendored_libraries && is_vendored)
243247
});
244248
let mut this = StaticIndex {
245249
files: vec![],
@@ -268,10 +272,13 @@ mod tests {
268272
use ide_db::{base_db::VfsPath, FileRange, FxHashSet};
269273
use syntax::TextSize;
270274

271-
fn check_all_ranges(ra_fixture: &str) {
275+
fn check_all_ranges(ra_fixture: &str, exclude_vendored_libraries: bool) {
272276
let (analysis, ranges) = fixture::annotations_without_marker(ra_fixture);
273-
let s =
274-
StaticIndex::compute(&analysis, &VfsPath::new_virtual_path("/workspace".to_owned()));
277+
let s = StaticIndex::compute(
278+
&analysis,
279+
&VfsPath::new_virtual_path("/workspace".to_owned()),
280+
exclude_vendored_libraries,
281+
);
275282
let mut range_set: FxHashSet<_> = ranges.iter().map(|it| it.0).collect();
276283
for f in s.files {
277284
for (range, _) in f.tokens {
@@ -288,10 +295,13 @@ mod tests {
288295
}
289296

290297
#[track_caller]
291-
fn check_definitions(ra_fixture: &str) {
298+
fn check_definitions(ra_fixture: &str, exclude_vendored_libraries: bool) {
292299
let (analysis, ranges) = fixture::annotations_without_marker(ra_fixture);
293-
let s =
294-
StaticIndex::compute(&analysis, &VfsPath::new_virtual_path("/workspace".to_owned()));
300+
let s = StaticIndex::compute(
301+
&analysis,
302+
&VfsPath::new_virtual_path("/workspace".to_owned()),
303+
exclude_vendored_libraries,
304+
);
295305
let mut range_set: FxHashSet<_> = ranges.iter().map(|it| it.0).collect();
296306
for (_, t) in s.tokens.iter() {
297307
if let Some(t) = t.definition {
@@ -319,6 +329,7 @@ struct Foo;
319329
enum E { X(Foo) }
320330
//^ ^ ^^^
321331
"#,
332+
false,
322333
);
323334
check_definitions(
324335
r#"
@@ -327,6 +338,7 @@ struct Foo;
327338
enum E { X(Foo) }
328339
//^ ^
329340
"#,
341+
false,
330342
);
331343
}
332344

@@ -349,6 +361,7 @@ pub func() {
349361
350362
}
351363
"#,
364+
false,
352365
);
353366
}
354367

@@ -367,9 +380,28 @@ struct ExternalLibrary(i32);
367380
struct VendoredLibrary(i32);
368381
//^^^^^^^^^^^^^^^ ^^^
369382
"#,
383+
false,
370384
);
371385
}
372386

387+
#[test]
388+
fn vendored_crate_excluded() {
389+
check_all_ranges(
390+
r#"
391+
//- /workspace/main.rs crate:main deps:external,vendored
392+
struct Main(i32);
393+
//^^^^ ^^^
394+
395+
//- /external/lib.rs new_source_root:library crate:[email protected],https://a.b/foo.git library
396+
struct ExternalLibrary(i32);
397+
398+
//- /workspace/vendored/lib.rs new_source_root:library crate:[email protected],https://a.b/bar.git library
399+
struct VendoredLibrary(i32);
400+
"#,
401+
true,
402+
)
403+
}
404+
373405
#[test]
374406
fn derives() {
375407
check_all_ranges(
@@ -384,6 +416,7 @@ pub macro Copy {}
384416
struct Hello(i32);
385417
//^^^^^ ^^^
386418
"#,
419+
false,
387420
);
388421
}
389422
}

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: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,8 @@ 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 si =
300+
StaticIndex::compute(&analysis, &path.clone().into(), self.exclude_vendored_libraries);
300301

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

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

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ 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 si =
67+
StaticIndex::compute(&analysis, &root.clone().into(), self.exclude_vendored_libraries);
6768

6869
let metadata = scip_types::Metadata {
6970
version: scip_types::ProtocolVersion::UnspecifiedProtocolVersion.into(),
@@ -352,8 +353,11 @@ mod test {
352353
let (host, position) = position(ra_fixture);
353354

354355
let analysis = host.analysis();
355-
let si =
356-
StaticIndex::compute(&analysis, &VfsPath::new_virtual_path("/workspace".to_owned()));
356+
let si = StaticIndex::compute(
357+
&analysis,
358+
&VfsPath::new_virtual_path("/workspace".to_owned()),
359+
false,
360+
);
357361

358362
let FilePosition { file_id, offset } = position;
359363

@@ -617,8 +621,11 @@ pub mod example_mod {
617621
host.raw_database_mut().apply_change(change_fixture.change);
618622

619623
let analysis = host.analysis();
620-
let si =
621-
StaticIndex::compute(&analysis, &VfsPath::new_virtual_path("/workspace".to_owned()));
624+
let si = StaticIndex::compute(
625+
&analysis,
626+
&VfsPath::new_virtual_path("/workspace".to_owned()),
627+
false,
628+
);
622629

623630
let file = si.files.first().unwrap();
624631
let (_, token_id) = file.tokens.first().unwrap();

0 commit comments

Comments
 (0)