Skip to content

Commit

Permalink
暂存
Browse files Browse the repository at this point in the history
  • Loading branch information
sumneko committed Jul 2, 2024
1 parent 7b0079b commit 140565e
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 13 deletions.
8 changes: 4 additions & 4 deletions src/editorTable/editorTable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ function getFileID(uri: vscode.Uri): number|undefined {
}

class EditorTable<N extends Table.NameCN> extends vscode.Disposable {
public tableUri;
public uri;
public nameEN;
private _objectCache: { [key: number]: EditorObject | null | undefined } = {};
private watcher?: vscode.FileSystemWatcher;
Expand All @@ -94,7 +94,7 @@ class EditorTable<N extends Table.NameCN> 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<EditorObject | null> {
Expand All @@ -108,7 +108,7 @@ class EditorTable<N extends Table.NameCN> 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;
Expand All @@ -133,7 +133,7 @@ class EditorTable<N extends Table.NameCN> extends vscode.Disposable {
private _onDidChange: vscode.EventEmitter<void> = 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) {
Expand Down
6 changes: 3 additions & 3 deletions src/editorTable/editorTableProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';


Expand Down Expand Up @@ -92,7 +92,7 @@ export class EditorTableDataProvider implements vscode.TreeDataProvider<FileNode

// 如果这个是物编数据的Json文件 那么它的label就需要加上其名称
let label: string = file;
if (isJson(filePath)) {
if (filePath.toLowerCase().endsWith('.json')) {
let editorTableJsonData: any;
try {
editorTableJsonData = await fs.promises.readFile(filePath, 'utf8');
Expand Down Expand Up @@ -179,7 +179,7 @@ export class FileNode extends vscode.TreeItem {
if (this.isDirectory) {
this.contextValue = 'directory';
}
else if (isJson(resourceUri.fsPath)) {
else if (resourceUri.fsPath.toLowerCase().endsWith('.json')) {
this.contextValue = 'json';
}
else {
Expand Down
2 changes: 0 additions & 2 deletions src/editorTable/fileView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,6 @@ export class GoEditorTableSymbolProvider implements vscode.WorkspaceSymbolProvid
}
}



/**
* 提供物编数据的Json文件内的中英文字段搜索的DocumentSymbolProvider
*/
Expand Down
102 changes: 98 additions & 4 deletions src/editorTable/treeView.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,87 @@
import * as vscode from 'vscode';
import { FileNode, EditorTableDataProvider } from './editorTableProvider';
import { GoEditorTableDocumentSymbolProvider, GoEditorTableSymbolProvider } from './fileView';
import { env } from '../env';
import { Table } from '../constants';
import * as editorTable from './editorTable';

export function init() {
class FileNode extends vscode.TreeItem {
constructor(public tableName: Table.NameCN, key: number) {
super(`读取中...(${key})`);

let table = editorTable.open(tableName);
table.get(key).then((object) => {
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<FileNode[]> {
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<TreeNode> {
private async getRoot(): Promise<DirNode[]> {
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<TreeNode|undefined>();
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,
});
Expand All @@ -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<TreeNode>;
if (env.editorTableUri) {
treeView = createTreeView();
}
env.onDidChange(() => {
if (treeView) {
treeView.dispose();
}
if (env.editorTableUri) {
treeView = createTreeView();
}
});

const goEditorTableSymbolProvider = new GoEditorTableSymbolProvider();

vscode.languages.registerWorkspaceSymbolProvider(goEditorTableSymbolProvider);
Expand Down

0 comments on commit 140565e

Please sign in to comment.