mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-08-23 03:44:23 +00:00
Make insert_use return a SyntaxRewriter
This commit is contained in:
parent
245e1b533b
commit
cd349dbbc4
5 changed files with 129 additions and 107 deletions
|
@ -351,6 +351,23 @@ pub fn visibility_pub_crate() -> ast::Visibility {
|
|||
ast_from_text("pub(crate) struct S")
|
||||
}
|
||||
|
||||
pub fn visibility_pub() -> ast::Visibility {
|
||||
ast_from_text("pub struct S")
|
||||
}
|
||||
|
||||
pub fn tuple_field_list(fields: impl IntoIterator<Item = ast::TupleField>) -> ast::TupleFieldList {
|
||||
let fields = fields.into_iter().join(", ");
|
||||
ast_from_text(&format!("struct f({});", fields))
|
||||
}
|
||||
|
||||
pub fn tuple_field(visibility: Option<ast::Visibility>, ty: ast::Type) -> ast::TupleField {
|
||||
let visibility = match visibility {
|
||||
None => String::new(),
|
||||
Some(it) => format!("{} ", it),
|
||||
};
|
||||
ast_from_text(&format!("struct f({}{});", visibility, ty))
|
||||
}
|
||||
|
||||
pub fn fn_(
|
||||
visibility: Option<ast::Visibility>,
|
||||
fn_name: ast::Name,
|
||||
|
@ -373,6 +390,26 @@ pub fn fn_(
|
|||
))
|
||||
}
|
||||
|
||||
pub fn struct_(
|
||||
visibility: Option<ast::Visibility>,
|
||||
strukt_name: ast::Name,
|
||||
type_params: Option<ast::GenericParamList>,
|
||||
field_list: ast::FieldList,
|
||||
) -> ast::Struct {
|
||||
let semicolon = if matches!(field_list, ast::FieldList::TupleFieldList(_)) { ";" } else { "" };
|
||||
let type_params =
|
||||
if let Some(type_params) = type_params { format!("<{}>", type_params) } else { "".into() };
|
||||
let visibility = match visibility {
|
||||
None => String::new(),
|
||||
Some(it) => format!("{} ", it),
|
||||
};
|
||||
|
||||
ast_from_text(&format!(
|
||||
"{}struct {}{}{}{}",
|
||||
visibility, strukt_name, type_params, field_list, semicolon
|
||||
))
|
||||
}
|
||||
|
||||
fn ast_from_text<N: AstNode>(text: &str) -> N {
|
||||
let parse = SourceFile::parse(text);
|
||||
let node = match parse.tree().syntax().descendants().find_map(N::cast) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue