about summary refs log tree commit diff
path: root/src/tools/rust-analyzer/crates/syntax
diff options
context:
space:
mode:
authorGiga Bowser <45986823+Giga-Bowser@users.noreply.github.com>2024-12-11 10:32:32 -0500
committerGiga Bowser <45986823+Giga-Bowser@users.noreply.github.com>2024-12-11 10:32:32 -0500
commit2d54e06b36509e1c7d06d77ec3975fdcc8453034 (patch)
tree7265c12945c99319171b0c00e49b10f11afba7f3 /src/tools/rust-analyzer/crates/syntax
parente2300523272ba9755872b83062083e7a04b5c94d (diff)
downloadrust-2d54e06b36509e1c7d06d77ec3975fdcc8453034.tar.gz
rust-2d54e06b36509e1c7d06d77ec3975fdcc8453034.zip
minor: Add `item_static` constructor to `SyntaxFactory`
Diffstat (limited to 'src/tools/rust-analyzer/crates/syntax')
-rw-r--r--src/tools/rust-analyzer/crates/syntax/src/ast/make.rs24
-rw-r--r--src/tools/rust-analyzer/crates/syntax/src/ast/syntax_factory/constructors.rs40
2 files changed, 63 insertions, 1 deletions
diff --git a/src/tools/rust-analyzer/crates/syntax/src/ast/make.rs b/src/tools/rust-analyzer/crates/syntax/src/ast/make.rs
index 05c2a8354da..eb96ab6ef59 100644
--- a/src/tools/rust-analyzer/crates/syntax/src/ast/make.rs
+++ b/src/tools/rust-analyzer/crates/syntax/src/ast/make.rs
@@ -895,7 +895,29 @@ pub fn item_const(
         None => String::new(),
         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 {
diff --git a/src/tools/rust-analyzer/crates/syntax/src/ast/syntax_factory/constructors.rs b/src/tools/rust-analyzer/crates/syntax/src/ast/syntax_factory/constructors.rs
index d2ab30fb47d..280c5c25cb9 100644
--- a/src/tools/rust-analyzer/crates/syntax/src/ast/syntax_factory/constructors.rs
+++ b/src/tools/rust-analyzer/crates/syntax/src/ast/syntax_factory/constructors.rs
@@ -215,6 +215,46 @@ impl SyntaxFactory {
         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(
         &self,
         args: impl IntoIterator<Item = ast::GenericArg> + Clone,