Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/util/common/glob.ts
13397 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import picomatch from 'picomatch';
7
import type vscode from 'vscode';
8
import * as path from '../vs/base/common/path';
9
import { isWindows } from '../vs/base/common/platform';
10
import { URI } from '../vs/base/common/uri';
11
12
export function isMatch(uri: URI, glob: vscode.GlobPattern): boolean {
13
if (typeof glob === 'string') {
14
return picomatch.isMatch(uri.fsPath, glob, { dot: true, windows: isWindows });
15
} else {
16
if (uri.fsPath === glob.baseUri.fsPath && glob.pattern === '*') {
17
return true;
18
}
19
20
const relativePath = path.relative(glob.baseUri.fsPath, uri.fsPath);
21
if (!relativePath.startsWith('..')) {
22
return picomatch.isMatch(relativePath, glob.pattern, { dot: true, windows: isWindows });
23
}
24
25
return picomatch.isMatch(uri.fsPath, glob.pattern, { dot: true, windows: isWindows });
26
}
27
}
28
29
export interface GlobIncludeOptions {
30
/**
31
* Globs for files to explicitly include in the search.
32
*
33
* If this is provided, only files matching these globs will be included.
34
*/
35
readonly include?: readonly vscode.GlobPattern[];
36
37
/**
38
* Globs for files to exclude from the search.
39
*
40
* This takes precedence over the {@linkcode include} globs.
41
*/
42
readonly exclude?: readonly vscode.GlobPattern[];
43
}
44
45
export function shouldInclude(uri: URI, options: GlobIncludeOptions | undefined): boolean {
46
if (!options) {
47
return true;
48
}
49
50
if (options.exclude?.some(x => isMatch(uri, x))) {
51
return false;
52
}
53
54
if (options.include) {
55
return options.include.some(x => isMatch(uri, x));
56
}
57
58
return true;
59
}
60
61
export function combineGlob(glob1: string | vscode.RelativePattern, glob2: string | vscode.RelativePattern): string {
62
let stringGlob1 = typeof glob1 === 'string' ? glob1 : glob1.baseUri.toString() + glob1.pattern;
63
let stringGlob2 = typeof glob2 === 'string' ? glob2 : glob2.baseUri.toString() + glob2.pattern;
64
// Remove any bracket expansion from the globs
65
stringGlob1 = stringGlob1.replace(/\{.*\}/g, '');
66
stringGlob2 = stringGlob2.replace(/\{.*\}/g, '');
67
// Combine them into one bracket expanded glob pattern
68
return `{${stringGlob1},${stringGlob2}}`;
69
}
70
71