All files / services/core workspace-file-service.ts

100% Statements 100/100
81.25% Branches 26/32
100% Functions 24/24
100% Lines 97/97

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290                                      22x                   22x         22x         22x         22x         22x     22x   66x   22x   22x 3x 1x     2x   2x           22x 3x 1x     2x   2x                     8x             3x             3x             13x 13x   13x   13x 13x   13x     13x     13x     13x     20x   13x     13x 13x   13x 13x             2x 1x     1x 1x 1x 1x                     2x   2x 1x     1x   1x 3x   3x 2x                     2x 1x 1x   2x   2x 1x 1x 2x   1x       2x                 3x 3x 3x 3x   3x   3x 8x 1x     7x   7x 3x 3x   3x 2x 2x   4x 4x 4x       3x 1x 1x               22x 22x 22x                 16x     16x 3x       16x 2x   2x 2x   1x   4x   1x 2x             16x      
import * as vscode from 'vscode';
import { Utils } from 'vscode-uri';
import { IDocumentFactory } from '../document/document-factory.interface';
import { IWorkspaceFileService, WorkspaceFileChangeEvent } from './workspace-file-service.interface';
import { getConfig } from '@src/utilities/vscode/config';
 
/**
 * Service for discovering and watching workspace files that match supported extensions.
 * Consolidates file discovery logic to avoid duplicate workspace scans.
 */
export class WorkspaceFileService implements IWorkspaceFileService {
	/**
	 * The included file extensions as glob patterns.
	 */
	private readonly _includePatterns: string[];
 
	/**
	 * A list of all discovered files in the workspace.
	 */
	private _files: vscode.Uri[] = [];
 
	/**
	 * A file system watcher for the workspace.
	 */
	private readonly _watcher: vscode.FileSystemWatcher;
 
	/**
	 * Indicates if file discovery has completed.
	 */
	private _initialized = false;
 
	/**
	 * Event emitter for discovery completion.
	 */
	private readonly _onDidFinishDiscovery = new vscode.EventEmitter<void>();
 
	/**
	 * Event emitter for file changes.
	 */
	private readonly _onDidChangeFiles = new vscode.EventEmitter<WorkspaceFileChangeEvent>();
 
	/**
	 * An event that is fired when file discovery has completed.
	 */
	readonly onDidFinishDiscovery = this._onDidFinishDiscovery.event;
 
	/**
	 * An event that is fired when workspace file contents change.
	 */
	readonly onDidChangeFiles = this._onDidChangeFiles.event;
 
	constructor(
		private readonly documentFactory: IDocumentFactory
	) {
		this._includePatterns = Object.keys(documentFactory.supportedExtensions).map(ext => `**/*${ext}`);
 
		this._watcher = vscode.workspace.createFileSystemWatcher('**/*', false, false, false);
 
		this._watcher.onDidCreate((uri: vscode.Uri) => {
			if (!documentFactory.isSupportedFile(uri)) {
				return;
			}
 
			this._files.push(uri);
 
			this._onDidChangeFiles.fire({
				type: vscode.FileChangeType.Created,
				uri: Utils.dirname(uri)
			});
		});
 
		this._watcher.onDidDelete((uri: vscode.Uri) => {
			if (!documentFactory.isSupportedFile(uri)) {
				return;
			}
 
			this._files = this._files.filter(f => f.path !== uri.path);
 
			this._onDidChangeFiles.fire({
				type: vscode.FileChangeType.Deleted,
				uri: Utils.dirname(uri)
			});
		});
	}
 
	/**
	 * Get all discovered files in the workspace.
	 */
	get files(): ReadonlyArray<vscode.Uri> {
		return this._files;
	}
 
	/**
	 * Indicates if file discovery has completed.
	 */
	get initialized(): boolean {
		return this._initialized;
	}
 
	/**
	 * Get the include patterns for supported file extensions.
	 */
	get includePatterns(): ReadonlyArray<string> {
		return this._includePatterns;
	}
 
	/**
	 * Discovers all supported files in the workspace.
	 */
	async discoverFiles(): Promise<void> {
		vscode.commands.executeCommand('setContext', 'mentor.workspace.isInitializing', true);
		vscode.commands.executeCommand('setContext', 'mentor.workspace.isEmpty', true);
 
		this._files = [];
 
		for (const folder of vscode.workspace.workspaceFolders ?? []) {
			const workspaceUri = folder.uri;
 
			const excludePatterns = await this.getExcludePatterns(workspaceUri);
 
			// Get the excluded folders pattern relative to the workspace folder.
			const excludedFolders = new vscode.RelativePattern(workspaceUri, '{' + excludePatterns.join(',') + '}');
 
			// Get the included files relative to the workspace folder.
			const includedFiles = new vscode.RelativePattern(workspaceUri, '{' + this._includePatterns.join(',') + '}');
 
			// Find all matching files.
			const files = await vscode.workspace.findFiles(includedFiles, excludedFolders);
 
			// Filter to ensure only files ending with supported extensions (glob may match mid-path).
			const filteredFiles = files.filter(uri => this.documentFactory.isSupportedFile(uri));
 
			this._files.push(...filteredFiles);
		}
 
		vscode.commands.executeCommand('setContext', 'mentor.workspace.isEmpty', this._files.length === 0);
		vscode.commands.executeCommand('setContext', 'mentor.workspace.isInitializing', false);
 
		this._initialized = true;
		this._onDidFinishDiscovery.fire();
	}
 
	/**
	 * Wait for file discovery to complete.
	 */
	async waitForDiscovery(): Promise<void> {
		if (this._initialized) {
			return;
		}
 
		return new Promise((resolve) => {
			const listener = this._onDidFinishDiscovery.event(() => {
				listener.dispose();
				resolve();
			});
		});
	}
 
	/**
	 * Generator that yields files matching the given language ID's extensions.
	 * @param languageId The VS Code language identifier (e.g., 'turtle', 'sparql')
	 * @returns Generator yielding matching files one by one.
	 */
	async* getFilesByLanguageId(languageId: string): AsyncGenerator<vscode.Uri, void, unknown> {
		const extensions = await this._getExtensionsForLanguageId(languageId);
 
		if (extensions.length === 0) {
			return;
		}
 
		const extSet = new Set<string>(extensions);
 
		for (const file of this._files) {
			const extension = file.path.split('.').pop() || '';
 
			if (extSet.has(extension)) {
				yield file;
			}
		}
	}
 
	/**
	 * Gets file extensions associated with a VS Code language ID.
	 * @param languageId The language identifier
	 * @returns Array of file extensions (without dots)
	 */
	private async _getExtensionsForLanguageId(languageId: string): Promise<string[]> {
		const languages = vscode.extensions.all
			.flatMap(ext => ext.packageJSON?.contributes?.languages || [])
			.filter(lang => lang.id === languageId);
 
		const extensions: string[] = [];
 
		for (const language of languages) {
			Eif (language.extensions) {
				const langExtensions = language.extensions.map((ext: string) =>
					ext.startsWith('.') ? ext.substring(1) : ext
				);
				extensions.push(...langExtensions);
			}
		}
 
		return [...new Set(extensions)];
	}
 
	/**
	 * Retrieves the contents of a folder in the workspace.
	 * @param folderUri The URI of the folder to search in.
	 * @returns A list of matching files and folders sorted by type and name.
	 */
	async getFolderContents(folderUri: vscode.Uri): Promise<vscode.Uri[]> {
		const files = [];
		const folders = [];
		const seenFiles = new Set<string>();
		const seenFolders = new Set<string>();
 
		const folder = folderUri.toString();
 
		for (const file of this._files) {
			if (!file.toString().startsWith(folder)) {
				continue;
			}
 
			const relativePath = file.toString().substring(folder.length + 1);
 
			if (relativePath.includes('/')) {
				const subFolderName = relativePath.substring(0, relativePath.indexOf('/'));
				const subFolderUri = vscode.Uri.joinPath(folderUri, subFolderName);
 
				if (!seenFolders.has(subFolderName)) {
					folders.push(subFolderUri);
					seenFolders.add(subFolderName);
				}
			} else if (E!seenFiles.has(relativePath)) {
				files.push(file);
				seenFiles.add(relativePath);
			}
		}
 
		return [
			...folders.sort((a, b) => a.path.localeCompare(b.path)),
			...files.sort((a, b) => a.path.localeCompare(b.path))
		];
	}
 
	/**
	 * Disposes of resources held by this service.
	 */
	dispose(): void {
		this._watcher.dispose();
		this._onDidFinishDiscovery.dispose();
		this._onDidChangeFiles.dispose();
	}
 
	/**
	 * Gets the list of patterns to exclude from indexing operations.
	 * @param workspaceUri The workspace URI to get patterns for.
	 * @returns An array of glob patterns to exclude.
	 */
	protected async getExcludePatterns(workspaceUri: vscode.Uri): Promise<string[]> {
		const result = new Set<string>();
 
		// Add the patterns from the configuration.
		for (const pattern of getConfig().get<string[]>('index.ignoreFolders', [])) {
			result.add(pattern);
		}
 
		// Add the patterns from the .gitignore file if enabled.
		if (getConfig().get<boolean>('index.useGitIgnore')) {
			const gitignore = vscode.Uri.joinPath(workspaceUri, '.gitignore');
 
			try {
				const content = await vscode.workspace.fs.readFile(gitignore);
 
				const excludePatterns = new TextDecoder().decode(content)
					.split('\n')
					.filter(line => !line.startsWith('#') && line.trim() !== '');
 
				for (const pattern of excludePatterns) {
					result.add(pattern);
				}
			} catch {
				// If the .gitignore file does not exist, ignore it.
			}
		}
 
		return Array.from(result);
	}
}