Skip to content

Commit

Permalink
修正小bug
Browse files Browse the repository at this point in the history
  • Loading branch information
sumneko committed Jul 11, 2024
1 parent e30c536 commit 13c3f54
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 29 deletions.
34 changes: 17 additions & 17 deletions src/editorTable/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,18 @@ function makeSandbox() {
}

export async function runEditor(uri: vscode.Uri, funcName: string = 'main') {
y3.log.show();
try {
const file = await y3.fs.readFile(uri);
let content = file!.string;
content = content + `\n\nmodule.exports = ${funcName}`;
let script = new vm.Script(content, {
filename: uri.path,
});
let func = script.runInNewContext(vm.createContext(makeSandbox()));
if (typeof func !== 'function') {
throw new Error('没有找到要执行的函数');
}
await func();
y3.log.info(`执行 "${uri.path.split('/').pop()}" 成功!`);
} catch (error) {
vscode.window.showErrorMessage(`运行物编脚本出错:${error}`);
const file = await y3.fs.readFile(uri);
let content = file!.string;
content = content + `\n\nmodule.exports = ${funcName}`;
let script = new vm.Script(content, {
filename: uri.path,
});
let func = script.runInNewContext(vm.createContext(makeSandbox()));
if (typeof func !== 'function') {
throw new Error('没有找到要执行的函数');
}
await func();
y3.log.info(`执行 "${uri.path.split('/').pop()}" 成功!`);
}

class RunButtonProvider implements vscode.CodeLensProvider {
Expand Down Expand Up @@ -92,7 +87,12 @@ export function init() {
return;
}
}
await runEditor(uri, funcName);
y3.log.show();
try {
await runEditor(uri, funcName);
} catch (error) {
vscode.window.showErrorMessage(`运行物编脚本出错:${error}`);
}
});

vscode.languages.registerCodeLensProvider({
Expand Down
24 changes: 13 additions & 11 deletions src/editorTable/editorTable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,18 @@ export class FieldInfo {
}
}

async function ready(tableName: Table.NameCN) {
export async function ready() {
await y3.language.ready();
if (tableMeta[tableName] === undefined) {
let nameEN = Table.name.fromCN[tableName];
let metaUri = vscode.Uri.joinPath(y3.helper.extensionUri, meta_dir, `${nameEN}.json`);
let metaFile = await y3.fs.readFile(metaUri);
if (metaFile) {
tableMeta[tableName] = JSON.parse(metaFile.string);
} else {
tableMeta[tableName] = {};
for (const tableName in Table.name.fromCN) {
if (tableMeta[tableName] === undefined) {
let nameEN = Table.name.fromCN[tableName as Table.NameCN];
let metaUri = vscode.Uri.joinPath(y3.helper.extensionUri, meta_dir, `${nameEN}.json`);
let metaFile = await y3.fs.readFile(metaUri);
if (metaFile) {
tableMeta[tableName] = JSON.parse(metaFile.string);
} else {
tableMeta[tableName] = {};
}
}
}
}
Expand Down Expand Up @@ -329,7 +331,7 @@ async function loadObject<N extends Table.NameCN>(tableName: N, key: number) {
if (!file) {
return null;
}
await ready(tableName);
await ready();
try {
let obj = new EditorObject(tableName, key);
obj.uri = uri;
Expand Down Expand Up @@ -702,7 +704,7 @@ export async function getObject(uri: vscode.Uri | string): Promise<EditorObject<
return;
}
const nameCN = Table.path.toCN[path as Table.Path];
await ready(nameCN);
await ready();
const file = await y3.fs.readFile(uri);
if (!file) {
return;
Expand Down
4 changes: 3 additions & 1 deletion src/editorTable/languageFeature/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import * as y3 from 'y3-helper';
import * as documentManager from './documentManager';
import * as documentSymbol from './documentSymbol';
import * as workspaceSymbol from './workspaceSymbol';
import * as hover from './hover';
import * as inlayHints from './inlayHints';

export function init() {
export async function init() {
await y3.table.ready();
documentManager.init();
documentSymbol.init();
workspaceSymbol.init();
Expand Down

0 comments on commit 13c3f54

Please sign in to comment.