From b911ee542b2f4d1cd62a655f24197856cd9b9097 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 28 Dec 2018 18:21:30 +0300 Subject: move macro to a separate module --- crates/ra_analysis/src/syntax_highlighting.rs | 62 +-------------------------- 1 file changed, 2 insertions(+), 60 deletions(-) (limited to 'crates/ra_analysis/src/syntax_highlighting.rs') diff --git a/crates/ra_analysis/src/syntax_highlighting.rs b/crates/ra_analysis/src/syntax_highlighting.rs index 98551df8f..38219da71 100644 --- a/crates/ra_analysis/src/syntax_highlighting.rs +++ b/crates/ra_analysis/src/syntax_highlighting.rs @@ -1,4 +1,4 @@ -use ra_syntax::{ast, AstNode, SourceFileNode, TextRange}; +use ra_syntax::{ast, AstNode,}; use ra_editor::HighlightedRange; use ra_db::SyntaxDatabase; @@ -15,7 +15,7 @@ pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Cancelable Cancelable Option { - let path = macro_call.path()?; - if path.qualifier().is_some() { - return None; - } - let name_ref = path.segment()?.name_ref()?; - if name_ref.text() != "ctry" { - return None; - } - - let arg = macro_call.token_tree()?; - let text = format!( - r" - fn dummy() {{ - match {} {{ - None => return Ok(None), - Some(it) => it, - }} - }}", - arg.syntax().text() - ); - let file = SourceFileNode::parse(&text); - let match_expr = file.syntax().descendants().find_map(ast::MatchExpr::cast)?; - let match_arg = match_expr.expr()?; - let ranges_map = vec![(arg.syntax().range(), match_arg.syntax().range())]; - let res = MacroExpansion { - source_file: file, - ranges_map, - }; - Some(res) -} - -struct MacroExpansion { - source_file: SourceFileNode, - ranges_map: Vec<(TextRange, TextRange)>, -} - -impl MacroExpansion { - fn source_file(&self) -> &SourceFileNode { - &self.source_file - } - fn map_range_back(&self, tgt_range: TextRange) -> Option { - for (s_range, t_range) in self.ranges_map.iter() { - if tgt_range.is_subrange(&t_range) { - let tgt_at_zero_range = tgt_range - tgt_range.start(); - let tgt_range_offset = tgt_range.start() - t_range.start(); - let src_range = tgt_at_zero_range + tgt_range_offset + s_range.start(); - return Some(src_range); - } - } - None - } -} - #[cfg(test)] mod tests { use crate::mock_analysis::single_file; -- cgit v1.2.3