Skip to content

Fixing Vi{ incorrectly grabbing the ending braces #9685

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

Merged
merged 4 commits into from
Jul 14, 2025
Merged
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
15 changes: 15 additions & 0 deletions src/actions/motion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1968,6 +1968,21 @@ export abstract class MoveInsideCharacter extends ExpandingSelection {
vimState.recordedState.operatorPositionDiff = openPos.subtract(selStart);
}

// Adjust for VisualLine mode: exclude the line containing the closing brace
// moves the cursor back to just within the brackets, accurately mirroring what
// Vim does for Vi{ Vi( Vi[ etc.
if (
!this.includeSurrounding &&
vimState.currentMode === Mode.VisualLine &&
closePos.line > openPos.line
) {
const adjustedLine = closePos.line - 1;
if (adjustedLine >= 0) {
const lineText = vimState.document.lineAt(adjustedLine).text;
closePos = new Position(adjustedLine, lineText.length);
}
}

// TODO: setting the cursor manually like this shouldn't be necessary (probably a Cursor, not Position, should be passed to `exec`)
vimState.cursorStartPosition = openPos;
return {
Expand Down
24 changes: 23 additions & 1 deletion test/mode/modeVisualLine.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as assert from 'assert';
import { getAndUpdateModeHandler } from '../../extension';
import { Mode } from '../../src/mode/mode';
import { ModeHandler } from '../../src/mode/modeHandler';
import { newTest } from '../testSimplifier';
import { newTest, newTestOnly } from '../testSimplifier';
import { assertEqualLines, setupWorkspace } from './../testUtils';

suite('Mode Visual Line', () => {
Expand Down Expand Up @@ -585,4 +585,26 @@ suite('Mode Visual Line', () => {
});
}
});

suite('Vi{ should not select the ending brace, if it is on a new line.', () => {
test('Vi{ selection content test', async () => {
// Insert the full block using insert mode simulation
await modeHandler.handleMultipleKeyEvents('i{\nsome text on new line\n}'.split(''));

// Back to normal mode
await modeHandler.handleKeyEvent('<Esc>');

// Move cursor to start of "some text..."
await modeHandler.handleMultipleKeyEvents(['g', 'g', 'j', 'l', 'l', 'l', 'l']);

// Simulate Vi{
await modeHandler.handleMultipleKeyEvents(['V', 'i', '{']);

const doc = modeHandler.vimState.editor.document;
const sel = modeHandler.vimState.editor.selection;
const selectedText = doc.getText(sel);

assert.strictEqual(selectedText, ' some text on new line');
});
});
});