Files
mixly3/common/modules/mixly-modules/workers/common/cpp-formatter/index.js
2026-01-13 02:54:45 +08:00

53 lines
1.6 KiB
JavaScript

importScripts('./libastyle.js');
importScripts('../../../../web-modules/workerpool.min.js');
const ASTYLE_CONFIG = `
# Braces / 基本风格
style=google # google 样式
# 缩进
indent=spaces=2 # 使用 2 空格
indent-switches # switch 里的 case 缩进
indent-cases # case 对齐
indent-after-parens # 括号之后新起一行也缩进
indent-preproc-block # 缩进宏块
indent-preproc-cond # 缩进条件宏
# Padding / 空格
pad-comma # 参数逗号后加空格
pad-oper # 运算符周围加空格
pad-header # 关键字后添加空格 (if ( … ))
# 对齐
align-pointer=name # 把指针符号 * 放在类型名边
align-reference=name # 引用符号 & 同上
# Formatting / 格式化行为
attach-return-type # 合并返回类型和函数名
attach-return-type-decl # 对声明也合并
add-braces # 强制加大括号
keep-one-line-blocks # 保留一行 {} 块
keep-one-line-statements # if/else 等保留一行语句
close-templates # 模板 <> 处理
break-after-logical # long expression 后换行
break-blocks # 块首尾换行
# 其他
delete-empty-lines # 删除空行
max-continuation-indent=4 # 设置表达式延续缩进最大值
`;
async function init() {
await self.cppFormatter.init();
}
async function format(code) {
const result = await self.cppFormatter.format(code, ASTYLE_CONFIG);
return result;
}
workerpool.worker({
init,
format
});