|
| 1 | +import path from 'path'; |
| 2 | +import { |
| 3 | + Event, |
| 4 | + EventEmitter, |
| 5 | + FileSystemWatcher, |
| 6 | + ProviderResult, |
| 7 | + RelativePattern, |
| 8 | + ThemeIcon, |
| 9 | + TreeDataProvider, |
| 10 | + TreeItem, |
| 11 | + TreeItemCollapsibleState, |
| 12 | + Uri, |
| 13 | + workspace, |
| 14 | +} from 'vscode'; |
| 15 | +import { getWorkspaceRoot } from '../util'; |
| 16 | + |
| 17 | +const WORKSPACE_ROOT = getWorkspaceRoot(); |
| 18 | + |
| 19 | +export class RootFileViewProvider implements TreeDataProvider<string> { |
| 20 | + private static _instance: RootFileViewProvider; |
| 21 | + |
| 22 | + private _filesWatcher: FileSystemWatcher; |
| 23 | + private _onDidChangeTreeData: EventEmitter<string | undefined | void> = |
| 24 | + new EventEmitter<string | undefined | void>(); |
| 25 | + readonly onDidChangeTreeData: Event<string | undefined | void> = |
| 26 | + this._onDidChangeTreeData.event; |
| 27 | + |
| 28 | + private constructor() { |
| 29 | + this._filesWatcher = workspace.createFileSystemWatcher( |
| 30 | + new RelativePattern(WORKSPACE_ROOT, '*') |
| 31 | + ); |
| 32 | + this._filesWatcher.onDidChange((f) => this._onDidChangeTreeData.fire()); |
| 33 | + this._filesWatcher.onDidCreate((f) => this._onDidChangeTreeData.fire()); |
| 34 | + this._filesWatcher.onDidDelete((f) => this._onDidChangeTreeData.fire()); |
| 35 | + } |
| 36 | + |
| 37 | + public static get instance(): RootFileViewProvider { |
| 38 | + if (!this._instance) { |
| 39 | + this._instance = new RootFileViewProvider(); |
| 40 | + } |
| 41 | + return this._instance; |
| 42 | + } |
| 43 | + |
| 44 | + getTreeItem(element: string): TreeItem | Thenable<TreeItem> { |
| 45 | + return new FileItem(element); |
| 46 | + } |
| 47 | + getChildren(element?: string | undefined): ProviderResult<string[]> { |
| 48 | + if (!element) { |
| 49 | + return workspace.fs |
| 50 | + .readDirectory(Uri.file(WORKSPACE_ROOT)) |
| 51 | + .then((val) => { |
| 52 | + return val.filter((v) => v[1] === 1).map((v) => v[0]); |
| 53 | + }); |
| 54 | + } |
| 55 | + return []; |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +class FileItem extends TreeItem { |
| 60 | + constructor(fileName: string, collapsibleState?: TreeItemCollapsibleState) { |
| 61 | + super(fileName, collapsibleState); |
| 62 | + this.command = { |
| 63 | + title: fileName, |
| 64 | + command: 'vscode.open', |
| 65 | + arguments: [`${WORKSPACE_ROOT}${path.sep}${fileName}`], |
| 66 | + }; |
| 67 | + switch (fileName.split('.').reverse()[0]) { |
| 68 | + case 'json': |
| 69 | + this.iconPath = new ThemeIcon('json'); |
| 70 | + break; |
| 71 | + default: |
| 72 | + this.iconPath = new ThemeIcon('file'); |
| 73 | + break; |
| 74 | + } |
| 75 | + } |
| 76 | +} |
0 commit comments