src/vs/platform/agentHost/node/agentHostFileMonitorService.ts
185 LOC · 178 covered · 7 uncovered · 49 ranges · 993 concepts · 11 introducers · 501 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.
/*---------------------------------------------------------------------------------------------
agentHostFileMonitorService.ts ×14
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { disposableTimeout } from '../../../base/common/async.js';
import { IExpression, ParsedExpression, parse } from '../../../base/common/glob.js';
import { Disposable, DisposableMap, DisposableStore, IDisposable, MutableDisposable, toDisposable } from '../../../base/common/lifecycle.js';
import { extUriBiasedIgnorePathCase } from '../../../base/common/resources.js';
import { URI } from '../../../base/common/uri.js';
import { FileChangesEvent, IFileService } from '../../files/common/files.js';
import { createDecorator } from '../../instantiation/common/instantiation.js';
import { ILogService } from '../../log/common/log.js';
export const IAgentHostFileMonitorService = createDecorator<IAgentHostFileMonitorService>('agentHostFileMonitorService');
export const DEFAULT_AGENT_HOST_WATCH_EXCLUDES: readonly string[] = Object.freeze([
'**/.git',
'**/.git/lfs/**',
'**/.git/logs/**',
'**/.git/objects/**',
'**/.git/subtree-cache/**',
'**/.git/**/*.lock',
'**/.git/**/FETCH_HEAD',
'**/.git/**/fsmonitor--daemon/**',
'**/*.watchman-cookie-*',
]);
export interface IAgentHostFileMonitorOptions {
readonly excludes?: readonly string[];
readonly debounceMs?: number;
}
export interface IAgentHostFileMonitorService extends IDisposable {
readonly _serviceBrand: undefined;
acquire(folder: URI, callback: () => void, options?: IAgentHostFileMonitorOptions): IDisposable | undefined;
}
interface IMonitorEntry extends IDisposable {
readonly folder: URI;
readonly callbacks: Set<() => void>;
readonly debounce: MutableDisposable<IDisposable>;
readonly debounceMs: number;
readonly excludeMatcher: ParsedExpression;
}
function normalizeExcludes(excludes: readonly string[]): readonly string[] {
agentHostFileMonitorService.ts ×8
return [...excludes].sort();
}
function parseExcludes(excludes: readonly string[]): ParsedExpression {
agentHostFileMonitorService.ts ×8
const expression: IExpression = Object.create(null);
for (const exclude of excludes) {
expression[exclude] = true;
}
return parse(expression);
}
export class AgentHostFileMonitorService extends Disposable implements IAgentHostFileMonitorService {
declare readonly _serviceBrand: undefined;
private static readonly _DEFAULT_DEBOUNCE_MS = 750;
private readonly _entries = this._register(new DisposableMap<string, IMonitorEntry>());
constructor(
@ILogService private readonly _logService: ILogService,
) {
super();
this._register(this._fileService.onDidFilesChange(event => this._onDidFilesChange(event)));
this._register(this._fileService.onDidWatchError(error => {
this._logService.warn('[AgentHostFileMonitorService] File watcher error', error);
}
acquire(folder: URI, callback: () => void, options: IAgentHostFileMonitorOptions = {}): IDisposable | undefined {
const excludes = normalizeExcludes(options.excludes ?? DEFAULT_AGENT_HOST_WATCH_EXCLUDES);
const debounceMs = options.debounceMs ?? AgentHostFileMonitorService._DEFAULT_DEBOUNCE_MS;
const key = this._key(canonicalFolder, excludes, debounceMs);
let entry = this._entries.get(key);
if (!entry) {
try {
entry = this._createEntry(key, canonicalFolder, excludes, debounceMs);
} catch (err) {
this._logService.warn(`[AgentHostFileMonitorService] Failed to watch ${canonicalFolder.toString()}`, err);
agentHostFileMonitorService.ts ×2
return undefined;
}
}
entry.callbacks.add(callback);
return toDisposable(() => {
const current = this._entries.get(key);
if (!current) {
}
if (current.callbacks.size === 0) {
this._entries.deleteAndDispose(key);
}
private _createEntry(_key: string, folder: URI, excludes: readonly string[], debounceMs: number): IMonitorEntry {
try {
const debounce = disposable.add(new MutableDisposable<IDisposable>());
const callbacks = new Set<() => void>();
const excludeMatcher = parseExcludes(excludes);
disposable.add(this._fileService.watch(folder, { recursive: true, excludes: [...excludes] }));
return { folder, callbacks, debounce, debounceMs, excludeMatcher, dispose: () => disposable.dispose() };
} catch (err) {
throw err;
}
private _onDidFilesChange(event: FileChangesEvent): void {
}
private _onDidFilesChangeEntry(key: string, event: FileChangesEvent): void {
if (!entry || entry.callbacks.size === 0) {
return;
}
if (!event.affects(entry.folder) || !this._hasRelevantRawChange(entry, event)) {
agentHostFileMonitorService.ts ×12
}
for (const callback of [...entry.callbacks]) {
try {
callback();
} catch (err) {
this._logService.warn('[AgentHostFileMonitorService] Folder change callback failed', err);
}
}
private _hasRelevantRawChange(entry: IMonitorEntry, event: FileChangesEvent): boolean {
return this._hasRelevantRawResources(entry, event.rawAdded)
agentHostFileMonitorService.ts ×12
|| this._hasRelevantRawResources(entry, event.rawUpdated)
private _hasRelevantRawResources(entry: IMonitorEntry, resources: readonly URI[]): boolean {
if (!extUriBiasedIgnorePathCase.isEqualOrParent(resource, entry.folder)) {
continue;
}
return true;
}
}
return false;
}
private _isExcluded(entry: IMonitorEntry, resource: URI): boolean {
const basename = extUriBiasedIgnorePathCase.basename(resource);
agentHostFileMonitorService.ts ×12
const relativePath = extUriBiasedIgnorePathCase.relativePath(entry.folder, resource);
if (relativePath !== undefined && this._matchesExclude(entry, relativePath, basename)) {
}
return this._matchesExclude(entry, resource.path, basename);
agentHostFileMonitorService.ts ×12
}
private _matchesExclude(entry: IMonitorEntry, path: string, basename: string): boolean {
return typeof entry.excludeMatcher(path, basename) === 'string';
agentHostFileMonitorService.ts ×12
}
private _canonicalizeFolder(folder: URI): URI {
return extUriBiasedIgnorePathCase.removeTrailingPathSeparator(extUriBiasedIgnorePathCase.normalizePath(folder));
agentHostFileMonitorService.ts ×8
}
private _key(folder: URI, excludes: readonly string[], debounceMs: number): string {
return `${extUriBiasedIgnorePathCase.getComparisonKey(folder)}\u0000${debounceMs}\u0000${excludes.join('\n')}`;
agentHostFileMonitorService.ts ×8
}