Skip to content

Fix: keep the selection of the space after the last character #9584

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions src/mode/modeHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -992,23 +992,27 @@ export class ModeHandler implements vscode.Disposable, IModeHandler {
!this.vimState.returnToInsertAfterCommand &&
(this.vimState.currentMode === Mode.Normal || isVisualMode(this.vimState.currentMode))
) {
const currentLineLength = TextEditor.getLineLength(cursor.stop.line);
const currentStopLineLength = TextEditor.getLineLength(cursor.stop.line);
const currentStartLineLength = TextEditor.getLineLength(cursor.start.line);

// When in visual mode you can move the cursor past the last character in order
// to select that character. We use this offset to allow for that, otherwise
// we would consider the position invalid and change it to the left of the last
// character.
const offsetAllowed =
isVisualMode(this.vimState.currentMode) && currentLineLength > 0 ? 1 : 0;
if (cursor.start.character >= currentStartLineLength) {
const offsetStartAllowed =
isVisualMode(this.vimState.currentMode) && currentStartLineLength > 0 ? 1 : 0;
const offsetStopAllowed =
isVisualMode(this.vimState.currentMode) && currentStopLineLength > 0 ? 1 : 0;

if (cursor.start.character >= currentStartLineLength + offsetStartAllowed) {
cursor = cursor.withNewStart(
cursor.start.withColumn(Math.max(currentStartLineLength - 1, 0)),
);
}

if (cursor.stop.character >= currentLineLength + offsetAllowed) {
cursor = cursor.withNewStop(cursor.stop.withColumn(Math.max(currentLineLength - 1, 0)));
if (cursor.stop.character >= currentStopLineLength + offsetStopAllowed) {
cursor = cursor.withNewStop(
cursor.stop.withColumn(Math.max(currentStopLineLength - 1, 0)),
);
}
}
return cursor;
Expand Down
19 changes: 19 additions & 0 deletions test/mode/modeVisual.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1699,6 +1699,25 @@ suite('Mode Visual', () => {
endMode: Mode.Normal,
});

suite('Can handle o', () => {
newTest({
title: 'Can select the space after the last character',
start: ['ab', 'ab|c', 'abcd'],
keysPressed: 'vkd',
end: ['a|b', 'abcd'],
endMode: Mode.Normal,
});

newTest({
title:
'After executing o twice, can keep the selection of the space after the last character',
start: ['ab', 'ab|c', 'abcd'],
keysPressed: 'vkood',
end: ['a|b', 'abcd'],
endMode: Mode.Normal,
});
});

suite('C, R, and S', () => {
for (const command of ['C', 'R', 'S']) {
newTest({
Expand Down