Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit c017233

Browse files
committed
add attr-related make functions
1 parent 4de7cbe commit c017233

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

crates/syntax/src/ast/make.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
//! `parse(format!())` we use internally is an implementation detail -- long
1111
//! term, it will be replaced with direct tree manipulation.
1212
use itertools::Itertools;
13+
use parser::T;
14+
use rowan::NodeOrToken;
1315
use stdx::{format_to, never};
1416

1517
use crate::{ast, utils::is_raw_identifier, AstNode, SourceFile, SyntaxKind, SyntaxToken};
@@ -1030,6 +1032,41 @@ pub fn struct_(
10301032
ast_from_text(&format!("{visibility}struct {strukt_name}{type_params}{field_list}{semicolon}",))
10311033
}
10321034

1035+
pub fn attr_outer(meta: ast::Meta) -> ast::Attr {
1036+
ast_from_text(&format!("#[{meta}]"))
1037+
}
1038+
1039+
pub fn attr_inner(meta: ast::Meta) -> ast::Attr {
1040+
ast_from_text(&format!("#![{meta}]"))
1041+
}
1042+
1043+
pub fn meta_expr(path: ast::Path, expr: ast::Expr) -> ast::Meta {
1044+
ast_from_text(&format!("#[{path} = {expr}]"))
1045+
}
1046+
1047+
pub fn meta_token_tree(path: ast::Path, tt: ast::TokenTree) -> ast::Meta {
1048+
ast_from_text(&format!("#[{path}{tt}]"))
1049+
}
1050+
1051+
pub fn meta_path(path: ast::Path) -> ast::Meta {
1052+
ast_from_text(&format!("#[{path}]"))
1053+
}
1054+
1055+
pub fn token_tree(
1056+
delimiter: SyntaxKind,
1057+
tt: Vec<NodeOrToken<ast::TokenTree, SyntaxToken>>,
1058+
) -> ast::TokenTree {
1059+
let (l_delimiter, r_delimiter) = match delimiter {
1060+
T!['('] => ('(', ')'),
1061+
T!['['] => ('[', ']'),
1062+
T!['{'] => ('{', '}'),
1063+
_ => panic!("invalid delimiter `{delimiter:?}`"),
1064+
};
1065+
let tt = tt.into_iter().join("");
1066+
1067+
ast_from_text(&format!("tt!{l_delimiter}{tt}{r_delimiter}"))
1068+
}
1069+
10331070
#[track_caller]
10341071
fn ast_from_text<N: AstNode>(text: &str) -> N {
10351072
let parse = SourceFile::parse(text);

0 commit comments

Comments
 (0)