From fd52d721e1ed9794048d63e546f43805d24d7ab8 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 17 Nov 2019 20:15:55 +0300 Subject: More correct expansion mapping We can't really map arbitrary ranges, we only can map tokens --- crates/ra_hir_expand/src/lib.rs | 44 ++++++++++++++++++++++++++++------------- 1 file changed, 30 insertions(+), 14 deletions(-) (limited to 'crates/ra_hir_expand') diff --git a/crates/ra_hir_expand/src/lib.rs b/crates/ra_hir_expand/src/lib.rs index 5927a03ba..57e2e6cb1 100644 --- a/crates/ra_hir_expand/src/lib.rs +++ b/crates/ra_hir_expand/src/lib.rs @@ -18,8 +18,9 @@ use std::sync::Arc; use ra_db::{salsa, CrateId, FileId}; use ra_syntax::{ + algo, ast::{self, AstNode}, - SyntaxNode, TextRange, TextUnit, + SyntaxNode, SyntaxToken, TextRange, TextUnit, }; use crate::ast_id_map::FileAstId; @@ -83,13 +84,21 @@ impl HirFileId { loc.def.ast_id.to_node(db).token_tree()?.syntax().text_range().start(); let macro_def = db.macro_def(loc.def)?; - let exp_map = db.parse_macro(macro_file)?.1; + let (parse, exp_map) = db.parse_macro(macro_file)?; + let expanded = Source::new(self, parse.syntax_node()); let macro_arg = db.macro_arg(macro_file.macro_call_id)?; let arg_start = (loc.ast_id.file_id, arg_start); let def_start = (loc.def.ast_id.file_id, def_start); - Some(ExpansionInfo { arg_start, def_start, macro_arg, macro_def, exp_map }) + Some(ExpansionInfo { + expanded, + arg_start, + def_start, + macro_arg, + macro_def, + exp_map, + }) } } } @@ -146,27 +155,34 @@ impl MacroCallId { } } -#[derive(Debug, Clone, PartialEq, Eq)] /// ExpansionInfo mainly describes how to map text range between src and expanded macro +#[derive(Debug, Clone, PartialEq, Eq)] pub struct ExpansionInfo { - pub(crate) arg_start: (HirFileId, TextUnit), - pub(crate) def_start: (HirFileId, TextUnit), + expanded: Source, + arg_start: (HirFileId, TextUnit), + def_start: (HirFileId, TextUnit), - pub(crate) macro_def: Arc<(db::TokenExpander, mbe::TokenMap)>, - pub(crate) macro_arg: Arc<(tt::Subtree, mbe::TokenMap)>, - pub(crate) exp_map: Arc, + macro_def: Arc<(db::TokenExpander, mbe::TokenMap)>, + macro_arg: Arc<(tt::Subtree, mbe::TokenMap)>, + exp_map: Arc, } impl ExpansionInfo { - pub fn translate_offset(&self, offset: TextUnit) -> Option { - let offset = offset.checked_sub(self.arg_start.1)?; - let token_id = self.macro_arg.1.token_by_offset(offset)?; + pub fn map_token_down(&self, token: Source<&SyntaxToken>) -> Option> { + assert_eq!(token.file_id, self.arg_start.0); + let range = token.ast.text_range().checked_sub(self.arg_start.1)?; + let token_id = self.macro_arg.1.token_by_range(range)?; let token_id = self.macro_def.0.map_id_down(token_id); - let (r, _) = self.exp_map.ranges.iter().find(|(_, tid)| *tid == token_id)?; - Some(r.start()) + let range = self.exp_map.range_by_token(token_id)?; + + let token = algo::find_covering_element(&self.expanded.ast, range).into_token()?; + + Some(self.expanded.with_ast(token)) } + // FIXME: a more correct signature would be + // `pub fn map_token_up(&self, token: Source<&SyntaxToken>) -> Option>` pub fn find_range(&self, from: TextRange) -> Option<(HirFileId, TextRange)> { let token_id = look_in_rev_map(&self.exp_map, from)?; -- cgit v1.2.3