Skip to content

Commit

Permalink
Fix replace character (r) behavior with newline (VSCodeVim#3735)
Browse files Browse the repository at this point in the history
The cursor now ends up at the beginning of the next line and will only ever insert one newline, regardless of count prefix.
Fixes VSCodeVim#1884
  • Loading branch information
J-Fields authored and jpoon committed May 7, 2019
1 parent 1ef304e commit 490e16a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/actions/commands/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3505,6 +3505,16 @@ class ActionReplaceCharacter extends BaseCommand {
cursorIndex: this.multicursorIndex,
diff: new PositionDiff(0, -1),
});
} else if (toReplace === '\n') {
// A newline replacement always inserts exactly one newline (regardless
// of count prefix) and puts the cursor on the next line.
vimState.recordedState.transformations.push({
type: 'replaceText',
text: '\n',
start: position,
end: endPos,
diff: PositionDiff.NewBOLDiff(1),
});
} else {
vimState.recordedState.transformations.push({
type: 'replaceText',
Expand Down
15 changes: 15 additions & 0 deletions test/mode/normalModeTests/commands.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,21 @@ suite('Mode Normal', () => {
end: ['one', '|Three'],
});

newTest({
title: "Can handle 'r\n'",
start: ['abc|defg', '12345'],
keysPressed: 'r\n',
end: ['abc', '|efg', '12345'],
});

// `r` only ever inserts one newline, regardless of count prefix
newTest({
title: "Can handle '<Count>r\n'",
start: ['abc|defg', '12345'],
keysPressed: '3r\n',
end: ['abc', '|g', '12345'],
});

newTest({
title: "Can handle 'J' once",
start: ['one', 'tw|o'],
Expand Down

0 comments on commit 490e16a

Please sign in to comment.