Skip to content

Fix #256037: wrong indentation caused by incorrect bracket matcing - #307271

Open
Leonor Azevedo (leonor-a-a-ist) wants to merge 1 commit into
microsoft:mainfrom
leonor-a-a-ist:bug_fix_pic
Open

Fix #256037: wrong indentation caused by incorrect bracket matcing#307271
Leonor Azevedo (leonor-a-a-ist) wants to merge 1 commit into
microsoft:mainfrom
leonor-a-a-ist:bug_fix_pic

Conversation

@leonor-a-a-ist

Copy link
Copy Markdown

When typing a closing bracket immediately before another closing token, the auto-indent logic could start searching from the wrong column. This sometimes led to matching the wrong opening bracket, causing incorrect indentation.

This has been fixed by using the cursor column (where the closing bracket is inserted) as the search bound, or 0 when no valid opening bracket is found.

Added regression tests covering different scenarios, including cases like } before ) and ], and ] before ), with varying initial indentation levels.

Fixes #256037

…atcing

When typing a closing bracket immediately before another closing
token, the auto-indent logic could start searching from the wrong
column. This sometimes led to matching the wrong opening bracket,
causing incorrect indentation.

This has been fixed by using the cursor column (where the closing
bracket is inserted) as the search bound, or 0 when no valid opening
bracket is found.

Added regression tests covering different scenarios, including cases
like } before ) and ], and ] before ), with varying initial
indentation levels.
Copilot AI review requested due to automatic review settings April 1, 2026 22:02
@leonor-a-a-ist

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree company="Instituto Superior Técnico"

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes an editor auto-indent edge case where typing a closing bracket immediately before another closing token could cause bracket matching to start from the wrong column, resulting in incorrect indentation. This adjusts the bracket-match search bound to use the cursor column and adds regression tests covering the reported scenarios.

Changes:

  • Adjust electric character indentation logic to bound bracket-matching by the cursor column.
  • Add regression tests for cases like } before ], ] before ), and } before ) under varying indentation states.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/vs/editor/common/cursor/cursorTypeEditOperations.ts Updates electric-character indentation bracket-match starting column to avoid mismatching when another closing token follows.
src/vs/editor/test/browser/controller/cursor.test.ts Adds regression tests reproducing issue #256037 and validating correct indentation outcomes.

Comment on lines +471 to +472
const lastOpenBracketIndex = (lineTokens.getLineContent() + ch).lastIndexOf(electricAction.matchOpenBracket);
const endColumn = lastOpenBracketIndex === -1 ? 0 : position.column;

Copilot AI Apr 1, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lastOpenBracketIndex is misleading here because electricAction.matchOpenBracket actually contains the closing bracket text (e.g. } / ] / )), not an opening bracket. Consider renaming this local to something like lastClosingBracketIndex (or removing it entirely if it’s only used for the === -1 check) to avoid future confusion when touching this indentation logic.

Suggested change
const lastOpenBracketIndex = (lineTokens.getLineContent() + ch).lastIndexOf(electricAction.matchOpenBracket);
const endColumn = lastOpenBracketIndex === -1 ? 0 : position.column;
const lastClosingBracketIndex = (lineTokens.getLineContent() + ch).lastIndexOf(electricAction.matchOpenBracket);
const endColumn = lastClosingBracketIndex === -1 ? 0 : position.column;

Copilot uses AI. Check for mistakes.
@leonor-a-a-ist

Copy link
Copy Markdown
Author

Aiday Marlen Kyzy (@aiday-mar)
Hi! Let me know if you have any feedback on this PR or if I should make any changes. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Auto indent is wrong when adding brackets sometimes

3 participants