Skip to content

Commit 2d54e06

Browse files
committed
minor: Add item_static constructor to SyntaxFactory
1 parent e230052 commit 2d54e06

File tree

2 files changed

+63
-1
lines changed

2 files changed

+63
-1
lines changed

src/tools/rust-analyzer/crates/syntax/src/ast/make.rs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -895,7 +895,29 @@ pub fn item_const(
895895
None => String::new(),
896896
Some(it) => format!("{it} "),
897897
};
898-
ast_from_text(&format!("{visibility} const {name}: {ty} = {expr};"))
898+
ast_from_text(&format!("{visibility}const {name}: {ty} = {expr};"))
899+
}
900+
901+
pub fn item_static(
902+
visibility: Option<ast::Visibility>,
903+
is_unsafe: bool,
904+
is_mut: bool,
905+
name: ast::Name,
906+
ty: ast::Type,
907+
expr: Option<ast::Expr>,
908+
) -> ast::Static {
909+
let visibility = match visibility {
910+
None => String::new(),
911+
Some(it) => format!("{it} "),
912+
};
913+
let is_unsafe = if is_unsafe { "unsafe " } else { "" };
914+
let is_mut = if is_mut { "mut " } else { "" };
915+
let expr = match expr {
916+
Some(it) => &format!(" = {it}"),
917+
None => "",
918+
};
919+
920+
ast_from_text(&format!("{visibility}{is_unsafe}static {is_mut}{name}: {ty}{expr};"))
899921
}
900922

901923
pub fn unnamed_param(ty: ast::Type) -> ast::Param {

src/tools/rust-analyzer/crates/syntax/src/ast/syntax_factory/constructors.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,46 @@ impl SyntaxFactory {
215215
ast
216216
}
217217

218+
pub fn item_static(
219+
&self,
220+
visibility: Option<ast::Visibility>,
221+
is_unsafe: bool,
222+
is_mut: bool,
223+
name: ast::Name,
224+
ty: ast::Type,
225+
expr: Option<ast::Expr>,
226+
) -> ast::Static {
227+
let ast = make::item_static(
228+
visibility.clone(),
229+
is_unsafe,
230+
is_mut,
231+
name.clone(),
232+
ty.clone(),
233+
expr.clone(),
234+
)
235+
.clone_for_update();
236+
237+
if let Some(mut mapping) = self.mappings() {
238+
let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone());
239+
if let Some(visibility) = visibility {
240+
builder.map_node(
241+
visibility.syntax().clone(),
242+
ast.visibility().unwrap().syntax().clone(),
243+
);
244+
}
245+
246+
builder.map_node(name.syntax().clone(), ast.name().unwrap().syntax().clone());
247+
builder.map_node(ty.syntax().clone(), ast.ty().unwrap().syntax().clone());
248+
249+
if let Some(expr) = expr {
250+
builder.map_node(expr.syntax().clone(), ast.body().unwrap().syntax().clone());
251+
}
252+
builder.finish(&mut mapping);
253+
}
254+
255+
ast
256+
}
257+
218258
pub fn turbofish_generic_arg_list(
219259
&self,
220260
args: impl IntoIterator<Item = ast::GenericArg> + Clone,

0 commit comments

Comments
 (0)