Skip to content

Commit

Permalink
Fix error when count exceeds max number of rows in "[count]Y/J" (#4628)
Browse files Browse the repository at this point in the history
`Position.getDown()` does bounds-checking, so it prevents going past the document's end.
  • Loading branch information
lusingander authored Mar 30, 2020
1 parent 38db172 commit 33f3c88
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/actions/commands/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2140,7 +2140,7 @@ export class CommandYankFullLine extends BaseCommand {
public async exec(position: Position, vimState: VimState): Promise<VimState> {
const linesDown = (vimState.recordedState.count || 1) - 1;
const start = position.getLineBegin();
const end = new Position(position.line + linesDown, 0).getLineEnd().getLeft();
const end = position.getDown(linesDown).getLeft();

vimState.currentRegisterMode = RegisterMode.LineWise;

Expand Down Expand Up @@ -3075,7 +3075,7 @@ class ActionJoin extends BaseCommand {
if (position.line + 1 < TextEditor.getLineCount()) {
startLineNumber = position.line;
startColumn = 0;
endLineNumber = startLineNumber + count;
endLineNumber = position.getDown(count).line;
endColumn = TextEditor.getLineLength(endLineNumber);
} else {
startLineNumber = position.line;
Expand Down
14 changes: 14 additions & 0 deletions test/mode/modeNormal.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2016,6 +2016,20 @@ suite('Mode Normal', () => {
end: ['blah blah', '|blah blah'],
});

newTest({
title: 'can do [count]Y',
start: ['|one', 'two', 'three'],
keysPressed: '2Yp',
end: ['one', '|one', 'two', 'two', 'three'],
});

newTest({
title: 'can do [count]Y if count is larger than EOF',
start: ['|one', 'two', 'three'],
keysPressed: '100Yp',
end: ['one', '|one', 'two', 'three', 'two', 'three'],
});

newTest({
title: 'Can do S',
start: [' one', ' tw|o', ' three'],
Expand Down
14 changes: 14 additions & 0 deletions test/mode/normalModeTests/commands.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,20 @@ suite('Mode Normal', () => {
end: ['one|two'],
});

newTest({
title: "Can handle 'J' with count",
start: ['|one', 'two', 'three', 'four'],
keysPressed: '3J',
end: ['one two| three', 'four'],
});

newTest({
title: "Can handle 'J' with count if count is larger than EOF",
start: ['|one', 'two', 'three', 'four'],
keysPressed: '100J',
end: ['one two three| four'],
});

newTest({
title: "Can handle 'J' in Visual Line mode",
start: ['on|e', 'two'],
Expand Down

0 comments on commit 33f3c88

Please sign in to comment.