mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-02 22:54:58 +00:00
Add ExternCrateDecl to HIR
This commit is contained in:
parent
151c750dac
commit
bcff166b3a
39 changed files with 585 additions and 167 deletions
|
@ -7,10 +7,10 @@
|
|||
|
||||
use arrayvec::ArrayVec;
|
||||
use hir::{
|
||||
Adt, AsAssocItem, AssocItem, BuiltinAttr, BuiltinType, Const, Crate, DeriveHelper, Field,
|
||||
Function, GenericParam, HasVisibility, Impl, Label, Local, Macro, Module, ModuleDef, Name,
|
||||
PathResolution, Semantics, Static, ToolModule, Trait, TraitAlias, TypeAlias, Variant,
|
||||
Visibility,
|
||||
Adt, AsAssocItem, AssocItem, BuiltinAttr, BuiltinType, Const, Crate, DeriveHelper,
|
||||
ExternCrateDecl, Field, Function, GenericParam, HasVisibility, Impl, Label, Local, Macro,
|
||||
Module, ModuleDef, Name, PathResolution, Semantics, Static, ToolModule, Trait, TraitAlias,
|
||||
TypeAlias, Variant, Visibility,
|
||||
};
|
||||
use stdx::impl_from;
|
||||
use syntax::{
|
||||
|
@ -42,6 +42,7 @@ pub enum Definition {
|
|||
DeriveHelper(DeriveHelper),
|
||||
BuiltinAttr(BuiltinAttr),
|
||||
ToolModule(ToolModule),
|
||||
ExternCrateDecl(ExternCrateDecl),
|
||||
}
|
||||
|
||||
impl Definition {
|
||||
|
@ -73,6 +74,7 @@ impl Definition {
|
|||
Definition::Local(it) => it.module(db),
|
||||
Definition::GenericParam(it) => it.module(db),
|
||||
Definition::Label(it) => it.module(db),
|
||||
Definition::ExternCrateDecl(it) => it.module(db),
|
||||
Definition::DeriveHelper(it) => it.derive().module(db),
|
||||
Definition::BuiltinAttr(_) | Definition::BuiltinType(_) | Definition::ToolModule(_) => {
|
||||
return None
|
||||
|
@ -93,6 +95,7 @@ impl Definition {
|
|||
Definition::TraitAlias(it) => it.visibility(db),
|
||||
Definition::TypeAlias(it) => it.visibility(db),
|
||||
Definition::Variant(it) => it.visibility(db),
|
||||
Definition::ExternCrateDecl(it) => it.visibility(db),
|
||||
Definition::BuiltinType(_) => Visibility::Public,
|
||||
Definition::Macro(_) => return None,
|
||||
Definition::BuiltinAttr(_)
|
||||
|
@ -127,6 +130,7 @@ impl Definition {
|
|||
Definition::BuiltinAttr(_) => return None, // FIXME
|
||||
Definition::ToolModule(_) => return None, // FIXME
|
||||
Definition::DeriveHelper(it) => it.name(db),
|
||||
Definition::ExternCrateDecl(it) => return it.alias_or_name(db),
|
||||
};
|
||||
Some(name)
|
||||
}
|
||||
|
@ -196,6 +200,10 @@ impl IdentClass {
|
|||
res.push(Definition::Local(local_ref));
|
||||
res.push(Definition::Field(field_ref));
|
||||
}
|
||||
IdentClass::NameRefClass(NameRefClass::ExternCrateShorthand { decl, krate }) => {
|
||||
res.push(Definition::ExternCrateDecl(decl));
|
||||
res.push(Definition::Module(krate.root_module()));
|
||||
}
|
||||
IdentClass::Operator(
|
||||
OperatorClass::Await(func)
|
||||
| OperatorClass::Prefix(func)
|
||||
|
@ -222,6 +230,10 @@ impl IdentClass {
|
|||
res.push(Definition::Local(local_ref));
|
||||
res.push(Definition::Field(field_ref));
|
||||
}
|
||||
IdentClass::NameRefClass(NameRefClass::ExternCrateShorthand { decl, krate }) => {
|
||||
res.push(Definition::ExternCrateDecl(decl));
|
||||
res.push(Definition::Module(krate.root_module()));
|
||||
}
|
||||
IdentClass::Operator(_) => (),
|
||||
}
|
||||
res
|
||||
|
@ -310,6 +322,7 @@ impl NameClass {
|
|||
ast::Item::Enum(it) => Definition::Adt(hir::Adt::Enum(sema.to_def(&it)?)),
|
||||
ast::Item::Struct(it) => Definition::Adt(hir::Adt::Struct(sema.to_def(&it)?)),
|
||||
ast::Item::Union(it) => Definition::Adt(hir::Adt::Union(sema.to_def(&it)?)),
|
||||
ast::Item::ExternCrate(it) => Definition::ExternCrateDecl(sema.to_def(&it)?),
|
||||
_ => return None,
|
||||
};
|
||||
Some(definition)
|
||||
|
@ -346,10 +359,8 @@ impl NameClass {
|
|||
let path = use_tree.path()?;
|
||||
sema.resolve_path(&path).map(Definition::from)
|
||||
} else {
|
||||
let extern_crate = rename.syntax().parent().and_then(ast::ExternCrate::cast)?;
|
||||
let krate = sema.resolve_extern_crate(&extern_crate)?;
|
||||
let root_module = krate.root_module(sema.db);
|
||||
Some(Definition::Module(root_module))
|
||||
sema.to_def(&rename.syntax().parent().and_then(ast::ExternCrate::cast)?)
|
||||
.map(Definition::ExternCrateDecl)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -427,7 +438,19 @@ impl OperatorClass {
|
|||
#[derive(Debug)]
|
||||
pub enum NameRefClass {
|
||||
Definition(Definition),
|
||||
FieldShorthand { local_ref: Local, field_ref: Field },
|
||||
FieldShorthand {
|
||||
local_ref: Local,
|
||||
field_ref: Field,
|
||||
},
|
||||
/// The specific situation where we have an extern crate decl without a rename
|
||||
/// Here we have both a declaration and a reference.
|
||||
/// ```rs
|
||||
/// extern crate foo;
|
||||
/// ```
|
||||
ExternCrateShorthand {
|
||||
decl: ExternCrateDecl,
|
||||
krate: Crate,
|
||||
},
|
||||
}
|
||||
|
||||
impl NameRefClass {
|
||||
|
@ -513,10 +536,14 @@ impl NameRefClass {
|
|||
}
|
||||
None
|
||||
},
|
||||
ast::ExternCrate(extern_crate) => {
|
||||
let krate = sema.resolve_extern_crate(&extern_crate)?;
|
||||
let root_module = krate.root_module(sema.db);
|
||||
Some(NameRefClass::Definition(Definition::Module(root_module)))
|
||||
ast::ExternCrate(extern_crate_ast) => {
|
||||
let extern_crate = sema.to_def(&extern_crate_ast)?;
|
||||
let krate = extern_crate.resolved_crate(sema.db);
|
||||
Some(if extern_crate_ast.rename().is_some() {
|
||||
NameRefClass::Definition(Definition::Module(krate.root_module()))
|
||||
} else {
|
||||
NameRefClass::ExternCrateShorthand { krate, decl: extern_crate }
|
||||
})
|
||||
},
|
||||
_ => None
|
||||
}
|
||||
|
|
|
@ -167,7 +167,7 @@ impl FamousDefs<'_, '_> {
|
|||
lang_crate => lang_crate,
|
||||
};
|
||||
let std_crate = self.find_lang_crate(lang_crate)?;
|
||||
let mut module = std_crate.root_module(db);
|
||||
let mut module = std_crate.root_module();
|
||||
for segment in path {
|
||||
module = module.children(db).find_map(|child| {
|
||||
let name = child.name(db)?;
|
||||
|
|
|
@ -82,8 +82,9 @@ impl Definition {
|
|||
}
|
||||
|
||||
/// Textual range of the identifier which will change when renaming this
|
||||
/// `Definition`. Note that some definitions, like builtin types, can't be
|
||||
/// renamed.
|
||||
/// `Definition`. Note that builtin types can't be
|
||||
/// renamed and extern crate names will report its range, though a rename will introduce
|
||||
/// an alias instead.
|
||||
pub fn range_for_rename(self, sema: &Semantics<'_, RootDatabase>) -> Option<FileRange> {
|
||||
let res = match self {
|
||||
Definition::Macro(mac) => {
|
||||
|
@ -146,6 +147,16 @@ impl Definition {
|
|||
let lifetime = src.value.lifetime()?;
|
||||
src.with_value(lifetime.syntax()).original_file_range_opt(sema.db)
|
||||
}
|
||||
Definition::ExternCrateDecl(it) => {
|
||||
let src = it.source(sema.db)?;
|
||||
if let Some(rename) = src.value.rename() {
|
||||
let name = rename.name()?;
|
||||
src.with_value(name.syntax()).original_file_range_opt(sema.db)
|
||||
} else {
|
||||
let name = src.value.name_ref()?;
|
||||
src.with_value(name.syntax()).original_file_range_opt(sema.db)
|
||||
}
|
||||
}
|
||||
Definition::BuiltinType(_) => return None,
|
||||
Definition::SelfType(_) => return None,
|
||||
Definition::BuiltinAttr(_) => return None,
|
||||
|
@ -526,6 +537,9 @@ fn source_edit_from_def(
|
|||
TextRange::new(range.start() + syntax::TextSize::from(1), range.end()),
|
||||
new_name.strip_prefix('\'').unwrap_or(new_name).to_owned(),
|
||||
),
|
||||
Definition::ExternCrateDecl(decl) if decl.alias(sema.db).is_none() => {
|
||||
(TextRange::empty(range.end()), format!(" as {new_name}"))
|
||||
}
|
||||
_ => (range, new_name.to_owned()),
|
||||
};
|
||||
edit.replace(range, new_name);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue