Skip to content

Commit

Permalink
Merge pull request microsoft#314 from Tyriar/278_blank_line_regex
Browse files Browse the repository at this point in the history
Enable regex's ^, $ and ^$ to be matched in file dialog
  • Loading branch information
alexdima committed Nov 23, 2015
2 parents 2543816 + c10aa8e commit cef9583
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 5 deletions.
9 changes: 7 additions & 2 deletions src/vs/base/common/strings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,9 +262,14 @@ export function createRegExp(searchString: string, isRegex: boolean, matchCase:
}

export function regExpLeadsToEndlessLoop(regexp: RegExp): boolean {
// Exit early if it's one of these special cases which are meant to match
// against an empty string
if (regexp.source === "^" || regexp.source === "^$" || regexp.source === "$") {
return false;
}

// We check against an empty string. If the regular expression doesn't advance
// (e.g. ends in an endless loop) it will match an empty string.

var match = regexp.exec('');
return (match && <any>regexp.lastIndex === 0);
}
Expand Down Expand Up @@ -599,4 +604,4 @@ export var UTF8_BOM_CHARACTER = String.fromCharCode(__utf8_bom);

export function startsWithUTF8BOM(str: string): boolean {
return (str && str.length > 0 && str.charCodeAt(0) === __utf8_bom);
}
}
11 changes: 9 additions & 2 deletions src/vs/editor/common/model/textModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -979,10 +979,17 @@ export class TextModel extends OrderGuaranteeEventEmitter implements EditorCommo

private _findMatchesInLine(searchRegex:RegExp, text:string, lineNumber:number, deltaOffset:number, counter:number, result:EditorCommon.IEditorRange[], limitResultCount:number): number {
var m:RegExpExecArray;
// Reset regex to search from the beginning
searchRegex.lastIndex = 0;
do {
m = searchRegex.exec(text);
if (m) {
result.push(new Range(lineNumber, m.index + 1 + deltaOffset, lineNumber, m.index + 1 + m[0].length + deltaOffset));
var range = new Range(lineNumber, m.index + 1 + deltaOffset, lineNumber, m.index + 1 + m[0].length + deltaOffset);
// Exit early if the regex matches the same range
if (range.equalsRange(result[result.length - 1])) {
return counter;
}
result.push(range);
counter++;
if (counter >= limitResultCount) {
return counter;
Expand All @@ -991,4 +998,4 @@ export class TextModel extends OrderGuaranteeEventEmitter implements EditorCommo
} while(m);
return counter;
}
}
}
52 changes: 51 additions & 1 deletion src/vs/editor/test/common/model/model.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -603,5 +603,55 @@ suite('Editor Model - Find', () => {
rangeEqual(matches[i], ranges[i][0], ranges[i][1], ranges[i][2], ranges[i][3]);
}
});
});

test('/^/ find', () => {
var ranges = [
[1, 1, 1, 1],
[2, 1, 2, 1],
[3, 1, 3, 1],
[4, 1, 4, 1],
[5, 1, 5, 1]
];
var matches = thisModel.findMatches('^', false, true, false, false);
assert.equal(matches.length, ranges.length);
for (var i = 0; i < matches.length; i++) {
rangeEqual(matches[i], ranges[i][0], ranges[i][1], ranges[i][2], ranges[i][3]);
}
});

test('/$/ find', () => {
var ranges = [
[1, 74, 1, 74],
[2, 69, 2, 69],
[3, 54, 3, 54],
[4, 65, 4, 65],
[5, 31, 5, 31]
];
var matches = thisModel.findMatches('$', false, true, false, false);
assert.equal(matches.length, ranges.length);
for (var i = 0; i < matches.length; i++) {
rangeEqual(matches[i], ranges[i][0], ranges[i][1], ranges[i][2], ranges[i][3]);
}
});

test('/^$/ find', () => {
var text = [
'This is some foo - bar text which contains foo and bar - as in Barcelona.',
'',
'And here\'s a dull line with nothing interesting in it',
'',
'Again nothing interesting here'
];
thisModel = new Model.Model(text.join('\n'), null);

var ranges = [
[2, 1, 2, 1],
[4, 1, 4, 1]
];
var matches = thisModel.findMatches('^$', false, true, false, false);
assert.equal(matches.length, ranges.length);
for (var i = 0; i < matches.length; i++) {
rangeEqual(matches[i], ranges[i][0], ranges[i][1], ranges[i][2], ranges[i][3]);
}
});
});

0 comments on commit cef9583

Please sign in to comment.