@movemama/opencode-legacy 1.0.1 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +62 -0
- package/package.json +1 -1
- package/tools/legacy-rules-loader.js +2 -1
package/README.md
CHANGED
|
@@ -72,6 +72,68 @@ OpenCode legacy 文本处理插件,面向 `GB2312` 文本、脚本型 `.txt`
|
|
|
72
72
|
|
|
73
73
|
这让插件可以按场景决定更合适的编辑策略,而不只是按编码读写。
|
|
74
74
|
|
|
75
|
+
## 自定义扩展规则
|
|
76
|
+
|
|
77
|
+
如果用户希望让其他文件类型也走 `GB2312` / `GBK` 的 legacy 读写链路,当前可以通过项目级规则扩展实现。
|
|
78
|
+
|
|
79
|
+
推荐在当前项目放置:
|
|
80
|
+
|
|
81
|
+
- `<worktree>/.opencode/legacy-rules.json`
|
|
82
|
+
|
|
83
|
+
例如:
|
|
84
|
+
|
|
85
|
+
```json
|
|
86
|
+
{
|
|
87
|
+
"rules": [
|
|
88
|
+
{
|
|
89
|
+
"glob": "**/*.{npc,msg,dialog}",
|
|
90
|
+
"encoding": "gb2312",
|
|
91
|
+
"strict": true,
|
|
92
|
+
"tool": "txt-gb2312",
|
|
93
|
+
"priority": 50,
|
|
94
|
+
"profile": "txt-gb2312-safe",
|
|
95
|
+
"editStrategy": "exact-first",
|
|
96
|
+
"fallbackMode": "legacy-safe-replace",
|
|
97
|
+
"scriptMarkers": []
|
|
98
|
+
}
|
|
99
|
+
]
|
|
100
|
+
}
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
字段说明:
|
|
104
|
+
|
|
105
|
+
- `glob`:匹配要走 legacy 读写链路的文件范围
|
|
106
|
+
- `encoding`:指定文件使用的编码,例如 `gb2312`、`gbk`
|
|
107
|
+
- `strict`:是否使用严格模式;通常脚本类文本建议保持 `true`
|
|
108
|
+
- `tool`:建议使用的处理器名称,当前常见值为 `txt-gb2312` 或 `legacy-text`
|
|
109
|
+
- `priority`:规则优先级,数值越大越优先匹配
|
|
110
|
+
- `profile`:场景配置名,用于帮助策略层判断文件类型与处理方式
|
|
111
|
+
- `editStrategy`:主编辑策略,例如 `exact-first`、`widget-field`
|
|
112
|
+
- `fallbackMode`:主策略失败后的回退方式,例如 `legacy-safe-replace`、`widget-field-update`
|
|
113
|
+
- `scriptMarkers`:用于辅助识别脚本型文件或 DSL 结构的关键标记数组
|
|
114
|
+
|
|
115
|
+
如果客户只是想让某类新文件按 `GB2312` 读写,最小配置通常只需要先关心:
|
|
116
|
+
|
|
117
|
+
- `glob`
|
|
118
|
+
- `encoding`
|
|
119
|
+
- `strict`
|
|
120
|
+
- `tool`
|
|
121
|
+
- `priority`
|
|
122
|
+
|
|
123
|
+
其余字段可以先参考现有示例再逐步细化。
|
|
124
|
+
|
|
125
|
+
当前规则加载优先级为:
|
|
126
|
+
|
|
127
|
+
1. `<worktree>/.opencode/legacy-rules.json`
|
|
128
|
+
2. `<worktree>/legacy-rules.json`
|
|
129
|
+
3. `~/.config/opencode/legacy-rules.json`
|
|
130
|
+
4. 包内 `legacy-rules.json`
|
|
131
|
+
|
|
132
|
+
这意味着用户现在既可以:
|
|
133
|
+
|
|
134
|
+
- 在 `~/.config/opencode/legacy-rules.json` 中定义全局通用扩展规则
|
|
135
|
+
- 又可以在单个项目里通过 `<worktree>/.opencode/legacy-rules.json` 或 `<worktree>/legacy-rules.json` 做更高优先级覆盖
|
|
136
|
+
|
|
75
137
|
## 自动策略层
|
|
76
138
|
|
|
77
139
|
当前 `edit` 已经接入自动策略层。
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@ import { existsSync, readFileSync } from 'node:fs'
|
|
|
2
2
|
import path from 'node:path'
|
|
3
3
|
|
|
4
4
|
import { createDefaultLegacyRules } from './legacy-router.mjs'
|
|
5
|
-
import { getBundledLegacyRulesPath } from './opencode-paths.mjs'
|
|
5
|
+
import { getBundledLegacyRulesPath, getGlobalLegacyRulesPath } from './opencode-paths.mjs'
|
|
6
6
|
|
|
7
7
|
export function loadLegacyRules(worktree) {
|
|
8
8
|
return loadLegacyRulesWithMeta(worktree).rules
|
|
@@ -12,6 +12,7 @@ export function loadLegacyRulesWithMeta(worktree) {
|
|
|
12
12
|
const candidates = [
|
|
13
13
|
{ filePath: path.join(worktree, '.opencode', 'legacy-rules.json'), source: 'project:.opencode' },
|
|
14
14
|
{ filePath: path.join(worktree, 'legacy-rules.json'), source: 'project:root' },
|
|
15
|
+
{ filePath: getGlobalLegacyRulesPath(), source: 'global:config' },
|
|
15
16
|
{ filePath: getBundledLegacyRulesPath(), source: 'bundled:default' },
|
|
16
17
|
]
|
|
17
18
|
|