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

Escape special glob chars when using "Find in Folder" #166318

Merged
merged 9 commits into from
Jan 31, 2023
14 changes: 13 additions & 1 deletion src/vs/workbench/services/search/common/queryBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,18 @@ function normalizeGlobPattern(pattern: string): string {
.replace(/\/+$/g, '');
}

/**
* Escapes a path for use as a glob pattern that would match the input precisely.
* Characters '?', '*', '[', and ']' are escaped into character range glob syntax
* (for example, '?' becomes '[?]').
* NOTE: This implementation makes no special cases for UNC paths. For example,
* given the input "//?/C:/A?.txt", this would produce output '//[?]/C:/A[?].txt',
* which may not be desirable in some cases. Use with caution if UNC paths could be expected.
*/
function escapeGlobPattern(path: string): string {
return path.replace(/([?*[\]])/g, '[$1]');
}

/**
* Construct an include pattern from a list of folders uris to search in.
*/
Expand Down Expand Up @@ -616,7 +628,7 @@ export function resolveResourcesForSearchIncludes(resources: URI[], contextServi
}

if (folderPath) {
folderPaths.push(folderPath);
folderPaths.push(escapeGlobPattern(folderPath));
}
});
}
Expand Down
30 changes: 30 additions & 0 deletions src/vs/workbench/services/search/test/common/queryBuilder.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as assert from 'assert';
import { isWindows } from 'vs/base/common/platform';
import { URI } from 'vs/base/common/uri';
import { IWorkspaceContextService } from 'vs/platform/workspace/common/workspace';
import { testWorkspace } from 'vs/platform/workspace/test/common/testWorkspace';
import { resolveResourcesForSearchIncludes } from 'vs/workbench/services/search/common/queryBuilder';
import { TestContextService } from 'vs/workbench/test/common/workbenchTestServices';

suite('QueryBuilderCommon', () => {
let context: IWorkspaceContextService;

setup(() => {
const workspace = testWorkspace(URI.file(isWindows ? 'C:\\testWorkspace' : '/testWorkspace'));
context = new TestContextService(workspace);
});

test('resolveResourcesForSearchIncludes passes through paths without special glob characters', () => {
const actual = resolveResourcesForSearchIncludes([URI.file(isWindows ? "C:\\testWorkspace\\pages\\blog" : "/testWorkspace/pages/blog")], context);
assert.deepStrictEqual(actual, ["./pages/blog"]);
});

test('resolveResourcesForSearchIncludes escapes paths with special characters', () => {
const actual = resolveResourcesForSearchIncludes([URI.file(isWindows ? "C:\\testWorkspace\\pages\\blog\\[postId]" : "/testWorkspace/pages/blog/[postId]")], context);
assert.deepStrictEqual(actual, ["./pages/blog/[[]postId[]]"]);
});
});