This repository was archived by the owner on Mar 3, 2023. It is now read-only.
Define indent/outdent behavior for single-line selected text #20137
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.
Description of the Change
Text borrowed from #15096, which I had to close due to a force-push.
While working on atom/snippets#239, I realized that there isn't a properly defined behavior for selecting a segment of text on single line and indenting/outdenting it:
Here is why I don't think this has been defined yet:
There is no spec for single-line selected text being indented via
indent():atom/spec/text-editor-spec.coffee
Lines 3673 to 3789 in a7736b8
indentSelectedRows(which is used inindent()) is poorly documented:atom/src/selection.coffee
Lines 671 to 679 in a7736b8
It's possible to interpret this in a way that if the selection does not span across multiple rows, then nothing gets indented. Insert your favorite programmer-goes-to-grocery-store-joke here.
The current behavior is not intuitive and unexpected for anyone coming from a different editor. With the exception of Brackets, other text editors tend to agree that selecting text and pressing tab within a single line should not indent the whole line, but insert/overwrite with a tab.
Outdent is done through
outdentSelectedRows, which has a spec for single line selected text. However it seems to be slightly mislabeled, there is actually less than one line selected. That is not a big deal and can be easily fixed, but it's possible that something is missing from here too.At minimum I am looking to define behavior for following cases:
indent().indent().outdentSelectedRows().outdentSelectedRows().There might be additional cases that might need be considered, but I don't want this to be come overly ambitious, so starting with a more managable goal might be better.
The motivation for all of this comes from behavior related to text selections that come from traversing tab stops within snippets, where indenting/outdenting may lead to unexpected results:
Personally I think that outdent behavior can be kept as-is and it is sufficient to simply improve the spec and cover cases 3 and 4. Outdenting does not break out of snippet's tab stops whereas indenting does, so indicating change with a slightly more destructive (but predictable) operation should be obvious enough that it's impossible to return to snippet's tab stops.
Visual Studio Code's behavior is a suitable candidate, which indents if the whole line is selected, but inserts a tab when only part of line is selected. Outdent is handled in the same way as Atom does it now. If there aren't any objections or better ideas, I might just implement that and propose this as the final solution.
Code at the time of writing in pull request is just to get the ball rolling and does not represent the final proposed solution in any way. But it does replace everything with a tab if at most only one line has been selected.
Currently the code implements Visual Studio Code behavior where fully selected single line indents and otherwise inserts a tab. Outdent remains the same as it is now.
Alternate Designs
Sublime text's behavior was considered, which replaces the line with a tab if the start and end cursor is on the same line. This however can be at times more inconvenient than the Visual Studio Code behavior of indenting the line instead.
Possible Drawbacks
Change in behavior that might upset users who are already used to the less flexible behavior in place right now.
Verification Process
Both hand-testing and automated testing of use cases that were not previously covered (selecting right amount of text and pressing Tab.
Release Notes
Not finalized.