mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-28 04:44:57 +00:00
add attr-related make
functions
This commit is contained in:
parent
4de7cbe04c
commit
c0172333c2
1 changed files with 37 additions and 0 deletions
|
@ -10,6 +10,8 @@
|
||||||
//! `parse(format!())` we use internally is an implementation detail -- long
|
//! `parse(format!())` we use internally is an implementation detail -- long
|
||||||
//! term, it will be replaced with direct tree manipulation.
|
//! term, it will be replaced with direct tree manipulation.
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
|
use parser::T;
|
||||||
|
use rowan::NodeOrToken;
|
||||||
use stdx::{format_to, never};
|
use stdx::{format_to, never};
|
||||||
|
|
||||||
use crate::{ast, utils::is_raw_identifier, AstNode, SourceFile, SyntaxKind, SyntaxToken};
|
use crate::{ast, utils::is_raw_identifier, AstNode, SourceFile, SyntaxKind, SyntaxToken};
|
||||||
|
@ -1030,6 +1032,41 @@ pub fn struct_(
|
||||||
ast_from_text(&format!("{visibility}struct {strukt_name}{type_params}{field_list}{semicolon}",))
|
ast_from_text(&format!("{visibility}struct {strukt_name}{type_params}{field_list}{semicolon}",))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn attr_outer(meta: ast::Meta) -> ast::Attr {
|
||||||
|
ast_from_text(&format!("#[{meta}]"))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn attr_inner(meta: ast::Meta) -> ast::Attr {
|
||||||
|
ast_from_text(&format!("#![{meta}]"))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn meta_expr(path: ast::Path, expr: ast::Expr) -> ast::Meta {
|
||||||
|
ast_from_text(&format!("#[{path} = {expr}]"))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn meta_token_tree(path: ast::Path, tt: ast::TokenTree) -> ast::Meta {
|
||||||
|
ast_from_text(&format!("#[{path}{tt}]"))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn meta_path(path: ast::Path) -> ast::Meta {
|
||||||
|
ast_from_text(&format!("#[{path}]"))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn token_tree(
|
||||||
|
delimiter: SyntaxKind,
|
||||||
|
tt: Vec<NodeOrToken<ast::TokenTree, SyntaxToken>>,
|
||||||
|
) -> ast::TokenTree {
|
||||||
|
let (l_delimiter, r_delimiter) = match delimiter {
|
||||||
|
T!['('] => ('(', ')'),
|
||||||
|
T!['['] => ('[', ']'),
|
||||||
|
T!['{'] => ('{', '}'),
|
||||||
|
_ => panic!("invalid delimiter `{delimiter:?}`"),
|
||||||
|
};
|
||||||
|
let tt = tt.into_iter().join("");
|
||||||
|
|
||||||
|
ast_from_text(&format!("tt!{l_delimiter}{tt}{r_delimiter}"))
|
||||||
|
}
|
||||||
|
|
||||||
#[track_caller]
|
#[track_caller]
|
||||||
fn ast_from_text<N: AstNode>(text: &str) -> N {
|
fn ast_from_text<N: AstNode>(text: &str) -> N {
|
||||||
let parse = SourceFile::parse(text);
|
let parse = SourceFile::parse(text);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue