hard-code expansion of query_group

This commit is contained in:
Aleksey Kladov 2019-01-01 19:23:03 +03:00
parent 832bae8e28
commit e5b2fd6771
2 changed files with 60 additions and 8 deletions

View file

@ -44,7 +44,7 @@ mod tests {
fn main() {
ctry!({ let x = 92; x});
}
",
",
);
let highlights = analysis.highlight(file_id).unwrap();
assert_eq_dbg(
@ -60,4 +60,26 @@ mod tests {
&highlights,
)
}
// FIXME: this test is not really necessary: artifact of the inital hacky
// macros implementation.
#[test]
fn highlight_query_group_macro() {
let (analysis, file_id) = single_file(
"
salsa::query_group! {
pub trait HirDatabase: SyntaxDatabase {}
}
",
);
let highlights = analysis.highlight(file_id).unwrap();
assert_eq_dbg(
r#"[HighlightedRange { range: [20; 32), tag: "macro" },
HighlightedRange { range: [13; 18), tag: "text" },
HighlightedRange { range: [51; 54), tag: "keyword" },
HighlightedRange { range: [55; 60), tag: "keyword" },
HighlightedRange { range: [61; 72), tag: "function" }]"#,
&highlights,
)
}
}

View file

@ -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<MacroExpansion> {
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<MacroExpansion> {
let text = format!(
r"
fn dummy() {{
@ -101,6 +107,30 @@ impl MacroDef {
};
Some(res)
}
fn expand_query_group(self, input: MacroInput) -> Option<MacroExpansion> {
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::<String>();
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)]