All files / commands open-file-from-language.ts

100% Statements 30/30
77.77% Branches 14/18
100% Functions 5/5
100% Lines 30/30

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                            1x     5x 5x 5x   5x 8x     5x 5x 5x   5x 5x   5x   5x 1x         4x 8x       8x             4x     5x 5x           5x               5x 5x 3x   3x 1x   1x 2x 1x       5x    
import * as vscode from 'vscode';
import { container } from 'tsyringe';
import { ServiceToken } from '@src/services/tokens';
import { WorkspaceUri } from '@src/providers/workspace-uri';
import { IWorkspaceFileService } from '@src/services/core';
import { IDocumentFactory } from '@src/services/document/document-factory.interface';
import { getFileName, getPath } from '@src/utilities';
 
export type FileQuickPickItem = vscode.QuickPickItem & {
	iri?: vscode.Uri,
	command?: string,
	args?: any
};
 
export const openFileFromLanguage = {
	id: 'mentor.command.openFileFromLanguage',
	handler: async (languageId: string) => {
		const files: vscode.Uri[] = [];
		const workspaceFileService = container.resolve<IWorkspaceFileService>(ServiceToken.WorkspaceFileService);
		const documentFactory = container.resolve<IDocumentFactory>(ServiceToken.DocumentFactory);
 
		for await (const file of workspaceFileService.getFilesByLanguageId(languageId)) {
			files.push(file);
		}
 
		const language = await documentFactory.getLanguageInfo(languageId);
		const languageName = language ? language.name : languageId;
		const languageIcon = language ? language.icon : 'file';
 
		const quickPick = vscode.window.createQuickPick<FileQuickPickItem>();
		quickPick.title = 'Select the file to open:';
 
		let items: FileQuickPickItem[] = [];
 
		if (files.length === 0) {
			items = [{
				iri: undefined,
				label: 'No files found for this language: ' + languageName
			}];
		} else {
			items = files
				.map(file => ({
					fileUri: file.toString(),
					workspaceUri: WorkspaceUri.toWorkspaceUri(file)
				}))
				.map(item => ({
					...item,
					icon: languageIcon,
					label: `$(${languageIcon})  ${getFileName(item.fileUri)}`,
					description: item.workspaceUri ? '~' + getPath(item.workspaceUri.fsPath) : undefined,
					iri: item.workspaceUri
				}))
				.sort((a, b) => a.label.localeCompare(b.label));
		}
 
		Eif (language) {
			items.push({
				iri: undefined,
				label: '',
				kind: vscode.QuickPickItemKind.Separator
			});
 
			items.push({
				iri: undefined,
				label: `$(${language.icon})  New ${language.typeName}`,
				command: 'mentor.command.createDocumentFromLanguage',
				args: language.id
			});
		}
 
		quickPick.items = items;
		quickPick.onDidChangeSelection(async (selection) => {
			const item = selection[0];
 
			if (item && item.iri) {
				const document = await vscode.workspace.openTextDocument(item.iri);
 
				vscode.window.showTextDocument(document);
			} else if (item && item.command) {
				vscode.commands.executeCommand(item.command, item.args);
			}
		});
 
		quickPick.show();
	}
};