about summary refs log tree commit diff
diff options
context:
space:
mode:
authorDropDemBits <r3usrlnd@gmail.com>2023-07-16 16:22:29 -0400
committerDropDemBits <r3usrlnd@gmail.com>2023-11-11 21:05:27 -0500
commit5fc8cc52e2ebc30d720873236293f5469bf5cfd8 (patch)
treee83e410e8983801941a03bb15d86f38742cf2a6a
parent92422f7488888c3868c9f5ddb080f26566f9f382 (diff)
downloadrust-5fc8cc52e2ebc30d720873236293f5469bf5cfd8.tar.gz
rust-5fc8cc52e2ebc30d720873236293f5469bf5cfd8.zip
Add `LetStmt::set_ty`
Way for setting and removing the type ascription of a let stmt
-rw-r--r--crates/syntax/src/ast/edit_in_place.rs41
1 files changed, 41 insertions, 0 deletions
diff --git a/crates/syntax/src/ast/edit_in_place.rs b/crates/syntax/src/ast/edit_in_place.rs
index 6c6a2bc71eb..b9059a527d0 100644
--- a/crates/syntax/src/ast/edit_in_place.rs
+++ b/crates/syntax/src/ast/edit_in_place.rs
@@ -646,6 +646,47 @@ impl ast::MatchArmList {
     }
 }
 
+impl ast::LetStmt {
+    pub fn set_ty(&self, ty: Option<ast::Type>) {
+        match ty {
+            None => {
+                if let Some(colon_token) = self.colon_token() {
+                    ted::remove(colon_token);
+                }
+
+                if let Some(existing_ty) = self.ty() {
+                    if let Some(sibling) = existing_ty.syntax().prev_sibling_or_token() {
+                        if sibling.kind() == SyntaxKind::WHITESPACE {
+                            ted::remove(sibling);
+                        }
+                    }
+
+                    ted::remove(existing_ty.syntax());
+                }
+            }
+            Some(new_ty) => {
+                if self.colon_token().is_none() {
+                    let mut to_insert: Vec<SyntaxElement> = vec![];
+
+                    let position = match self.pat() {
+                        Some(pat) => Position::after(pat.syntax()),
+                        None => {
+                            to_insert.push(make::tokens::single_space().into());
+                            Position::after(self.let_token().unwrap())
+                        }
+                    };
+
+                    to_insert.push(make::token(T![:]).into());
+
+                    ted::insert_all_raw(position, to_insert);
+                }
+
+                ted::insert(Position::after(self.colon_token().unwrap()), new_ty.syntax());
+            }
+        }
+    }
+}
+
 impl ast::RecordExprFieldList {
     pub fn add_field(&self, field: ast::RecordExprField) {
         let is_multiline = self.syntax().text().contains_char('\n');