From d75365619212983d2b7abf7b5b5e73c70dd07fb4 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 31 Dec 2018 16:05:07 +0300 Subject: introduce MacrosDatabase --- crates/ra_hir/src/lib.rs | 1 + crates/ra_hir/src/macros.rs | 57 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 crates/ra_hir/src/macros.rs (limited to 'crates/ra_hir/src') diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs index 5bbb09c01..e89410a76 100644 --- a/crates/ra_hir/src/lib.rs +++ b/crates/ra_hir/src/lib.rs @@ -22,6 +22,7 @@ mod path; mod arena; pub mod source_binder; +mod macros; mod name; mod krate; mod module; diff --git a/crates/ra_hir/src/macros.rs b/crates/ra_hir/src/macros.rs new file mode 100644 index 000000000..050b97081 --- /dev/null +++ b/crates/ra_hir/src/macros.rs @@ -0,0 +1,57 @@ +use std::sync::Arc; + +use ra_db::SyntaxDatabase; +use ra_syntax::{TextRange, TextUnit, SourceFileNode, AstNode, ast}; + +// Hard-coded defs for now :-( +#[derive(Debug, Clone, PartialEq, Eq, Hash)] +pub enum MacroDef { + CTry, +} + +#[derive(Debug, Clone, PartialEq, Eq, Hash)] +pub struct MacroInput { + // Should be token trees + text: String, +} + +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct MacroExpansion { + text: String, + ranges_map: Vec<(TextRange, TextRange)>, +} + +salsa::query_group! { + +pub trait MacrosDatabase: SyntaxDatabase { + fn expand_macro(def: MacroDef, input: MacroInput) -> Option> { + type ExpandMacroQuery; + } +} + +} + +fn expand_macro( + _db: &impl MacrosDatabase, + def: MacroDef, + input: MacroInput, +) -> Option> { + let MacroDef::CTry = def; + let text = format!( + r" + fn dummy() {{ + match {} {{ + None => return Ok(None), + Some(it) => it, + }} + }}", + input.text + ); + let file = SourceFileNode::parse(&text); + let match_expr = file.syntax().descendants().find_map(ast::MatchExpr::cast)?; + let match_arg = match_expr.expr()?; + let src_range = TextRange::offset_len(0.into(), TextUnit::of_str(&input.text)); + let ranges_map = vec![(src_range, match_arg.syntax().range())]; + let res = MacroExpansion { text, ranges_map }; + Some(Arc::new(res)) +} -- cgit v1.2.3