src/vs/workbench/common/resources.ts
224 LOC · 208 covered · 16 uncovered · 20 ranges · 515 concepts · 2 introducers · 258 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.
/*---------------------------------------------------------------------------------------------
resources.ts ×7
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { URI } from '../../base/common/uri.js';
import { equals } from '../../base/common/objects.js';
import { isAbsolute } from '../../base/common/path.js';
import { Emitter } from '../../base/common/event.js';
import { relativePath } from '../../base/common/resources.js';
import { Disposable } from '../../base/common/lifecycle.js';
import { ParsedExpression, IExpression, parse } from '../../base/common/glob.js';
import { IWorkspaceContextService } from '../../platform/workspace/common/workspace.js';
import { IConfigurationService, IConfigurationChangeEvent } from '../../platform/configuration/common/configuration.js';
import { Schemas } from '../../base/common/network.js';
import { ResourceSet } from '../../base/common/map.js';
import { getDriveLetter } from '../../base/common/extpath.js';
interface IConfiguredExpression {
readonly expression: IExpression;
readonly hasAbsolutePath: boolean;
}
export class ResourceGlobMatcher extends Disposable {
private static readonly NO_FOLDER = null;
private readonly _onExpressionChange = this._register(new Emitter<void>());
readonly onExpressionChange = this._onExpressionChange.event;
private readonly mapFolderToParsedExpression = new Map<string | null, ParsedExpression>();
private readonly mapFolderToConfiguredExpression = new Map<string | null, IConfiguredExpression>();
constructor(
private shouldUpdate: (event: IConfigurationChangeEvent) => boolean,
@IWorkspaceContextService private readonly contextService: IWorkspaceContextService,
@IConfigurationService private readonly configurationService: IConfigurationService
) {
super();
this.updateExpressions(false);
this.registerListeners();
}
private registerListeners(): void {
if (this.shouldUpdate(e)) {
this.updateExpressions(true);
}
}));
this._register(this.contextService.onDidChangeWorkspaceFolders(() => this.updateExpressions(true)));
}
private updateExpressions(fromEvent: boolean): void {
// Add expressions per workspaces that got added
for (const folder of this.contextService.getWorkspace().folders) {
const folderUriStr = folder.uri.toString();
const newExpression = this.doGetExpression(folder.uri);
const currentExpression = this.mapFolderToConfiguredExpression.get(folderUriStr);
if (newExpression) {
if (!currentExpression || !equals(currentExpression.expression, newExpression.expression)) {
changed = true;
this.mapFolderToParsedExpression.set(folderUriStr, parse(newExpression.expression));
this.mapFolderToConfiguredExpression.set(folderUriStr, newExpression);
}
} else {
if (currentExpression) {
changed = true;
this.mapFolderToParsedExpression.delete(folderUriStr);
this.mapFolderToConfiguredExpression.delete(folderUriStr);
}
}
}
// Remove expressions per workspace no longer present
const foldersMap = new ResourceSet(this.contextService.getWorkspace().folders.map(folder => folder.uri));
for (const [folder] of this.mapFolderToConfiguredExpression) {
if (folder === ResourceGlobMatcher.NO_FOLDER) {
continue; // always keep this one
}
if (!foldersMap.has(URI.parse(folder))) {
this.mapFolderToParsedExpression.delete(folder);
this.mapFolderToConfiguredExpression.delete(folder);
changed = true;
}
// Always set for resources outside workspace as well
const globalNewExpression = this.doGetExpression(undefined);
const globalCurrentExpression = this.mapFolderToConfiguredExpression.get(ResourceGlobMatcher.NO_FOLDER);
if (globalNewExpression) {
if (!globalCurrentExpression || !equals(globalCurrentExpression.expression, globalNewExpression.expression)) {
changed = true;
this.mapFolderToParsedExpression.set(ResourceGlobMatcher.NO_FOLDER, parse(globalNewExpression.expression));
this.mapFolderToConfiguredExpression.set(ResourceGlobMatcher.NO_FOLDER, globalNewExpression);
}
} else {
if (globalCurrentExpression) {
changed = true;
this.mapFolderToParsedExpression.delete(ResourceGlobMatcher.NO_FOLDER);
this.mapFolderToConfiguredExpression.delete(ResourceGlobMatcher.NO_FOLDER);
}
}
if (fromEvent && changed) {
this._onExpressionChange.fire();
}
}
private doGetExpression(resource: URI | undefined): IConfiguredExpression | undefined {
if (!expression) {
return undefined;
}
const keys = Object.keys(expression);
if (keys.length === 0) {
return undefined;
}
let hasAbsolutePath = false;
// Check the expression for absolute paths/globs
// and specifically for Windows, make sure the
// drive letter is lowercased, because we later
// check with `URI.fsPath` which is always putting
// the drive letter lowercased.
const massagedExpression: IExpression = Object.create(null);
for (const key of keys) {
if (!hasAbsolutePath) {
hasAbsolutePath = isAbsolute(key);
}
let massagedKey = key;
const driveLetter = getDriveLetter(massagedKey, true /* probe for windows */);
if (driveLetter) {
const driveLetterLower = driveLetter.toLowerCase();
if (driveLetter !== driveLetter.toLowerCase()) {
massagedKey = `${driveLetterLower}${massagedKey.substring(1)}`;
}
}
massagedExpression[massagedKey] = expression[key];
}
return {
expression: massagedExpression,
hasAbsolutePath
};
}
matches(
hasSibling?: (name: string) => boolean
): boolean {
if (this.mapFolderToParsedExpression.size === 0) {
return false; // return early: no expression for this matcher
}
const folder = this.contextService.getWorkspaceFolder(resource);
let expressionForFolder: ParsedExpression | undefined;
let expressionConfigForFolder: IConfiguredExpression | undefined;
if (folder && this.mapFolderToParsedExpression.has(folder.uri.toString())) {
expressionForFolder = this.mapFolderToParsedExpression.get(folder.uri.toString());
expressionConfigForFolder = this.mapFolderToConfiguredExpression.get(folder.uri.toString());
expressionForFolder = this.mapFolderToParsedExpression.get(ResourceGlobMatcher.NO_FOLDER);
expressionConfigForFolder = this.mapFolderToConfiguredExpression.get(ResourceGlobMatcher.NO_FOLDER);
}
if (!expressionForFolder) {
return false; // return early: no expression for this resource
}
// If the resource if from a workspace, convert its absolute path to a relative
// path so that glob patterns have a higher probability to match. For example
// a glob pattern of "src/**" will not match on an absolute path "/folder/src/file.txt"
// but can match on "src/file.txt"
let resourcePathToMatch: string | undefined;
if (folder) {
resourcePathToMatch = relativePath(folder.uri, resource);
resourcePathToMatch = this.uriToPath(resource);
}
if (typeof resourcePathToMatch === 'string' && !!expressionForFolder(resourcePathToMatch, undefined, hasSibling)) {
return true;
}
// If the configured expression has an absolute path, we also check for absolute paths
// to match, otherwise we potentially miss out on matches. We only do that if we previously
// matched on the relative path.
if (resourcePathToMatch !== this.uriToPath(resource) && expressionConfigForFolder?.hasAbsolutePath) {
return !!expressionForFolder(this.uriToPath(resource), undefined, hasSibling);
}
return false;
}
private uriToPath(uri: URI): string {
return uri.fsPath;
}
return uri.path;