Skip to content
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

Fixed a bug where insertLineAbove would leave extra whitespace. #7450

Merged
merged 21 commits into from
Feb 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
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
44 changes: 24 additions & 20 deletions src/actions/commands/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1536,28 +1536,32 @@ export class CommandInsertNewLineAbove extends BaseCommand {
}

vimState.cursors = getCursorsAfterSync(vimState.editor);
const indentAmt = Math.max(charPos - vimState.cursors[0].start.character, 0);
const endPos = vimState.cursors[0].start.character;
const indentAmt = charPos - endPos;

const firstPos = new Position(vimState.cursors[0].start.line, charPos);
vimState.cursors[0] = new Cursor(firstPos, firstPos);
vimState.recordedState.transformer.addTransformation({
type: 'insertText',
text: TextEditor.setIndentationLevel('', indentAmt),
position: firstPos,
cursorIndex: 0,
manuallySetCursorPositions: true,
});

for (let i = 1; i < count; i++) {
for (let i = 0; i < count; i++) {
const newPos = new Position(vimState.cursors[0].start.line + i, charPos);
vimState.cursors.push(new Cursor(newPos, newPos));
vimState.recordedState.transformer.addTransformation({
type: 'insertText',
text: TextEditor.setIndentationLevel('', indentAmt),
position: newPos,
cursorIndex: i,
manuallySetCursorPositions: true,
});
if (i === 0) {
vimState.cursors[0] = new Cursor(newPos, newPos);
} else {
vimState.cursors.push(new Cursor(newPos, newPos));
}
if (indentAmt >= 0) {
vimState.recordedState.transformer.addTransformation({
type: 'insertText',
text: TextEditor.setIndentationLevel('', indentAmt),
position: newPos,
cursorIndex: i,
manuallySetCursorPositions: true,
});
} else {
vimState.recordedState.transformer.addTransformation({
type: 'deleteRange',
cursorIndex: i,
range: new vscode.Range(newPos, new Position(newPos.line, endPos)),
manuallySetCursorPositions: true,
});
}
}
vimState.cursors = vimState.cursors.reverse();
vimState.isFakeMultiCursor = true;
Expand Down
26 changes: 26 additions & 0 deletions test/actions/insertLine.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,32 @@ suite('insertLineBefore', () => {
assert.strictEqual(text[2].replace(/[\n\r]/g, ''), text[3].replace(/[\n\r]/g, ''));
});

test('no extra whitespace left when insertLineBefore inserts more than correct amount', async () => {
await modeHandler.handleMultipleKeyEvents(['<Esc>', 'g', 'g', 'd', 'G']);
await modeHandler.handleMultipleKeyEvents('i\na'.split(''));
await modeHandler.handleMultipleKeyEvents(['<Esc>']);
await modeHandler.handleMultipleKeyEvents('2G>>ob\nc'.split(''));
await modeHandler.handleMultipleKeyEvents(['<Esc>']);
await modeHandler.handleMultipleKeyEvents('3G>>'.split(''));

// This is the current state of the document
//
// a
// b
// c
await modeHandler.handleMultipleKeyEvents(['<Esc>', '4', 'G', '2', 'O', 'c']);
const text = vscode.window.activeTextEditor?.document.getText().split('\n');
//
// a
// b
// c
// c
// c
assert.ok(text);
assert.strictEqual(text[3].replace(/[\n\r]/g, ''), text[4].replace(/[\n\r]/g, ''));
assert.strictEqual(text[4].replace(/[\n\r]/g, ''), text[5].replace(/[\n\r]/g, ''));
});

test('works at the top of the document', async () => {
await modeHandler.handleMultipleKeyEvents(['<Esc>', 'g', 'g', 'd', 'G']);
await modeHandler.handleMultipleKeyEvents('ia'.split(''));
Expand Down