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

Implement ampersand (&) action #3925

Merged
merged 1 commit into from
Jul 26, 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
14 changes: 14 additions & 0 deletions src/actions/commands/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2153,6 +2153,20 @@ class CommandDot extends BaseCommand {
}
}

@RegisterAction
class CommandRepeatSubstitution extends BaseCommand {
modes = [ModeName.Normal];
keys = ['&'];

public async exec(position: Position, vimState: VimState): Promise<VimState> {
// Parsing the command from a string, while not ideal, is currently
// necessary to make this work with and without neovim integration
await commandLine.Run('s', vimState);

return vimState;
}
}

abstract class CommandFold extends BaseCommand {
modes = [ModeName.Normal, ModeName.Visual, ModeName.VisualLine];
commandName: string;
Expand Down
15 changes: 15 additions & 0 deletions test/cmd_line/substitute.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,7 @@ suite('Basic substitute', () => {
assertEqualLines(['bz']);
});
});

suite('Substitute should use various previous search/substitute states', () => {
test('Substitute with previous search using *', async () => {
await modeHandler.handleMultipleKeyEvents([
Expand Down Expand Up @@ -364,6 +365,7 @@ suite('Basic substitute', () => {

assertEqualLines(['fighters', 'bar', 'fighters', 'bar']);
});

test('Substitute with previous search using #', async () => {
await modeHandler.handleMultipleKeyEvents([
'i',
Expand Down Expand Up @@ -392,6 +394,7 @@ suite('Basic substitute', () => {

assertEqualLines(['foo', 'fighters', 'foo', 'fighters']);
});

test('Substitute with previous search using /', async () => {
await modeHandler.handleMultipleKeyEvents([
'i',
Expand Down Expand Up @@ -424,6 +427,7 @@ suite('Basic substitute', () => {

assertEqualLines(['fighters', 'bar', 'fighters', 'bar']);
});

newTest({
title: 'Substitute with parameters should update search state',
start: ['foo', 'bar', 'foo', 'bar|'],
Expand All @@ -434,6 +438,7 @@ suite('Basic substitute', () => {
'rr', // and replace a with r
end: ['foo', 'bite', 'foo', 'b|rr'],
});

newTest({
title:
'Substitute with empty replacement should delete previous substitution (all variants) and accepts flags',
Expand Down Expand Up @@ -474,6 +479,7 @@ suite('Basic substitute', () => {
'| is here',
],
});

newTest({
title:
'Substitute with no pattern should repeat previous substitution and not alter search state',
Expand All @@ -486,12 +492,14 @@ suite('Basic substitute', () => {
'rp', // and replace l with p (confirming search state was unaltered)
end: ['legend', 'zelda', 'legend', 'zelda', '|pink'],
});

newTest({
title: 'Substitute repeat previous should accept flags',
start: ['|fooo'],
keysPressed: ':s/o/un\n:s g\n', // repeated replacement accepts g flag, replacing all other occurrences
end: ['|fununun'],
});

test('Substitute with empty search string should use last searched pattern', async () => {
await modeHandler.handleMultipleKeyEvents([
'i',
Expand Down Expand Up @@ -528,5 +536,12 @@ suite('Basic substitute', () => {

assertEqualLines(['foo', 'fighters', 'foo', 'fighters']);
});

newTest({
title: 'Ampersand (&) should repeat the last substitution',
start: ['|foo bar baz'],
keysPressed: ':s/ba/t\n' + '&',
end: ['|foo tr tz'],
});
});
});