mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-25 08:13:41 +00:00
Add ModuleSource::Block
This commit is contained in:
parent
7cbedc50bc
commit
38853459e3
@ -11,6 +11,7 @@ use hir_def::{
|
||||
builtin_type::BuiltinType,
|
||||
docs::Documentation,
|
||||
expr::{BindingAnnotation, Pat, PatId},
|
||||
nameres::ModuleSource,
|
||||
per_ns::PerNs,
|
||||
resolver::HasResolver,
|
||||
type_ref::{Mutability, TypeRef},
|
||||
@ -21,11 +22,11 @@ use hir_def::{
|
||||
use hir_expand::{
|
||||
diagnostics::DiagnosticSink,
|
||||
name::{self, AsName},
|
||||
AstId, MacroDefId,
|
||||
MacroDefId,
|
||||
};
|
||||
use hir_ty::expr::ExprValidator;
|
||||
use ra_db::{CrateId, Edition, FileId, FilePosition};
|
||||
use ra_syntax::{ast, AstNode, SyntaxNode};
|
||||
use ra_db::{CrateId, Edition};
|
||||
use ra_syntax::ast;
|
||||
|
||||
use crate::{
|
||||
db::{DefDatabase, HirDatabase},
|
||||
@ -79,64 +80,6 @@ impl Crate {
|
||||
}
|
||||
}
|
||||
|
||||
pub enum ModuleSource {
|
||||
SourceFile(ast::SourceFile),
|
||||
Module(ast::Module),
|
||||
}
|
||||
|
||||
impl ModuleSource {
|
||||
pub fn new(
|
||||
db: &impl DefDatabase,
|
||||
file_id: Option<FileId>,
|
||||
decl_id: Option<AstId<ast::Module>>,
|
||||
) -> ModuleSource {
|
||||
match (file_id, decl_id) {
|
||||
(Some(file_id), _) => {
|
||||
let source_file = db.parse(file_id).tree();
|
||||
ModuleSource::SourceFile(source_file)
|
||||
}
|
||||
(None, Some(item_id)) => {
|
||||
let module = item_id.to_node(db);
|
||||
assert!(module.item_list().is_some(), "expected inline module");
|
||||
ModuleSource::Module(module)
|
||||
}
|
||||
(None, None) => panic!(),
|
||||
}
|
||||
}
|
||||
|
||||
// FIXME: this methods do not belong here
|
||||
pub fn from_position(db: &impl DefDatabase, position: FilePosition) -> ModuleSource {
|
||||
let parse = db.parse(position.file_id);
|
||||
match &ra_syntax::algo::find_node_at_offset::<ast::Module>(
|
||||
parse.tree().syntax(),
|
||||
position.offset,
|
||||
) {
|
||||
Some(m) if !m.has_semi() => ModuleSource::Module(m.clone()),
|
||||
_ => {
|
||||
let source_file = parse.tree();
|
||||
ModuleSource::SourceFile(source_file)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn from_child_node(db: &impl DefDatabase, child: InFile<&SyntaxNode>) -> ModuleSource {
|
||||
if let Some(m) =
|
||||
child.value.ancestors().filter_map(ast::Module::cast).find(|it| !it.has_semi())
|
||||
{
|
||||
ModuleSource::Module(m)
|
||||
} else {
|
||||
let file_id = child.file_id.original_file(db);
|
||||
let source_file = db.parse(file_id).tree();
|
||||
ModuleSource::SourceFile(source_file)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn from_file_id(db: &impl DefDatabase, file_id: FileId) -> ModuleSource {
|
||||
let source_file = db.parse(file_id).tree();
|
||||
ModuleSource::SourceFile(source_file)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
pub struct Module {
|
||||
pub(crate) id: ModuleId,
|
||||
|
@ -2,6 +2,7 @@
|
||||
|
||||
use either::Either;
|
||||
use hir_def::{
|
||||
nameres::ModuleSource,
|
||||
src::{HasChildSource, HasSource as _},
|
||||
AstItemDef, Lookup, VariantId,
|
||||
};
|
||||
@ -9,7 +10,7 @@ use ra_syntax::ast;
|
||||
|
||||
use crate::{
|
||||
db::DefDatabase, Const, Enum, EnumVariant, FieldSource, Function, ImplBlock, Import, MacroDef,
|
||||
Module, ModuleSource, Static, Struct, StructField, Trait, TypeAlias, Union,
|
||||
Module, Static, Struct, StructField, Trait, TypeAlias, Union,
|
||||
};
|
||||
|
||||
pub use hir_expand::InFile;
|
||||
|
@ -1,6 +1,6 @@
|
||||
//! FIXME: write short doc here
|
||||
|
||||
use hir_def::{AstItemDef, LocationCtx, ModuleId};
|
||||
use hir_def::{nameres::ModuleSource, AstItemDef, LocationCtx, ModuleId};
|
||||
use hir_expand::{name::AsName, AstId, MacroDefId, MacroDefKind};
|
||||
use ra_syntax::{
|
||||
ast::{self, AstNode, NameOwner},
|
||||
@ -10,8 +10,8 @@ use ra_syntax::{
|
||||
use crate::{
|
||||
db::{AstDatabase, DefDatabase, HirDatabase},
|
||||
AssocItem, Const, DefWithBody, Enum, EnumVariant, FieldSource, Function, HasSource, ImplBlock,
|
||||
InFile, Local, MacroDef, Module, ModuleDef, ModuleSource, Static, Struct, StructField, Trait,
|
||||
TypeAlias, Union, VariantDef,
|
||||
InFile, Local, MacroDef, Module, ModuleDef, Static, Struct, StructField, Trait, TypeAlias,
|
||||
Union, VariantDef,
|
||||
};
|
||||
|
||||
pub trait FromSource: Sized {
|
||||
@ -257,7 +257,7 @@ impl Module {
|
||||
InFile { file_id: src.file_id, value: module.clone() },
|
||||
);
|
||||
}
|
||||
ModuleSource::SourceFile(_) => (),
|
||||
ModuleSource::SourceFile(_) | ModuleSource::Block(_) => (),
|
||||
};
|
||||
|
||||
let original_file = src.file_id.original_file(db);
|
||||
|
@ -43,8 +43,8 @@ pub use crate::{
|
||||
code_model::{
|
||||
src::HasSource, Adt, AssocItem, AttrDef, Const, Container, Crate, CrateDependency,
|
||||
DefWithBody, Docs, Enum, EnumVariant, FieldSource, Function, GenericDef, GenericParam,
|
||||
HasAttrs, ImplBlock, Import, Local, MacroDef, Module, ModuleDef, ModuleSource, ScopeDef,
|
||||
Static, Struct, StructField, Trait, Type, TypeAlias, Union, VariantDef,
|
||||
HasAttrs, ImplBlock, Import, Local, MacroDef, Module, ModuleDef, ScopeDef, Static, Struct,
|
||||
StructField, Trait, Type, TypeAlias, Union, VariantDef,
|
||||
},
|
||||
from_source::FromSource,
|
||||
source_binder::{PathResolution, ScopeEntryWithSyntax, SourceAnalyzer},
|
||||
@ -59,6 +59,7 @@ pub use hir_def::{
|
||||
body::scope::ExprScopes,
|
||||
builtin_type::BuiltinType,
|
||||
docs::Documentation,
|
||||
nameres::ModuleSource,
|
||||
path::{Path, PathKind},
|
||||
type_ref::Mutability,
|
||||
};
|
||||
|
@ -14,6 +14,7 @@ use hir_def::{
|
||||
BodySourceMap,
|
||||
},
|
||||
expr::{ExprId, PatId},
|
||||
nameres::ModuleSource,
|
||||
path::known,
|
||||
resolver::{self, resolver_for_scope, HasResolver, Resolver, TypeNs, ValueNs},
|
||||
AssocItemId, DefWithBodyId,
|
||||
@ -46,7 +47,7 @@ fn try_get_resolver_for_node(db: &impl HirDatabase, node: InFile<&SyntaxNode>) -
|
||||
Some(crate::Module::from_declaration(db, src)?.id.resolver(db))
|
||||
},
|
||||
ast::SourceFile(it) => {
|
||||
let src = node.with_value(crate::ModuleSource::SourceFile(it));
|
||||
let src = node.with_value(ModuleSource::SourceFile(it));
|
||||
Some(crate::Module::from_definition(db, src)?.id.resolver(db))
|
||||
},
|
||||
ast::StructDef(it) => {
|
||||
|
@ -63,9 +63,12 @@ use hir_expand::{
|
||||
};
|
||||
use once_cell::sync::Lazy;
|
||||
use ra_arena::Arena;
|
||||
use ra_db::{CrateId, Edition, FileId};
|
||||
use ra_db::{CrateId, Edition, FileId, FilePosition};
|
||||
use ra_prof::profile;
|
||||
use ra_syntax::ast;
|
||||
use ra_syntax::{
|
||||
ast::{self, AstNode},
|
||||
SyntaxNode,
|
||||
};
|
||||
use rustc_hash::FxHashMap;
|
||||
|
||||
use crate::{
|
||||
@ -361,6 +364,67 @@ impl ModuleData {
|
||||
}
|
||||
}
|
||||
|
||||
pub enum ModuleSource {
|
||||
SourceFile(ast::SourceFile),
|
||||
Module(ast::Module),
|
||||
Block(ast::Block),
|
||||
}
|
||||
|
||||
impl ModuleSource {
|
||||
pub fn new(
|
||||
db: &impl DefDatabase,
|
||||
file_id: Option<FileId>,
|
||||
decl_id: Option<AstId<ast::Module>>,
|
||||
) -> ModuleSource {
|
||||
match (file_id, decl_id) {
|
||||
(Some(file_id), _) => {
|
||||
let source_file = db.parse(file_id).tree();
|
||||
ModuleSource::SourceFile(source_file)
|
||||
}
|
||||
(None, Some(item_id)) => {
|
||||
let module = item_id.to_node(db);
|
||||
assert!(module.item_list().is_some(), "expected inline module");
|
||||
ModuleSource::Module(module)
|
||||
}
|
||||
(None, None) => panic!(),
|
||||
}
|
||||
}
|
||||
|
||||
// FIXME: this methods do not belong here
|
||||
pub fn from_position(db: &impl DefDatabase, position: FilePosition) -> ModuleSource {
|
||||
let parse = db.parse(position.file_id);
|
||||
match &ra_syntax::algo::find_node_at_offset::<ast::Module>(
|
||||
parse.tree().syntax(),
|
||||
position.offset,
|
||||
) {
|
||||
Some(m) if !m.has_semi() => ModuleSource::Module(m.clone()),
|
||||
_ => {
|
||||
let source_file = parse.tree();
|
||||
ModuleSource::SourceFile(source_file)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn from_child_node(db: &impl DefDatabase, child: InFile<&SyntaxNode>) -> ModuleSource {
|
||||
if let Some(m) =
|
||||
child.value.ancestors().filter_map(ast::Module::cast).find(|it| !it.has_semi())
|
||||
{
|
||||
ModuleSource::Module(m)
|
||||
} else if let Some(b) = child.value.ancestors().filter_map(ast::Block::cast).next() {
|
||||
ModuleSource::Block(b)
|
||||
} else {
|
||||
let file_id = child.file_id.original_file(db);
|
||||
let source_file = db.parse(file_id).tree();
|
||||
ModuleSource::SourceFile(source_file)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn from_file_id(db: &impl DefDatabase, file_id: FileId) -> ModuleSource {
|
||||
let source_file = db.parse(file_id).tree();
|
||||
ModuleSource::SourceFile(source_file)
|
||||
}
|
||||
}
|
||||
|
||||
mod diagnostics {
|
||||
use hir_expand::diagnostics::DiagnosticSink;
|
||||
use ra_db::RelativePathBuf;
|
||||
|
@ -231,34 +231,21 @@ impl ToNav for hir::Module {
|
||||
fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
|
||||
let src = self.definition_source(db);
|
||||
let name = self.name(db).map(|it| it.to_string().into()).unwrap_or_default();
|
||||
match &src.value {
|
||||
ModuleSource::SourceFile(node) => {
|
||||
let frange = original_range(db, src.with_value(node.syntax()));
|
||||
|
||||
NavigationTarget::from_syntax(
|
||||
frange.file_id,
|
||||
name,
|
||||
None,
|
||||
frange.range,
|
||||
node.syntax().kind(),
|
||||
None,
|
||||
None,
|
||||
)
|
||||
}
|
||||
ModuleSource::Module(node) => {
|
||||
let frange = original_range(db, src.with_value(node.syntax()));
|
||||
|
||||
NavigationTarget::from_syntax(
|
||||
frange.file_id,
|
||||
name,
|
||||
None,
|
||||
frange.range,
|
||||
node.syntax().kind(),
|
||||
node.doc_comment_text(),
|
||||
node.short_label(),
|
||||
)
|
||||
}
|
||||
}
|
||||
let syntax = match &src.value {
|
||||
ModuleSource::SourceFile(node) => node.syntax(),
|
||||
ModuleSource::Module(node) => node.syntax(),
|
||||
ModuleSource::Block(node) => node.syntax(),
|
||||
};
|
||||
let frange = original_range(db, src.with_value(syntax));
|
||||
NavigationTarget::from_syntax(
|
||||
frange.file_id,
|
||||
name,
|
||||
None,
|
||||
frange.range,
|
||||
syntax.kind(),
|
||||
None,
|
||||
None,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -88,7 +88,7 @@ fn rename_mod(
|
||||
file_system_edits.push(move_file);
|
||||
}
|
||||
}
|
||||
ModuleSource::Module(..) => {}
|
||||
ModuleSource::Module(..) | ModuleSource::Block(..) => {}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -96,6 +96,10 @@ impl NameDefinition {
|
||||
let range = Some(m.syntax().text_range());
|
||||
res.insert(file_id, range);
|
||||
}
|
||||
ModuleSource::Block(b) => {
|
||||
let range = Some(b.syntax().text_range());
|
||||
res.insert(file_id, range);
|
||||
}
|
||||
ModuleSource::SourceFile(_) => {
|
||||
res.insert(file_id, None);
|
||||
res.extend(parent_module.children(db).map(|m| {
|
||||
@ -137,6 +141,7 @@ impl NameDefinition {
|
||||
let mut res = FxHashMap::default();
|
||||
let range = match module_src.value {
|
||||
ModuleSource::Module(m) => Some(m.syntax().text_range()),
|
||||
ModuleSource::Block(b) => Some(b.syntax().text_range()),
|
||||
ModuleSource::SourceFile(_) => None,
|
||||
};
|
||||
res.insert(file_id, range);
|
||||
|
Loading…
Reference in New Issue
Block a user