Skip to content

Commit 59d55e1

Browse files
Fix #7159 bad handling of auto-surrounding selections (#8533)
1 parent 00cbd68 commit 59d55e1

File tree

3 files changed

+61
-7
lines changed

3 files changed

+61
-7
lines changed

src/mode/modeHandler.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1272,6 +1272,10 @@ export class ModeHandler implements vscode.Disposable, IModeHandler {
12721272
}
12731273
break;
12741274

1275+
case Mode.Insert:
1276+
// Don't collapse existing selections in insert mode
1277+
selections.push(new vscode.Selection(start, stop));
1278+
break;
12751279
default:
12761280
// Note that this collapses the selection onto one position
12771281
selections.push(new vscode.Selection(stop, stop));

src/transformations/execute.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -241,13 +241,19 @@ export async function executeTransformations(
241241
}
242242
}
243243

244-
const selections = vimState.editor.selections.map((sel) => {
245-
let range = Cursor.FromVSCodeSelection(sel);
246-
if (range.start.isBefore(range.stop)) {
247-
range = range.withNewStop(range.stop.getLeftThroughLineBreaks(true));
248-
}
249-
return new vscode.Selection(range.start, range.stop);
250-
});
244+
let selections;
245+
if (vimState.currentMode === Mode.Insert) {
246+
// Insert mode selections do not need to be modified
247+
selections = vimState.editor.selections;
248+
} else {
249+
selections = vimState.editor.selections.map((sel) => {
250+
let range = Cursor.FromVSCodeSelection(sel);
251+
if (range.start.isBefore(range.stop)) {
252+
range = range.withNewStop(range.stop.getLeftThroughLineBreaks(true));
253+
}
254+
return new vscode.Selection(range.start, range.stop);
255+
});
256+
}
251257
const firstTransformation = transformations[0];
252258
const manuallySetCursorPositions =
253259
(firstTransformation.type === 'deleteRange' ||

test/mode/modeInsert.test.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -625,4 +625,48 @@ suite('Mode Insert', () => {
625625
endMode: Mode.Insert,
626626
});
627627
});
628+
629+
suite('VSCode auto-surround', () => {
630+
test('preserves selection', async () => {
631+
await modeHandler.handleMultipleKeyEvents(['i', 's', 'e', 'l', 'e', 'c', 't']);
632+
await vscode.commands.executeCommand('editor.action.selectAll');
633+
await modeHandler.handleKeyEvent('"');
634+
assertEqualLines(['"select"']);
635+
assert.strictEqual(modeHandler.currentMode, Mode.Insert);
636+
assert.strictEqual(vscode.window.activeTextEditor!.selection.start.character, 1);
637+
assert.strictEqual(vscode.window.activeTextEditor!.selection.end.character, 7);
638+
});
639+
640+
test('replaces selection', async () => {
641+
await modeHandler.handleMultipleKeyEvents(['i', 't', 'e', 'm', 'p']);
642+
await vscode.commands.executeCommand('editor.action.selectAll');
643+
await modeHandler.handleMultipleKeyEvents(['"', 'f', 'i', 'n', 'a', 'l']);
644+
assertEqualLines(['"final"']);
645+
assert.strictEqual(modeHandler.currentMode, Mode.Insert);
646+
assert.strictEqual(vscode.window.activeTextEditor!.selection.start.character, 6);
647+
assert.strictEqual(vscode.window.activeTextEditor!.selection.end.character, 6);
648+
});
649+
650+
test('stacks', async () => {
651+
await modeHandler.handleMultipleKeyEvents(['i', 't', 'e', 'x', 't']);
652+
await vscode.commands.executeCommand('editor.action.selectAll');
653+
654+
await modeHandler.handleMultipleKeyEvents(['"', "'", '(', '[', '{', '<', '`']);
655+
assertEqualLines(['"\'([{<`text`>}])\'"']);
656+
});
657+
658+
test('handles snippet', async () => {
659+
await modeHandler.handleKeyEvent('i');
660+
await vscode.commands.executeCommand('editor.action.insertSnippet', {
661+
snippet: '${3:foo} ${1:bar} ${2:baz}',
662+
});
663+
await modeHandler.handleMultipleKeyEvents(['(', 'o', 'n', 'e']);
664+
await vscode.commands.executeCommand('jumpToNextSnippetPlaceholder');
665+
await modeHandler.handleMultipleKeyEvents(['<', 't', 'w', 'o']);
666+
await vscode.commands.executeCommand('jumpToNextSnippetPlaceholder');
667+
await modeHandler.handleKeyEvent('`');
668+
assertEqualLines(['`foo` (one) <two>']);
669+
assert.strictEqual(modeHandler.currentMode, Mode.Insert);
670+
});
671+
});
628672
});

0 commit comments

Comments
 (0)