Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make ReplaceWithRegister work in visual mode #4016

Merged
merged 4 commits into from
Aug 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 5 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -598,12 +598,11 @@ Based on [ReplaceWithRegister](https://github.com/vim-scripts/ReplaceWithRegiste

Once active, type `gr` (say "go replace") followed by a motion to describe the text you want replaced by the contents of the register.

| Motion Command | Description |
| -------------- | ---------------------------------------------------------------------------------- |
| `gr<motion>` | Replace the text described by the motion with the contents of the default register |
| `"agr<motion>` | Replace the text described by the motion with the contents of the `a` register |
| `grr` | Replace the currentline with the contents of the default register |
| `<count>grr` | Replace the specified number of lines with the contents of the default register |
| Motion Command | Description |
| ----------------------- | --------------------------------------------------------------------------------------- |
| `[count]["a]gr<motion>` | Replace the text described by the motion with the contents of the specified register |
| `[count]["a]grr` | Replace the \[count\] lines or current line with the contents of the specified register |
| `{Visual}["a]gr` | Replace the selection with the contents of the specified register |

## 🎩 VSCodeVim tricks!

Expand Down
4 changes: 2 additions & 2 deletions src/actions/plugins/replaceWithRegister.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { RegisterAction } from './../base';
@RegisterAction
export class ReplaceOperator extends BaseOperator {
public keys = ['g', 'r'];
public modes = [ModeName.Normal];
public modes = [ModeName.Normal, ModeName.Visual, ModeName.VisualLine];

public doesActionApply(vimState: VimState, keysPressed: string[]): boolean {
return configuration.replaceWithRegister && super.doesActionApply(vimState, keysPressed);
Expand All @@ -27,7 +27,7 @@ export class ReplaceOperator extends BaseOperator {
const replaceWith = register.text as string;

await TextEditor.replace(range, replaceWith);

await vimState.setCurrentMode(ModeName.Normal);
return updateCursorPosition(vimState, range, replaceWith);
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/cmd_line/commands/sort.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ export class SortCommand extends node.CommandBase {

let lastLineLength = originalLines[originalLines.length - 1].length;

const compareFn = this._arguments.ignoreCase ?
(a: string, b: string) => a.toLowerCase().localeCompare(b.toLowerCase()) :
(a: string, b: string) => a.localeCompare(b);
const compareFn = this._arguments.ignoreCase
? (a: string, b: string) => a.toLowerCase().localeCompare(b.toLowerCase())
: (a: string, b: string) => a.localeCompare(b);

let sortedLines = originalLines.sort(compareFn);

Expand Down
2 changes: 1 addition & 1 deletion src/configuration/notation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export class Notation {

// Converts keystroke like <tab> to a single control character like \t
public static ToControlCharacter(key: string) {
if (key === "<tab>") {
if (key === '<tab>') {
return '\t';
}

Expand Down
21 changes: 21 additions & 0 deletions test/plugins/replaceWithRegister.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,25 @@ suite('replaceWithRegister plugin', () => {
keysPressed: `${YankInnerWord}2grr`,
end: ['|first', 'third'],
});

newTest({
title: 'Replaces in visual mode',
start: ['|first second'],
keysPressed: `${YankInnerWord}wviw${ReplaceOperator}`,
end: ['first firs|t'],
});

newTest({
title: 'Replaces in visual mode using a specified register',
start: ['|first second'],
keysPressed: `"a${YankInnerWord}wviw"a${ReplaceOperator}`,
end: ['first firs|t'],
});

newTest({
title: 'Replaces in visual line mode',
start: ['|first second'],
keysPressed: `${YankInnerWord}wV${ReplaceOperator}`,
end: ['firs|t'],
});
});