mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-28 21:05:02 +00:00
Sourcify some things
If we want to support macros properly, we need to get rid of those FileIds everywhere...
This commit is contained in:
parent
1889b3c7b5
commit
4c90b7e2ec
6 changed files with 22 additions and 21 deletions
|
@ -196,9 +196,8 @@ where
|
||||||
N: AstNode,
|
N: AstNode,
|
||||||
DEF: AstItemDef<N>,
|
DEF: AstItemDef<N>,
|
||||||
{
|
{
|
||||||
let module_src =
|
let module_src = ModuleSource::from_child_node(db, src.as_ref().map(|it| it.syntax()));
|
||||||
crate::ModuleSource::from_child_node(db, src.file_id.original_file(db), &src.ast.syntax());
|
let module = Module::from_definition(db, Source::new(src.file_id, module_src))?;
|
||||||
let module = Module::from_definition(db, Source { file_id: src.file_id, ast: module_src })?;
|
|
||||||
let ctx = LocationCtx::new(db, module.id, src.file_id);
|
let ctx = LocationCtx::new(db, module.id, src.file_id);
|
||||||
Some(DEF::from_ast(ctx, &src.ast))
|
Some(DEF::from_ast(ctx, &src.ast))
|
||||||
}
|
}
|
||||||
|
|
|
@ -56,7 +56,7 @@ fn try_get_resolver_for_node(
|
||||||
},
|
},
|
||||||
_ => {
|
_ => {
|
||||||
if node.kind() == FN_DEF || node.kind() == CONST_DEF || node.kind() == STATIC_DEF {
|
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 {
|
} else {
|
||||||
// FIXME add missing cases
|
// FIXME add missing cases
|
||||||
None
|
None
|
||||||
|
@ -68,14 +68,13 @@ fn try_get_resolver_for_node(
|
||||||
|
|
||||||
fn def_with_body_from_child_node(
|
fn def_with_body_from_child_node(
|
||||||
db: &impl HirDatabase,
|
db: &impl HirDatabase,
|
||||||
file_id: FileId,
|
child: Source<&SyntaxNode>,
|
||||||
node: &SyntaxNode,
|
|
||||||
) -> Option<DefWithBody> {
|
) -> Option<DefWithBody> {
|
||||||
let src = crate::ModuleSource::from_child_node(db, file_id, node);
|
let module_source = crate::ModuleSource::from_child_node(db, child);
|
||||||
let module = Module::from_definition(db, crate::Source { file_id: file_id.into(), ast: src })?;
|
let module = Module::from_definition(db, Source::new(child.file_id, module_source))?;
|
||||||
let ctx = LocationCtx::new(db, module.id, file_id.into());
|
let ctx = LocationCtx::new(db, module.id, child.file_id);
|
||||||
|
|
||||||
node.ancestors().find_map(|node| {
|
child.ast.ancestors().find_map(|node| {
|
||||||
match_ast! {
|
match_ast! {
|
||||||
match node {
|
match node {
|
||||||
ast::FnDef(def) => { Some(Function {id: ctx.to_def(&def) }.into()) },
|
ast::FnDef(def) => { Some(Function {id: ctx.to_def(&def) }.into()) },
|
||||||
|
@ -142,7 +141,7 @@ impl SourceAnalyzer {
|
||||||
node: &SyntaxNode,
|
node: &SyntaxNode,
|
||||||
offset: Option<TextUnit>,
|
offset: Option<TextUnit>,
|
||||||
) -> SourceAnalyzer {
|
) -> 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 {
|
if let Some(def) = def_with_body {
|
||||||
let source_map = def.body_source_map(db);
|
let source_map = def.body_source_map(db);
|
||||||
let scopes = def.expr_scopes(db);
|
let scopes = def.expr_scopes(db);
|
||||||
|
|
|
@ -78,14 +78,13 @@ impl ModuleSource {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn from_child_node(
|
pub fn from_child_node(db: &impl db::DefDatabase2, child: Source<&SyntaxNode>) -> ModuleSource {
|
||||||
db: &impl db::DefDatabase2,
|
if let Some(m) =
|
||||||
file_id: FileId,
|
child.ast.ancestors().filter_map(ast::Module::cast).find(|it| !it.has_semi())
|
||||||
child: &SyntaxNode,
|
{
|
||||||
) -> ModuleSource {
|
|
||||||
if let Some(m) = child.ancestors().filter_map(ast::Module::cast).find(|it| !it.has_semi()) {
|
|
||||||
ModuleSource::Module(m)
|
ModuleSource::Module(m)
|
||||||
} else {
|
} else {
|
||||||
|
let file_id = child.file_id.original_file(db);
|
||||||
let source_file = db.parse(file_id).tree();
|
let source_file = db.parse(file_id).tree();
|
||||||
ModuleSource::SourceFile(source_file)
|
ModuleSource::SourceFile(source_file)
|
||||||
}
|
}
|
||||||
|
|
|
@ -230,6 +230,10 @@ pub struct Source<T> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> 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> {
|
pub fn map<F: FnOnce(T) -> U, U>(self, f: F) -> Source<U> {
|
||||||
Source { file_id: self.file_id, ast: f(self.ast) }
|
Source { file_id: self.file_id, ast: f(self.ast) }
|
||||||
}
|
}
|
||||||
|
|
|
@ -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 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
|
// FIXME: find correct container and visibility for each case
|
||||||
let container = Module::from_definition(db, Source { file_id, ast })?;
|
let container = Module::from_definition(db, Source { file_id, ast })?;
|
||||||
let visibility = None;
|
let visibility = None;
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
//! FIXME: write short doc here
|
//! FIXME: write short doc here
|
||||||
|
|
||||||
|
use hir::Source;
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
use ra_db::SourceDatabase;
|
use ra_db::SourceDatabase;
|
||||||
use ra_syntax::{
|
use ra_syntax::{
|
||||||
|
@ -65,9 +66,8 @@ fn runnable_mod(db: &RootDatabase, file_id: FileId, module: ast::Module) -> Opti
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
let range = module.syntax().text_range();
|
let range = module.syntax().text_range();
|
||||||
let src = hir::ModuleSource::from_child_node(db, file_id, &module.syntax());
|
let src = hir::ModuleSource::from_child_node(db, Source::new(file_id.into(), &module.syntax()));
|
||||||
let module =
|
let module = hir::Module::from_definition(db, Source::new(file_id.into(), src))?;
|
||||||
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("::");
|
let path = module.path_to_root(db).into_iter().rev().filter_map(|it| it.name(db)).join("::");
|
||||||
Some(Runnable { range, kind: RunnableKind::TestMod { path } })
|
Some(Runnable { range, kind: RunnableKind::TestMod { path } })
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue