mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-30 22:01:37 +00:00
Create modules via SourceBinder
This commit is contained in:
parent
9a6c26e348
commit
595b06a1b8
10 changed files with 65 additions and 112 deletions
|
@ -52,15 +52,11 @@ impl<'a> CompletionContext<'a> {
|
|||
original_parse: &'a Parse<ast::SourceFile>,
|
||||
position: FilePosition,
|
||||
) -> Option<CompletionContext<'a>> {
|
||||
let src = hir::ModuleSource::from_position(db, position);
|
||||
let module = hir::Module::from_definition(
|
||||
db,
|
||||
hir::InFile { file_id: position.file_id.into(), value: src },
|
||||
);
|
||||
let mut sb = hir::SourceBinder::new(db);
|
||||
let module = sb.to_module_def(position.file_id);
|
||||
let token =
|
||||
original_parse.tree().syntax().token_at_offset(position.offset).left_biased()?;
|
||||
let analyzer = hir::SourceAnalyzer::new(
|
||||
db,
|
||||
let analyzer = sb.analyze(
|
||||
hir::InFile::new(position.file_id.into(), &token.parent()),
|
||||
Some(position.offset),
|
||||
);
|
||||
|
|
|
@ -23,6 +23,7 @@ pub enum Severity {
|
|||
|
||||
pub(crate) fn diagnostics(db: &RootDatabase, file_id: FileId) -> Vec<Diagnostic> {
|
||||
let _p = profile("diagnostics");
|
||||
let mut sb = hir::SourceBinder::new(db);
|
||||
let parse = db.parse(file_id);
|
||||
let mut res = Vec::new();
|
||||
|
||||
|
@ -108,10 +109,7 @@ pub(crate) fn diagnostics(db: &RootDatabase, file_id: FileId) -> Vec<Diagnostic>
|
|||
fix: Some(fix),
|
||||
})
|
||||
});
|
||||
let source_file = db.parse(file_id).tree();
|
||||
let src =
|
||||
hir::InFile { file_id: file_id.into(), value: hir::ModuleSource::SourceFile(source_file) };
|
||||
if let Some(m) = hir::Module::from_definition(db, src) {
|
||||
if let Some(m) = sb.to_module_def(file_id) {
|
||||
m.diagnostics(db, &mut sink);
|
||||
};
|
||||
drop(sink);
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
//! FIXME: write short doc here
|
||||
|
||||
use hir::{ImplBlock, SourceBinder};
|
||||
use hir::{Crate, ImplBlock, SourceBinder};
|
||||
use ra_db::SourceDatabase;
|
||||
use ra_syntax::{algo::find_node_at_offset, ast, AstNode};
|
||||
|
||||
|
@ -14,21 +14,17 @@ pub(crate) fn goto_implementation(
|
|||
let syntax = parse.tree().syntax().clone();
|
||||
let mut sb = SourceBinder::new(db);
|
||||
|
||||
let src = hir::ModuleSource::from_position(db, position);
|
||||
let module = hir::Module::from_definition(
|
||||
db,
|
||||
hir::InFile { file_id: position.file_id.into(), value: src },
|
||||
)?;
|
||||
let krate = sb.to_module_def(position.file_id)?.krate();
|
||||
|
||||
if let Some(nominal_def) = find_node_at_offset::<ast::NominalDef>(&syntax, position.offset) {
|
||||
return Some(RangeInfo::new(
|
||||
nominal_def.syntax().text_range(),
|
||||
impls_for_def(&mut sb, position, &nominal_def, module)?,
|
||||
impls_for_def(&mut sb, position, &nominal_def, krate)?,
|
||||
));
|
||||
} else if let Some(trait_def) = find_node_at_offset::<ast::TraitDef>(&syntax, position.offset) {
|
||||
return Some(RangeInfo::new(
|
||||
trait_def.syntax().text_range(),
|
||||
impls_for_trait(&mut sb, position, &trait_def, module)?,
|
||||
impls_for_trait(&mut sb, position, &trait_def, krate)?,
|
||||
));
|
||||
}
|
||||
|
||||
|
@ -39,7 +35,7 @@ fn impls_for_def(
|
|||
sb: &mut SourceBinder<RootDatabase>,
|
||||
position: FilePosition,
|
||||
node: &ast::NominalDef,
|
||||
module: hir::Module,
|
||||
krate: Crate,
|
||||
) -> Option<Vec<NavigationTarget>> {
|
||||
let ty = match node {
|
||||
ast::NominalDef::StructDef(def) => {
|
||||
|
@ -56,7 +52,6 @@ fn impls_for_def(
|
|||
}
|
||||
};
|
||||
|
||||
let krate = module.krate();
|
||||
let impls = ImplBlock::all_in_crate(sb.db, krate);
|
||||
|
||||
Some(
|
||||
|
@ -72,12 +67,11 @@ fn impls_for_trait(
|
|||
sb: &mut SourceBinder<RootDatabase>,
|
||||
position: FilePosition,
|
||||
node: &ast::TraitDef,
|
||||
module: hir::Module,
|
||||
krate: Crate,
|
||||
) -> Option<Vec<NavigationTarget>> {
|
||||
let src = hir::InFile { file_id: position.file_id.into(), value: node.clone() };
|
||||
let tr = sb.to_def(src)?;
|
||||
|
||||
let krate = module.krate();
|
||||
let impls = ImplBlock::for_trait(sb.db, krate, tr);
|
||||
|
||||
Some(impls.into_iter().map(|imp| imp.to_nav(sb.db)).collect())
|
||||
|
|
|
@ -1,17 +1,23 @@
|
|||
//! FIXME: write short doc here
|
||||
|
||||
use ra_db::{CrateId, FileId, FilePosition, SourceDatabase};
|
||||
use ra_syntax::{
|
||||
algo::find_node_at_offset,
|
||||
ast::{self, AstNode},
|
||||
};
|
||||
|
||||
use crate::{db::RootDatabase, NavigationTarget};
|
||||
|
||||
/// This returns `Vec` because a module may be included from several places. We
|
||||
/// don't handle this case yet though, so the Vec has length at most one.
|
||||
pub(crate) fn parent_module(db: &RootDatabase, position: FilePosition) -> Vec<NavigationTarget> {
|
||||
let src = hir::ModuleSource::from_position(db, position);
|
||||
let module = match hir::Module::from_definition(
|
||||
db,
|
||||
hir::InFile { file_id: position.file_id.into(), value: src },
|
||||
) {
|
||||
let mut sb = hir::SourceBinder::new(db);
|
||||
let parse = db.parse(position.file_id);
|
||||
let module = match find_node_at_offset::<ast::Module>(parse.tree().syntax(), position.offset) {
|
||||
Some(module) => sb.to_def(hir::InFile::new(position.file_id.into(), module)),
|
||||
None => sb.to_module_def(position.file_id),
|
||||
};
|
||||
let module = match module {
|
||||
None => return Vec::new(),
|
||||
Some(it) => it,
|
||||
};
|
||||
|
@ -21,14 +27,11 @@ pub(crate) fn parent_module(db: &RootDatabase, position: FilePosition) -> Vec<Na
|
|||
|
||||
/// Returns `Vec` for the same reason as `parent_module`
|
||||
pub(crate) fn crate_for(db: &RootDatabase, file_id: FileId) -> Vec<CrateId> {
|
||||
let source_file = db.parse(file_id).tree();
|
||||
let src = hir::ModuleSource::SourceFile(source_file);
|
||||
let module =
|
||||
match hir::Module::from_definition(db, hir::InFile { file_id: file_id.into(), value: src })
|
||||
{
|
||||
Some(it) => it,
|
||||
None => return Vec::new(),
|
||||
};
|
||||
let mut sb = hir::SourceBinder::new(db);
|
||||
let module = match sb.to_module_def(file_id) {
|
||||
Some(it) => it,
|
||||
None => return Vec::new(),
|
||||
};
|
||||
let krate = module.krate();
|
||||
vec![krate.into()]
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
//! Functions that are used to classify an element from its definition or reference.
|
||||
|
||||
use hir::{InFile, Module, ModuleSource, PathResolution, SourceBinder};
|
||||
use hir::{InFile, PathResolution, SourceBinder};
|
||||
use ra_prof::profile;
|
||||
use ra_syntax::{ast, match_ast, AstNode};
|
||||
use test_utils::tested_by;
|
||||
|
@ -35,16 +35,7 @@ pub(crate) fn classify_name(
|
|||
Some(from_struct_field(sb.db, field))
|
||||
},
|
||||
ast::Module(it) => {
|
||||
let def = {
|
||||
if !it.has_semi() {
|
||||
let ast = hir::ModuleSource::Module(it);
|
||||
let src = name.with_value(ast);
|
||||
hir::Module::from_definition(sb.db, src)
|
||||
} else {
|
||||
let src = name.with_value(it);
|
||||
sb.to_def(src)
|
||||
}
|
||||
}?;
|
||||
let def = sb.to_def(name.with_value(it))?;
|
||||
Some(from_module_def(sb.db, def.into(), None))
|
||||
},
|
||||
ast::StructDef(it) => {
|
||||
|
@ -103,8 +94,7 @@ pub(crate) fn classify_name(
|
|||
let src = name.with_value(it);
|
||||
let def = sb.to_def(src.clone())?;
|
||||
|
||||
let module_src = ModuleSource::from_child_node(sb.db, src.as_ref().map(|it| it.syntax()));
|
||||
let module = Module::from_definition(sb.db, src.with_value(module_src))?;
|
||||
let module = sb.to_module_def(src.file_id.original_file(sb.db))?;
|
||||
|
||||
Some(NameDefinition {
|
||||
visibility: None,
|
||||
|
@ -157,10 +147,9 @@ pub(crate) fn classify_name_ref(
|
|||
}
|
||||
}
|
||||
|
||||
let ast = ModuleSource::from_child_node(sb.db, name_ref.with_value(&parent));
|
||||
// FIXME: find correct container and visibility for each case
|
||||
let container = Module::from_definition(sb.db, name_ref.with_value(ast))?;
|
||||
let visibility = None;
|
||||
let container = sb.to_module_def(name_ref.file_id.original_file(sb.db))?;
|
||||
|
||||
if let Some(macro_call) = parent.ancestors().find_map(ast::MacroCall::cast) {
|
||||
tested_by!(goto_def_for_macros);
|
||||
|
@ -178,12 +167,13 @@ pub(crate) fn classify_name_ref(
|
|||
PathResolution::Def(def) => Some(from_module_def(sb.db, def, Some(container))),
|
||||
PathResolution::AssocItem(item) => Some(from_assoc_item(sb.db, item)),
|
||||
PathResolution::Local(local) => {
|
||||
let container = local.module(sb.db);
|
||||
let kind = NameKind::Local(local);
|
||||
let container = local.module(sb.db);
|
||||
Some(NameDefinition { kind, container, visibility: None })
|
||||
}
|
||||
PathResolution::TypeParam(par) => {
|
||||
let kind = NameKind::TypeParam(par);
|
||||
let container = par.module(sb.db);
|
||||
Some(NameDefinition { kind, container, visibility })
|
||||
}
|
||||
PathResolution::Macro(def) => {
|
||||
|
|
|
@ -25,6 +25,8 @@ pub enum NameKind {
|
|||
#[derive(PartialEq, Eq)]
|
||||
pub(crate) struct NameDefinition {
|
||||
pub visibility: Option<ast::Visibility>,
|
||||
/// FIXME: this doesn't really make sense. For example, builtin types don't
|
||||
/// really have a module.
|
||||
pub container: Module,
|
||||
pub kind: NameKind,
|
||||
}
|
||||
|
|
|
@ -66,8 +66,8 @@ fn runnable_mod(db: &RootDatabase, file_id: FileId, module: ast::Module) -> Opti
|
|||
return None;
|
||||
}
|
||||
let range = module.syntax().text_range();
|
||||
let src = hir::ModuleSource::from_child_node(db, InFile::new(file_id.into(), &module.syntax()));
|
||||
let module = hir::Module::from_definition(db, InFile::new(file_id.into(), src))?;
|
||||
let mut sb = hir::SourceBinder::new(db);
|
||||
let module = sb.to_def(InFile::new(file_id.into(), module))?;
|
||||
|
||||
let path = module.path_to_root(db).into_iter().rev().filter_map(|it| it.name(db)).join("::");
|
||||
Some(Runnable { range, kind: RunnableKind::TestMod { path } })
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue