From 7cb2c03c2784366defd3c98438ba86517af63365 Mon Sep 17 00:00:00 2001 From: amzxyz Date: Mon, 5 Jan 2026 10:51:46 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E8=8B=B1=E6=96=87lua=E6=B4=BE=E7=94=9F?= =?UTF-8?q?=E5=8D=95=E5=AD=97=E6=AF=8D=E6=8C=89=E7=85=A7=E8=BE=93=E5=85=A5?= =?UTF-8?q?=E4=BB=80=E4=B9=88=E4=BB=80=E4=B9=88=E4=BC=98=E5=85=88=EF=BC=8C?= =?UTF-8?q?=E4=B8=94=E4=B8=8D=E5=B9=B2=E6=89=B0=E7=BD=AE=E9=A1=B6=E7=9A=84?= =?UTF-8?q?=E5=8D=95=E8=AF=8D=EF=BC=8C=E7=BD=AE=E9=A1=B6=E4=BC=98=E5=85=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lua/super_english.lua | 57 +++++++++++++++++++++++++++++-------------- 1 file changed, 39 insertions(+), 18 deletions(-) diff --git a/lua/super_english.lua b/lua/super_english.lua index e69f406..2859f77 100644 --- a/lua/super_english.lua +++ b/lua/super_english.lua @@ -22,8 +22,19 @@ local sub = string.sub local match = string.match local format = string.format --- 1. 基础工具函数 +-- 辅助函数:获取候选类型 +local function fast_type(c) + local t = c.type + if t then return t end + local g = c.get_genuine and c:get_genuine() or nil + return (g and g.type) or "" +end +-- 辅助函数:判断是否为置顶表词汇 +local function is_table_type(c) + local t = fast_type(c) + return t == "table" or t == "user_table" or t == "fixed" +end -- [Time] 封装统一的时间获取函数 (单位: 秒, 带小数) local function get_now() -- 使用用户指定的原生 API (毫秒转秒,以便和配置文件里的 0.5 秒兼容) @@ -77,7 +88,6 @@ local function find_target_in_text(text, start_pos, target_fp) end -- 2. 核心逻辑:格式化与还原 --- [锚点切分] 修复 hi'vcs 等简拼分词问题 (保留修复) local function restore_sentence_spacing(cand, split_pattern, check_pattern) local guide = cand.preedit or "" if not find(guide, check_pattern) then return cand end @@ -316,29 +326,41 @@ function F.func(input, env) } local single_char_injected = false - local c_lower, c_upper = nil, nil + local single_chars = {} + if code_len == 1 then local b = byte(curr_input) - if (b >= 65 and b <= 90) or (b >= 97 and b <= 122) then - local lower_t = lower(curr_input) - local upper_t = upper(curr_input) - c_lower = Candidate("completion", 0, 1, lower_t, "") - c_upper = Candidate("completion", 0, 1, upper_t, "") - else single_char_injected = true end - else single_char_injected = true end + local is_upper = (b >= 65 and b <= 90) + local is_lower = (b >= 97 and b <= 122) + + if is_upper or is_lower then + -- 根据输入大小写决定排序:输入 N -> [N, n]; 输入 n -> [n, N] + local t1 = curr_input + local t2 = is_upper and lower(curr_input) or upper(curr_input) + + table.insert(single_chars, Candidate("completion", 0, 1, t1, "")) + table.insert(single_chars, Candidate("completion", 0, 1, t2, "")) + else + single_char_injected = true + end + else + single_char_injected = true + end for cand in input:iter() do local good_cand = restore_sentence_spacing(cand, env.split_pattern, env.delim_check_pattern) local fmt_cand = apply_formatting(good_cand, code_ctx) local is_ascii = is_ascii_phrase_fast(fmt_cand.text) - if not single_char_injected and is_ascii and c_lower then + local is_tbl = is_table_type(cand) + + -- table/fixed 类型会先输出,直到遇到第一个 completion类型前,插入单字母 + if not single_char_injected and is_ascii and #single_chars > 0 and not is_tbl then if not best_candidate_saved then - env.memory[curr_input] = { text = c_lower.text, preedit = c_lower.text } + env.memory[curr_input] = { text = single_chars[1].text, preedit = single_chars[1].text } best_candidate_saved = true end - yield(c_lower) - yield(c_upper) + for _, c in ipairs(single_chars) do yield(c) end single_char_injected = true has_valid_candidate = true end @@ -359,13 +381,12 @@ function F.func(input, env) yield(fmt_cand) end - if not single_char_injected and c_lower then + if not single_char_injected and #single_chars > 0 then if not best_candidate_saved then - env.memory[curr_input] = { text = c_lower.text, preedit = c_lower.text } + env.memory[curr_input] = { text = single_chars[1].text, preedit = single_chars[1].text } best_candidate_saved = true end - yield(c_lower) - yield(c_upper) + for _, c in ipairs(single_chars) do yield(c) end has_valid_candidate = true end