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

Allow regex in / search #627

Merged
merged 6 commits into from
Aug 28, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion src/actions/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -782,7 +782,7 @@ export class CommandSearchForwards extends BaseCommand {
isMotion = true;

public async exec(position: Position, vimState: VimState): Promise<VimState> {
vimState.searchState = new SearchState(SearchDirection.Forward, vimState.cursorPosition);
vimState.searchState = new SearchState(SearchDirection.Forward, vimState.cursorPosition, "", true);
Copy link
Member

Choose a reason for hiding this comment

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

Minor, but I'm trying to move towards a style where we explicitly name all our boolean arguments. Something like { isRegex: true } is what I'm thinking here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sounds good.

vimState.currentMode = ModeName.SearchInProgressMode;

return vimState;
Expand Down
12 changes: 9 additions & 3 deletions src/mode/modeHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ export class SearchState {

private _matchesDocVersion: number;
private _searchDirection: SearchDirection = SearchDirection.Forward;
private isRegex: boolean;

private _searchString = "";
public get searchString(): string {
Expand Down Expand Up @@ -213,7 +214,11 @@ export class SearchState {
ignorecase = false;
}

const regex = new RegExp(search.replace(SearchState.specialCharactersRegex, "\\$&"), ignorecase ? 'gi' : 'g');
let searchRE = search;
if (!this.isRegex) {
searchRE = search.replace(SearchState.specialCharactersRegex, "\\$&");
Copy link
Member

Choose a reason for hiding this comment

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

This is very cool. I never knew about $&.

}
const regex = new RegExp(searchRE, ignorecase ? 'gi' : 'g');

outer:
for (let lineIdx = 0; lineIdx < TextEditor.getLineCount(); lineIdx++) {
Expand All @@ -227,7 +232,7 @@ export class SearchState {

this.matchRanges.push(new vscode.Range(
new Position(lineIdx, result.index),
new Position(lineIdx, result.index + search.length)
new Position(lineIdx, result.index + result[0].length)
));

if (result.index === regex.lastIndex) {
Expand Down Expand Up @@ -280,10 +285,11 @@ export class SearchState {
}
}

constructor(direction: SearchDirection, startPosition: Position, searchString = "") {
constructor(direction: SearchDirection, startPosition: Position, searchString = "", isRegex = false) {
this._searchDirection = direction;
this._searchCursorStartPosition = startPosition;
this.searchString = searchString;
this.isRegex = isRegex;
}
}

Expand Down
7 changes: 7 additions & 0 deletions test/mode/modeNormal.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1191,6 +1191,13 @@ suite("Mode Normal", () => {
end: ["|one", "twooo", "thurr"]
});

newTest({
title: "/ can search with regex",
start: ["|", "one two"],
keysPressed: "/o.*o",
end: ["", "|one two"]
});

newTest({
title: "Can do C",
start: ["export const options = {", "|", "};"],
Expand Down