mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-02 06:41:48 +00:00
cleanup the api
This commit is contained in:
parent
a16f6bb27d
commit
b4b522fb39
3 changed files with 70 additions and 56 deletions
|
@ -1,8 +1,7 @@
|
|||
use ra_syntax::{ast, AstNode, SyntaxNode, SyntaxKind::*};
|
||||
|
||||
pub fn macro_call_to_tt(call: &ast::MacroCall) -> Option<tt::Subtree> {
|
||||
let tt = call.token_tree()?;
|
||||
convert_tt(tt.syntax())
|
||||
pub fn ast_to_token_tree(ast: &ast::TokenTree) -> Option<tt::Subtree> {
|
||||
convert_tt(ast.syntax())
|
||||
}
|
||||
|
||||
fn convert_tt(tt: &SyntaxNode) -> Option<tt::Subtree> {
|
||||
|
@ -66,48 +65,3 @@ fn convert_tt(tt: &SyntaxNode) -> Option<tt::Subtree> {
|
|||
};
|
||||
Some(res)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_convert_tt() {
|
||||
let macro_definition = r#"
|
||||
macro_rules! impl_froms {
|
||||
($e:ident: $($v:ident),*) => {
|
||||
$(
|
||||
impl From<$v> for $e {
|
||||
fn from(it: $v) -> $e {
|
||||
$e::$v(it)
|
||||
}
|
||||
}
|
||||
)*
|
||||
}
|
||||
}
|
||||
"#;
|
||||
|
||||
let macro_invocation = r#"
|
||||
impl_froms!(TokenTree: Leaf, Subtree);
|
||||
"#;
|
||||
|
||||
let source_file = ast::SourceFile::parse(macro_definition);
|
||||
let macro_definition = source_file
|
||||
.syntax()
|
||||
.descendants()
|
||||
.find_map(ast::MacroCall::cast)
|
||||
.unwrap();
|
||||
|
||||
let source_file = ast::SourceFile::parse(macro_invocation);
|
||||
let macro_invocation = source_file
|
||||
.syntax()
|
||||
.descendants()
|
||||
.find_map(ast::MacroCall::cast)
|
||||
.unwrap();
|
||||
|
||||
let definition_tt = macro_call_to_tt(macro_definition).unwrap();
|
||||
let invocation_tt = macro_call_to_tt(macro_invocation).unwrap();
|
||||
let mbe = crate::parse(&definition_tt).unwrap();
|
||||
let expansion = crate::exapnd(&mbe, &invocation_tt).unwrap();
|
||||
assert_eq!(
|
||||
expansion.to_string(),
|
||||
"impl From < Leaf > for TokenTree {fn from (it : Leaf) -> TokenTree {TokenTree :: Leaf (it)}} \
|
||||
impl From < Subtree > for TokenTree {fn from (it : Subtree) -> TokenTree {TokenTree :: Subtree (it)}}"
|
||||
)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue