Add an expr_source method analogous to the source methods in the code model

... and use that instead of exposing the source map.
This commit is contained in:
Florian Diebold 2019-08-30 20:16:28 +02:00
parent a7858bb7bf
commit f92177cfb5
8 changed files with 107 additions and 60 deletions

View file

@ -1,9 +1,9 @@
use ra_syntax::ast;
use ra_syntax::ast::{self, AstNode};
use crate::{
ids::AstItemDef, AstDatabase, Const, DefDatabase, Enum, EnumVariant, FieldSource, Function,
HirFileId, MacroDef, Module, ModuleSource, Static, Struct, StructField, Trait, TypeAlias,
Union,
HasBody, HirDatabase, HirFileId, MacroDef, Module, ModuleSource, Static, Struct, StructField,
Trait, TypeAlias, Union,
};
pub struct Source<T> {
@ -108,3 +108,27 @@ impl HasSource for MacroDef {
Source { file_id: self.id.0.file_id(), ast: self.id.0.to_node(db) }
}
}
pub trait HasBodySource: HasBody + HasSource
where
Self::Ast: AstNode,
{
fn expr_source(
self,
db: &impl HirDatabase,
expr_id: crate::expr::ExprId,
) -> Option<Source<ast::Expr>> {
let source_map = self.body_source_map(db);
let expr_syntax = source_map.expr_syntax(expr_id)?;
let source = self.source(db);
let node = expr_syntax.to_node(&source.ast.syntax());
ast::Expr::cast(node).map(|ast| Source { file_id: source.file_id, ast })
}
}
impl<T> HasBodySource for T
where
T: HasBody + HasSource,
T::Ast: AstNode,
{
}