722: remove hard-coded support for ctry macro r=matklad a=matklad

It was used mainly to prevent HirFileId infra from bitroting, but the
`vec![]` macro can serve that just as well!

Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
bors[bot] 2019-02-01 07:54:32 +00:00
commit 21c5935931
5 changed files with 16 additions and 73 deletions

View file

@ -19,7 +19,6 @@ use crate::{HirDatabase, MacroCallId};
// Hard-coded defs for now :-( // Hard-coded defs for now :-(
#[derive(Debug, Clone, PartialEq, Eq, Hash)] #[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum MacroDef { pub enum MacroDef {
CTry,
Vec, Vec,
QueryGroup, QueryGroup,
} }
@ -38,9 +37,7 @@ impl MacroDef {
let def = { let def = {
let path = macro_call.path()?; let path = macro_call.path()?;
let name_ref = path.segment()?.name_ref()?; let name_ref = path.segment()?.name_ref()?;
if name_ref.text() == "ctry" { if name_ref.text() == "vec" {
MacroDef::CTry
} else if name_ref.text() == "vec" {
MacroDef::Vec MacroDef::Vec
} else if name_ref.text() == "query_group" { } else if name_ref.text() == "query_group" {
MacroDef::QueryGroup MacroDef::QueryGroup
@ -60,35 +57,10 @@ impl MacroDef {
fn expand(self, input: MacroInput) -> Option<MacroExpansion> { fn expand(self, input: MacroInput) -> Option<MacroExpansion> {
match self { match self {
MacroDef::CTry => self.expand_ctry(input),
MacroDef::Vec => self.expand_vec(input), MacroDef::Vec => self.expand_vec(input),
MacroDef::QueryGroup => self.expand_query_group(input), MacroDef::QueryGroup => self.expand_query_group(input),
} }
} }
fn expand_ctry(self, input: MacroInput) -> Option<MacroExpansion> {
let text = format!(
r"
fn dummy() {{
match {} {{
None => return Ok(None),
Some(it) => it,
}}
}}",
input.text
);
let file = SourceFile::parse(&text);
let match_expr = file.syntax().descendants().find_map(ast::MatchExpr::cast)?;
let match_arg = match_expr.expr()?;
let ptr = SyntaxNodePtr::new(match_arg.syntax());
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,
ptr,
};
Some(res)
}
fn expand_vec(self, input: MacroInput) -> Option<MacroExpansion> { fn expand_vec(self, input: MacroInput) -> Option<MacroExpansion> {
let text = format!(r"fn dummy() {{ {}; }}", input.text); let text = format!(r"fn dummy() {{ {}; }}", input.text);
let file = SourceFile::parse(&text); let file = SourceFile::parse(&text);

View file

@ -47,11 +47,11 @@ mod tests {
let (analysis, frange) = single_file_with_range( let (analysis, frange) = single_file_with_range(
" "
fn main() { fn main() {
ctry!(foo(|x| <|>x<|>)); vec![foo(|x| <|>x<|>)];
} }
", ",
); );
let r = analysis.extend_selection(frange).unwrap(); let r = analysis.extend_selection(frange).unwrap();
assert_eq!(r, TextRange::from_to(51.into(), 56.into())); assert_eq!(r, TextRange::from_to(50.into(), 55.into()));
} }
} }

View file

@ -1,8 +1,8 @@
--- ---
created: "2019-01-22T14:45:01.017117100+00:00" created: "2019-02-01T07:52:15.689836752+00:00"
creator: insta@0.4.0 creator: insta@0.5.3
expression: "&highlights" expression: "&highlights"
source: "crates\\ra_ide_api\\src\\syntax_highlighting.rs" source: crates/ra_ide_api/src/syntax_highlighting.rs
--- ---
[ [
HighlightedRange { HighlightedRange {

View file

@ -1,8 +1,8 @@
--- ---
created: "2019-01-22T14:45:01.043047100+00:00" created: "2019-02-01T07:46:59.130146403+00:00"
creator: insta@0.4.0 creator: insta@0.5.3
expression: "&highlights" expression: "&highlights"
source: "crates\\ra_ide_api\\src\\syntax_highlighting.rs" source: crates/ra_ide_api/src/syntax_highlighting.rs
--- ---
[ [
HighlightedRange { HighlightedRange {
@ -14,59 +14,31 @@ source: "crates\\ra_ide_api\\src\\syntax_highlighting.rs"
tag: "function" tag: "function"
}, },
HighlightedRange { HighlightedRange {
range: [41; 46), range: [41; 45),
tag: "macro" tag: "macro"
}, },
HighlightedRange { HighlightedRange {
range: [49; 52), range: [48; 51),
tag: "keyword" tag: "keyword"
}, },
HighlightedRange { HighlightedRange {
range: [57; 59), range: [56; 58),
tag: "literal" tag: "literal"
}, },
HighlightedRange { HighlightedRange {
range: [82; 86), range: [48; 51),
tag: "macro"
},
HighlightedRange {
range: [89; 92),
tag: "keyword" tag: "keyword"
}, },
HighlightedRange { HighlightedRange {
range: [97; 99), range: [52; 53),
tag: "literal"
},
HighlightedRange {
range: [49; 52),
tag: "keyword"
},
HighlightedRange {
range: [53; 54),
tag: "function" tag: "function"
}, },
HighlightedRange { HighlightedRange {
range: [57; 59), range: [56; 58),
tag: "literal" tag: "literal"
}, },
HighlightedRange { HighlightedRange {
range: [61; 62), range: [60; 61),
tag: "text"
},
HighlightedRange {
range: [89; 92),
tag: "keyword"
},
HighlightedRange {
range: [93; 94),
tag: "function"
},
HighlightedRange {
range: [97; 99),
tag: "literal"
},
HighlightedRange {
range: [101; 102),
tag: "text" tag: "text"
} }
] ]

View file

@ -42,7 +42,6 @@ mod tests {
let (analysis, file_id) = single_file( let (analysis, file_id) = single_file(
" "
fn main() { fn main() {
ctry!({ let x = 92; x});
vec![{ let x = 92; x}]; vec![{ let x = 92; x}];
} }
", ",