From e5b2fd67711eeab6146021542937028dff3bf8a7 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 1 Jan 2019 19:23:03 +0300 Subject: hard-code expansion of query_group --- crates/ra_hir/src/macros.rs | 44 +++++++++++++++++++++++++++++++++++++------- 1 file changed, 37 insertions(+), 7 deletions(-) (limited to 'crates/ra_hir/src') diff --git a/crates/ra_hir/src/macros.rs b/crates/ra_hir/src/macros.rs index d9de2d8bd..932e2b574 100644 --- a/crates/ra_hir/src/macros.rs +++ b/crates/ra_hir/src/macros.rs @@ -3,7 +3,7 @@ use std::sync::Arc; use ra_db::{LocalSyntaxPtr, LocationIntener}; use ra_syntax::{ TextRange, TextUnit, SourceFileNode, AstNode, SyntaxNode, - ast, + ast::{self, NameOwner}, }; use crate::{SourceRootId, module::ModuleId, SourceItemId, HirDatabase}; @@ -44,6 +44,7 @@ impl MacroCallLoc { #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub enum MacroDef { CTry, + QueryGroup, } impl MacroDef { @@ -57,14 +58,14 @@ impl MacroDef { fn from_call(macro_call: ast::MacroCall) -> Option<(MacroDef, MacroInput)> { let def = { let path = macro_call.path()?; - if path.qualifier().is_some() { - return None; - } let name_ref = path.segment()?.name_ref()?; - if name_ref.text() != "ctry" { + if name_ref.text() == "ctry" { + MacroDef::CTry + } else if name_ref.text() == "query_group" { + MacroDef::QueryGroup + } else { return None; } - MacroDef::CTry }; let input = { @@ -77,7 +78,12 @@ impl MacroDef { } fn expand(self, input: MacroInput) -> Option { - let MacroDef::CTry = self; + match self { + MacroDef::CTry => self.expand_ctry(input), + MacroDef::QueryGroup => self.expand_query_group(input), + } + } + fn expand_ctry(self, input: MacroInput) -> Option { let text = format!( r" fn dummy() {{ @@ -101,6 +107,30 @@ impl MacroDef { }; Some(res) } + fn expand_query_group(self, input: MacroInput) -> Option { + let anchor = "trait "; + let pos = input.text.find(anchor)? + anchor.len(); + let trait_name = input.text[pos..] + .chars() + .take_while(|c| c.is_alphabetic()) + .collect::(); + if trait_name.is_empty() { + return None; + } + let src_range = TextRange::offset_len((pos as u32).into(), TextUnit::of_str(&trait_name)); + let text = format!(r"trait {} {{ }}", trait_name); + let file = SourceFileNode::parse(&text); + let trait_def = file.syntax().descendants().find_map(ast::TraitDef::cast)?; + let name = trait_def.name()?; + let ptr = LocalSyntaxPtr::new(trait_def.syntax()); + let ranges_map = vec![(src_range, name.syntax().range())]; + let res = MacroExpansion { + text, + ranges_map, + ptr, + }; + Some(res) + } } #[derive(Debug, Clone, PartialEq, Eq, Hash)] -- cgit v1.2.3