Skip to content

Commit 041f8f0

Browse files
authored
Fixing Vi{ incorrectly grabbing the ending braces (#9685)
1 parent 943ec17 commit 041f8f0

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

src/actions/motion.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1968,6 +1968,21 @@ export abstract class MoveInsideCharacter extends ExpandingSelection {
19681968
vimState.recordedState.operatorPositionDiff = openPos.subtract(selStart);
19691969
}
19701970

1971+
// Adjust for VisualLine mode: exclude the line containing the closing brace
1972+
// moves the cursor back to just within the brackets, accurately mirroring what
1973+
// Vim does for Vi{ Vi( Vi[ etc.
1974+
if (
1975+
!this.includeSurrounding &&
1976+
vimState.currentMode === Mode.VisualLine &&
1977+
closePos.line > openPos.line
1978+
) {
1979+
const adjustedLine = closePos.line - 1;
1980+
if (adjustedLine >= 0) {
1981+
const lineText = vimState.document.lineAt(adjustedLine).text;
1982+
closePos = new Position(adjustedLine, lineText.length);
1983+
}
1984+
}
1985+
19711986
// TODO: setting the cursor manually like this shouldn't be necessary (probably a Cursor, not Position, should be passed to `exec`)
19721987
vimState.cursorStartPosition = openPos;
19731988
return {

test/mode/modeVisualLine.test.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import * as assert from 'assert';
33
import { getAndUpdateModeHandler } from '../../extension';
44
import { Mode } from '../../src/mode/mode';
55
import { ModeHandler } from '../../src/mode/modeHandler';
6-
import { newTest } from '../testSimplifier';
6+
import { newTest, newTestOnly } from '../testSimplifier';
77
import { assertEqualLines, setupWorkspace } from './../testUtils';
88

99
suite('Mode Visual Line', () => {
@@ -585,4 +585,26 @@ suite('Mode Visual Line', () => {
585585
});
586586
}
587587
});
588+
589+
suite('Vi{ should not select the ending brace, if it is on a new line.', () => {
590+
test('Vi{ selection content test', async () => {
591+
// Insert the full block using insert mode simulation
592+
await modeHandler.handleMultipleKeyEvents('i{\nsome text on new line\n}'.split(''));
593+
594+
// Back to normal mode
595+
await modeHandler.handleKeyEvent('<Esc>');
596+
597+
// Move cursor to start of "some text..."
598+
await modeHandler.handleMultipleKeyEvents(['g', 'g', 'j', 'l', 'l', 'l', 'l']);
599+
600+
// Simulate Vi{
601+
await modeHandler.handleMultipleKeyEvents(['V', 'i', '{']);
602+
603+
const doc = modeHandler.vimState.editor.document;
604+
const sel = modeHandler.vimState.editor.selection;
605+
const selectedText = doc.getText(sel);
606+
607+
assert.strictEqual(selectedText, ' some text on new line');
608+
});
609+
});
588610
});

0 commit comments

Comments
 (0)