Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
rebornix committed Oct 21, 2016
1 parent 56b0cba commit 18b02a3
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/actions/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1605,23 +1605,29 @@ export class DeleteOperator extends BaseOperator {
}
}

let text = vscode.window.activeTextEditor.document.getText(new vscode.Range(start, end));

// If we delete linewise to the final line of the document, we expect the line
// to be removed. This is actually a special case because the newline
// character we've selected to delete is the newline on the end of the document,
// but we actually delete the newline on the second to last line.

// Just writing about this is making me more confused. -_-

// rebornix: johnfn's description about this corner case is perfectly correct. The only catch is
// that we definitely don't want to put the EOL in the register. So here we run the `getText`
// expression first and then update the start position.

// Now rebornix is confused as well.
if (isOnLastLine &&
start.line !== 0 &&
registerMode === RegisterMode.LineWise) {
start = start.getPreviousLineBegin().getLineEnd();
}

let text = vscode.window.activeTextEditor.document.getText(new vscode.Range(start, end));

if (registerMode === RegisterMode.LineWise) {
// slice final newline in linewise mode - linewise put will add it back.
text = text.endsWith("\r\n") ? text.slice(0, -2) : text.slice(0, -1);
text = text.endsWith("\r\n") ? text.slice(0, -2) : (text.endsWith('\n') ? text.slice(0, -1) : text);
}

if (yank) {
Expand Down Expand Up @@ -1971,9 +1977,11 @@ export class PutCommand extends BaseCommand {
}

if (after) {
// P insert before current line
textToAdd = text + "\n";
whereToAddText = dest.getLineBegin();
} else {
// p paste after current line
textToAdd = "\n" + text;
whereToAddText = dest.getLineEnd();
}
Expand All @@ -1991,7 +1999,7 @@ export class PutCommand extends BaseCommand {
if (after) {
diff = PositionDiff.NewBOLDiff(-numNewlines - 1, numWhitespace);
} else {
diff = PositionDiff.NewBOLDiff(currentLineLength > 0 ? 1 : 0, numWhitespace);
diff = PositionDiff.NewBOLDiff(currentLineLength > 0 ? 1 : -numNewlines, numWhitespace);
}
} else {
if (text.indexOf("\n") === -1) {
Expand Down
30 changes: 30 additions & 0 deletions test/mode/modeVisualLine.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,4 +261,34 @@ suite("Mode Visual", () => {
end: ['blah', '|duh']
});
});

suite("Can handle d correctly in Visual Line Mode", () => {
newTest({
title: "Can handle d key",
start: ['|{', ' a = 1;', '}'],
keysPressed: 'VGdp',
end: ['', '|{', ' a = 1;', '}']
});

newTest({
title: "Can handle d key",
start: ['|{', ' a = 1;', '}'],
keysPressed: 'VGdP',
end: ['|{', ' a = 1;', '}', '']
});

newTest({
title: "Can handle d key",
start: ['1', '2', '|{', ' a = 1;', '}'],
keysPressed: 'VGdp',
end: ['1', '2', '|{', ' a = 1;', '}']
});

newTest({
title: "Can handle d key",
start: ['1', '2', '|{', ' a = 1;', '}'],
keysPressed: 'VGdP',
end: ['1', '|{', ' a = 1;', '}', '2']
});
});
});

0 comments on commit 18b02a3

Please sign in to comment.