Add "Change Indentation Width" command (fixes #294080)#317228
Open
NSExceptional wants to merge 2 commits into
Open
Add "Change Indentation Width" command (fixes #294080)#317228NSExceptional wants to merge 2 commits into
NSExceptional wants to merge 2 commits into
Conversation
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.
Contributor
There was a problem hiding this comment.
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.
Contributor
Author
|
Addressed each of Copilot's feedback, should I resolve them or leave them for a reviewer to verify? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds a single editor action
editor.action.changeIndentationWidththat 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:
indentSize/insertSpacesfrom the active model as the current indent width.tabSize/indentSize(matchingIndent Using Tabs).tabSize/indentSizeon the model so subsequent edits use the new width.The command is also surfaced in the indentation status-bar picker alongside
Detect IndentationandConvert Indentation to ….Test plan
compile-check-ts-nativeclean for the three touched files.Change Indentation Width — TypeScript/Javascriptunit-test suite covers widen, narrow, odd-remainder, whitespace-only lines, stray tabs, and no-op cases.Spaces: 4.