Skip to content

Commit

Permalink
Implement search include/exclude precedence per #27226
Browse files Browse the repository at this point in the history
  • Loading branch information
roblourens committed Jul 25, 2017
1 parent 5159c7d commit 9bbd449
Showing 1 changed file with 32 additions and 24 deletions.
56 changes: 32 additions & 24 deletions src/vs/workbench/services/search/node/ripgrepTextSearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { StringDecoder, NodeStringDecoder } from 'string_decoder';
import * as cp from 'child_process';
import { rgPath } from 'vscode-ripgrep';

import arrays = require('vs/base/common/arrays');
import objects = require('vs/base/common/objects');
import platform = require('vs/base/common/platform');
import * as strings from 'vs/base/common/strings';
Expand Down Expand Up @@ -382,15 +383,27 @@ export class LineMatch implements ILineMatch {

interface IRgGlobResult {
globArgs: string[];
siblingClauses: glob.IExpression;
siblingClauses?: glob.IExpression;
}

function foldersToRgExcludeGlobs(folderQueries: IFolderSearch[], globalExclude: glob.IExpression): IRgGlobResult {
function foldersToRgExcludeGlobs(folderQueries: IFolderSearch[]): IRgGlobResult {
return foldersToRgGlobs(folderQueries, fq => fq.excludePattern);
}

function foldersToRgIncludeGlobs(folderQueries: IFolderSearch[], globalInclude: glob.IExpression): IRgGlobResult {
return foldersToRgGlobs(folderQueries, fq => objects.assign({}, fq.includePattern || {}, globalInclude || {}));
}

function foldersToRgGlobalExcludeGlobs(folderQueries: IFolderSearch[], globalExclude: glob.IExpression): IRgGlobResult {
return foldersToRgGlobs(folderQueries, () => globalExclude);
}

function foldersToRgGlobs(folderQueries: IFolderSearch[], patternProvider: (fs: IFolderSearch) => glob.IExpression): IRgGlobResult {
const globArgs: string[] = [];
let siblingClauses: glob.IExpression = {};
folderQueries.forEach(folderQuery => {
const totalExcludePattern = objects.assign({}, globalExclude || {}, folderQuery.excludePattern || {});
const result = globExprsToRgGlobs(totalExcludePattern, folderQuery.folder);
const pattern = patternProvider(folderQuery);
const result = globExprsToRgGlobs(pattern, folderQuery.folder);
globArgs.push(...result.globArgs);
if (result.siblingClauses) {
siblingClauses = objects.assign(siblingClauses, result.siblingClauses);
Expand All @@ -400,17 +413,6 @@ function foldersToRgExcludeGlobs(folderQueries: IFolderSearch[], globalExclude:
return { globArgs, siblingClauses };
}

function foldersToIncludeGlobs(folderQueries: IFolderSearch[], globalInclude: glob.IExpression): string[] {
const globArgs = [];
folderQueries.forEach(folderQuery => {
const totalIncludePattern = objects.assign({}, globalInclude || {}, folderQuery.includePattern || {});
const result = globExprsToRgGlobs(totalIncludePattern, folderQuery.folder);
globArgs.push(...result.globArgs);
});

return globArgs;
}

function globExprsToRgGlobs(patterns: glob.IExpression, folder: string): IRgGlobResult {
const globArgs: string[] = [];
let siblingClauses: glob.IExpression = null;
Expand Down Expand Up @@ -454,16 +456,22 @@ function getRgArgs(config: IRawSearch): IRgGlobResult {
const args = ['--hidden', '--heading', '--line-number', '--color', 'ansi', '--colors', 'path:none', '--colors', 'line:none', '--colors', 'match:fg:red', '--colors', 'match:style:nobold'];
args.push(config.contentPattern.isCaseSensitive ? '--case-sensitive' : '--ignore-case');

// includePattern can't have siblingClauses
foldersToIncludeGlobs(config.folderQueries, config.includePattern).forEach(globArg => {
args.push('-g', globArg);
});
const globsToGlobArgs = (globArgs: string[]) => arrays.flatten(globArgs.map(arg => ['-g', arg]));
const globToNotGlob = (glob: string) => '!' + glob;

let siblingClauses: glob.IExpression;
const rgGlobs = foldersToRgExcludeGlobs(config.folderQueries, config.excludePattern);
rgGlobs.globArgs
.forEach(rgGlob => args.push('-g', `!${rgGlob}`));
siblingClauses = rgGlobs.siblingClauses;
// Include/exclude precedence:
// settings exclude < global include < global exclude
const excludeResult = foldersToRgExcludeGlobs(config.folderQueries);
args.push(...globsToGlobArgs(excludeResult.globArgs.map(globToNotGlob)));

const includeResult = foldersToRgIncludeGlobs(config.folderQueries, config.includePattern);
args.push(...globsToGlobArgs(includeResult.globArgs));

const globalExcludeResult = foldersToRgGlobalExcludeGlobs(config.folderQueries, config.excludePattern);
args.push(...globsToGlobArgs(globalExcludeResult.globArgs.map(globToNotGlob)));

// includePattern can't have siblingClauses
const siblingClauses = excludeResult.siblingClauses;

if (config.maxFilesize) {
args.push('--max-filesize', config.maxFilesize + '');
Expand Down

0 comments on commit 9bbd449

Please sign in to comment.