mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-26 09:49:15 +00:00
minor: Add item_static constructor to SyntaxFactory
This commit is contained in:
parent
1979d3f9b5
commit
135e71fcb3
2 changed files with 63 additions and 1 deletions
|
|
@ -895,7 +895,29 @@ pub fn item_const(
|
||||||
None => String::new(),
|
None => String::new(),
|
||||||
Some(it) => format!("{it} "),
|
Some(it) => format!("{it} "),
|
||||||
};
|
};
|
||||||
ast_from_text(&format!("{visibility} const {name}: {ty} = {expr};"))
|
ast_from_text(&format!("{visibility}const {name}: {ty} = {expr};"))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn item_static(
|
||||||
|
visibility: Option<ast::Visibility>,
|
||||||
|
is_unsafe: bool,
|
||||||
|
is_mut: bool,
|
||||||
|
name: ast::Name,
|
||||||
|
ty: ast::Type,
|
||||||
|
expr: Option<ast::Expr>,
|
||||||
|
) -> ast::Static {
|
||||||
|
let visibility = match visibility {
|
||||||
|
None => String::new(),
|
||||||
|
Some(it) => format!("{it} "),
|
||||||
|
};
|
||||||
|
let is_unsafe = if is_unsafe { "unsafe " } else { "" };
|
||||||
|
let is_mut = if is_mut { "mut " } else { "" };
|
||||||
|
let expr = match expr {
|
||||||
|
Some(it) => &format!(" = {it}"),
|
||||||
|
None => "",
|
||||||
|
};
|
||||||
|
|
||||||
|
ast_from_text(&format!("{visibility}{is_unsafe}static {is_mut}{name}: {ty}{expr};"))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn unnamed_param(ty: ast::Type) -> ast::Param {
|
pub fn unnamed_param(ty: ast::Type) -> ast::Param {
|
||||||
|
|
|
||||||
|
|
@ -215,6 +215,46 @@ impl SyntaxFactory {
|
||||||
ast
|
ast
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn item_static(
|
||||||
|
&self,
|
||||||
|
visibility: Option<ast::Visibility>,
|
||||||
|
is_unsafe: bool,
|
||||||
|
is_mut: bool,
|
||||||
|
name: ast::Name,
|
||||||
|
ty: ast::Type,
|
||||||
|
expr: Option<ast::Expr>,
|
||||||
|
) -> ast::Static {
|
||||||
|
let ast = make::item_static(
|
||||||
|
visibility.clone(),
|
||||||
|
is_unsafe,
|
||||||
|
is_mut,
|
||||||
|
name.clone(),
|
||||||
|
ty.clone(),
|
||||||
|
expr.clone(),
|
||||||
|
)
|
||||||
|
.clone_for_update();
|
||||||
|
|
||||||
|
if let Some(mut mapping) = self.mappings() {
|
||||||
|
let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone());
|
||||||
|
if let Some(visibility) = visibility {
|
||||||
|
builder.map_node(
|
||||||
|
visibility.syntax().clone(),
|
||||||
|
ast.visibility().unwrap().syntax().clone(),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
builder.map_node(name.syntax().clone(), ast.name().unwrap().syntax().clone());
|
||||||
|
builder.map_node(ty.syntax().clone(), ast.ty().unwrap().syntax().clone());
|
||||||
|
|
||||||
|
if let Some(expr) = expr {
|
||||||
|
builder.map_node(expr.syntax().clone(), ast.body().unwrap().syntax().clone());
|
||||||
|
}
|
||||||
|
builder.finish(&mut mapping);
|
||||||
|
}
|
||||||
|
|
||||||
|
ast
|
||||||
|
}
|
||||||
|
|
||||||
pub fn turbofish_generic_arg_list(
|
pub fn turbofish_generic_arg_list(
|
||||||
&self,
|
&self,
|
||||||
args: impl IntoIterator<Item = ast::GenericArg> + Clone,
|
args: impl IntoIterator<Item = ast::GenericArg> + Clone,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue