2021-11-22 11:44:46 -06:00
|
|
|
//! This module generates [moniker](https://microsoft.github.io/language-server-protocol/specifications/lsif/0.6.0/specification/#exportsImports)
|
|
|
|
//! for LSIF and LSP.
|
|
|
|
|
2021-11-30 10:16:11 -06:00
|
|
|
use hir::{db::DefDatabase, AsAssocItem, AssocItemContainer, Crate, Name, Semantics};
|
2021-11-22 11:44:46 -06:00
|
|
|
use ide_db::{
|
2022-03-26 15:22:35 -05:00
|
|
|
base_db::{CrateOrigin, FileId, FileLoader, FilePosition, LangCrateOrigin},
|
2022-01-04 11:29:20 -06:00
|
|
|
defs::{Definition, IdentClass},
|
2021-11-22 11:44:46 -06:00
|
|
|
helpers::pick_best_token,
|
|
|
|
RootDatabase,
|
|
|
|
};
|
|
|
|
use itertools::Itertools;
|
|
|
|
use syntax::{AstNode, SyntaxKind::*, T};
|
|
|
|
|
|
|
|
use crate::{doc_links::token_as_doc_comment, RangeInfo};
|
|
|
|
|
2022-06-10 20:29:04 -05:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
|
|
|
pub enum MonikerDescriptorKind {
|
|
|
|
Namespace,
|
|
|
|
Type,
|
|
|
|
Term,
|
|
|
|
Method,
|
|
|
|
TypeParameter,
|
|
|
|
Parameter,
|
|
|
|
Macro,
|
|
|
|
Meta,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
|
|
|
pub struct MonikerDescriptor {
|
|
|
|
pub name: Name,
|
|
|
|
pub desc: MonikerDescriptorKind,
|
|
|
|
}
|
|
|
|
|
2021-11-22 11:44:46 -06:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
|
|
|
pub struct MonikerIdentifier {
|
2022-06-10 20:29:04 -05:00
|
|
|
pub crate_name: String,
|
|
|
|
pub description: Vec<MonikerDescriptor>,
|
2021-11-22 11:44:46 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ToString for MonikerIdentifier {
|
|
|
|
fn to_string(&self) -> String {
|
|
|
|
match self {
|
2022-06-10 20:29:04 -05:00
|
|
|
MonikerIdentifier { description, crate_name } => {
|
|
|
|
format!(
|
|
|
|
"{}::{}",
|
|
|
|
crate_name,
|
|
|
|
description.iter().map(|x| x.name.to_string()).join("::")
|
|
|
|
)
|
2021-11-22 11:44:46 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
|
|
|
pub enum MonikerKind {
|
|
|
|
Import,
|
|
|
|
Export,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
|
|
|
pub struct MonikerResult {
|
|
|
|
pub identifier: MonikerIdentifier,
|
|
|
|
pub kind: MonikerKind,
|
|
|
|
pub package_information: PackageInformation,
|
|
|
|
}
|
|
|
|
|
2022-06-10 20:29:04 -05:00
|
|
|
impl MonikerResult {
|
|
|
|
pub fn from_def(db: &RootDatabase, def: Definition, from_crate: Crate) -> Option<Self> {
|
|
|
|
def_to_moniker(db, def, from_crate)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-22 11:44:46 -06:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
|
|
|
pub struct PackageInformation {
|
|
|
|
pub name: String,
|
|
|
|
pub repo: String,
|
|
|
|
pub version: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn crate_for_file(db: &RootDatabase, file_id: FileId) -> Option<Crate> {
|
|
|
|
for &krate in db.relevant_crates(file_id).iter() {
|
|
|
|
let crate_def_map = db.crate_def_map(krate);
|
|
|
|
for (_, data) in crate_def_map.modules() {
|
|
|
|
if data.origin.file_id() == Some(file_id) {
|
|
|
|
return Some(krate.into());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn moniker(
|
|
|
|
db: &RootDatabase,
|
|
|
|
FilePosition { file_id, offset }: FilePosition,
|
|
|
|
) -> Option<RangeInfo<Vec<MonikerResult>>> {
|
|
|
|
let sema = &Semantics::new(db);
|
|
|
|
let file = sema.parse(file_id).syntax().clone();
|
|
|
|
let current_crate = crate_for_file(db, file_id)?;
|
|
|
|
let original_token = pick_best_token(file.token_at_offset(offset), |kind| match kind {
|
2022-03-05 16:20:06 -06:00
|
|
|
IDENT
|
|
|
|
| INT_NUMBER
|
|
|
|
| LIFETIME_IDENT
|
|
|
|
| T![self]
|
|
|
|
| T![super]
|
|
|
|
| T![crate]
|
|
|
|
| T![Self]
|
|
|
|
| COMMENT => 2,
|
2021-11-22 11:44:46 -06:00
|
|
|
kind if kind.is_trivia() => 0,
|
|
|
|
_ => 1,
|
|
|
|
})?;
|
|
|
|
if let Some(doc_comment) = token_as_doc_comment(&original_token) {
|
|
|
|
return doc_comment.get_definition_with_descend_at(sema, offset, |def, _, _| {
|
|
|
|
let m = def_to_moniker(db, def, current_crate)?;
|
|
|
|
Some(RangeInfo::new(original_token.text_range(), vec![m]))
|
|
|
|
});
|
|
|
|
}
|
|
|
|
let navs = sema
|
|
|
|
.descend_into_macros(original_token.clone())
|
|
|
|
.into_iter()
|
2022-01-04 11:29:20 -06:00
|
|
|
.filter_map(|token| {
|
2022-08-05 07:16:36 -05:00
|
|
|
IdentClass::classify_token(sema, &token).map(IdentClass::definitions_no_ops).map(|it| {
|
2022-01-04 11:29:20 -06:00
|
|
|
it.into_iter().flat_map(|def| def_to_moniker(sema.db, def, current_crate))
|
|
|
|
})
|
2021-11-22 11:44:46 -06:00
|
|
|
})
|
|
|
|
.flatten()
|
|
|
|
.unique()
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
Some(RangeInfo::new(original_token.text_range(), navs))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn def_to_moniker(
|
|
|
|
db: &RootDatabase,
|
|
|
|
def: Definition,
|
|
|
|
from_crate: Crate,
|
|
|
|
) -> Option<MonikerResult> {
|
2022-06-10 20:29:04 -05:00
|
|
|
if matches!(
|
|
|
|
def,
|
|
|
|
Definition::GenericParam(_)
|
|
|
|
| Definition::Label(_)
|
|
|
|
| Definition::DeriveHelper(_)
|
|
|
|
| Definition::BuiltinAttr(_)
|
|
|
|
| Definition::ToolModule(_)
|
|
|
|
) {
|
2021-11-22 11:44:46 -06:00
|
|
|
return None;
|
|
|
|
}
|
2022-06-10 20:29:04 -05:00
|
|
|
|
2021-11-22 11:44:46 -06:00
|
|
|
let module = def.module(db)?;
|
|
|
|
let krate = module.krate();
|
2022-06-10 20:29:04 -05:00
|
|
|
let mut description = vec![];
|
|
|
|
description.extend(module.path_to_root(db).into_iter().filter_map(|x| {
|
|
|
|
Some(MonikerDescriptor { name: x.name(db)?, desc: MonikerDescriptorKind::Namespace })
|
|
|
|
}));
|
2021-11-30 10:16:11 -06:00
|
|
|
|
2021-12-01 10:43:52 -06:00
|
|
|
// Handle associated items within a trait
|
|
|
|
if let Some(assoc) = def.as_assoc_item(db) {
|
|
|
|
let container = assoc.container(db);
|
2021-12-01 23:04:03 -06:00
|
|
|
match container {
|
|
|
|
AssocItemContainer::Trait(trait_) => {
|
|
|
|
// Because different traits can have functions with the same name,
|
|
|
|
// we have to include the trait name as part of the moniker for uniqueness.
|
2022-06-10 20:29:04 -05:00
|
|
|
description.push(MonikerDescriptor {
|
|
|
|
name: trait_.name(db),
|
|
|
|
desc: MonikerDescriptorKind::Type,
|
|
|
|
});
|
2021-12-01 23:04:03 -06:00
|
|
|
}
|
|
|
|
AssocItemContainer::Impl(impl_) => {
|
|
|
|
// Because a struct can implement multiple traits, for implementations
|
|
|
|
// we add both the struct name and the trait name to the path
|
|
|
|
if let Some(adt) = impl_.self_ty(db).as_adt() {
|
2022-06-10 20:29:04 -05:00
|
|
|
description.push(MonikerDescriptor {
|
|
|
|
name: adt.name(db),
|
|
|
|
desc: MonikerDescriptorKind::Type,
|
|
|
|
});
|
2021-12-01 23:04:03 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(trait_) = impl_.trait_(db) {
|
2022-06-10 20:29:04 -05:00
|
|
|
description.push(MonikerDescriptor {
|
|
|
|
name: trait_.name(db),
|
|
|
|
desc: MonikerDescriptorKind::Type,
|
|
|
|
});
|
2021-12-01 23:04:03 -06:00
|
|
|
}
|
|
|
|
}
|
2021-11-30 10:16:11 -06:00
|
|
|
}
|
2021-12-01 10:43:52 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if let Definition::Field(it) = def {
|
2022-06-10 20:29:04 -05:00
|
|
|
description.push(MonikerDescriptor {
|
|
|
|
name: it.parent_def(db).name(db),
|
|
|
|
desc: MonikerDescriptorKind::Type,
|
|
|
|
});
|
2021-11-22 11:44:46 -06:00
|
|
|
}
|
2021-11-30 10:16:11 -06:00
|
|
|
|
2022-06-10 20:29:04 -05:00
|
|
|
let name_desc = match def {
|
|
|
|
// These are handled by top-level guard (for performance).
|
|
|
|
Definition::GenericParam(_)
|
|
|
|
| Definition::Label(_)
|
|
|
|
| Definition::DeriveHelper(_)
|
|
|
|
| Definition::BuiltinAttr(_)
|
|
|
|
| Definition::ToolModule(_) => return None,
|
|
|
|
|
|
|
|
Definition::Local(local) => {
|
|
|
|
if !local.is_param(db) {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
MonikerDescriptor { name: local.name(db), desc: MonikerDescriptorKind::Parameter }
|
|
|
|
}
|
|
|
|
Definition::Macro(m) => {
|
|
|
|
MonikerDescriptor { name: m.name(db), desc: MonikerDescriptorKind::Macro }
|
|
|
|
}
|
|
|
|
Definition::Function(f) => {
|
|
|
|
MonikerDescriptor { name: f.name(db), desc: MonikerDescriptorKind::Method }
|
|
|
|
}
|
|
|
|
Definition::Variant(v) => {
|
|
|
|
MonikerDescriptor { name: v.name(db), desc: MonikerDescriptorKind::Type }
|
|
|
|
}
|
|
|
|
Definition::Const(c) => {
|
|
|
|
MonikerDescriptor { name: c.name(db)?, desc: MonikerDescriptorKind::Term }
|
|
|
|
}
|
|
|
|
Definition::Trait(trait_) => {
|
|
|
|
MonikerDescriptor { name: trait_.name(db), desc: MonikerDescriptorKind::Type }
|
|
|
|
}
|
|
|
|
Definition::TypeAlias(ta) => {
|
|
|
|
MonikerDescriptor { name: ta.name(db), desc: MonikerDescriptorKind::TypeParameter }
|
|
|
|
}
|
|
|
|
Definition::Module(m) => {
|
|
|
|
MonikerDescriptor { name: m.name(db)?, desc: MonikerDescriptorKind::Namespace }
|
|
|
|
}
|
|
|
|
Definition::BuiltinType(b) => {
|
|
|
|
MonikerDescriptor { name: b.name(), desc: MonikerDescriptorKind::Type }
|
|
|
|
}
|
|
|
|
Definition::SelfType(imp) => MonikerDescriptor {
|
|
|
|
name: imp.self_ty(db).as_adt()?.name(db),
|
|
|
|
desc: MonikerDescriptorKind::Type,
|
|
|
|
},
|
|
|
|
Definition::Field(it) => {
|
|
|
|
MonikerDescriptor { name: it.name(db), desc: MonikerDescriptorKind::Term }
|
|
|
|
}
|
|
|
|
Definition::Adt(adt) => {
|
|
|
|
MonikerDescriptor { name: adt.name(db), desc: MonikerDescriptorKind::Type }
|
|
|
|
}
|
|
|
|
Definition::Static(s) => {
|
|
|
|
MonikerDescriptor { name: s.name(db), desc: MonikerDescriptorKind::Meta }
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
description.push(name_desc);
|
|
|
|
|
2021-11-22 11:44:46 -06:00
|
|
|
Some(MonikerResult {
|
|
|
|
identifier: MonikerIdentifier {
|
|
|
|
crate_name: krate.display_name(db)?.crate_name().to_string(),
|
2022-06-10 20:29:04 -05:00
|
|
|
description,
|
2021-11-22 11:44:46 -06:00
|
|
|
},
|
|
|
|
kind: if krate == from_crate { MonikerKind::Export } else { MonikerKind::Import },
|
|
|
|
package_information: {
|
2021-11-29 01:40:39 -06:00
|
|
|
let name = krate.display_name(db)?.to_string();
|
|
|
|
let (repo, version) = match krate.origin(db) {
|
|
|
|
CrateOrigin::CratesIo { repo } => (repo?, krate.version(db)?),
|
2022-03-26 15:22:35 -05:00
|
|
|
CrateOrigin::Lang(lang) => (
|
2021-11-22 11:44:46 -06:00
|
|
|
"https://github.com/rust-lang/rust/".to_string(),
|
2022-04-10 05:42:16 -05:00
|
|
|
match lang {
|
|
|
|
LangCrateOrigin::Other => {
|
|
|
|
"https://github.com/rust-lang/rust/library/".into()
|
2022-03-26 15:22:35 -05:00
|
|
|
}
|
2022-04-10 05:42:16 -05:00
|
|
|
lang => format!("https://github.com/rust-lang/rust/library/{lang}",),
|
|
|
|
},
|
2021-11-22 11:44:46 -06:00
|
|
|
),
|
|
|
|
};
|
|
|
|
PackageInformation { name, repo, version }
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use crate::fixture;
|
|
|
|
|
|
|
|
use super::MonikerKind;
|
|
|
|
|
|
|
|
#[track_caller]
|
|
|
|
fn no_moniker(ra_fixture: &str) {
|
|
|
|
let (analysis, position) = fixture::position(ra_fixture);
|
|
|
|
if let Some(x) = analysis.moniker(position).unwrap() {
|
|
|
|
assert_eq!(x.info.len(), 0, "Moniker founded but no moniker expected: {:?}", x);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[track_caller]
|
|
|
|
fn check_moniker(ra_fixture: &str, identifier: &str, package: &str, kind: MonikerKind) {
|
|
|
|
let (analysis, position) = fixture::position(ra_fixture);
|
|
|
|
let x = analysis.moniker(position).unwrap().expect("no moniker found").info;
|
|
|
|
assert_eq!(x.len(), 1);
|
|
|
|
let x = x.into_iter().next().unwrap();
|
|
|
|
assert_eq!(identifier, x.identifier.to_string());
|
|
|
|
assert_eq!(package, format!("{:?}", x.package_information));
|
|
|
|
assert_eq!(kind, x.kind);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn basic() {
|
|
|
|
check_moniker(
|
|
|
|
r#"
|
|
|
|
//- /lib.rs crate:main deps:foo
|
|
|
|
use foo::module::func;
|
|
|
|
fn main() {
|
|
|
|
func$0();
|
|
|
|
}
|
|
|
|
//- /foo/lib.rs crate:foo@CratesIo:0.1.0,https://a.b/foo.git
|
|
|
|
pub mod module {
|
|
|
|
pub fn func() {}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
"foo::module::func",
|
|
|
|
r#"PackageInformation { name: "foo", repo: "https://a.b/foo.git", version: "0.1.0" }"#,
|
|
|
|
MonikerKind::Import,
|
|
|
|
);
|
|
|
|
check_moniker(
|
|
|
|
r#"
|
|
|
|
//- /lib.rs crate:main deps:foo
|
|
|
|
use foo::module::func;
|
|
|
|
fn main() {
|
|
|
|
func();
|
|
|
|
}
|
|
|
|
//- /foo/lib.rs crate:foo@CratesIo:0.1.0,https://a.b/foo.git
|
2021-11-30 10:16:11 -06:00
|
|
|
pub mod module {
|
|
|
|
pub fn func$0() {}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
"foo::module::func",
|
|
|
|
r#"PackageInformation { name: "foo", repo: "https://a.b/foo.git", version: "0.1.0" }"#,
|
|
|
|
MonikerKind::Export,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn moniker_for_trait() {
|
|
|
|
check_moniker(
|
|
|
|
r#"
|
|
|
|
//- /foo/lib.rs crate:foo@CratesIo:0.1.0,https://a.b/foo.git
|
|
|
|
pub mod module {
|
|
|
|
pub trait MyTrait {
|
|
|
|
pub fn func$0() {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
"foo::module::MyTrait::func",
|
|
|
|
r#"PackageInformation { name: "foo", repo: "https://a.b/foo.git", version: "0.1.0" }"#,
|
|
|
|
MonikerKind::Export,
|
|
|
|
);
|
2021-12-01 10:43:52 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn moniker_for_trait_constant() {
|
2021-11-30 10:16:11 -06:00
|
|
|
check_moniker(
|
|
|
|
r#"
|
2021-12-01 10:43:52 -06:00
|
|
|
//- /foo/lib.rs crate:foo@CratesIo:0.1.0,https://a.b/foo.git
|
|
|
|
pub mod module {
|
|
|
|
pub trait MyTrait {
|
|
|
|
const MY_CONST$0: u8;
|
|
|
|
}
|
2021-11-30 10:16:11 -06:00
|
|
|
}
|
2021-12-01 10:43:52 -06:00
|
|
|
"#,
|
|
|
|
"foo::module::MyTrait::MY_CONST",
|
|
|
|
r#"PackageInformation { name: "foo", repo: "https://a.b/foo.git", version: "0.1.0" }"#,
|
|
|
|
MonikerKind::Export,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn moniker_for_trait_type() {
|
|
|
|
check_moniker(
|
|
|
|
r#"
|
2021-11-30 10:16:11 -06:00
|
|
|
//- /foo/lib.rs crate:foo@CratesIo:0.1.0,https://a.b/foo.git
|
2021-11-22 11:44:46 -06:00
|
|
|
pub mod module {
|
2021-12-01 10:43:52 -06:00
|
|
|
pub trait MyTrait {
|
|
|
|
type MyType$0;
|
|
|
|
}
|
2021-11-22 11:44:46 -06:00
|
|
|
}
|
|
|
|
"#,
|
2021-12-01 10:43:52 -06:00
|
|
|
"foo::module::MyTrait::MyType",
|
2021-11-22 11:44:46 -06:00
|
|
|
r#"PackageInformation { name: "foo", repo: "https://a.b/foo.git", version: "0.1.0" }"#,
|
|
|
|
MonikerKind::Export,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-12-01 23:04:03 -06:00
|
|
|
#[test]
|
|
|
|
fn moniker_for_trait_impl_function() {
|
|
|
|
check_moniker(
|
|
|
|
r#"
|
|
|
|
//- /foo/lib.rs crate:foo@CratesIo:0.1.0,https://a.b/foo.git
|
|
|
|
pub mod module {
|
|
|
|
pub trait MyTrait {
|
|
|
|
pub fn func() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct MyStruct {}
|
|
|
|
|
|
|
|
impl MyTrait for MyStruct {
|
|
|
|
pub fn func$0() {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
"foo::module::MyStruct::MyTrait::func",
|
|
|
|
r#"PackageInformation { name: "foo", repo: "https://a.b/foo.git", version: "0.1.0" }"#,
|
|
|
|
MonikerKind::Export,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-11-22 11:44:46 -06:00
|
|
|
#[test]
|
|
|
|
fn moniker_for_field() {
|
|
|
|
check_moniker(
|
|
|
|
r#"
|
|
|
|
//- /lib.rs crate:main deps:foo
|
|
|
|
use foo::St;
|
|
|
|
fn main() {
|
|
|
|
let x = St { a$0: 2 };
|
|
|
|
}
|
|
|
|
//- /foo/lib.rs crate:foo@CratesIo:0.1.0,https://a.b/foo.git
|
|
|
|
pub struct St {
|
|
|
|
pub a: i32,
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
"foo::St::a",
|
|
|
|
r#"PackageInformation { name: "foo", repo: "https://a.b/foo.git", version: "0.1.0" }"#,
|
|
|
|
MonikerKind::Import,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn no_moniker_for_local() {
|
|
|
|
no_moniker(
|
|
|
|
r#"
|
|
|
|
//- /lib.rs crate:main deps:foo
|
|
|
|
use foo::module::func;
|
|
|
|
fn main() {
|
|
|
|
func();
|
|
|
|
}
|
|
|
|
//- /foo/lib.rs crate:foo@CratesIo:0.1.0,https://a.b/foo.git
|
|
|
|
pub mod module {
|
|
|
|
pub fn func() {
|
|
|
|
let x$0 = 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|