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

Improve glob module to support OS separator agnostic matching #114810

Merged
merged 2 commits into from Jan 25, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 16 additions & 7 deletions src/vs/base/common/glob.ts
Expand Up @@ -393,15 +393,24 @@ function trivia3(pattern: string, options: IGlobOptions): ParsedStringPattern {
}

// common patterns: **/something/else just need endsWith check, something/else just needs and equals check
function trivia4and5(path: string, pattern: string, matchPathEnds: boolean): ParsedStringPattern {
const nativePath = paths.sep !== paths.posix.sep ? path.replace(ALL_FORWARD_SLASHES, paths.sep) : path;
function trivia4and5(targetPath: string, pattern: string, matchPathEnds: boolean): ParsedStringPattern {
const usingPosixSep = paths.sep === paths.posix.sep;
const nativePath = usingPosixSep ? targetPath : targetPath.replace(ALL_FORWARD_SLASHES, paths.sep);
const nativePathEnd = paths.sep + nativePath;
const parsedPattern: ParsedStringPattern = matchPathEnds ? function (path, basename) {
return typeof path === 'string' && (path === nativePath || path.endsWith(nativePathEnd)) ? pattern : null;
} : function (path, basename) {
return typeof path === 'string' && path === nativePath ? pattern : null;
const targetPathEnd = paths.posix.sep + targetPath;

const parsedPattern: ParsedStringPattern = matchPathEnds ? function (testPath, basename) {
return typeof testPath === 'string' &&
((testPath === nativePath || testPath.endsWith(nativePathEnd))
|| !usingPosixSep && (testPath === targetPath || testPath.endsWith(targetPathEnd)))
? pattern : null;
} : function (testPath, basename) {
return typeof testPath === 'string' &&
(testPath === nativePath
|| (!usingPosixSep && testPath === targetPath))
? pattern : null;
};
parsedPattern.allPaths = [(matchPathEnds ? '*/' : './') + path];
parsedPattern.allPaths = [(matchPathEnds ? '*/' : './') + targetPath];
return parsedPattern;
}

Expand Down
10 changes: 8 additions & 2 deletions src/vs/base/test/common/glob.test.ts
Expand Up @@ -63,10 +63,12 @@ suite('Glob', () => {

function assertGlobMatch(pattern: string | glob.IRelativePattern, input: string) {
assert(glob.match(pattern, input), `${pattern} should match ${input}`);
assert(glob.match(pattern, nativeSep(input)), `${pattern} should match ${nativeSep(input)}`);
}

function assertNoGlobMatch(pattern: string | glob.IRelativePattern, input: string) {
assert(!glob.match(pattern, input), `${pattern} should not match ${input}`);
assert(!glob.match(pattern, nativeSep(input)), `${pattern} should not match ${nativeSep(input)}`);
}

test('simple', () => {
Expand Down Expand Up @@ -538,9 +540,13 @@ suite('Glob', () => {
});

test('full path', function () {
let p = 'testing/this/foo.txt';
assertGlobMatch('testing/this/foo.txt', 'testing/this/foo.txt');
// assertGlobMatch('testing/this/foo.txt', 'testing\\this\\foo.txt');
});

assert(glob.match(p, nativeSep('testing/this/foo.txt')));
test('ending path', function () {
assertGlobMatch('**/testing/this/foo.txt', 'some/path/testing/this/foo.txt');
// assertGlobMatch('**/testing/this/foo.txt', 'some\\path\\testing\\this\\foo.txt');
});

test('prefix agnostic', function () {
Expand Down
5 changes: 0 additions & 5 deletions src/vs/workbench/services/search/common/search.ts
Expand Up @@ -597,7 +597,6 @@ export function resolvePatternsForProvider(globalPattern: glob.IExpression | und
});
}

const ALL_FORWARD_SLASHES = /\//g;
export class QueryGlobTester {

private _excludeExpression: glob.IExpression;
Expand Down Expand Up @@ -634,8 +633,6 @@ export class QueryGlobTester {
* Guaranteed sync - siblingsFn should not return a promise.
*/
includedInQuerySync(testPath: string, basename?: string, hasSibling?: (name: string) => boolean): boolean {
testPath = paths.sep !== paths.posix.sep ? testPath.replace(ALL_FORWARD_SLASHES, paths.sep) : testPath;

if (this._parsedExcludeExpression && this._parsedExcludeExpression(testPath, basename, hasSibling)) {
return false;
}
Expand All @@ -651,8 +648,6 @@ export class QueryGlobTester {
* Guaranteed async.
*/
includedInQuery(testPath: string, basename?: string, hasSibling?: (name: string) => boolean | Promise<boolean>): Promise<boolean> {
testPath = paths.sep !== paths.posix.sep ? testPath.replace(ALL_FORWARD_SLASHES, paths.sep) : testPath;

const excludeP = Promise.resolve(this._parsedExcludeExpression(testPath, basename, hasSibling)).then(result => !!result);

return excludeP.then(excluded => {
Expand Down