mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-28 21:05:02 +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,
|
||||
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,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue