mirror of
https://github.com/rust-lang/rust.git
synced 2025-05-14 02:49:40 +00:00
Remove imports from hir
This commit is contained in:
parent
973b5cf7e2
commit
4e0168ec14
@ -12,8 +12,8 @@ use hir_def::{
|
|||||||
resolver::HasResolver,
|
resolver::HasResolver,
|
||||||
type_ref::{Mutability, TypeRef},
|
type_ref::{Mutability, TypeRef},
|
||||||
AdtId, ConstId, DefWithBodyId, EnumId, FunctionId, HasModule, ImplId, LocalEnumVariantId,
|
AdtId, ConstId, DefWithBodyId, EnumId, FunctionId, HasModule, ImplId, LocalEnumVariantId,
|
||||||
LocalImportId, LocalModuleId, LocalStructFieldId, Lookup, ModuleId, StaticId, StructId,
|
LocalModuleId, LocalStructFieldId, Lookup, ModuleId, StaticId, StructId, TraitId, TypeAliasId,
|
||||||
TraitId, TypeAliasId, TypeParamId, UnionId,
|
TypeParamId, UnionId,
|
||||||
};
|
};
|
||||||
use hir_expand::{
|
use hir_expand::{
|
||||||
diagnostics::DiagnosticSink,
|
diagnostics::DiagnosticSink,
|
||||||
@ -180,13 +180,11 @@ impl Module {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Returns a `ModuleScope`: a set of items, visible in this module.
|
/// Returns a `ModuleScope`: a set of items, visible in this module.
|
||||||
pub fn scope(self, db: &impl HirDatabase) -> Vec<(Name, ScopeDef, Option<Import>)> {
|
pub fn scope(self, db: &impl HirDatabase) -> Vec<(Name, ScopeDef)> {
|
||||||
db.crate_def_map(self.id.krate)[self.id.local_id]
|
db.crate_def_map(self.id.krate)[self.id.local_id]
|
||||||
.scope
|
.scope
|
||||||
.entries()
|
.entries()
|
||||||
.map(|(name, res)| {
|
.map(|(name, res)| (name.clone(), res.def.into()))
|
||||||
(name.clone(), res.def.into(), res.import.map(|id| Import { parent: self, id }))
|
|
||||||
})
|
|
||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -229,11 +227,6 @@ impl Module {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Import {
|
|
||||||
pub(crate) parent: Module,
|
|
||||||
pub(crate) id: LocalImportId,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||||
pub struct StructField {
|
pub struct StructField {
|
||||||
pub(crate) parent: VariantDef,
|
pub(crate) parent: VariantDef,
|
||||||
|
@ -9,8 +9,8 @@ use hir_def::{
|
|||||||
use ra_syntax::ast;
|
use ra_syntax::ast;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
db::DefDatabase, Const, Enum, EnumVariant, FieldSource, Function, ImplBlock, Import, MacroDef,
|
db::DefDatabase, Const, Enum, EnumVariant, FieldSource, Function, ImplBlock, MacroDef, Module,
|
||||||
Module, Static, Struct, StructField, Trait, TypeAlias, TypeParam, Union,
|
Static, Struct, StructField, Trait, TypeAlias, TypeParam, Union,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub use hir_expand::InFile;
|
pub use hir_expand::InFile;
|
||||||
@ -117,18 +117,6 @@ impl HasSource for ImplBlock {
|
|||||||
self.id.lookup(db).source(db)
|
self.id.lookup(db).source(db)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl HasSource for Import {
|
|
||||||
type Ast = Either<ast::UseTree, ast::ExternCrateItem>;
|
|
||||||
|
|
||||||
/// Returns the syntax of the last path segment corresponding to this import
|
|
||||||
fn source(self, db: &impl DefDatabase) -> InFile<Self::Ast> {
|
|
||||||
let src = self.parent.definition_source(db);
|
|
||||||
let (_, source_map) = db.raw_items_with_source_map(src.file_id);
|
|
||||||
let root = db.parse_or_expand(src.file_id).unwrap();
|
|
||||||
let ptr = source_map.get(self.id);
|
|
||||||
src.with_value(ptr.map_left(|it| it.to_node(&root)).map_right(|it| it.to_node(&root)))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl HasSource for TypeParam {
|
impl HasSource for TypeParam {
|
||||||
type Ast = Either<ast::TraitDef, ast::TypeParam>;
|
type Ast = Either<ast::TraitDef, ast::TypeParam>;
|
||||||
|
@ -40,8 +40,8 @@ mod from_source;
|
|||||||
pub use crate::{
|
pub use crate::{
|
||||||
code_model::{
|
code_model::{
|
||||||
Adt, AssocItem, AttrDef, Const, Crate, CrateDependency, DefWithBody, Docs, Enum,
|
Adt, AssocItem, AttrDef, Const, Crate, CrateDependency, DefWithBody, Docs, Enum,
|
||||||
EnumVariant, FieldSource, Function, GenericDef, HasAttrs, ImplBlock, Import, Local,
|
EnumVariant, FieldSource, Function, GenericDef, HasAttrs, ImplBlock, Local, MacroDef,
|
||||||
MacroDef, Module, ModuleDef, ScopeDef, Static, Struct, StructField, Trait, Type, TypeAlias,
|
Module, ModuleDef, ScopeDef, Static, Struct, StructField, Trait, Type, TypeAlias,
|
||||||
TypeParam, Union, VariantDef,
|
TypeParam, Union, VariantDef,
|
||||||
},
|
},
|
||||||
from_source::FromSource,
|
from_source::FromSource,
|
||||||
|
@ -161,5 +161,5 @@ pub struct Resolution {
|
|||||||
/// None for unresolved
|
/// None for unresolved
|
||||||
pub def: PerNs,
|
pub def: PerNs,
|
||||||
/// ident by which this is imported into local scope.
|
/// ident by which this is imported into local scope.
|
||||||
pub import: Option<LocalImportId>,
|
pub(crate) import: Option<LocalImportId>,
|
||||||
}
|
}
|
||||||
|
@ -52,7 +52,7 @@ use crate::body::Expander;
|
|||||||
use crate::builtin_type::BuiltinType;
|
use crate::builtin_type::BuiltinType;
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||||
pub struct LocalImportId(RawId);
|
pub(crate) struct LocalImportId(RawId);
|
||||||
impl_arena_id!(LocalImportId);
|
impl_arena_id!(LocalImportId);
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||||
|
@ -48,12 +48,6 @@ pub struct ImportSourceMap {
|
|||||||
|
|
||||||
type ImportSourcePtr = Either<AstPtr<ast::UseTree>, AstPtr<ast::ExternCrateItem>>;
|
type ImportSourcePtr = Either<AstPtr<ast::UseTree>, AstPtr<ast::ExternCrateItem>>;
|
||||||
|
|
||||||
impl ImportSourceMap {
|
|
||||||
pub fn get(&self, import: LocalImportId) -> ImportSourcePtr {
|
|
||||||
self.map[import].clone()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl RawItems {
|
impl RawItems {
|
||||||
pub(crate) fn raw_items_query(
|
pub(crate) fn raw_items_query(
|
||||||
db: &(impl DefDatabase + AstDatabase),
|
db: &(impl DefDatabase + AstDatabase),
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
//! FIXME: write short doc here
|
//! FIXME: write short doc here
|
||||||
|
|
||||||
use either::Either;
|
use hir::{Adt, PathResolution, ScopeDef};
|
||||||
use hir::{Adt, HasSource, PathResolution};
|
|
||||||
use ra_syntax::AstNode;
|
use ra_syntax::AstNode;
|
||||||
use test_utils::tested_by;
|
use test_utils::tested_by;
|
||||||
|
|
||||||
@ -19,17 +18,15 @@ pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) {
|
|||||||
match def {
|
match def {
|
||||||
hir::ModuleDef::Module(module) => {
|
hir::ModuleDef::Module(module) => {
|
||||||
let module_scope = module.scope(ctx.db);
|
let module_scope = module.scope(ctx.db);
|
||||||
for (name, def, import) in module_scope {
|
for (name, def) in module_scope {
|
||||||
if let hir::ScopeDef::ModuleDef(hir::ModuleDef::BuiltinType(..)) = def {
|
if ctx.use_item_syntax.is_some() {
|
||||||
if ctx.use_item_syntax.is_some() {
|
if let hir::ScopeDef::ModuleDef(hir::ModuleDef::BuiltinType(..)) = def {
|
||||||
tested_by!(dont_complete_primitive_in_use);
|
tested_by!(dont_complete_primitive_in_use);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
if let ScopeDef::Unknown = def {
|
||||||
if Some(module) == ctx.module {
|
if let Some(name_ref) = ctx.name_ref_syntax.as_ref() {
|
||||||
if let Some(import) = import {
|
if &name_ref.syntax().text() == name.to_string().as_str() {
|
||||||
if let Either::Left(use_tree) = import.source(ctx.db).value {
|
|
||||||
if use_tree.syntax().text_range().contains_inclusive(ctx.offset) {
|
|
||||||
// for `use self::foo<|>`, don't suggest `foo` as a completion
|
// for `use self::foo<|>`, don't suggest `foo` as a completion
|
||||||
tested_by!(dont_complete_current_use);
|
tested_by!(dont_complete_current_use);
|
||||||
continue;
|
continue;
|
||||||
@ -37,6 +34,7 @@ pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
acc.add_resolution(ctx, name.to_string(), &def);
|
acc.add_resolution(ctx, name.to_string(), &def);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -19,6 +19,7 @@ pub(crate) struct CompletionContext<'a> {
|
|||||||
pub(super) offset: TextUnit,
|
pub(super) offset: TextUnit,
|
||||||
pub(super) token: SyntaxToken,
|
pub(super) token: SyntaxToken,
|
||||||
pub(super) module: Option<hir::Module>,
|
pub(super) module: Option<hir::Module>,
|
||||||
|
pub(super) name_ref_syntax: Option<ast::NameRef>,
|
||||||
pub(super) function_syntax: Option<ast::FnDef>,
|
pub(super) function_syntax: Option<ast::FnDef>,
|
||||||
pub(super) use_item_syntax: Option<ast::UseItem>,
|
pub(super) use_item_syntax: Option<ast::UseItem>,
|
||||||
pub(super) record_lit_syntax: Option<ast::RecordLit>,
|
pub(super) record_lit_syntax: Option<ast::RecordLit>,
|
||||||
@ -69,6 +70,7 @@ impl<'a> CompletionContext<'a> {
|
|||||||
token,
|
token,
|
||||||
offset: position.offset,
|
offset: position.offset,
|
||||||
module,
|
module,
|
||||||
|
name_ref_syntax: None,
|
||||||
function_syntax: None,
|
function_syntax: None,
|
||||||
use_item_syntax: None,
|
use_item_syntax: None,
|
||||||
record_lit_syntax: None,
|
record_lit_syntax: None,
|
||||||
@ -142,6 +144,8 @@ impl<'a> CompletionContext<'a> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn classify_name_ref(&mut self, original_file: SourceFile, name_ref: ast::NameRef) {
|
fn classify_name_ref(&mut self, original_file: SourceFile, name_ref: ast::NameRef) {
|
||||||
|
self.name_ref_syntax =
|
||||||
|
find_node_at_offset(original_file.syntax(), name_ref.syntax().text_range().start());
|
||||||
let name_range = name_ref.syntax().text_range();
|
let name_range = name_ref.syntax().text_range();
|
||||||
if name_ref.syntax().parent().and_then(ast::RecordField::cast).is_some() {
|
if name_ref.syntax().parent().and_then(ast::RecordField::cast).is_some() {
|
||||||
self.record_lit_syntax = find_node_at_offset(original_file.syntax(), self.offset);
|
self.record_lit_syntax = find_node_at_offset(original_file.syntax(), self.offset);
|
||||||
|
Loading…
Reference in New Issue
Block a user