Skip to content
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
4 changes: 4 additions & 0 deletions src/mode/modeHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1272,6 +1272,10 @@ export class ModeHandler implements vscode.Disposable, IModeHandler {
}
break;

case Mode.Insert:
// Don't collapse existing selections in insert mode
selections.push(new vscode.Selection(start, stop));
break;
default:
// Note that this collapses the selection onto one position
selections.push(new vscode.Selection(stop, stop));
Expand Down
20 changes: 13 additions & 7 deletions src/transformations/execute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,13 +241,19 @@ export async function executeTransformations(
}
}

const selections = vimState.editor.selections.map((sel) => {
let range = Cursor.FromVSCodeSelection(sel);
if (range.start.isBefore(range.stop)) {
range = range.withNewStop(range.stop.getLeftThroughLineBreaks(true));
}
return new vscode.Selection(range.start, range.stop);
});
let selections;
if (vimState.currentMode === Mode.Insert) {
// Insert mode selections do not need to be modified
selections = vimState.editor.selections;
} else {
selections = vimState.editor.selections.map((sel) => {
let range = Cursor.FromVSCodeSelection(sel);
if (range.start.isBefore(range.stop)) {
range = range.withNewStop(range.stop.getLeftThroughLineBreaks(true));
}
return new vscode.Selection(range.start, range.stop);
});
}
const firstTransformation = transformations[0];
const manuallySetCursorPositions =
(firstTransformation.type === 'deleteRange' ||
Expand Down
44 changes: 44 additions & 0 deletions test/mode/modeInsert.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -625,4 +625,48 @@ suite('Mode Insert', () => {
endMode: Mode.Insert,
});
});

suite('VSCode auto-surround', () => {
test('preserves selection', async () => {
await modeHandler.handleMultipleKeyEvents(['i', 's', 'e', 'l', 'e', 'c', 't']);
await vscode.commands.executeCommand('editor.action.selectAll');
await modeHandler.handleKeyEvent('"');
assertEqualLines(['"select"']);
assert.strictEqual(modeHandler.currentMode, Mode.Insert);
assert.strictEqual(vscode.window.activeTextEditor!.selection.start.character, 1);
assert.strictEqual(vscode.window.activeTextEditor!.selection.end.character, 7);
});

test('replaces selection', async () => {
await modeHandler.handleMultipleKeyEvents(['i', 't', 'e', 'm', 'p']);
await vscode.commands.executeCommand('editor.action.selectAll');
await modeHandler.handleMultipleKeyEvents(['"', 'f', 'i', 'n', 'a', 'l']);
assertEqualLines(['"final"']);
assert.strictEqual(modeHandler.currentMode, Mode.Insert);
assert.strictEqual(vscode.window.activeTextEditor!.selection.start.character, 6);
assert.strictEqual(vscode.window.activeTextEditor!.selection.end.character, 6);
});

test('stacks', async () => {
await modeHandler.handleMultipleKeyEvents(['i', 't', 'e', 'x', 't']);
await vscode.commands.executeCommand('editor.action.selectAll');

await modeHandler.handleMultipleKeyEvents(['"', "'", '(', '[', '{', '<', '`']);
assertEqualLines(['"\'([{<`text`>}])\'"']);
});

test('handles snippet', async () => {
await modeHandler.handleKeyEvent('i');
await vscode.commands.executeCommand('editor.action.insertSnippet', {
snippet: '${3:foo} ${1:bar} ${2:baz}',
});
await modeHandler.handleMultipleKeyEvents(['(', 'o', 'n', 'e']);
await vscode.commands.executeCommand('jumpToNextSnippetPlaceholder');
await modeHandler.handleMultipleKeyEvents(['<', 't', 'w', 'o']);
await vscode.commands.executeCommand('jumpToNextSnippetPlaceholder');
await modeHandler.handleKeyEvent('`');
assertEqualLines(['`foo` (one) <two>']);
assert.strictEqual(modeHandler.currentMode, Mode.Insert);
});
});
});