'use strict'; import { commands, Event, EventEmitter, ExtensionContext, TreeDataProvider, TreeItem, Uri } from 'vscode'; import { UriComparer } from '../comparers'; import { ExplorerNode, FileHistoryNode, RepositoryNode, ResourceType, StashNode } from './explorerNodes'; import { GitService, GitUri } from '../gitService'; export * from './explorerNodes'; export class GitExplorer implements TreeDataProvider { private _onDidChangeTreeData = new EventEmitter(); public get onDidChangeTreeData(): Event { return this._onDidChangeTreeData.event; } private _roots: ExplorerNode[] = []; constructor(private context: ExtensionContext, private git: GitService) { commands.registerCommand('gitlens.gitExplorer.refresh', () => this.refresh()); // const editor = window.activeTextEditor; // const uri = (editor !== undefined && editor.document !== undefined) // ? new GitUri(editor.document.uri, { repoPath: git.repoPath, fileName: editor.document.uri.fsPath }) // : new GitUri(Uri.file(git.repoPath), { repoPath: git.repoPath, fileName: git.repoPath }); const uri = new GitUri(Uri.file(git.repoPath), { repoPath: git.repoPath, fileName: git.repoPath }); this._roots.push(new RepositoryNode(uri, context, git)); } async getTreeItem(node: ExplorerNode): Promise { return node.getTreeItem(); } async getChildren(node?: ExplorerNode): Promise { if (this._roots.length === 0) return []; if (node === undefined) return this._roots; return node.getChildren(); } addHistory(uri: GitUri) { this._add(uri, FileHistoryNode); } addStash(uri: GitUri) { this._add(uri, StashNode); } private _add(uri: GitUri, type: { new (uri: GitUri, context: ExtensionContext, git: GitService): T, rootType: ResourceType }) { if (!this._roots.some(_ => _.resourceType === type.rootType && UriComparer.equals(uri, _.uri))) { this._roots.push(new type(uri, this.context, this.git)); } this._onDidChangeTreeData.fire(); } clear() { this._roots = []; this._onDidChangeTreeData.fire(); } refresh() { this._onDidChangeTreeData.fire(); } }