Skip to content

Commit 6b044ce

Browse files
committed
增加自动async
1 parent bcf13a1 commit 6b044ce

3 files changed

Lines changed: 49 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# 2.1.3
2+
3+
## 新增
4+
5+
1. 现在支持直接在编辑器中调用异步函数而不需要定义async或链式调用了
6+
17
# 2.1.2
28

39
## 新增

extension.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"uuid": "7ca98ae04b7142599ab107e34acc8e5e",
44
"displayName": "嘉立创EDA代码编辑器",
55
"description": "支持中文联想的EDA脚本代码编辑器,支持代码高亮、自动补全、函数提示,AI注释,AI报错分析等功能。",
6-
"version": "2.1.2",
6+
"version": "2.1.3",
77
"publisher": "嘉立创EDA",
88
"engines": {
99
"eda": "^2.2.0"

iframe/script/User_config/ACE_Config.js

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,11 @@ function ACE_RunCode(editor) {
154154
return;
155155
}
156156
try {
157-
eval(code);
157+
// 使用 IIFE:定义并立即调用 async 函数
158+
const newcode = `(async () => {\n ${code}\n})();`;
159+
eval(newcode);
158160
} catch (error) {
161+
eda.sys_Message.showToastMessage('啊偶,执行出错了', 'error', 2);
159162
console.error('执行出错:', error);
160163
}
161164
}
@@ -950,3 +953,41 @@ function ExportFileForJs(text, filename = 'script.js') {
950953
document.body.removeChild(link);
951954
URL.revokeObjectURL(url);
952955
}
956+
957+
/**
958+
* 格式化 Ace 编辑器中的 JavaScript 代码,并将光标移至原光标所在行的行末
959+
*
960+
* @param {Object} editor - Ace 编辑器实例(必须已初始化)
961+
* @throws {Error} 如果 editor 无效或无法获取内容
962+
* @returns {void}
963+
*/
964+
function formatEditorCode(editor) {
965+
if (!editor || typeof editor.getValue !== 'function') {
966+
throw new Error('Invalid Ace editor instance provided.');
967+
}
968+
const rawCode = editor.getValue();
969+
// 若内容为空或仅空白,清空编辑器并退出
970+
if (!rawCode || rawCode.trim() === '') {
971+
editor.setValue('', -1);
972+
return;
973+
}
974+
// 记录当前光标所在行(0-based)
975+
const originalRow = editor.getCursorPosition().row;
976+
// 使用 js-beautify 格式化代码
977+
const formattedCode = js_beautify(rawCode, {
978+
indent_size: 4,
979+
space_in_empty_paren: true,
980+
end_with_newline: false,
981+
});
982+
// 应用格式化后的内容
983+
editor.setValue(formattedCode, -1);
984+
// 获取格式化后的总行数(getLength() 返回行数,0 行时为 1)
985+
const session = editor.getSession();
986+
const totalRows = session.getLength() - 1; // 最大有效行索引(0-based)
987+
// 确保目标行不越界
988+
const targetRow = Math.min(originalRow, totalRows);
989+
// 获取该行内容长度(即行末列位置)
990+
const lineEndColumn = session.getLine(targetRow).length;
991+
// 移动光标到目标行末尾(Ace 的 gotoLine 行号是 1-based)
992+
editor.gotoLine(targetRow + 1, lineEndColumn, false);
993+
}

0 commit comments

Comments
 (0)