2266: Sourcify some things r=matklad a=matklad



Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
bors[bot] 2019-11-15 20:25:53 +00:00 committed by GitHub
commit 920848940a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 22 additions and 21 deletions

View File

@ -196,9 +196,8 @@ where
N: AstNode,
DEF: AstItemDef<N>,
{
let module_src =
crate::ModuleSource::from_child_node(db, src.file_id.original_file(db), &src.ast.syntax());
let module = Module::from_definition(db, Source { file_id: src.file_id, ast: module_src })?;
let module_src = ModuleSource::from_child_node(db, src.as_ref().map(|it| it.syntax()));
let module = Module::from_definition(db, Source::new(src.file_id, module_src))?;
let ctx = LocationCtx::new(db, module.id, src.file_id);
Some(DEF::from_ast(ctx, &src.ast))
}

View File

@ -56,7 +56,7 @@ fn try_get_resolver_for_node(
},
_ => {
if node.kind() == FN_DEF || node.kind() == CONST_DEF || node.kind() == STATIC_DEF {
Some(def_with_body_from_child_node(db, file_id, node)?.resolver(db))
Some(def_with_body_from_child_node(db, Source::new(file_id.into(), node))?.resolver(db))
} else {
// FIXME add missing cases
None
@ -68,14 +68,13 @@ fn try_get_resolver_for_node(
fn def_with_body_from_child_node(
db: &impl HirDatabase,
file_id: FileId,
node: &SyntaxNode,
child: Source<&SyntaxNode>,
) -> Option<DefWithBody> {
let src = crate::ModuleSource::from_child_node(db, file_id, node);
let module = Module::from_definition(db, crate::Source { file_id: file_id.into(), ast: src })?;
let ctx = LocationCtx::new(db, module.id, file_id.into());
let module_source = crate::ModuleSource::from_child_node(db, child);
let module = Module::from_definition(db, Source::new(child.file_id, module_source))?;
let ctx = LocationCtx::new(db, module.id, child.file_id);
node.ancestors().find_map(|node| {
child.ast.ancestors().find_map(|node| {
match_ast! {
match node {
ast::FnDef(def) => { Some(Function {id: ctx.to_def(&def) }.into()) },
@ -142,7 +141,7 @@ impl SourceAnalyzer {
node: &SyntaxNode,
offset: Option<TextUnit>,
) -> SourceAnalyzer {
let def_with_body = def_with_body_from_child_node(db, file_id, node);
let def_with_body = def_with_body_from_child_node(db, Source::new(file_id.into(), node));
if let Some(def) = def_with_body {
let source_map = def.body_source_map(db);
let scopes = def.expr_scopes(db);

View File

@ -78,14 +78,13 @@ impl ModuleSource {
}
}
pub fn from_child_node(
db: &impl db::DefDatabase2,
file_id: FileId,
child: &SyntaxNode,
) -> ModuleSource {
if let Some(m) = child.ancestors().filter_map(ast::Module::cast).find(|it| !it.has_semi()) {
pub fn from_child_node(db: &impl db::DefDatabase2, child: Source<&SyntaxNode>) -> ModuleSource {
if let Some(m) =
child.ast.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)
}

View File

@ -230,6 +230,10 @@ pub struct Source<T> {
}
impl<T> Source<T> {
pub fn new(file_id: HirFileId, ast: T) -> Source<T> {
Source { file_id, ast }
}
pub fn map<F: FnOnce(T) -> U, U>(self, f: F) -> Source<U> {
Source { file_id: self.file_id, ast: f(self.ast) }
}

View File

@ -144,8 +144,8 @@ pub(crate) fn classify_name_ref(
}
}
let ast = ModuleSource::from_child_node(db, file_id, &parent);
let file_id = file_id.into();
let ast = ModuleSource::from_child_node(db, Source::new(file_id, &parent));
// FIXME: find correct container and visibility for each case
let container = Module::from_definition(db, Source { file_id, ast })?;
let visibility = None;

View File

@ -1,5 +1,6 @@
//! FIXME: write short doc here
use hir::Source;
use itertools::Itertools;
use ra_db::SourceDatabase;
use ra_syntax::{
@ -65,9 +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, file_id, &module.syntax());
let module =
hir::Module::from_definition(db, hir::Source { file_id: file_id.into(), ast: src })?;
let src = hir::ModuleSource::from_child_node(db, Source::new(file_id.into(), &module.syntax()));
let module = hir::Module::from_definition(db, Source::new(file_id.into(), 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 } })