Autoclose brackets before a language-specific set of characters

Fixes #588
This commit is contained in:
Max Brunsfeld
2022-03-14 15:17:40 -07:00
parent 7bdb91f4ec
commit 325e6c31ae
5 changed files with 16 additions and 6 deletions
+5 -6
View File
@@ -1910,15 +1910,10 @@ impl Editor {
selection.start.saturating_sub(pair.start.len()),
&pair.start,
) {
// Autoclose only if the next character is a whitespace or a pair end
// (possibly a different one from the pair we are inserting).
snapshot
.chars_at(selection.start)
.next()
.map_or(true, |ch| ch.is_whitespace())
|| language.brackets().iter().any(|pair| {
snapshot.contains_str_at(selection.start, &pair.end)
})
.map_or(true, |c| language.should_autoclose_before(c))
} else {
false
}
@@ -8125,6 +8120,7 @@ mod tests {
newline: true,
},
],
autoclose_before: "})]".to_string(),
..Default::default()
},
Some(tree_sitter_rust::language()),
@@ -8152,6 +8148,7 @@ mod tests {
],
cx,
);
view.handle_input(&Input("{".to_string()), cx);
view.handle_input(&Input("{".to_string()), cx);
view.handle_input(&Input("{".to_string()), cx);
@@ -8215,6 +8212,8 @@ mod tests {
.unindent()
);
// Don't autoclose if the next character isn't whitespace and isn't
// listed in the language's "autoclose_before" section.
view.finalize_last_transaction(cx);
view.select_display_ranges(&[DisplayPoint::new(0, 0)..DisplayPoint::new(0, 0)], cx);
view.handle_input(&Input("{".to_string()), cx);