Skip to content

Fix surrounding a selection with brackets after arrow functions (#225916) - #321210

Merged
Dmitriy Vasyura (dmitrivMS) merged 4 commits into
microsoft:mainfrom
mst-mkt:fix/225916-surround-selection
Aug 31, 2026
Merged

Dmitriy Vasyura (dmitrivMS) merged 4 commits into
microsoft:mainfrom
mst-mkt:fix/225916-surround-selection

Conversation

@mst-mkt

Copy link
Copy Markdown

issue

resolve #225916

  • I am not the author of this issue and I am not assigned to it.
  • I ran into the same problem myself, and since the issue has been open since 2024 with no recent activity, I decided to submit a fix.

background

  • Since VS Code 1.91, typing {, [ or ( over a selected text deletes the selection instead of surrounding it, when the line above ends with => (or is an if/for/while without braces) in TypeScript/JavaScript.

    const fn = () =>
      doSomething()   // select `doSomething()` and type `{`
  • Expected: {doSomething()}

  • Actual: the selection is deleted and { is inserted at the start of the line.

  • This happens with the default settings (editor.autoIndent: "full").

problem

const textAroundRangeContainsOnlyWhitespace = /^\s*$/.test(textAroundRange);
const autoClosingPairs = cursorConfig.autoClosingPairs.autoClosingPairsOpenByEnd.get(ch);
const autoClosingPairExists = autoClosingPairs && autoClosingPairs.length > 0;
const isChFirstNonWhitespaceCharacterAndInAutoClosingPair = autoClosingPairExists && textAroundRangeContainsOnlyWhitespace;
if (inferredIndentationEqualsActual && isChFirstNonWhitespaceCharacterAndInAutoClosingPair) {
return inheritedIndentation;
}

  • AutoIndentOperation runs before SurroundSelectionOperation in the typing pipeline, so once the branch fires, surround-with-brackets is never reached.

const autoIndentEdits = AutoIndentOperation.getEdits(config, model, selections, ch, isDoingComposition);
if (autoIndentEdits !== undefined) {
return autoIndentEdits;
}
const autoClosingOverTypeEdits = AutoClosingOvertypeOperation.getEdits(prevEditOperationType, config, model, selections, autoClosedCharacters, ch);
if (autoClosingOverTypeEdits !== undefined) {
return autoClosingOverTypeEdits;
}
const autoClosingOpenCharEdits = AutoClosingOpenCharTypeOperation.getEdits(config, model, selections, ch, false, isDoingComposition);
if (autoClosingOpenCharEdits !== undefined) {
return autoClosingOpenCharEdits;
}
const surroundSelectionEdits = SurroundSelectionOperation.getEdits(config, model, selections, ch, isDoingComposition);
if (surroundSelectionEdits !== undefined) {
return surroundSelectionEdits;
}

  • The resulting edit rewrites the line from column 1 through the end of the selection without the selected text — the selection is deleted.

private static _getEditFromIndentationAndSelection(config: CursorConfiguration, model: ITextModel, indentation: string, selection: Selection, ch: string, includeChInEdit: boolean = true): { range: Range; text: string } {
const startLineNumber = selection.startLineNumber;
const firstNonWhitespaceColumn = model.getLineFirstNonWhitespaceColumn(startLineNumber);
let text: string = config.normalizeIndentation(indentation);
if (firstNonWhitespaceColumn !== 0) {
const startLine = model.getLineContent(startLineNumber);
text += startLine.substring(firstNonWhitespaceColumn - 1, selection.startColumn - 1);
}
text += includeChInEdit ? ch : '';
const range = new Range(startLineNumber, 1, selection.endLineNumber, selection.endColumn);
return { range, text };
}

fix

  • Require an empty selection for that branch (range.isEmpty()).
  • Non-empty selections now fall through to SurroundSelectionOperation, restoring the pre-1.91 behavior.
  • The empty-selection case the branch was added for (allman-style braces, Wrong auto-indentation in typescript #209802) is unaffected.

checks

  • Verified manually with a build from source

    npm run compile
    ./scripts/code.sh

    Followed the repro steps from the issue: the selection is now surrounded ({doSomething()}), and [ / ( work as well.

  • Added a regression test, and confirmed it fails without the fix (reproducing the exact behavior reported in the issue)

    ./scripts/test.sh --glob '**/indentation.test.js'   # 60 passing, including the allman-brace test for #209802
  • ./scripts/test.sh (full unit tests), hygiene, eslint and valid-layers-check all pass

Copilot AI review requested due to automatic review settings June 12, 2026 20:14

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

Note

Copilot was unable to run its full agentic suite in this review.

This PR fixes an auto-indent edge case when typing a bracket/brace while a selection exists (e.g., “surround selection”) after an arrow function, and adds a regression test for the reported issue.

Changes:

  • Add a regression test covering “surround selection with {} after an arrow function” (issue #225916).
  • Adjust auto-indent logic to only consult previous-line “indent next line” rules when the typed range is empty (caret insertion, not replacement).

Reviewed changes

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

File Description
src/vs/editor/contrib/indentation/test/browser/indentation.test.ts Adds a regression test reproducing the surround-selection behavior after an arrow function.
src/vs/editor/common/languages/autoIndent.ts Prevents previous-line indent-next-line inheritance when typing replaces a selection.

@mst-mkt

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

@royy92

Copy link
Copy Markdown

question: Since the issue description mentions {, [ and (, would it be worth covering one additional auto-closing pair in the regression test?
The implementation path is shared, but an extra assertion for [ or ( could help document that the regression is tied to non-empty selection handling rather than a specific brace type.

@mst-mkt

Copy link
Copy Markdown
Author

Roqia AlQoran (@royy92)
Thanks for the question.

an extra assertion for [ or ( could help document that the regression is tied to non-empty selection handling rather than a specific brace type.

Copilot pointed at the same test, too.
Honestly, I'd been unsure whether to make this change, since the bracket type isn't really the core of the bug. But the test references the issue, so your point makes sense to me. I've added [] and () in 83a2c7d, so take a look if you're interested.

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.

Review details

  • Files reviewed: 2/2 changed files
  • Comments generated: 0 new
  • Review effort level: Balanced

@dmitrivMS

Copy link
Copy Markdown
Collaborator

Closing since I am unable to repro the issue this is fixing.

@mst-mkt

Copy link
Copy Markdown
Author

Dmitriy Vasyura (@dmitrivMS)

Thanks for taking a look at the PR.

I can still reproduce this locally and on vscode.dev, so I looked into what differs. The bug only appears when the indentation in the file matches the editor's indentation setting. The snippet in the issue uses 2 spaces, so the editor needs to be on 2 spaces as well.

const fn = () =>
  doSomething()
^^ // 2 space indent
image

Sorry for not covering that in the repro steps. I'd appreciate it if you could try again with the indentation matched.

@dmitrivMS

Copy link
Copy Markdown
Collaborator

I independently validated this fix on Windows against PR head 4946dd1f611 and current main.

Baseline

I applied only the new regression test to current main (718038e170d). It failed with the behavior from #225916:

expected: const fn = () =>\n    {doSomething()}
actual:   const fn = () =>\n{

PR head

I also applied the two-file PR diff onto current main, transpiled, and ran both test files together: 314 passing, 26 pre-existing pending.

Code OSS verification

Built and launched Code OSS from the PR head with a TypeScript file using Spaces: 2 and editor.autoIndent: full:

  • Selecting doSomething() after const fn = () => and typing {, [, or ( produced {doSomething()}, [doSomething()], and (doSomething()) respectively.
  • The original Wrong auto-indentation in typescript #209802 Allman-style path remains intact: after if (condition), Enter initially indents the next line by two spaces, and typing { outdents it to column 1 and inserts {}.

The range.isEmpty() guard therefore looks correctly scoped: it prevents the caret-only Allman auto-indent branch from intercepting replacement/surround-selection operations, while preserving its intended empty-selection behavior. I found no regression in the relevant typing and indentation paths.

@dmitrivMS
Dmitriy Vasyura (dmitrivMS) merged commit 45a37d5 into microsoft:main Aug 31, 2026
27 checks passed
@vs-code-engineering vs-code-engineering Bot added this to the 1.137.0 milestone Aug 31, 2026
Abdon Morales (abdonmorales) pushed a commit to abdonmorales/vscode-utcs that referenced this pull request Sep 23, 2026
…osoft#225916) (microsoft#321210)

* fix: only auto indent on type when the selection is empty

* test: surround a selection with brackets after arrow functions

* test: surround a selection with all bracket pairs after arrow functions

---------

Co-authored-by: Dmitriy Vasyura <[email protected]>
(cherry picked from commit 45a37d5)
@vs-code-engineering vs-code-engineering Bot locked and limited conversation to collaborators Oct 22, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Surrounding with brackets doesn’t work in some cases

7 participants