Skip to content

Commit

Permalink
Auto merge of rust-lang#13456 - emilio:scip-local-symbol, r=Veykril
Browse files Browse the repository at this point in the history
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".
  • Loading branch information
bors committed Nov 3, 2022
2 parents f3a6871 + 8039a07 commit bbcb77e
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 18 deletions.
28 changes: 14 additions & 14 deletions crates/ide/src/moniker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ impl MonikerResult {
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct PackageInformation {
pub name: String,
pub repo: String,
pub version: String,
pub repo: Option<String>,
pub version: Option<String>,
}

pub(crate) fn crate_for_file(db: &RootDatabase, file_id: FileId) -> Option<Crate> {
Expand Down Expand Up @@ -256,18 +256,18 @@ pub(crate) fn def_to_moniker(
let (name, repo, version) = match krate.origin(db) {
CrateOrigin::CratesIo { repo, name } => (
name.unwrap_or(krate.display_name(db)?.canonical_name().to_string()),
repo?,
krate.version(db)?,
repo,
krate.version(db),
),
CrateOrigin::Lang(lang) => (
krate.display_name(db)?.canonical_name().to_string(),
"https://github.com/rust-lang/rust/".to_string(),
match lang {
Some("https://github.com/rust-lang/rust/".to_string()),
Some(match lang {
LangCrateOrigin::Other => {
"https://github.com/rust-lang/rust/library/".into()
}
lang => format!("https://github.com/rust-lang/rust/library/{lang}",),
},
}),
),
};
PackageInformation { name, repo, version }
Expand Down Expand Up @@ -315,7 +315,7 @@ pub mod module {
}
"#,
"foo::module::func",
r#"PackageInformation { name: "foo", repo: "https://a.b/foo.git", version: "0.1.0" }"#,
r#"PackageInformation { name: "foo", repo: Some("https://a.b/foo.git"), version: Some("0.1.0") }"#,
MonikerKind::Import,
);
check_moniker(
Expand All @@ -331,7 +331,7 @@ pub mod module {
}
"#,
"foo::module::func",
r#"PackageInformation { name: "foo", repo: "https://a.b/foo.git", version: "0.1.0" }"#,
r#"PackageInformation { name: "foo", repo: Some("https://a.b/foo.git"), version: Some("0.1.0") }"#,
MonikerKind::Export,
);
}
Expand All @@ -348,7 +348,7 @@ pub mod module {
}
"#,
"foo::module::MyTrait::func",
r#"PackageInformation { name: "foo", repo: "https://a.b/foo.git", version: "0.1.0" }"#,
r#"PackageInformation { name: "foo", repo: Some("https://a.b/foo.git"), version: Some("0.1.0") }"#,
MonikerKind::Export,
);
}
Expand All @@ -365,7 +365,7 @@ pub mod module {
}
"#,
"foo::module::MyTrait::MY_CONST",
r#"PackageInformation { name: "foo", repo: "https://a.b/foo.git", version: "0.1.0" }"#,
r#"PackageInformation { name: "foo", repo: Some("https://a.b/foo.git"), version: Some("0.1.0") }"#,
MonikerKind::Export,
);
}
Expand All @@ -382,7 +382,7 @@ pub mod module {
}
"#,
"foo::module::MyTrait::MyType",
r#"PackageInformation { name: "foo", repo: "https://a.b/foo.git", version: "0.1.0" }"#,
r#"PackageInformation { name: "foo", repo: Some("https://a.b/foo.git"), version: Some("0.1.0") }"#,
MonikerKind::Export,
);
}
Expand All @@ -405,7 +405,7 @@ pub mod module {
}
"#,
"foo::module::MyStruct::MyTrait::func",
r#"PackageInformation { name: "foo", repo: "https://a.b/foo.git", version: "0.1.0" }"#,
r#"PackageInformation { name: "foo", repo: Some("https://a.b/foo.git"), version: Some("0.1.0") }"#,
MonikerKind::Export,
);
}
Expand All @@ -425,7 +425,7 @@ pub struct St {
}
"#,
"foo::St::a",
r#"PackageInformation { name: "foo", repo: "https://a.b/foo.git", version: "0.1.0" }"#,
r#"PackageInformation { name: "foo", repo: Some("https://a.b/foo.git"), version: Some("0.1.0") }"#,
MonikerKind::Import,
);
}
Expand Down
6 changes: 3 additions & 3 deletions crates/rust-analyzer/src/cli/lsif.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,12 @@ impl LsifManager<'_> {
manager: "cargo".to_string(),
uri: None,
content: None,
repository: Some(lsif::Repository {
url: pi.repo,
repository: pi.repo.map(|url| lsif::Repository {
url,
r#type: "git".to_string(),
commit_id: None,
}),
version: Some(pi.version),
version: pi.version,
}));
self.package_map.insert(package_information, result_set_id);
result_set_id
Expand Down
40 changes: 39 additions & 1 deletion crates/rust-analyzer/src/cli/scip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ fn token_to_symbol(token: &TokenStaticData) -> Option<scip_types::Symbol> {
package: Some(scip_types::Package {
manager: "cargo".to_string(),
name: package_name,
version,
version: version.unwrap_or_else(|| ".".to_string()),
..Default::default()
})
.into(),
Expand Down Expand Up @@ -415,4 +415,42 @@ pub mod module {
"",
);
}

#[test]
fn global_symbol_for_pub_struct() {
check_symbol(
r#"
//- /lib.rs crate:main
mod foo;
fn main() {
let _bar = foo::Bar { i: 0 };
}
//- /foo.rs
pub struct Bar$0 {
pub i: i32,
}
"#,
"rust-analyzer cargo main . foo/Bar#",
);
}

#[test]
fn global_symbol_for_pub_struct_reference() {
check_symbol(
r#"
//- /lib.rs crate:main
mod foo;
fn main() {
let _bar = foo::Bar$0 { i: 0 };
}
//- /foo.rs
pub struct Bar {
pub i: i32,
}
"#,
"rust-analyzer cargo main . foo/Bar#",
);
}
}

0 comments on commit bbcb77e

Please sign in to comment.