Merge pull request #1756 from zed-industries/autoclose-wrong-closing-bracket

Avoid skipping over a different closing bracket in autoclose
This commit is contained in:
Max Brunsfeld 2022-10-14 09:34:33 -07:00 committed by GitHub
commit 330968434f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 6 deletions

View file

@ -1938,9 +1938,10 @@ impl Editor {
} }
} else if let Some(region) = autoclose_region { } else if let Some(region) = autoclose_region {
// If the selection is followed by an auto-inserted closing bracket, // If the selection is followed by an auto-inserted closing bracket,
// then don't insert anything else; just move the selection past the // then don't insert that closing bracket again; just move the selection
// closing bracket. // past the closing bracket.
let should_skip = selection.end == region.range.end.to_point(&snapshot); let should_skip = selection.end == region.range.end.to_point(&snapshot)
&& text.as_ref() == region.pair.end.as_str();
if should_skip { if should_skip {
let anchor = snapshot.anchor_after(selection.end); let anchor = snapshot.anchor_after(selection.end);
new_selections.push(( new_selections.push((

View file

@ -2907,6 +2907,12 @@ async fn test_autoclose_pairs(cx: &mut gpui::TestAppContext) {
close: true, close: true,
newline: true, newline: true,
}, },
BracketPair {
start: "(".to_string(),
end: ")".to_string(),
close: true,
newline: true,
},
BracketPair { BracketPair {
start: "/*".to_string(), start: "/*".to_string(),
end: " */".to_string(), end: " */".to_string(),
@ -2957,6 +2963,19 @@ async fn test_autoclose_pairs(cx: &mut gpui::TestAppContext) {
.unindent(), .unindent(),
); );
// insert a different closing bracket
cx.update_editor(|view, cx| {
view.handle_input(")", cx);
});
cx.assert_editor_state(
&"
🏀{{{)ˇ}}}
ε{{{)ˇ}}}
{{{)ˇ}}}
"
.unindent(),
);
// skip over the auto-closed brackets when typing a closing bracket // skip over the auto-closed brackets when typing a closing bracket
cx.update_editor(|view, cx| { cx.update_editor(|view, cx| {
view.move_right(&MoveRight, cx); view.move_right(&MoveRight, cx);
@ -2966,9 +2985,9 @@ async fn test_autoclose_pairs(cx: &mut gpui::TestAppContext) {
}); });
cx.assert_editor_state( cx.assert_editor_state(
&" &"
🏀{{{}}}}ˇ 🏀{{{)}}}}ˇ
ε{{{}}}}ˇ ε{{{)}}}}ˇ
{{{}}}}ˇ {{{)}}}}ˇ
" "
.unindent(), .unindent(),
); );