Skip to content

Commit

Permalink
Add regex failure handling
Browse files Browse the repository at this point in the history
  • Loading branch information
bnason-nf committed May 7, 2020
1 parent e42fc86 commit 9ee0584
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 36 deletions.
84 changes: 48 additions & 36 deletions src/findAllInFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,27 +100,33 @@ export function findRegexCase(
return;
}

outputSink.begin(doc, findText, true, true);

// Search each line of the document
const lineCount: number = doc.lineCount;
const findRegExp: RegExp = new RegExp(findText, "g");
for (let line: number = 0; line < lineCount; line += 1) {
const textLine: vscode.TextLine = doc.lineAt(line);
const text: string = textLine.text;
// Search for all the instances within each line
while (true) {
const match: RegExpExecArray | null = findRegExp.exec(text);
if (match === null) {
break;
try {
const findRegExp: RegExp = new RegExp(findText, "g");

outputSink.begin(doc, findText, true, true);

// Search each line of the document
const lineCount: number = doc.lineCount;
for (let line: number = 0; line < lineCount; line += 1) {
const textLine: vscode.TextLine = doc.lineAt(line);
const text: string = textLine.text;
// Search for all the instances within each line
while (true) {
const match: RegExpExecArray | null = findRegExp.exec(text);
if (match === null) {
break;
}
outputSink.item(
new FindResult(text, line, match.index, findRegExp.lastIndex)
);
}
outputSink.item(
new FindResult(text, line, match.index, findRegExp.lastIndex)
);
}
}

outputSink.end();
outputSink.end();
} catch (e) {
// tslint:disable:no-unsafe-any
outputSink.regexFailure(e);
}
}

// Search for all occurrences of a case insensitive search regex within the current file
Expand All @@ -135,25 +141,31 @@ export function findRegexNoCase(
return;
}

outputSink.begin(doc, findText, true, true);

// Search each line of the document
const lineCount: number = doc.lineCount;
const findRegExp: RegExp = new RegExp(findText, "gi");
for (let line: number = 0; line < lineCount; line += 1) {
const textLine: vscode.TextLine = doc.lineAt(line);
const text: string = textLine.text;
// Search for all the instances within each line
while (true) {
const match: RegExpExecArray | null = findRegExp.exec(text);
if (match === null) {
break;
try {
const findRegExp: RegExp = new RegExp(findText, "gi");

outputSink.begin(doc, findText, true, true);

// Search each line of the document
const lineCount: number = doc.lineCount;
for (let line: number = 0; line < lineCount; line += 1) {
const textLine: vscode.TextLine = doc.lineAt(line);
const text: string = textLine.text;
// Search for all the instances within each line
while (true) {
const match: RegExpExecArray | null = findRegExp.exec(text);
if (match === null) {
break;
}
outputSink.item(
new FindResult(text, line, match.index, findRegExp.lastIndex)
);
}
outputSink.item(
new FindResult(text, line, match.index, findRegExp.lastIndex)
);
}
}

outputSink.end();
outputSink.end();
} catch (e) {
// tslint:disable:no-unsafe-any
outputSink.regexFailure(e);
}
}
1 change: 1 addition & 0 deletions src/iOutputSink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ export interface IOutputSink {
end(): void;
item(findResult: FindResult): void;
noDocument(): void;
regexFailure(e: string): void;
}
6 changes: 6 additions & 0 deletions src/treeDataProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ export class TreeDataProvider implements vscode.TreeDataProvider<FindResult>, IO
this.refreshTree();
}

public regexFailure(e: string): void {
this.findResults.length = 0;
this.findResults.push(new FindResult(`Regex failure: ${e}`));
this.refreshTree();
}

private refreshTree(): void {
this.eventEmitter.fire();
}
Expand Down

0 comments on commit 9ee0584

Please sign in to comment.