Path: blob/main/extensions/copilot/src/util/common/glob.ts
13397 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import picomatch from 'picomatch';6import type vscode from 'vscode';7import * as path from '../vs/base/common/path';8import { isWindows } from '../vs/base/common/platform';9import { URI } from '../vs/base/common/uri';1011export function isMatch(uri: URI, glob: vscode.GlobPattern): boolean {12if (typeof glob === 'string') {13return picomatch.isMatch(uri.fsPath, glob, { dot: true, windows: isWindows });14} else {15if (uri.fsPath === glob.baseUri.fsPath && glob.pattern === '*') {16return true;17}1819const relativePath = path.relative(glob.baseUri.fsPath, uri.fsPath);20if (!relativePath.startsWith('..')) {21return picomatch.isMatch(relativePath, glob.pattern, { dot: true, windows: isWindows });22}2324return picomatch.isMatch(uri.fsPath, glob.pattern, { dot: true, windows: isWindows });25}26}2728export interface GlobIncludeOptions {29/**30* Globs for files to explicitly include in the search.31*32* If this is provided, only files matching these globs will be included.33*/34readonly include?: readonly vscode.GlobPattern[];3536/**37* Globs for files to exclude from the search.38*39* This takes precedence over the {@linkcode include} globs.40*/41readonly exclude?: readonly vscode.GlobPattern[];42}4344export function shouldInclude(uri: URI, options: GlobIncludeOptions | undefined): boolean {45if (!options) {46return true;47}4849if (options.exclude?.some(x => isMatch(uri, x))) {50return false;51}5253if (options.include) {54return options.include.some(x => isMatch(uri, x));55}5657return true;58}5960export function combineGlob(glob1: string | vscode.RelativePattern, glob2: string | vscode.RelativePattern): string {61let stringGlob1 = typeof glob1 === 'string' ? glob1 : glob1.baseUri.toString() + glob1.pattern;62let stringGlob2 = typeof glob2 === 'string' ? glob2 : glob2.baseUri.toString() + glob2.pattern;63// Remove any bracket expansion from the globs64stringGlob1 = stringGlob1.replace(/\{.*\}/g, '');65stringGlob2 = stringGlob2.replace(/\{.*\}/g, '');66// Combine them into one bracket expanded glob pattern67return `{${stringGlob1},${stringGlob2}}`;68}697071