src/vs/workbench/services/search/node/ripgrepFileSearch.ts
184 LOC · 44 covered · 140 uncovered · 11 ranges · 62 concepts · 2 introducers · 33 tests
File neighbourhood
The centred file is linked to every concept that introduces one of its ranges, every test that runs code from the file, and the gray connector concepts standing between those tests and the file's own introducer concepts. Undirected links join concepts to every file where they introduce source and concepts to the tests they introduce; arrows show specialization between the displayed concepts and bridge only concepts omitted from this view. Concept colors match the source ranges below; connector concepts have no source color and are shown in gray.
Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the file
In the embedded map, ordinary wheel input scrolls the page; use the visible controls to zoom and drag to pan. Open the full-screen map for canvas navigation: wheel pans, Ctrl/Command plus wheel zooms, and arrow keys pan when this region is focused. On touch screens, open the full-screen map to pan or pinch. If JavaScript or WebGL is unavailable, use the related-file, concept, and source links on this page.
Graph controls are ready.
Interactive rendering requires JavaScript and WebGL. Use the related-file, concept, and source links on this page while the interactive map is unavailable.
/*---------------------------------------------------------------------------------------------
ripgrepFileSearch.ts ×8
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as cp from 'child_process';
import * as path from '../../../../base/common/path.js';
import * as glob from '../../../../base/common/glob.js';
import { normalizeNFD } from '../../../../base/common/normalization.js';
import * as extpath from '../../../../base/common/extpath.js';
import { isMacintosh as isMac } from '../../../../base/common/platform.js';
import * as strings from '../../../../base/common/strings.js';
import { IFileQuery, IFolderQuery } from '../common/search.js';
import { anchorGlob } from './ripgrepSearchUtils.js';
import { rgDiskPath } from '../../../../base/node/ripgrep.js';
export async function spawnRipgrepCmd(config: IFileQuery, folderQuery: IFolderQuery, includePattern?: glob.IExpression, excludePattern?: glob.IExpression, numThreads?: number) {
const rgArgs = getRgArgs(config, folderQuery, includePattern, excludePattern, numThreads);
const cwd = folderQuery.folder.fsPath;
const resolvedRgDiskPath = await rgDiskPath();
return {
cmd: cp.spawn(resolvedRgDiskPath, rgArgs.args, { cwd }),
rgDiskPath: resolvedRgDiskPath,
siblingClauses: rgArgs.siblingClauses,
rgArgs,
cwd
};
}
function getRgArgs(config: IFileQuery, folderQuery: IFolderQuery, includePattern?: glob.IExpression, excludePattern?: glob.IExpression, numThreads?: number) {
const args = ['--files', '--hidden', '--case-sensitive', '--no-require-git'];
if (config.ignoreGlobCase || folderQuery.ignoreGlobCase) {
args.push('--glob-case-insensitive');
args.push('--ignore-file-case-insensitive');
}
// includePattern can't have siblingClauses
foldersToIncludeGlobs([folderQuery], includePattern, false).forEach(globArg => {
const inclusion = anchorGlob(globArg);
args.push('-g', inclusion);
if (isMac) {
const normalized = normalizeNFD(inclusion);
if (normalized !== inclusion) {
args.push('-g', normalized);
}
}
});
const rgGlobs = foldersToRgExcludeGlobs([folderQuery], excludePattern, undefined, false);
rgGlobs.globArgs.forEach(globArg => {
const exclusion = `!${anchorGlob(globArg)}`;
args.push('-g', exclusion);
if (isMac) {
const normalized = normalizeNFD(exclusion);
if (normalized !== exclusion) {
args.push('-g', normalized);
}
}
});
if (folderQuery.disregardIgnoreFiles !== false) {
// Don't use .gitignore or .ignore
args.push('--no-ignore');
} else if (folderQuery.disregardParentIgnoreFiles !== false) {
args.push('--no-ignore-parent');
}
// Follow symlinks
if (!folderQuery.ignoreSymlinks) {
args.push('--follow');
}
if (config.exists) {
args.push('--quiet');
}
if (numThreads) {
args.push('--threads', `${numThreads}`);
}
args.push('--no-config');
if (folderQuery.disregardGlobalIgnoreFiles) {
args.push('--no-ignore-global');
}
return {
args,
siblingClauses: rgGlobs.siblingClauses
};
}
interface IRgGlobResult {
globArgs: string[];
siblingClauses: glob.IExpression;
}
function foldersToRgExcludeGlobs(folderQueries: IFolderQuery[], globalExclude?: glob.IExpression, excludesToSkip?: Set<string>, absoluteGlobs = true): IRgGlobResult {
const globArgs: string[] = [];
let siblingClauses: glob.IExpression = {};
folderQueries.forEach(folderQuery => {
const totalExcludePattern = Object.assign({}, folderQuery.excludePattern || {}, globalExclude || {});
const result = globExprsToRgGlobs(totalExcludePattern, absoluteGlobs ? folderQuery.folder.fsPath : undefined, excludesToSkip);
globArgs.push(...result.globArgs);
if (result.siblingClauses) {
siblingClauses = Object.assign(siblingClauses, result.siblingClauses);
}
});
return { globArgs, siblingClauses };
}
function foldersToIncludeGlobs(folderQueries: IFolderQuery[], globalInclude?: glob.IExpression, absoluteGlobs = true): string[] {
const globArgs: string[] = [];
folderQueries.forEach(folderQuery => {
const totalIncludePattern = Object.assign({}, globalInclude || {}, folderQuery.includePattern || {});
const result = globExprsToRgGlobs(totalIncludePattern, absoluteGlobs ? folderQuery.folder.fsPath : undefined);
globArgs.push(...result.globArgs);
});
return globArgs;
}
function globExprsToRgGlobs(patterns: glob.IExpression, folder?: string, excludesToSkip?: Set<string>): IRgGlobResult {
const globArgs: string[] = [];
const siblingClauses: glob.IExpression = {};
Object.keys(patterns)
.forEach(key => {
if (excludesToSkip && excludesToSkip.has(key)) {
return;
}
if (!key) {
return;
}
const value = patterns[key];
key = trimTrailingSlash(folder ? getAbsoluteGlob(folder, key) : key);
// glob.ts requires forward slashes, but a UNC path still must start with \\
// #38165 and #38151
if (key.startsWith('\\\\')) {
key = '\\\\' + key.substr(2).replace(/\\/g, '/');
} else {
key = key.replace(/\\/g, '/');
}
if (typeof value === 'boolean' && value) {
if (key.startsWith('\\\\')) {
// Absolute globs UNC paths don't work properly, see #58758
key += '**';
}
globArgs.push(fixDriveC(key));
} else if (value && value.when) {
siblingClauses[key] = value;
}
});
return { globArgs, siblingClauses };
}
/**
* Resolves a glob like "node_modules/**" in "/foo/bar" to "/foo/bar/node_modules/**".
* Special cases C:/foo paths to write the glob like /foo instead - see https://github.com/BurntSushi/ripgrep/issues/530.
*
* Exported for testing
*/
export function getAbsoluteGlob(folder: string, key: string): string {
key :
path.join(folder, key);
}
function trimTrailingSlash(str: string): string {
str = strings.rtrim(str, '\\');
return strings.rtrim(str, '/');
}
export function fixDriveC(path: string): string {
return root.toLowerCase() === 'c:/' ?
path.replace(/^c:[/\\]/i, '/') :
}