mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-29 13:25:09 +00:00
Add ModuleSource::Block
This commit is contained in:
parent
7cbedc50bc
commit
38853459e3
9 changed files with 102 additions and 100 deletions
|
@ -11,6 +11,7 @@ use hir_def::{
|
||||||
builtin_type::BuiltinType,
|
builtin_type::BuiltinType,
|
||||||
docs::Documentation,
|
docs::Documentation,
|
||||||
expr::{BindingAnnotation, Pat, PatId},
|
expr::{BindingAnnotation, Pat, PatId},
|
||||||
|
nameres::ModuleSource,
|
||||||
per_ns::PerNs,
|
per_ns::PerNs,
|
||||||
resolver::HasResolver,
|
resolver::HasResolver,
|
||||||
type_ref::{Mutability, TypeRef},
|
type_ref::{Mutability, TypeRef},
|
||||||
|
@ -21,11 +22,11 @@ use hir_def::{
|
||||||
use hir_expand::{
|
use hir_expand::{
|
||||||
diagnostics::DiagnosticSink,
|
diagnostics::DiagnosticSink,
|
||||||
name::{self, AsName},
|
name::{self, AsName},
|
||||||
AstId, MacroDefId,
|
MacroDefId,
|
||||||
};
|
};
|
||||||
use hir_ty::expr::ExprValidator;
|
use hir_ty::expr::ExprValidator;
|
||||||
use ra_db::{CrateId, Edition, FileId, FilePosition};
|
use ra_db::{CrateId, Edition};
|
||||||
use ra_syntax::{ast, AstNode, SyntaxNode};
|
use ra_syntax::ast;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
db::{DefDatabase, HirDatabase},
|
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)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||||
pub struct Module {
|
pub struct Module {
|
||||||
pub(crate) id: ModuleId,
|
pub(crate) id: ModuleId,
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
use either::Either;
|
use either::Either;
|
||||||
use hir_def::{
|
use hir_def::{
|
||||||
|
nameres::ModuleSource,
|
||||||
src::{HasChildSource, HasSource as _},
|
src::{HasChildSource, HasSource as _},
|
||||||
AstItemDef, Lookup, VariantId,
|
AstItemDef, Lookup, VariantId,
|
||||||
};
|
};
|
||||||
|
@ -9,7 +10,7 @@ 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, Import, MacroDef,
|
||||||
Module, ModuleSource, Static, Struct, StructField, Trait, TypeAlias, Union,
|
Module, Static, Struct, StructField, Trait, TypeAlias, Union,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub use hir_expand::InFile;
|
pub use hir_expand::InFile;
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
//! FIXME: write short doc here
|
//! 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 hir_expand::{name::AsName, AstId, MacroDefId, MacroDefKind};
|
||||||
use ra_syntax::{
|
use ra_syntax::{
|
||||||
ast::{self, AstNode, NameOwner},
|
ast::{self, AstNode, NameOwner},
|
||||||
|
@ -10,8 +10,8 @@ use ra_syntax::{
|
||||||
use crate::{
|
use crate::{
|
||||||
db::{AstDatabase, DefDatabase, HirDatabase},
|
db::{AstDatabase, DefDatabase, HirDatabase},
|
||||||
AssocItem, Const, DefWithBody, Enum, EnumVariant, FieldSource, Function, HasSource, ImplBlock,
|
AssocItem, Const, DefWithBody, Enum, EnumVariant, FieldSource, Function, HasSource, ImplBlock,
|
||||||
InFile, Local, MacroDef, Module, ModuleDef, ModuleSource, Static, Struct, StructField, Trait,
|
InFile, Local, MacroDef, Module, ModuleDef, Static, Struct, StructField, Trait, TypeAlias,
|
||||||
TypeAlias, Union, VariantDef,
|
Union, VariantDef,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub trait FromSource: Sized {
|
pub trait FromSource: Sized {
|
||||||
|
@ -257,7 +257,7 @@ impl Module {
|
||||||
InFile { file_id: src.file_id, value: module.clone() },
|
InFile { file_id: src.file_id, value: module.clone() },
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
ModuleSource::SourceFile(_) => (),
|
ModuleSource::SourceFile(_) | ModuleSource::Block(_) => (),
|
||||||
};
|
};
|
||||||
|
|
||||||
let original_file = src.file_id.original_file(db);
|
let original_file = src.file_id.original_file(db);
|
||||||
|
|
|
@ -43,8 +43,8 @@ pub use crate::{
|
||||||
code_model::{
|
code_model::{
|
||||||
src::HasSource, Adt, AssocItem, AttrDef, Const, Container, Crate, CrateDependency,
|
src::HasSource, Adt, AssocItem, AttrDef, Const, Container, Crate, CrateDependency,
|
||||||
DefWithBody, Docs, Enum, EnumVariant, FieldSource, Function, GenericDef, GenericParam,
|
DefWithBody, Docs, Enum, EnumVariant, FieldSource, Function, GenericDef, GenericParam,
|
||||||
HasAttrs, ImplBlock, Import, Local, MacroDef, Module, ModuleDef, ModuleSource, ScopeDef,
|
HasAttrs, ImplBlock, Import, Local, MacroDef, Module, ModuleDef, ScopeDef, Static, Struct,
|
||||||
Static, Struct, StructField, Trait, Type, TypeAlias, Union, VariantDef,
|
StructField, Trait, Type, TypeAlias, Union, VariantDef,
|
||||||
},
|
},
|
||||||
from_source::FromSource,
|
from_source::FromSource,
|
||||||
source_binder::{PathResolution, ScopeEntryWithSyntax, SourceAnalyzer},
|
source_binder::{PathResolution, ScopeEntryWithSyntax, SourceAnalyzer},
|
||||||
|
@ -59,6 +59,7 @@ pub use hir_def::{
|
||||||
body::scope::ExprScopes,
|
body::scope::ExprScopes,
|
||||||
builtin_type::BuiltinType,
|
builtin_type::BuiltinType,
|
||||||
docs::Documentation,
|
docs::Documentation,
|
||||||
|
nameres::ModuleSource,
|
||||||
path::{Path, PathKind},
|
path::{Path, PathKind},
|
||||||
type_ref::Mutability,
|
type_ref::Mutability,
|
||||||
};
|
};
|
||||||
|
|
|
@ -14,6 +14,7 @@ use hir_def::{
|
||||||
BodySourceMap,
|
BodySourceMap,
|
||||||
},
|
},
|
||||||
expr::{ExprId, PatId},
|
expr::{ExprId, PatId},
|
||||||
|
nameres::ModuleSource,
|
||||||
path::known,
|
path::known,
|
||||||
resolver::{self, resolver_for_scope, HasResolver, Resolver, TypeNs, ValueNs},
|
resolver::{self, resolver_for_scope, HasResolver, Resolver, TypeNs, ValueNs},
|
||||||
AssocItemId, DefWithBodyId,
|
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))
|
Some(crate::Module::from_declaration(db, src)?.id.resolver(db))
|
||||||
},
|
},
|
||||||
ast::SourceFile(it) => {
|
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))
|
Some(crate::Module::from_definition(db, src)?.id.resolver(db))
|
||||||
},
|
},
|
||||||
ast::StructDef(it) => {
|
ast::StructDef(it) => {
|
||||||
|
|
|
@ -63,9 +63,12 @@ use hir_expand::{
|
||||||
};
|
};
|
||||||
use once_cell::sync::Lazy;
|
use once_cell::sync::Lazy;
|
||||||
use ra_arena::Arena;
|
use ra_arena::Arena;
|
||||||
use ra_db::{CrateId, Edition, FileId};
|
use ra_db::{CrateId, Edition, FileId, FilePosition};
|
||||||
use ra_prof::profile;
|
use ra_prof::profile;
|
||||||
use ra_syntax::ast;
|
use ra_syntax::{
|
||||||
|
ast::{self, AstNode},
|
||||||
|
SyntaxNode,
|
||||||
|
};
|
||||||
use rustc_hash::FxHashMap;
|
use rustc_hash::FxHashMap;
|
||||||
|
|
||||||
use crate::{
|
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 {
|
mod diagnostics {
|
||||||
use hir_expand::diagnostics::DiagnosticSink;
|
use hir_expand::diagnostics::DiagnosticSink;
|
||||||
use ra_db::RelativePathBuf;
|
use ra_db::RelativePathBuf;
|
||||||
|
|
|
@ -231,34 +231,21 @@ impl ToNav for hir::Module {
|
||||||
fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
|
fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
|
||||||
let src = self.definition_source(db);
|
let src = self.definition_source(db);
|
||||||
let name = self.name(db).map(|it| it.to_string().into()).unwrap_or_default();
|
let name = self.name(db).map(|it| it.to_string().into()).unwrap_or_default();
|
||||||
match &src.value {
|
let syntax = match &src.value {
|
||||||
ModuleSource::SourceFile(node) => {
|
ModuleSource::SourceFile(node) => node.syntax(),
|
||||||
let frange = original_range(db, src.with_value(node.syntax()));
|
ModuleSource::Module(node) => node.syntax(),
|
||||||
|
ModuleSource::Block(node) => node.syntax(),
|
||||||
NavigationTarget::from_syntax(
|
};
|
||||||
frange.file_id,
|
let frange = original_range(db, src.with_value(syntax));
|
||||||
name,
|
NavigationTarget::from_syntax(
|
||||||
None,
|
frange.file_id,
|
||||||
frange.range,
|
name,
|
||||||
node.syntax().kind(),
|
None,
|
||||||
None,
|
frange.range,
|
||||||
None,
|
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(),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -88,7 +88,7 @@ fn rename_mod(
|
||||||
file_system_edits.push(move_file);
|
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());
|
let range = Some(m.syntax().text_range());
|
||||||
res.insert(file_id, range);
|
res.insert(file_id, range);
|
||||||
}
|
}
|
||||||
|
ModuleSource::Block(b) => {
|
||||||
|
let range = Some(b.syntax().text_range());
|
||||||
|
res.insert(file_id, range);
|
||||||
|
}
|
||||||
ModuleSource::SourceFile(_) => {
|
ModuleSource::SourceFile(_) => {
|
||||||
res.insert(file_id, None);
|
res.insert(file_id, None);
|
||||||
res.extend(parent_module.children(db).map(|m| {
|
res.extend(parent_module.children(db).map(|m| {
|
||||||
|
@ -137,6 +141,7 @@ impl NameDefinition {
|
||||||
let mut res = FxHashMap::default();
|
let mut res = FxHashMap::default();
|
||||||
let range = match module_src.value {
|
let range = match module_src.value {
|
||||||
ModuleSource::Module(m) => Some(m.syntax().text_range()),
|
ModuleSource::Module(m) => Some(m.syntax().text_range()),
|
||||||
|
ModuleSource::Block(b) => Some(b.syntax().text_range()),
|
||||||
ModuleSource::SourceFile(_) => None,
|
ModuleSource::SourceFile(_) => None,
|
||||||
};
|
};
|
||||||
res.insert(file_id, range);
|
res.insert(file_id, range);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue