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 all 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
4 changes: 2 additions & 2 deletions 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, "", { isRegex: true });
vimState.currentMode = ModeName.SearchInProgressMode;

return vimState;
Expand All @@ -796,7 +796,7 @@ export class CommandSearchBackwards extends BaseCommand {
isMotion = true;

public async exec(position: Position, vimState: VimState): Promise<VimState> {
vimState.searchState = new SearchState(SearchDirection.Backward, vimState.cursorPosition);
vimState.searchState = new SearchState(SearchDirection.Backward, vimState.cursorPosition, "", { isRegex: true });
vimState.currentMode = ModeName.SearchInProgressMode;

return vimState;
Expand Down
22 changes: 19 additions & 3 deletions src/mode/modeHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,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 @@ -214,7 +215,21 @@ 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 regexFlags = ignorecase ? 'gi' : 'g';

let regex: RegExp;
try {
regex = new RegExp(searchRE, regexFlags);
} catch (err) {
// Couldn't compile the regexp, try again with special characters escaped
searchRE = search.replace(SearchState.specialCharactersRegex, "\\$&");
regex = new RegExp(searchRE, regexFlags);
}

outer:
for (let lineIdx = 0; lineIdx < TextEditor.getLineCount(); lineIdx++) {
Expand All @@ -228,7 +243,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 @@ -281,10 +296,11 @@ export class SearchState {
}
}

constructor(direction: SearchDirection, startPosition: Position, searchString = "") {
constructor(direction: SearchDirection, startPosition: Position, searchString = "", { isRegex = false } = {}) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not really sure about this syntax (still a TS noob). Is this what you were thinking?

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, that looks fine to me!

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 two2o"],
keysPressed: "/o\\do",
end: ["", "one tw|o2o"]
});

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