From 159da285e9d8594a2cc4436b5cee7874b07806c9 Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Sun, 3 Nov 2019 22:46:12 +0800 Subject: Add macro_expansion_info in hir_expand --- crates/ra_hir_expand/src/lib.rs | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) (limited to 'crates/ra_hir_expand/src/lib.rs') diff --git a/crates/ra_hir_expand/src/lib.rs b/crates/ra_hir_expand/src/lib.rs index dd07a16b4..194020b45 100644 --- a/crates/ra_hir_expand/src/lib.rs +++ b/crates/ra_hir_expand/src/lib.rs @@ -16,7 +16,7 @@ use std::hash::{Hash, Hasher}; use ra_db::{salsa, CrateId, FileId}; use ra_syntax::{ ast::{self, AstNode}, - SyntaxNode, + SyntaxNode, TextRange, }; use crate::ast_id_map::FileAstId; @@ -112,6 +112,39 @@ impl MacroCallId { } } +#[derive(Debug, Clone, PartialEq, Eq)] +/// ExpansionInfo mainly describle how to map text range between src and expaned macro +pub struct ExpansionInfo { + pub arg_map: Vec<(TextRange, TextRange)>, + pub def_map: Vec<(TextRange, TextRange)>, +} + +impl ExpansionInfo { + pub fn find_range( + &self, + from: TextRange, + (arg_file_id, def_file_id): (HirFileId, HirFileId), + ) -> Option<(HirFileId, TextRange)> { + for (src, dest) in &self.arg_map { + dbg!((src, *dest, "arg_map")); + if src.is_subrange(&from) { + dbg!((arg_file_id, *dest)); + return Some((arg_file_id, *dest)); + } + } + + for (src, dest) in &self.def_map { + dbg!((src, *dest, "def_map")); + if src.is_subrange(&from) { + dbg!((arg_file_id, *dest)); + return Some((def_file_id, *dest)); + } + } + + None + } +} + /// `AstId` points to an AST node in any file. /// /// It is stable across reparses, and can be used as salsa key/value. -- cgit v1.2.3 From ae609d7953297b355616c7862b8deefe74a8f95f Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Mon, 4 Nov 2019 01:44:23 +0800 Subject: Add parent_expansion to HirFileId --- crates/ra_hir_expand/src/lib.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'crates/ra_hir_expand/src/lib.rs') diff --git a/crates/ra_hir_expand/src/lib.rs b/crates/ra_hir_expand/src/lib.rs index 194020b45..180cd1f57 100644 --- a/crates/ra_hir_expand/src/lib.rs +++ b/crates/ra_hir_expand/src/lib.rs @@ -20,6 +20,7 @@ use ra_syntax::{ }; use crate::ast_id_map::FileAstId; +use std::sync::Arc; /// Input to the analyzer is a set of files, where each file is identified by /// `FileId` and contains source code. However, another source of source code in @@ -66,6 +67,24 @@ impl HirFileId { } } } + + /// Return expansion information if it is a macro-expansion file + pub fn parent_expansion( + self, + db: &dyn db::AstDatabase, + ) -> Option<((HirFileId, HirFileId), Arc)> { + match self.0 { + HirFileIdRepr::FileId(_) => None, + HirFileIdRepr::MacroFile(macro_file) => { + let loc: MacroCallLoc = db.lookup_intern_macro(macro_file.macro_call_id); + + let def_file = loc.def.ast_id.file_id; + let arg_file = loc.ast_id.file_id; + + db.macro_expansion_info(macro_file).map(|ex| ((arg_file, def_file), ex)) + } + } + } } #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] -- cgit v1.2.3 From 67226ebc8288b9489867ea2454f9d976b65ff15e Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Mon, 4 Nov 2019 01:47:59 +0800 Subject: Remove dbg! --- crates/ra_hir_expand/src/lib.rs | 4 ---- 1 file changed, 4 deletions(-) (limited to 'crates/ra_hir_expand/src/lib.rs') diff --git a/crates/ra_hir_expand/src/lib.rs b/crates/ra_hir_expand/src/lib.rs index 180cd1f57..b6e55aa6a 100644 --- a/crates/ra_hir_expand/src/lib.rs +++ b/crates/ra_hir_expand/src/lib.rs @@ -145,17 +145,13 @@ impl ExpansionInfo { (arg_file_id, def_file_id): (HirFileId, HirFileId), ) -> Option<(HirFileId, TextRange)> { for (src, dest) in &self.arg_map { - dbg!((src, *dest, "arg_map")); if src.is_subrange(&from) { - dbg!((arg_file_id, *dest)); return Some((arg_file_id, *dest)); } } for (src, dest) in &self.def_map { - dbg!((src, *dest, "def_map")); if src.is_subrange(&from) { - dbg!((arg_file_id, *dest)); return Some((def_file_id, *dest)); } } -- cgit v1.2.3 From e6709f64af1836460aee41aca34eb19ed3a337dd Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Mon, 4 Nov 2019 03:12:19 +0800 Subject: Rename and fix typos --- crates/ra_hir_expand/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'crates/ra_hir_expand/src/lib.rs') diff --git a/crates/ra_hir_expand/src/lib.rs b/crates/ra_hir_expand/src/lib.rs index b6e55aa6a..63ba17158 100644 --- a/crates/ra_hir_expand/src/lib.rs +++ b/crates/ra_hir_expand/src/lib.rs @@ -132,7 +132,7 @@ impl MacroCallId { } #[derive(Debug, Clone, PartialEq, Eq)] -/// ExpansionInfo mainly describle how to map text range between src and expaned macro +/// ExpansionInfo mainly describes how to map text range between src and expanded macro pub struct ExpansionInfo { pub arg_map: Vec<(TextRange, TextRange)>, pub def_map: Vec<(TextRange, TextRange)>, -- cgit v1.2.3 From d01e0abdb57c5be340934cb51e2f6b1a2e6c0373 Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Sat, 9 Nov 2019 04:00:27 +0800 Subject: Refactor and simpfily --- crates/ra_hir_expand/src/lib.rs | 40 ++++++++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 16 deletions(-) (limited to 'crates/ra_hir_expand/src/lib.rs') diff --git a/crates/ra_hir_expand/src/lib.rs b/crates/ra_hir_expand/src/lib.rs index 63ba17158..1a1d6bdd4 100644 --- a/crates/ra_hir_expand/src/lib.rs +++ b/crates/ra_hir_expand/src/lib.rs @@ -20,7 +20,6 @@ use ra_syntax::{ }; use crate::ast_id_map::FileAstId; -use std::sync::Arc; /// Input to the analyzer is a set of files, where each file is identified by /// `FileId` and contains source code. However, another source of source code in @@ -69,19 +68,29 @@ impl HirFileId { } /// Return expansion information if it is a macro-expansion file - pub fn parent_expansion( - self, - db: &dyn db::AstDatabase, - ) -> Option<((HirFileId, HirFileId), Arc)> { + pub fn parent_expansion(self, db: &dyn db::AstDatabase) -> Option { match self.0 { HirFileIdRepr::FileId(_) => None, HirFileIdRepr::MacroFile(macro_file) => { let loc: MacroCallLoc = db.lookup_intern_macro(macro_file.macro_call_id); - let def_file = loc.def.ast_id.file_id; + let arg_range = loc.ast_id.to_node(db).token_tree()?.syntax().text_range(); + let def_range = loc.def.ast_id.to_node(db).token_tree()?.syntax().text_range(); + + let macro_def = db.macro_def(loc.def)?; + let shift = macro_def.0.shift(); + let rev_map = db.parse_macro(macro_file)?.1; + + let arg_token_map = db.macro_arg(macro_file.macro_call_id)?.1; + let def_token_map = macro_def.1; + + let arg_map = rev_map.map_ranges(&arg_token_map, arg_range, shift); + let def_map = rev_map.map_ranges(&def_token_map, def_range, 0); + let arg_file = loc.ast_id.file_id; + let def_file = loc.def.ast_id.file_id; - db.macro_expansion_info(macro_file).map(|ex| ((arg_file, def_file), ex)) + Some(ExpansionInfo { arg_file, def_file, arg_map, def_map }) } } } @@ -134,25 +143,24 @@ impl MacroCallId { #[derive(Debug, Clone, PartialEq, Eq)] /// ExpansionInfo mainly describes how to map text range between src and expanded macro pub struct ExpansionInfo { - pub arg_map: Vec<(TextRange, TextRange)>, - pub def_map: Vec<(TextRange, TextRange)>, + pub(crate) arg_file: HirFileId, + pub(crate) def_file: HirFileId, + + pub(crate) arg_map: Vec<(TextRange, TextRange)>, + pub(crate) def_map: Vec<(TextRange, TextRange)>, } impl ExpansionInfo { - pub fn find_range( - &self, - from: TextRange, - (arg_file_id, def_file_id): (HirFileId, HirFileId), - ) -> Option<(HirFileId, TextRange)> { + pub fn find_range(&self, from: TextRange) -> Option<(HirFileId, TextRange)> { for (src, dest) in &self.arg_map { if src.is_subrange(&from) { - return Some((arg_file_id, *dest)); + return Some((self.arg_file, *dest)); } } for (src, dest) in &self.def_map { if src.is_subrange(&from) { - return Some((def_file_id, *dest)); + return Some((self.def_file, *dest)); } } -- cgit v1.2.3 From 0a5ec69404a2556dd82e5bb00b295aebaa291f04 Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Sat, 9 Nov 2019 12:00:46 +0800 Subject: Remove map_ranges in RevTokenMap --- crates/ra_hir_expand/src/lib.rs | 55 ++++++++++++++++++++--------------------- 1 file changed, 27 insertions(+), 28 deletions(-) (limited to 'crates/ra_hir_expand/src/lib.rs') diff --git a/crates/ra_hir_expand/src/lib.rs b/crates/ra_hir_expand/src/lib.rs index 1a1d6bdd4..b219b8fbf 100644 --- a/crates/ra_hir_expand/src/lib.rs +++ b/crates/ra_hir_expand/src/lib.rs @@ -12,11 +12,12 @@ pub mod hygiene; pub mod diagnostics; use std::hash::{Hash, Hasher}; +use std::sync::Arc; use ra_db::{salsa, CrateId, FileId}; use ra_syntax::{ ast::{self, AstNode}, - SyntaxNode, TextRange, + SyntaxNode, TextRange, TextUnit, }; use crate::ast_id_map::FileAstId; @@ -68,29 +69,25 @@ impl HirFileId { } /// Return expansion information if it is a macro-expansion file - pub fn parent_expansion(self, db: &dyn db::AstDatabase) -> Option { + pub fn expansion_info(self, db: &dyn db::AstDatabase) -> Option { match self.0 { HirFileIdRepr::FileId(_) => None, HirFileIdRepr::MacroFile(macro_file) => { let loc: MacroCallLoc = db.lookup_intern_macro(macro_file.macro_call_id); - let arg_range = loc.ast_id.to_node(db).token_tree()?.syntax().text_range(); - let def_range = loc.def.ast_id.to_node(db).token_tree()?.syntax().text_range(); + let arg_start = loc.ast_id.to_node(db).token_tree()?.syntax().text_range().start(); + let def_start = + loc.def.ast_id.to_node(db).token_tree()?.syntax().text_range().start(); let macro_def = db.macro_def(loc.def)?; let shift = macro_def.0.shift(); - let rev_map = db.parse_macro(macro_file)?.1; + let exp_map = db.parse_macro(macro_file)?.1; + let macro_arg = db.macro_arg(macro_file.macro_call_id)?; - let arg_token_map = db.macro_arg(macro_file.macro_call_id)?.1; - let def_token_map = macro_def.1; + let arg_start = (loc.ast_id.file_id, arg_start); + let def_start = (loc.def.ast_id.file_id, def_start); - let arg_map = rev_map.map_ranges(&arg_token_map, arg_range, shift); - let def_map = rev_map.map_ranges(&def_token_map, def_range, 0); - - let arg_file = loc.ast_id.file_id; - let def_file = loc.def.ast_id.file_id; - - Some(ExpansionInfo { arg_file, def_file, arg_map, def_map }) + Some(ExpansionInfo { arg_start, def_start, macro_arg, macro_def, exp_map, shift }) } } } @@ -143,28 +140,30 @@ impl MacroCallId { #[derive(Debug, Clone, PartialEq, Eq)] /// ExpansionInfo mainly describes how to map text range between src and expanded macro pub struct ExpansionInfo { - pub(crate) arg_file: HirFileId, - pub(crate) def_file: HirFileId, + pub(crate) arg_start: (HirFileId, TextUnit), + pub(crate) def_start: (HirFileId, TextUnit), + pub(crate) shift: u32, - pub(crate) arg_map: Vec<(TextRange, TextRange)>, - pub(crate) def_map: Vec<(TextRange, TextRange)>, + pub(crate) macro_def: Arc<(mbe::MacroRules, mbe::TokenMap)>, + pub(crate) macro_arg: Arc<(tt::Subtree, mbe::TokenMap)>, + pub(crate) exp_map: Arc, } impl ExpansionInfo { pub fn find_range(&self, from: TextRange) -> Option<(HirFileId, TextRange)> { - for (src, dest) in &self.arg_map { - if src.is_subrange(&from) { - return Some((self.arg_file, *dest)); - } + fn look_in_rev_map(exp_map: &mbe::RevTokenMap, from: TextRange) -> Option { + exp_map.ranges.iter().find(|&it| it.0.is_subrange(&from)).map(|it| it.1) } - for (src, dest) in &self.def_map { - if src.is_subrange(&from) { - return Some((self.def_file, *dest)); - } - } + let token_id = look_in_rev_map(&self.exp_map, from)?; + let (token_map, file_offset, token_id) = if token_id.0 >= self.shift { + (&self.macro_arg.1, self.arg_start, tt::TokenId(token_id.0 - self.shift).into()) + } else { + (&self.macro_def.1, self.def_start, token_id) + }; - None + let range = token_map.relative_range_of(token_id)?; + Some((file_offset.0, TextRange::offset_len(range.start() + file_offset.1, range.len()))) } } -- cgit v1.2.3