diff --git a/src/editorTable/editorTable.ts b/src/editorTable/editorTable.ts index 527a9a7..93e92bc 100644 --- a/src/editorTable/editorTable.ts +++ b/src/editorTable/editorTable.ts @@ -82,7 +82,7 @@ function getFileID(uri: vscode.Uri): number|undefined { } class EditorTable extends vscode.Disposable { - public tableUri; + public uri; public nameEN; private _objectCache: { [key: number]: EditorObject | null | undefined } = {}; private watcher?: vscode.FileSystemWatcher; @@ -94,7 +94,7 @@ class EditorTable extends vscode.Disposable { throw new Error('未选择地图路径'); } this.nameEN = Table.name.fromCN[nameCN]; - this.tableUri = vscode.Uri.joinPath(env.editorTableUri, Table.path.fromCN[nameCN]); + this.uri = vscode.Uri.joinPath(env.editorTableUri, Table.path.fromCN[nameCN]); } public async get(id: number): Promise { @@ -108,7 +108,7 @@ class EditorTable extends vscode.Disposable { public async list() { if (!this._listCache) { this._listCache = []; - let files = await y3.fs.dir(this.tableUri); + let files = await y3.fs.dir(this.uri); this.initWatcher(); if (!files) { return this._listCache; @@ -133,7 +133,7 @@ class EditorTable extends vscode.Disposable { private _onDidChange: vscode.EventEmitter = new vscode.EventEmitter(); private initWatcher() { - this.watcher = vscode.workspace.createFileSystemWatcher(new vscode.RelativePattern(this.tableUri, '*.json')); + this.watcher = vscode.workspace.createFileSystemWatcher(new vscode.RelativePattern(this.uri, '*.json')); this.watcher.onDidChange((fileUri) => { let id = getFileID(fileUri); if (id === undefined) { diff --git a/src/editorTable/editorTableProvider.ts b/src/editorTable/editorTableProvider.ts index 6a6dd73..a2385de 100644 --- a/src/editorTable/editorTableProvider.ts +++ b/src/editorTable/editorTableProvider.ts @@ -4,7 +4,7 @@ import * as fs from 'fs'; import { env } from '../env'; import { addNewEditorTableItemInProject } from './editorTableUtility'; import { Table } from '../constants'; -import { isPathValid, isJson, getFileNameByVscodeUri, hash, toUnicodeIgnoreASCII } from '../utility'; +import { hash, toUnicodeIgnoreASCII } from '../utility'; import * as y3 from 'y3-helper'; @@ -92,7 +92,7 @@ export class EditorTableDataProvider implements vscode.TreeDataProvider { + if (!object) { + return; + } + this.label = `${object.name}(${key})`; + this.resourceUri = object.uri; + }); + } + + readonly contextValue = 'json'; +} + +class DirNode extends vscode.TreeItem { + constructor(public tableName: Table.NameCN) { + super(`${tableName}(加载中...)`); + + let table = editorTable.open(tableName); + this.resourceUri = table.uri; + + table.list().then((keys) => { + this.label = `${tableName}(${keys.length})`; + }); + } + + readonly contextValue = 'directory'; + + public async getChildren(): Promise { + let nodes: FileNode[] = []; + let table = editorTable.open(this.tableName); + let keys = await table.list(); + for (const key of keys) { + nodes.push(new FileNode(this.tableName, key)); + } + return nodes; + } +} + +type TreeNode = FileNode | DirNode; + +class EditorTableDataProvider implements vscode.TreeDataProvider { + private async getRoot(): Promise { + let nodes: DirNode[] = []; + for (const nameCN in Table.name.fromCN) { + nodes.push(new DirNode(nameCN as Table.NameCN)); + } + return nodes; + } + + public async getChildren(node?: TreeNode | undefined) { + if (node === undefined) { + return await this.getRoot(); + } else if(node instanceof DirNode) { + return await node.getChildren(); + } else { + return []; + } + } + + public async getTreeItem(element: FileNode) { + return element; + } + + private _onDidChange = new vscode.EventEmitter(); + readonly onDidChangeTreeData = this._onDidChange.event; + public refresh() { + this._onDidChange.fire(undefined); + } +} + +function createTreeView() { const editorTableDataProvider = new EditorTableDataProvider(); - - vscode.window.createTreeView('y3-helper.editorTableView', { + + const treeView = vscode.window.createTreeView('y3-helper.editorTableView', { treeDataProvider: editorTableDataProvider, showCollapseAll: true, }); @@ -17,6 +92,25 @@ export function init() { vscode.commands.registerCommand('y3-helper.editorTableView.refresh', () => editorTableDataProvider.refresh()); + return treeView; +} + +export async function init() { + await env.mapReady(); + + let treeView: vscode.TreeView; + if (env.editorTableUri) { + treeView = createTreeView(); + } + env.onDidChange(() => { + if (treeView) { + treeView.dispose(); + } + if (env.editorTableUri) { + treeView = createTreeView(); + } + }); + const goEditorTableSymbolProvider = new GoEditorTableSymbolProvider(); vscode.languages.registerWorkspaceSymbolProvider(goEditorTableSymbolProvider);