fix: 首选格式化支持连续字符的简写

This commit is contained in:
amzxyz
2026-01-11 20:09:48 +08:00
parent 4c7e6c4be1
commit 8071a14e22
2 changed files with 38 additions and 24 deletions

View File

@@ -1,3 +1,4 @@
#给自定义用户词扩展一个换行:\n, 制表符:\t, 回车符\r, 空格:\s
吧 b 50
静夜思\n\s\s李白\n床前明月光\n疑似地上霜\n举头望明月\n低头思故乡 jys 50
#给自定义用户词扩展一个换行:\n, 制表符:\t, 空格\s, 看不到的零宽空格\z
#连续多个前面的字符:{3} 恢复自身:\{
吧 b 5
静夜思\n\s{3}李白\n床前明月光\n疑似地上霜\n举头望明月\n低头思故乡 jys 5

View File

@@ -85,40 +85,53 @@ local function is_english_candidate(cand)
return true
end
-- 文本格式化(转义 + 自动大写)
-- 文本格式化 (精简版:仅保留转义与重复)
local escape_map = {
["\\n"] = "\n", ["\\t"] = "\t", ["\\r"] = "\r",
["\\\\"] = "\\", ["\\s"] = " ", ["\\d"] = "-",
["\\n"] = "\n", -- 换行
["\\r"] = "\r", -- 回车
["\\t"] = "\t", -- 制表符
["\\s"] = " ", -- 空格
["\\z"] = "\226\128\139", -- 零宽空格 (隐形阻断符)
}
local esc_pattern = "\\[ntrsd\\\\]"
local utf8_char_pattern = "[%z\1-\127\194-\244][\128-\191]*"
local function apply_escape_fast(text)
if not text or find(text, "\\", 1, true) == nil then return text, false end
local new_text = gsub(text, esc_pattern, function(esc) return escape_map[esc] or esc end)
-- 快速预检
if not text or (not find(text, "\\", 1, true) and not find(text, "{", 1, true)) then
return text, false
end
local new_text = text
if find(new_text, "\\{", 1, true) then
new_text = gsub(new_text, "\\{", "\1")
end
if find(new_text, "\\", 1, true) then
new_text = gsub(new_text, "\\[ntrsz]", escape_map)
end
if find(new_text, "{", 1, true) then
new_text = gsub(new_text, "(" .. utf8_char_pattern .. ")%{(%d+)}", function(char, count)
local n = tonumber(count)
if n and n > 0 and n < 200 then
return string.rep(char, n)
end
return char .. "{" .. count .. "}"
end)
end
if find(new_text, "\1", 1, true) then
new_text = gsub(new_text, "\1", "{")
end
return new_text, new_text ~= text
end
local function format_and_autocap(cand)
local text = cand.text
if not text or text == "" then return cand end
local changed = false
-- 转义替换 (\n, \t, \s 等)
-- 必须先处理转义,因为转义可能会改变字符串开头 (如 \sApple -> Apple)
if find(text, "\\", 1, true) then
local t2, ch = apply_escape_fast(text)
if ch then
text = t2
changed = true
end
end
-- 输出结果
local t2, changed = apply_escape_fast(text)
if not changed then return cand end
local nc = Candidate(cand.type, cand.start, cand._end, text, cand.comment)
local nc = Candidate(cand.type, cand.start, cand._end, t2, cand.comment)
nc.preedit = cand.preedit
return nc
end
local function clone_candidate(c)
local nc = Candidate(c.type, c.start, c._end, c.text, c.comment)
nc.preedit = c.preedit