Skip to content

Commit

Permalink
Fix a few issues with A in visual block mode
Browse files Browse the repository at this point in the history
- Adds spaces when line isn't long enough (fixes #4795)
- Don't delete text when selection is on whitespace (fixes #4796)
- Works with multiple cursors
  • Loading branch information
J-Fields committed May 4, 2020
1 parent 1427f38 commit dc43ab9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 12 deletions.
30 changes: 18 additions & 12 deletions src/actions/commands/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3769,22 +3769,28 @@ class ActionGoToInsertVisualBlockModeAppend extends BaseCommand {
}

public async exec(position: Position, vimState: VimState): Promise<VimState> {
const newCursors: Range[] = [];
for (const cursor of vimState.cursors) {
const [start, end] = Position.sorted(cursor.start, cursor.stop);
for (let lineNum = start.line; lineNum <= end.line; lineNum++) {
const line = TextEditor.getLine(lineNum);
const insertionColumn =
vimState.desiredColumn === Number.POSITIVE_INFINITY
? line.text.length
: Math.max(cursor.start.character, cursor.stop.character) + 1;
if (line.text.length < insertionColumn) {
await TextEditor.insertAt(' '.repeat(insertionColumn - line.text.length), line.range.end);
}
const newCursor = new Position(lineNum, insertionColumn);
newCursors.push(new Range(newCursor, newCursor));
}
}

vimState.cursors = newCursors;
await vimState.setCurrentMode(Mode.Insert);
vimState.isMultiCursor = true;
vimState.isFakeMultiCursor = true;

for (const { line, end } of TextEditor.iterateLinesInBlock(vimState)) {
if (line.trim() === '') {
vimState.recordedState.transformations.push({
type: 'replaceText',
text: TextEditor.setIndentationLevel(line, end.character),
start: new Position(end.line, 0),
end: new Position(end.line, end.character),
});
}
vimState.cursors.push(new Range(end, end));
}
vimState.cursors = vimState.cursors.slice(1);
return vimState;
}
}
Expand Down
7 changes: 7 additions & 0 deletions test/mode/modeVisualBlock.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ suite('Mode Visual Block', () => {
end: ['tes123|t', 'tes123t'],
});

newTest({
title: '`A` over shorter line adds necessary spaces',
start: ['te|st', 'te', 't'],
keysPressed: '<C-v>jjA123',
end: ['tes123|t', 'te 123', 't 123'],
});

newTest({
title: 'Can handle I forward select',
start: ['|test', 'test'],
Expand Down

0 comments on commit dc43ab9

Please sign in to comment.