Skip to content

Add "Change Indentation Width" command (fixes #294080)#317228

Open
NSExceptional wants to merge 2 commits into
microsoft:mainfrom
NSExceptional:feat/294080-change-indentation-width
Open

Add "Change Indentation Width" command (fixes #294080)#317228
NSExceptional wants to merge 2 commits into
microsoft:mainfrom
NSExceptional:feat/294080-change-indentation-width

Conversation

@NSExceptional
Copy link
Copy Markdown
Contributor

@NSExceptional NSExceptional commented May 19, 2026

Summary

Adds a single editor action editor.action.changeIndentationWidth that re-indents the current file with a new indentation width, eliminating the four-step "Indent Using Spaces (old) → Convert to Tabs → Indent Using Tabs (new) → Convert to Spaces" dance users currently have to do to widen or narrow space-based indentation without a formatter. Resolves #294080.

The command:

  • Reads indentSize / insertSpaces from the active model as the current indent width.
  • Prompts the user for a new width via a quick pick (1–8), with the current value labelled.
  • For space-indented files, rewrites every line's leading whitespace by counting its visual columns against the current width and regenerating with the new width (multi-level indents, partial-step remainders, and stray tabs are all preserved).
  • For tab-indented files, falls through to just updating tabSize / indentSize (matching Indent Using Tabs).
  • Updates tabSize / indentSize on the model so subsequent edits use the new width.

The command is also surfaced in the indentation status-bar picker alongside Detect Indentation and Convert Indentation to ….

Test plan

  • compile-check-ts-native clean for the three touched files.
  • New Change Indentation Width — TypeScript/Javascript unit-test suite covers widen, narrow, odd-remainder, whitespace-only lines, stray tabs, and no-op cases.
  • Manual: open a 2-space-indented file, run "Change Indentation Width" → 4, confirm file is reflowed and status bar shows Spaces: 4.
  • Manual: tab-indented file, run command → only the displayed tab width changes.

Adds a single editor action that re-indents the active file with a new
indentation width, eliminating the four-step tabs<->spaces dance users
currently have to perform to widen or narrow space-based indentation
without a formatter.

For space-indented files, every line's leading whitespace is rewritten
by counting visual columns against the current indent width and
regenerating with the new width; for tab-indented files the action only
updates tabSize/indentSize, matching "Indent Using Tabs". The command is
also surfaced in the indentation status-bar picker.
Copilot AI review requested due to automatic review settings May 19, 2026 01:55
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR adds a new editor action, editor.action.changeIndentationWidth, to let users re-indent a space-indented file to a new indentation width in a single step (and for tab-indented files, to update the tab/indent size only). It also wires the action into the editor indentation status-bar picker and adds browser unit tests for the new command behavior.

Changes:

  • Added ChangeIndentationWidthAction (+ ChangeIndentationWidthCommand) to rewrite leading whitespace based on a newly selected indent width.
  • Surfaced the new action in the editor status-bar indentation picker.
  • Added a new unit test suite covering widening/narrowing, odd remainders, whitespace-only lines, tabs, and no-op cases.

Reviewed changes

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

File Description
src/vs/workbench/browser/parts/editor/editorStatus.ts Adds the new indentation-width action to the indentation status-bar picker actions list.
src/vs/editor/contrib/indentation/browser/indentation.ts Implements and registers the new editor action/command and the edit-operation generator for rewriting indentation.
src/vs/editor/contrib/indentation/test/browser/indentation.test.ts Adds unit tests for ChangeIndentationWidthCommand.

Comment on lines +705 to +710
const originalIndentation = model.getValueInRange(originalIndentationRange);

const visualSpaceCount = indentUtils.getSpaceCnt(originalIndentation, currentIndentSize);
const indentLevel = Math.floor(visualSpaceCount / currentIndentSize);
const remainder = visualSpaceCount - indentLevel * currentIndentSize;
const newIndentation = indentUtils.generateIndent(indentLevel * newIndentSize + remainder, newIndentSize, true);
Comment on lines +125 to +129
const selection = editor.getSelection();
if (!selection) {
return;
}

- Thread current tabSize through the command so stray tabs expand to
  tabSize visual columns, not indentSize. Fixes incorrect reindent when
  tabSize and indentSize diverge (e.g. after Change Tab Display Size).
- Read editor.getSelection() after the quick pick resolves so the
  selection tracked through trackSelection() reflects the caret the
  user actually has, not a stale one captured before the picker opened.
- Add a regression test exercising tabSize != indentSize.
@NSExceptional
Copy link
Copy Markdown
Contributor Author

Addressed each of Copilot's feedback, should I resolve them or leave them for a reviewer to verify?

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.

Feature request: command to adjust space-based indentation width, without a formatter

3 participants