Skip to content

Commit

Permalink
Implement combining search paths with glob patterns - #27226
Browse files Browse the repository at this point in the history
  • Loading branch information
roblourens committed Jul 14, 2017
1 parent d2b82c6 commit 6dab0f7
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 16 deletions.
55 changes: 40 additions & 15 deletions src/vs/workbench/parts/search/common/searchQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,19 @@ export class QueryBuilder {
return folderConfig.search.useRipgrep;
});

const searchPaths = this.getSearchPaths(options).searchPaths;
const { searchPaths, additionalIncludePatterns } = this.getSearchPaths(options);
const includePattern = objects.clone(options.includePattern);
for (const additionalInclude of additionalIncludePatterns) {
includePattern[additionalInclude] = true;
}

return {
type,
folderQueries,
extraFileResources: options.extraFileResources,
filePattern: options.filePattern,
excludePattern: options.excludePattern,
includePattern: options.includePattern,
includePattern,
maxResults: options.maxResults,
sortByScore: options.sortByScore,
cacheKey: options.cacheKey,
Expand Down Expand Up @@ -84,29 +88,31 @@ export class QueryBuilder {
};
}

const searchPaths: string[] = [];
const additionalIncludePatterns: string[] = [];

const workspace = this.workspaceContextService.getWorkspace();
if (workspace.roots.length < 2) {
// 1 open folder => just resolve the search paths to absolute paths
const searchPaths = options.searchPaths.map(searchPath => {
const relativeSearchPathMatch = searchPath.match(/\.\/(.+)/);
if (relativeSearchPathMatch) {
return paths.join(workspace.roots[0].fsPath, relativeSearchPathMatch[1]);
} else {
// throw new Error(nls.localize('search.invalidRelativeInclude', 'Invalid folder include pattern: {}', searchPath));
return null;
for (const searchPath of options.searchPaths) {
// 1 open folder => just resolve the search paths to absolute paths
const { pathPortion, globPortion } = splitGlobFromPath(searchPath);
const absolutePathPortion = paths.isAbsolute(pathPortion) ?
pathPortion :
paths.join(workspace.roots[0].fsPath, pathPortion);
searchPaths.push(absolutePathPortion);

if (globPortion) {
additionalIncludePatterns.push(paths.join(absolutePathPortion, globPortion));
}
});
}

return {
searchPaths,
additionalIncludePatterns: []
additionalIncludePatterns
};
}

// Is a multiroot workspace
const searchPaths: string[] = [];
const additionalIncludePatterns: string[] = [];

// Resolve searchPaths, relative or absolute, against roots
for (const searchPath of options.searchPaths) {
if (paths.isAbsolute(searchPath)) {
Expand All @@ -131,4 +137,23 @@ export class QueryBuilder {

return { searchPaths, additionalIncludePatterns };
}
}

function splitGlobFromPath(searchPath: string): { pathPortion: string, globPortion?: string } {
const globCharMatch = searchPath.match(/[\*\{\}\(\)\[\]\?]/);
if (globCharMatch) {
const globCharIdx = globCharMatch.index;
const lastSlashMatch = searchPath.substr(0, globCharIdx).match(/[/|\\][^/\\]*$/);
if (lastSlashMatch) {
return {
pathPortion: searchPath.substr(0, lastSlashMatch.index),
globPortion: searchPath.substr(lastSlashMatch.index + 1)
};
}
}

// No glob char, or malformed
return {
pathPortion: searchPath
};
}
5 changes: 4 additions & 1 deletion src/vs/workbench/services/search/node/ripgrepTextSearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,10 @@ function globExprsToRgGlobs(patterns: glob.IExpression, folder: string): IRgGlob
* Exported for testing
*/
export function getAbsoluteGlob(folder: string, key: string): string {
const absolutePathKey = path.join(folder, key);
const absolutePathKey = paths.isAbsolute(key) ?
key :
path.join(folder, key);

const root = paths.getRoot(folder);
return root.toLowerCase() === 'c:/' ?
absolutePathKey.replace(/^c:[/\\]/i, '/') :
Expand Down

0 comments on commit 6dab0f7

Please sign in to comment.