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

bug: fixes behaviour of search when using * and # (fixes #2517) #2518

Merged
merged 5 commits into from
Apr 10, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions src/cmd_line/commands/substitute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as vscode from 'vscode';
import * as node from '../node';
import * as token from '../token';
import { VimState } from '../../state/vimState';
import { VimError, ErrorCode } from '../../error';
import { TextEditor } from '../../textEditor';
import { configuration } from '../../configuration/configuration';

Expand Down Expand Up @@ -79,15 +80,16 @@ export class SubstituteCommand extends node.CommandBase {
jsRegexFlags += 'i';
}

// If no pattern is entered, use previous search state
// If no pattern is entered, use previous search state (including search with * and #)
if (args.pattern === '') {
const prevSearchState = vimState.globalState.searchStatePrevious;
if (prevSearchState) {
const prevSearchString = prevSearchState[prevSearchState.length - 1].searchString;
args.pattern = prevSearchString;
const prevSearchState = vimState.globalState.searchState;
if (prevSearchState === undefined || prevSearchState.searchString === '') {
throw VimError.fromCode(ErrorCode.E35);
// throw new Error('Empty search string!');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this comment

} else {
args.pattern = prevSearchState.searchString;
}
}

return new RegExp(args.pattern, jsRegexFlags);
}

Expand Down
4 changes: 4 additions & 0 deletions src/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,24 @@ interface IErrorMessage {

export enum ErrorCode {
E32 = 32,
E35 = 35,
E37 = 37,
E208 = 208,
E348 = 348,
E444 = 444,
E476 = 476,
E488 = 488,
E492 = 492,
}

export const ErrorMessage: IErrorMessage = {
32: 'No file name',
35: 'No previous regular expression',
37: 'No write since last change (add ! to override)',
208: 'Error writing to file',
348: 'No string under cursor',
444: 'Cannot close last window',
476: 'Invalid command',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we using this somewhere?

488: 'Trailing characters',
492: 'Not an editor command',
};
Expand Down
128 changes: 128 additions & 0 deletions test/cmd_line/substitute.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,4 +212,132 @@ suite('Basic substitute', () => {
assertEqualLines(['bz']);
});
});
suite('Substitute with empty search string should use previous search', () => {
test('Substitute with previous search using *', async () => {
await modeHandler.handleMultipleKeyEvents([
'i',
'f',
'o',
'o',
'<Esc>',
'o',
'b',
'a',
'r',
'<Esc>',
'o',
'f',
'o',
'o',
'<Esc>',
'o',
'b',
'a',
'r',
'<Esc>',
'g',
'g', // back to the first line
'*', // search for foo
]);
await CommandLine.Run('%s//fighters', modeHandler.vimState);

assertEqualLines(['fighters', 'bar', 'fighters', 'bar']);
});
test('Substitute with previous search using #', async () => {
await modeHandler.handleMultipleKeyEvents([
'i',
'f',
'o',
'o',
'<Esc>',
'o',
'b',
'a',
'r',
'<Esc>',
'o',
'f',
'o',
'o',
'<Esc>',
'o',
'b',
'a',
'r',
'<Esc>',
'#', // search for bar
]);
await CommandLine.Run('%s//fighters', modeHandler.vimState);

assertEqualLines(['foo', 'fighters', 'foo', 'fighters']);
});
test('Substitute with previous search using /', async () => {
await modeHandler.handleMultipleKeyEvents([
'i',
'f',
'o',
'o',
'<Esc>',
'o',
'b',
'a',
'r',
'<Esc>',
'o',
'f',
'o',
'o',
'<Esc>',
'o',
'b',
'a',
'r',
'<Esc>',
'/',
'f',
'o',
'o', // search for foo
'\n',
]);
await CommandLine.Run('%s//fighters', modeHandler.vimState);

assertEqualLines(['fighters', 'bar', 'fighters', 'bar']);
});
test('Substitute with empty search string should use last searched pattern', async () => {
await modeHandler.handleMultipleKeyEvents([
'i',
'f',
'o',
'o',
'<Esc>',
'o',
'b',
'a',
'r',
'<Esc>',
'o',
'f',
'o',
'o',
'<Esc>',
'o',
'b',
'a',
'r',
'<Esc>',
'/',
'f',
'o',
'o', // search for foo
'\n',
'2', // go to the second line
'g',
'g',
'*', // now search for bar
]);
await CommandLine.Run('%s//fighters', modeHandler.vimState);

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