Fix: visual block yank, delete and put behavior for single line selections.

Fixes: #3231, #2961, #2336, #2093, #1796
This commit is contained in:
Faddi 2019-01-16 07:22:18 +01:00
parent 01b73fbbd3
commit 34d27e3347
3 changed files with 68 additions and 11 deletions

View File

@ -1411,15 +1411,19 @@ export class PutCommand extends BaseCommand {
.join('\n');
}
if (after) {
if (register.registerMode === RegisterMode.LineWise) {
// P insert before current line
if (after) {
textToAdd = text + '\n';
whereToAddText = dest.getLineBegin();
} else {
// p paste after current line
textToAdd = '\n' + text;
whereToAddText = dest.getLineEnd();
}
} else {
textToAdd = text;
whereToAddText = after ? position : position.getRight();
}
}
// After using "p" or "P" in Visual mode the text that was put will be
@ -1470,12 +1474,22 @@ export class PutCommand extends BaseCommand {
} else {
if (text.indexOf('\n') === -1) {
if (!position.isLineEnd()) {
if (
register.registerMode === RegisterMode.BlockWise
) {
if (after) {
diff = new PositionDiff(0, -1 * text.length);
} else {
diff = new PositionDiff(0, 1);
}
} else {
if (after) {
diff = new PositionDiff(0, -1);
} else {
diff = new PositionDiff(0, textToAdd.length);
}
}
}
} else {
if (position.isLineEnd()) {
diff = PositionDiff.NewBOLDiff(-numNewlines, position.character);
@ -3604,7 +3618,10 @@ class ActionXVisualBlock extends BaseCommand {
}
public async exec(position: Position, vimState: VimState): Promise<VimState> {
for (const { start, end } of Position.IterateLine(vimState)) {
const lines: string[] = [];
for (const { line, start, end } of Position.IterateLine(vimState)) {
lines.push(line);
vimState.recordedState.transformations.push({
type: 'deleteRange',
range: new Range(start, end),
@ -3612,6 +3629,10 @@ class ActionXVisualBlock extends BaseCommand {
});
}
const text = lines.length === 1 ? lines[0] : lines.join('\n');
vimState.currentRegisterMode = RegisterMode.BlockWise;
Register.put(text, vimState, this.multicursorIndex);
const topLeft = VisualBlockMode.getTopLeftPosition(
vimState.cursorPosition,
vimState.cursorStartPosition

View File

@ -648,9 +648,17 @@ export class YankVisualBlockMode extends BaseOperator {
public async run(vimState: VimState, start: Position, end: Position): Promise<VimState> {
let toCopy: string = '';
const isMultiline = start.line !== end.line;
for (const { line } of Position.IterateLine(vimState)) {
if (isMultiline) {
toCopy += line + '\n';
} else {
toCopy = line;
}
}
vimState.currentRegisterMode = RegisterMode.BlockWise;
Register.put(toCopy, vimState, this.multicursorIndex);

View File

@ -44,4 +44,32 @@ suite('put operator', () => {
keysPressed: '2yyjjpk',
end: ['one', 'two', '|three', 'one', 'two', 'four'],
});
newTest({
title: 'test visual block single line yank p',
start: ['12|345'],
keysPressed: '<C-v>llyhp',
end: ['12|345345'],
});
newTest({
title: 'test visual block single line yank P',
start: ['12|345'],
keysPressed: '<C-v>llyhP',
end: ['1|3452345'],
});
newTest({
title: 'test visual block single line delete p',
start: ['12|345'],
keysPressed: '<C-v>lldhp',
end: ['1|3452'],
});
newTest({
title: 'test visual block single line delete P',
start: ['12|345'],
keysPressed: '<C-v>lldhP',
end: ['|34512'],
});
});