mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-29 21:35:20 +00:00
introduce FromSource trait
This commit is contained in:
parent
c35ef7e1ed
commit
2867c40925
13 changed files with 294 additions and 138 deletions
|
@ -1,4 +1,3 @@
|
|||
use hir::source_binder;
|
||||
use ra_syntax::{
|
||||
algo::{find_covering_element, find_node_at_offset},
|
||||
ast, AstNode, Parse, SourceFile,
|
||||
|
@ -47,7 +46,11 @@ impl<'a> CompletionContext<'a> {
|
|||
original_parse: &'a Parse<ast::SourceFile>,
|
||||
position: FilePosition,
|
||||
) -> Option<CompletionContext<'a>> {
|
||||
let module = source_binder::module_from_position(db, position);
|
||||
let src = hir::ModuleSource::from_position(db, position);
|
||||
let module = hir::Module::from_definition(
|
||||
db,
|
||||
hir::Source { file_id: position.file_id.into(), ast: src },
|
||||
);
|
||||
let token =
|
||||
original_parse.tree().syntax().token_at_offset(position.offset).left_biased()?;
|
||||
let analyzer =
|
||||
|
|
|
@ -1,9 +1,6 @@
|
|||
use std::cell::RefCell;
|
||||
|
||||
use hir::{
|
||||
diagnostics::{AstDiagnostic, Diagnostic as _, DiagnosticSink},
|
||||
source_binder,
|
||||
};
|
||||
use hir::diagnostics::{AstDiagnostic, Diagnostic as _, DiagnosticSink};
|
||||
use itertools::Itertools;
|
||||
use ra_assists::ast_editor::{AstBuilder, AstEditor};
|
||||
use ra_db::SourceDatabase;
|
||||
|
@ -89,7 +86,10 @@ pub(crate) fn diagnostics(db: &RootDatabase, file_id: FileId) -> Vec<Diagnostic>
|
|||
fix: Some(fix),
|
||||
})
|
||||
});
|
||||
if let Some(m) = source_binder::module_from_file_id(db, file_id) {
|
||||
let source_file = db.parse(file_id).tree().to_owned();
|
||||
let src =
|
||||
hir::Source { file_id: file_id.into(), ast: hir::ModuleSource::SourceFile(source_file) };
|
||||
if let Some(m) = hir::Module::from_definition(db, src) {
|
||||
m.diagnostics(db, &mut sink);
|
||||
};
|
||||
drop(sink);
|
||||
|
|
|
@ -96,9 +96,8 @@ pub(crate) fn name_definition(
|
|||
|
||||
if let Some(module) = ast::Module::cast(parent.clone()) {
|
||||
if module.has_semi() {
|
||||
if let Some(child_module) =
|
||||
hir::source_binder::module_from_declaration(db, file_id, module)
|
||||
{
|
||||
let src = hir::Source { file_id: file_id.into(), ast: module };
|
||||
if let Some(child_module) = hir::Module::from_declaration(db, src) {
|
||||
let nav = NavigationTarget::from_module(db, child_module);
|
||||
return Some(vec![nav]);
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use hir::{db::HirDatabase, source_binder, ApplicationTy, Ty, TypeCtor};
|
||||
use hir::{db::HirDatabase, ApplicationTy, FromSource, Ty, TypeCtor};
|
||||
use ra_db::SourceDatabase;
|
||||
use ra_syntax::{algo::find_node_at_offset, ast, AstNode};
|
||||
|
||||
|
@ -11,17 +11,21 @@ pub(crate) fn goto_implementation(
|
|||
let parse = db.parse(position.file_id);
|
||||
let syntax = parse.tree().syntax().clone();
|
||||
|
||||
let module = source_binder::module_from_position(db, position)?;
|
||||
let src = hir::ModuleSource::from_position(db, position);
|
||||
let module = hir::Module::from_definition(
|
||||
db,
|
||||
hir::Source { file_id: position.file_id.into(), ast: src },
|
||||
)?;
|
||||
|
||||
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(db, &nominal_def, module)?,
|
||||
impls_for_def(db, position, &nominal_def, module)?,
|
||||
));
|
||||
} 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(db, &trait_def, module)?,
|
||||
impls_for_trait(db, position, &trait_def, module)?,
|
||||
));
|
||||
}
|
||||
|
||||
|
@ -30,14 +34,19 @@ pub(crate) fn goto_implementation(
|
|||
|
||||
fn impls_for_def(
|
||||
db: &RootDatabase,
|
||||
position: FilePosition,
|
||||
node: &ast::NominalDef,
|
||||
module: hir::Module,
|
||||
) -> Option<Vec<NavigationTarget>> {
|
||||
let ty = match node {
|
||||
ast::NominalDef::StructDef(def) => {
|
||||
source_binder::struct_from_module(db, module, &def).ty(db)
|
||||
let src = hir::Source { file_id: position.file_id.into(), ast: def.clone() };
|
||||
hir::Struct::from_source(db, src)?.ty(db)
|
||||
}
|
||||
ast::NominalDef::EnumDef(def) => {
|
||||
let src = hir::Source { file_id: position.file_id.into(), ast: def.clone() };
|
||||
hir::Enum::from_source(db, src)?.ty(db)
|
||||
}
|
||||
ast::NominalDef::EnumDef(def) => source_binder::enum_from_module(db, module, &def).ty(db),
|
||||
};
|
||||
|
||||
let krate = module.krate(db)?;
|
||||
|
@ -54,10 +63,12 @@ fn impls_for_def(
|
|||
|
||||
fn impls_for_trait(
|
||||
db: &RootDatabase,
|
||||
position: FilePosition,
|
||||
node: &ast::TraitDef,
|
||||
module: hir::Module,
|
||||
) -> Option<Vec<NavigationTarget>> {
|
||||
let tr = source_binder::trait_from_module(db, module, node);
|
||||
let src = hir::Source { file_id: position.file_id.into(), ast: node.clone() };
|
||||
let tr = hir::Trait::from_source(db, src)?;
|
||||
|
||||
let krate = module.krate(db)?;
|
||||
let impls = db.impls_in_crate(krate);
|
||||
|
|
|
@ -5,7 +5,11 @@ 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 module = match hir::source_binder::module_from_position(db, position) {
|
||||
let src = hir::ModuleSource::from_position(db, position);
|
||||
let module = match hir::Module::from_definition(
|
||||
db,
|
||||
hir::Source { file_id: position.file_id.into(), ast: src },
|
||||
) {
|
||||
None => return Vec::new(),
|
||||
Some(it) => it,
|
||||
};
|
||||
|
@ -15,10 +19,12 @@ 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 module = match hir::source_binder::module_from_file_id(db, file_id) {
|
||||
Some(it) => it,
|
||||
None => return Vec::new(),
|
||||
};
|
||||
let src = hir::ModuleSource::from_file_id(db, file_id);
|
||||
let module =
|
||||
match hir::Module::from_definition(db, hir::Source { file_id: file_id.into(), ast: src }) {
|
||||
Some(it) => it,
|
||||
None => return Vec::new(),
|
||||
};
|
||||
let krate = match module.krate(db) {
|
||||
Some(it) => it,
|
||||
None => return Vec::new(),
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use hir::{source_binder, Either, ModuleSource};
|
||||
use hir::{Either, ModuleSource};
|
||||
use ra_db::SourceDatabase;
|
||||
use ra_syntax::{algo::find_node_at_offset, ast, AstNode, SourceFile, SyntaxNode};
|
||||
use relative_path::{RelativePath, RelativePathBuf};
|
||||
|
@ -135,9 +135,8 @@ fn rename_mod(
|
|||
) -> Option<SourceChange> {
|
||||
let mut source_file_edits = Vec::new();
|
||||
let mut file_system_edits = Vec::new();
|
||||
if let Some(module) =
|
||||
source_binder::module_from_declaration(db, position.file_id, ast_module.clone())
|
||||
{
|
||||
let module_src = hir::Source { file_id: position.file_id.into(), ast: ast_module.clone() };
|
||||
if let Some(module) = hir::Module::from_declaration(db, module_src) {
|
||||
let src = module.definition_source(db);
|
||||
let file_id = src.file_id.as_original_file();
|
||||
match src.ast {
|
||||
|
|
|
@ -63,7 +63,9 @@ fn runnable_mod(db: &RootDatabase, file_id: FileId, module: ast::Module) -> Opti
|
|||
return None;
|
||||
}
|
||||
let range = module.syntax().text_range();
|
||||
let module = hir::source_binder::module_from_child_node(db, file_id, module.syntax())?;
|
||||
let src = hir::ModuleSource::from_child_node(db, file_id, &module.syntax());
|
||||
let module =
|
||||
hir::Module::from_definition(db, hir::Source { file_id: file_id.into(), ast: src })?;
|
||||
|
||||
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