From f92177cfb5088809892455262841e24cf1ecf5b6 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Fri, 30 Aug 2019 20:16:28 +0200 Subject: Add an expr_source method analogous to the source methods in the code model ... and use that instead of exposing the source map. --- crates/ra_hir/src/code_model/src.rs | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) (limited to 'crates/ra_hir/src/code_model') diff --git a/crates/ra_hir/src/code_model/src.rs b/crates/ra_hir/src/code_model/src.rs index 32bd9c661..e5bae16ab 100644 --- a/crates/ra_hir/src/code_model/src.rs +++ b/crates/ra_hir/src/code_model/src.rs @@ -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 { @@ -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> { + 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 HasBodySource for T +where + T: HasBody + HasSource, + T::Ast: AstNode, +{ +} -- cgit v1.2.3