Skip to content

Commit bbcb77e

Browse files
committed
Auto merge of #13456 - emilio:scip-local-symbol, r=Veykril
scip: Generate symbols for local crates. Consider something like: ``` // a.rs pub struct Foo { .. } // Foo is "local 1" fn something() { crate::b::Bar::new() // Bar is "local 1", but of "b.rs" } // b.rs pub struct Bar { .. } // "local 1" ``` Without this there's no way to disambiguate whether "local 1" references "Bar" or "Foo".
2 parents f3a6871 + 8039a07 commit bbcb77e

File tree

3 files changed

+56
-18
lines changed

3 files changed

+56
-18
lines changed

crates/ide/src/moniker.rs

+14-14
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ impl MonikerResult {
7373
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
7474
pub struct PackageInformation {
7575
pub name: String,
76-
pub repo: String,
77-
pub version: String,
76+
pub repo: Option<String>,
77+
pub version: Option<String>,
7878
}
7979

8080
pub(crate) fn crate_for_file(db: &RootDatabase, file_id: FileId) -> Option<Crate> {
@@ -256,18 +256,18 @@ pub(crate) fn def_to_moniker(
256256
let (name, repo, version) = match krate.origin(db) {
257257
CrateOrigin::CratesIo { repo, name } => (
258258
name.unwrap_or(krate.display_name(db)?.canonical_name().to_string()),
259-
repo?,
260-
krate.version(db)?,
259+
repo,
260+
krate.version(db),
261261
),
262262
CrateOrigin::Lang(lang) => (
263263
krate.display_name(db)?.canonical_name().to_string(),
264-
"https://github.com/rust-lang/rust/".to_string(),
265-
match lang {
264+
Some("https://github.com/rust-lang/rust/".to_string()),
265+
Some(match lang {
266266
LangCrateOrigin::Other => {
267267
"https://github.com/rust-lang/rust/library/".into()
268268
}
269269
lang => format!("https://github.com/rust-lang/rust/library/{lang}",),
270-
},
270+
}),
271271
),
272272
};
273273
PackageInformation { name, repo, version }
@@ -315,7 +315,7 @@ pub mod module {
315315
}
316316
"#,
317317
"foo::module::func",
318-
r#"PackageInformation { name: "foo", repo: "https://a.b/foo.git", version: "0.1.0" }"#,
318+
r#"PackageInformation { name: "foo", repo: Some("https://a.b/foo.git"), version: Some("0.1.0") }"#,
319319
MonikerKind::Import,
320320
);
321321
check_moniker(
@@ -331,7 +331,7 @@ pub mod module {
331331
}
332332
"#,
333333
"foo::module::func",
334-
r#"PackageInformation { name: "foo", repo: "https://a.b/foo.git", version: "0.1.0" }"#,
334+
r#"PackageInformation { name: "foo", repo: Some("https://a.b/foo.git"), version: Some("0.1.0") }"#,
335335
MonikerKind::Export,
336336
);
337337
}
@@ -348,7 +348,7 @@ pub mod module {
348348
}
349349
"#,
350350
"foo::module::MyTrait::func",
351-
r#"PackageInformation { name: "foo", repo: "https://a.b/foo.git", version: "0.1.0" }"#,
351+
r#"PackageInformation { name: "foo", repo: Some("https://a.b/foo.git"), version: Some("0.1.0") }"#,
352352
MonikerKind::Export,
353353
);
354354
}
@@ -365,7 +365,7 @@ pub mod module {
365365
}
366366
"#,
367367
"foo::module::MyTrait::MY_CONST",
368-
r#"PackageInformation { name: "foo", repo: "https://a.b/foo.git", version: "0.1.0" }"#,
368+
r#"PackageInformation { name: "foo", repo: Some("https://a.b/foo.git"), version: Some("0.1.0") }"#,
369369
MonikerKind::Export,
370370
);
371371
}
@@ -382,7 +382,7 @@ pub mod module {
382382
}
383383
"#,
384384
"foo::module::MyTrait::MyType",
385-
r#"PackageInformation { name: "foo", repo: "https://a.b/foo.git", version: "0.1.0" }"#,
385+
r#"PackageInformation { name: "foo", repo: Some("https://a.b/foo.git"), version: Some("0.1.0") }"#,
386386
MonikerKind::Export,
387387
);
388388
}
@@ -405,7 +405,7 @@ pub mod module {
405405
}
406406
"#,
407407
"foo::module::MyStruct::MyTrait::func",
408-
r#"PackageInformation { name: "foo", repo: "https://a.b/foo.git", version: "0.1.0" }"#,
408+
r#"PackageInformation { name: "foo", repo: Some("https://a.b/foo.git"), version: Some("0.1.0") }"#,
409409
MonikerKind::Export,
410410
);
411411
}
@@ -425,7 +425,7 @@ pub struct St {
425425
}
426426
"#,
427427
"foo::St::a",
428-
r#"PackageInformation { name: "foo", repo: "https://a.b/foo.git", version: "0.1.0" }"#,
428+
r#"PackageInformation { name: "foo", repo: Some("https://a.b/foo.git"), version: Some("0.1.0") }"#,
429429
MonikerKind::Import,
430430
);
431431
}

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -106,12 +106,12 @@ impl LsifManager<'_> {
106106
manager: "cargo".to_string(),
107107
uri: None,
108108
content: None,
109-
repository: Some(lsif::Repository {
110-
url: pi.repo,
109+
repository: pi.repo.map(|url| lsif::Repository {
110+
url,
111111
r#type: "git".to_string(),
112112
commit_id: None,
113113
}),
114-
version: Some(pi.version),
114+
version: pi.version,
115115
}));
116116
self.package_map.insert(package_information, result_set_id);
117117
result_set_id

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

+39-1
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ fn token_to_symbol(token: &TokenStaticData) -> Option<scip_types::Symbol> {
231231
package: Some(scip_types::Package {
232232
manager: "cargo".to_string(),
233233
name: package_name,
234-
version,
234+
version: version.unwrap_or_else(|| ".".to_string()),
235235
..Default::default()
236236
})
237237
.into(),
@@ -415,4 +415,42 @@ pub mod module {
415415
"",
416416
);
417417
}
418+
419+
#[test]
420+
fn global_symbol_for_pub_struct() {
421+
check_symbol(
422+
r#"
423+
//- /lib.rs crate:main
424+
mod foo;
425+
426+
fn main() {
427+
let _bar = foo::Bar { i: 0 };
428+
}
429+
//- /foo.rs
430+
pub struct Bar$0 {
431+
pub i: i32,
432+
}
433+
"#,
434+
"rust-analyzer cargo main . foo/Bar#",
435+
);
436+
}
437+
438+
#[test]
439+
fn global_symbol_for_pub_struct_reference() {
440+
check_symbol(
441+
r#"
442+
//- /lib.rs crate:main
443+
mod foo;
444+
445+
fn main() {
446+
let _bar = foo::Bar$0 { i: 0 };
447+
}
448+
//- /foo.rs
449+
pub struct Bar {
450+
pub i: i32,
451+
}
452+
"#,
453+
"rust-analyzer cargo main . foo/Bar#",
454+
);
455+
}
418456
}

0 commit comments

Comments
 (0)