Skip to content

Commit 5fc8cc5

Browse files
committed
Add LetStmt::set_ty
Way for setting and removing the type ascription of a let stmt
1 parent 92422f7 commit 5fc8cc5

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

crates/syntax/src/ast/edit_in_place.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -646,6 +646,47 @@ impl ast::MatchArmList {
646646
}
647647
}
648648

649+
impl ast::LetStmt {
650+
pub fn set_ty(&self, ty: Option<ast::Type>) {
651+
match ty {
652+
None => {
653+
if let Some(colon_token) = self.colon_token() {
654+
ted::remove(colon_token);
655+
}
656+
657+
if let Some(existing_ty) = self.ty() {
658+
if let Some(sibling) = existing_ty.syntax().prev_sibling_or_token() {
659+
if sibling.kind() == SyntaxKind::WHITESPACE {
660+
ted::remove(sibling);
661+
}
662+
}
663+
664+
ted::remove(existing_ty.syntax());
665+
}
666+
}
667+
Some(new_ty) => {
668+
if self.colon_token().is_none() {
669+
let mut to_insert: Vec<SyntaxElement> = vec![];
670+
671+
let position = match self.pat() {
672+
Some(pat) => Position::after(pat.syntax()),
673+
None => {
674+
to_insert.push(make::tokens::single_space().into());
675+
Position::after(self.let_token().unwrap())
676+
}
677+
};
678+
679+
to_insert.push(make::token(T![:]).into());
680+
681+
ted::insert_all_raw(position, to_insert);
682+
}
683+
684+
ted::insert(Position::after(self.colon_token().unwrap()), new_ty.syntax());
685+
}
686+
}
687+
}
688+
}
689+
649690
impl ast::RecordExprFieldList {
650691
pub fn add_field(&self, field: ast::RecordExprField) {
651692
let is_multiline = self.syntax().text().contains_char('\n');

0 commit comments

Comments
 (0)