All files / views/trees/workspace-tree workspace-tree.ts

75% Statements 9/12
16.66% Branches 1/6
66.66% Functions 2/3
75% Lines 9/12

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                        6x         6x               6x         6x   6x       2x   2x                           6x 6x    
import * as vscode from 'vscode';
import { container } from 'tsyringe';
import { ServiceToken } from '@src/services/tokens';
import { IWorkspaceFileService } from '@src/services/core';
import { TreeView } from '@src/views/trees/tree-view';
import { renameWorkspaceItem } from '@src/commands/workspace/rename-workspace-item';
import { WorkspaceNodeProvider } from './workspace-node-provider';
 
export class WorkspaceTree implements TreeView {
	/**
	 * The ID which is used to register the view and make it visible in VS Code.
	 */
	readonly id = "mentor.view.workspaceTree";
 
	/**
	 * The tree node provider.
	 */
	readonly treeDataProvider = new WorkspaceNodeProvider();
 
	/**
	 * The tree view.
	 */
	readonly treeView: vscode.TreeView<string>;
 
	constructor() {
		this.treeView = vscode.window.createTreeView(this.id, {
			treeDataProvider: this.treeDataProvider,
			showCollapseAll: true
		});
 
		this.treeView.title = vscode.workspace.name ?? "Workspace";
 
		const disposables = [
			this.treeView,
			vscode.commands.registerCommand('mentor.command.refreshWorkspaceTree', async () => {
				// Rediscover the files to recover from file system changes the watcher did not report.
				await container.resolve<IWorkspaceFileService>(ServiceToken.WorkspaceFileService).discoverFiles();
 
				this.treeDataProvider.refresh();
			}),
			vscode.commands.registerCommand(renameWorkspaceItem.id, async (clicked?: string) => {
				// Context menu invocations pass the clicked node; the F2 keybinding
				// passes nothing, so fall back to the current tree selection.
				const target = clicked ?? this.treeView.selection[0];
 
				if (target) {
					await renameWorkspaceItem.handler(target);
				}
			})
		];
 
		// Self-register with the extension context for automatic disposal
		const context = container.resolve<vscode.ExtensionContext>(ServiceToken.ExtensionContext);
		context.subscriptions.push(...disposables);
	}
}