提交 fd875356 编写于 作者: R Rob Lourens

Fix #62409 - resolve exclude patterns for searchPaths correctly

上级 07340673
...@@ -109,14 +109,9 @@ export class QueryBuilder { ...@@ -109,14 +109,9 @@ export class QueryBuilder {
let excludePattern = this.parseExcludePattern(options.excludePattern || ''); let excludePattern = this.parseExcludePattern(options.excludePattern || '');
// Build folderQueries from searchPaths, if given, otherwise folderResources // Build folderQueries from searchPaths, if given, otherwise folderResources
let folderQueries = folderResources && folderResources.map(uri => this.getFolderQueryForRoot(uri, options)); const folderQueries = searchPaths && searchPaths.length ?
if (searchPaths && searchPaths.length) { searchPaths.map(searchPath => this.getFolderQueryForSearchPath(searchPath, options)) :
const allRootExcludes = folderQueries && this.mergeExcludesFromFolderQueries(folderQueries); folderResources && folderResources.map(uri => this.getFolderQueryForRoot(uri, options));
folderQueries = searchPaths.map(searchPath => this.getFolderQueryForSearchPath(searchPath)); // TODO Rob
if (allRootExcludes) {
excludePattern = objects.mixin(excludePattern || Object.create(null), allRootExcludes);
}
}
const useRipgrep = !folderResources || folderResources.every(folder => { const useRipgrep = !folderResources || folderResources.every(folder => {
const folderConfig = this.configurationService.getValue<ISearchConfiguration>({ resource: folder }); const folderConfig = this.configurationService.getValue<ISearchConfiguration>({ resource: folder });
...@@ -238,31 +233,6 @@ export class QueryBuilder { ...@@ -238,31 +233,6 @@ export class QueryBuilder {
return Object.keys(excludeExpression).length ? excludeExpression : undefined; return Object.keys(excludeExpression).length ? excludeExpression : undefined;
} }
private mergeExcludesFromFolderQueries(folderQueries: IFolderQuery[]): glob.IExpression | undefined {
const mergedExcludes = folderQueries.reduce((merged: glob.IExpression, fq: IFolderQuery) => {
if (fq.excludePattern) {
objects.mixin(merged, this.getAbsoluteIExpression(fq.excludePattern, fq.folder.fsPath));
}
return merged;
}, Object.create(null));
// Don't return an empty IExpression
return Object.keys(mergedExcludes).length ? mergedExcludes : undefined;
}
private getAbsoluteIExpression(expr: glob.IExpression, root: string): glob.IExpression {
return Object.keys(expr)
.reduce((absExpr: glob.IExpression, key: string) => {
if (expr[key] && !paths.isAbsolute(key)) {
const absPattern = paths.join(root, key);
absExpr[absPattern] = expr[key];
}
return absExpr;
}, Object.create(null));
}
private getExcludesForFolder(folderConfig: ISearchConfiguration, options: ICommonQueryBuilderOptions): glob.IExpression | undefined { private getExcludesForFolder(folderConfig: ISearchConfiguration, options: ICommonQueryBuilderOptions): glob.IExpression | undefined {
return options.disregardExcludeSettings ? return options.disregardExcludeSettings ?
undefined : undefined :
...@@ -331,13 +301,36 @@ export class QueryBuilder { ...@@ -331,13 +301,36 @@ export class QueryBuilder {
return []; return [];
} }
private getFolderQueryForSearchPath(searchPath: ISearchPathPattern): IFolderQuery { private getFolderQueryForSearchPath(searchPath: ISearchPathPattern, options: ICommonQueryBuilderOptions): IFolderQuery {
const folder = searchPath.searchPath; const searchPathWorkspaceFolder = this.workspaceContextService.getWorkspaceFolder(searchPath.searchPath);
const folderConfig = this.configurationService.getValue<ISearchConfiguration>({ resource: folder }); const searchPathRelativePath = searchPathWorkspaceFolder && searchPath.searchPath.path.substr(searchPathWorkspaceFolder.uri.path.length + 1);
return <IFolderQuery>{
folder, const rootConfig = this.getFolderQueryForRoot(searchPath.searchPath, options);
includePattern: searchPath.pattern && patternListToIExpression([searchPath.pattern]), let resolvedExcludes: glob.IExpression = {};
fileEncoding: folderConfig.files && folderConfig.files.encoding if (searchPathWorkspaceFolder && rootConfig.excludePattern) {
// Resolve excludes relative to the search path
for (let excludePattern in rootConfig.excludePattern) {
const { pathPortion, globPortion } = splitSimpleGlob(excludePattern);
if (!pathPortion) { // **/foo
resolvedExcludes[globPortion] = rootConfig.excludePattern[excludePattern];
} else if (strings.startsWith(pathPortion, searchPathRelativePath)) { // searchPathRelativePath/something/**/foo
// Strip `searchPathRelativePath/`
const resolvedPathPortion = pathPortion.substr(searchPathRelativePath.length + 1);
const resolvedPattern = globPortion ?
resolvedPathPortion + globPortion :
resolvedPathPortion;
resolvedExcludes[resolvedPattern] = rootConfig.excludePattern[excludePattern];
}
}
}
return {
...rootConfig,
...{
includePattern: searchPath.pattern && patternListToIExpression([searchPath.pattern]),
excludePattern: Object.keys(resolvedExcludes).length ? resolvedExcludes : undefined
}
}; };
} }
...@@ -362,7 +355,7 @@ function splitGlobFromPath(searchPath: string): { pathPortion: string, globPorti ...@@ -362,7 +355,7 @@ function splitGlobFromPath(searchPath: string): { pathPortion: string, globPorti
if (lastSlashMatch) { if (lastSlashMatch) {
let pathPortion = searchPath.substr(0, lastSlashMatch.index); let pathPortion = searchPath.substr(0, lastSlashMatch.index);
if (!pathPortion.match(/[/\\]/)) { if (!pathPortion.match(/[/\\]/)) {
// If the last slash was the only slash, then we now have '' or 'C:'. Append a slash. // If the last slash was the only slash, then we now have '' or 'C:' or '.'. Append a slash.
pathPortion += '/'; pathPortion += '/';
} }
...@@ -379,6 +372,22 @@ function splitGlobFromPath(searchPath: string): { pathPortion: string, globPorti ...@@ -379,6 +372,22 @@ function splitGlobFromPath(searchPath: string): { pathPortion: string, globPorti
}; };
} }
function splitSimpleGlob(searchPath: string): { pathPortion: string, globPortion?: string } {
const globCharMatch = searchPath.match(/[\*\{\}\(\)\[\]\?]/);
if (globCharMatch) {
const globCharIdx = globCharMatch.index;
return {
pathPortion: searchPath.substr(0, globCharIdx),
globPortion: searchPath.substr(globCharIdx)
};
}
// No glob char
return {
pathPortion: searchPath
};
}
function patternListToIExpression(patterns: string[]): glob.IExpression { function patternListToIExpression(patterns: string[]): glob.IExpression {
return patterns.length ? return patterns.length ?
patterns.reduce((glob, cur) => { glob[cur] = true; return glob; }, Object.create(null)) : patterns.reduce((glob, cur) => { glob[cur] = true; return glob; }, Object.create(null)) :
......
...@@ -153,14 +153,11 @@ suite('QueryBuilder', () => { ...@@ -153,14 +153,11 @@ suite('QueryBuilder', () => {
<ITextQuery>{ <ITextQuery>{
contentPattern: PATTERN_INFO, contentPattern: PATTERN_INFO,
folderQueries: [{ folderQueries: [{
folder: getUri(paths.join(ROOT_1, 'foo')) folder: getUri(paths.join(ROOT_1, 'foo')),
}], excludePattern: {
excludePattern: { ['**/*.js']: true
[paths.join(ROOT_1, 'foo/**/*.js')]: true,
[paths.join(ROOT_1, 'bar/**')]: {
'when': '$(basename).ts'
} }
}, }],
type: QueryType.Text type: QueryType.Text
}); });
}); });
...@@ -212,7 +209,6 @@ suite('QueryBuilder', () => { ...@@ -212,7 +209,6 @@ suite('QueryBuilder', () => {
folderQueries: [ folderQueries: [
{ folder: getUri(paths.join(ROOT_2, 'src')) } { folder: getUri(paths.join(ROOT_2, 'src')) }
], ],
excludePattern: patternsToIExpression(paths.join(ROOT_1, 'foo/**/*.js'), paths.join(ROOT_2, 'bar')),
type: QueryType.Text type: QueryType.Text
} }
); );
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册