Skip to content

Add editor.preserveAlignmentSpacesOnIndent option#309369

Open
yogeshwaran-c wants to merge 1 commit intomicrosoft:mainfrom
yogeshwaran-c:fix/preserve-alignment-spaces-on-tab-277764
Open

Add editor.preserveAlignmentSpacesOnIndent option#309369
yogeshwaran-c wants to merge 1 commit intomicrosoft:mainfrom
yogeshwaran-c:fix/preserve-alignment-spaces-on-tab-277764

Conversation

@yogeshwaran-c
Copy link
Copy Markdown
Contributor

Summary

When a tab-indented line contains spaces after the last tab that are used to visually align content with a token on a previous line, the existing Tab/Shift+Tab behavior on a selection rewrites the leading whitespace as pure tab indentation and destroys the alignment. For example, pressing Shift+Tab on:

<TAB><TAB>  arg

currently produces <TAB><TAB>arg (the two alignment spaces are gone) instead of <TAB> arg (one level less of tab indentation, alignment preserved).

This PR introduces a new boolean editor option, editor.preserveAlignmentSpacesOnIndent, defaulting to false so existing behavior is unchanged. When the option is enabled and the file is indented with tabs (editor.insertSpaces === false), ShiftCommand treats spaces that follow the last tab in the leading whitespace as alignment spaces and preserves them across shift and unshift operations. This keeps column alignment within the code intact when adjusting the indentation level with Tab or Shift+Tab on a selection.

Changes:

  • New preserveAlignmentSpacesOnIndent option (editorOptions.ts, standaloneEnums.ts).
  • Plumbed through CursorConfiguration (cursorCommon.ts) to the ShiftCommand construction sites in cursorTypeOperations.ts and cursorTypeEditOperations.ts.
  • ShiftCommand.getEditOperations gains an alignment-spaces-stripping step when the new option is enabled, and re-appends the preserved spaces after the re-computed indent.
  • Added four unit tests in shiftCommand.test.ts covering: Tab with preservation, Shift+Tab with preservation, Shift+Tab with the option off (the previous behavior), and a no-op case when the file uses spaces for indentation.

Fixes #277764

Test plan

  • yarn test-browser --run test/browser/commands/shiftCommand.test.ts passes (new + existing tests).
  • Manual smoke test: open a tab-indented file, enable editor.preserveAlignmentSpacesOnIndent, select lines that have alignment spaces after the tabs, and confirm Tab / Shift+Tab shift the tab indentation while preserving the trailing alignment spaces. With the option off, behavior is unchanged.

When a tab-indented line contains spaces after the last tab that are
used to visually align content with a token on a previous line, the
existing Tab/Shift+Tab behavior on a selection rewrites the leading
whitespace as pure tab indentation and destroys the alignment.

Introduce a new boolean editor option
`editor.preserveAlignmentSpacesOnIndent` (default: false). When enabled
and the file is indented with tabs, `ShiftCommand` treats spaces that
follow the last tab in the leading whitespace as alignment spaces and
preserves them across shift and unshift operations so that the column
alignment within the code is kept intact.

Fixes microsoft#277764
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.

Option to not replace aligning spaces during indentation by Tab

2 participants