From a9254e7894e92713f083a782ff8ffaa60d9f0d36 Mon Sep 17 00:00:00 2001 From: amzxyz <129564993+amzxyz@users.noreply.github.com> Date: Fri, 12 Jul 2024 08:14:07 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BD=BF=E5=85=B6?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E7=9B=B4=E6=8E=A5=E4=BB=8E1-3=E4=B8=AA?= =?UTF-8?q?=E7=AE=80=E7=A0=81=E7=9A=84=E8=AF=8D=E5=BA=93=E4=B8=AD=E8=8E=B7?= =?UTF-8?q?=E5=8F=96=E5=80=99=E9=80=89=E8=AF=8D=EF=BC=8C=E6=97=A0=E9=9C=80?= =?UTF-8?q?=E5=8F=A6=E5=BB=BAno=20conflict?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lua/stick.lua | 129 ++++++++++++++++++++++++++++---------------------- 1 file changed, 72 insertions(+), 57 deletions(-) diff --git a/lua/stick.lua b/lua/stick.lua index b2aa379..0cef8a4 100644 --- a/lua/stick.lua +++ b/lua/stick.lua @@ -1,73 +1,88 @@ --- 2码 3码时tab上屏词 置顶到第一候选的comment中 --- @gaboolic - +-- 配合简码词库将编码类型为(安装包 avb/ ),遇到这类词库1-3码简码时Tab上屏词 置顶到第一候选词的注释中,无基础候选词时将检索到的词典内容显示为第一候选词,不受词频影响 +-- 增加在快捷词汇后面显示闪电符号来表示快速输入字符、也可以理解为来自简码词库,配合方案快捷设置,闪电出现时按下Tab可以上屏。 +-- @gaboolic @amzxyz local M = {} function M.init(env) - -- 提升 count 个词语,插入到第 idx 个位置,默认 2、4。 - local config = env.engine.schema.config - env.name_space = env.name_space:gsub("^*", "") - env.fixed = {} - M.count = config:get_int(env.name_space .. "/count") or 2 - M.idx = config:get_int(env.name_space .. "/idx") or 4 - M.input_str = env.engine.context.input + -- 初始化逻辑,加载固定词典等 + local config = env.engine.schema.config + env.name_space = env.name_space:gsub("^*", "") + env.fixed = {} + M.count = config:get_int(env.name_space .. "/count") or 2 -- 获取配置中的 count 参数,默认值为 2 + M.idx = config:get_int(env.name_space .. "/idx") or 4 -- 获取配置中的 idx 参数,默认值为 4 + M.input_str = env.engine.context.input -- 获取当前输入的字符串 - local path_1 = rime_api.get_user_data_dir() .. ("/custom_phrase/custom_phrase_super_1jian.txt") - local path_2 = rime_api.get_user_data_dir() .. ("/custom_phrase/custom_phrase_super_2jian.txt") - local path_3 = rime_api.get_user_data_dir() .. ("/custom_phrase/custom_phrase_super_3jian.txt") - local paths = { - path_1, - path_2, - path_3 - } - -- 遍历表中的每个路径 - for _, path in ipairs(paths) do - -- 尝试打开文件 - local file = io.open(path, "r") - if not file then - log.info("stick path not file") - return - end - for line in file:lines() do - if string.sub(line, 1, 1) == "#" then - goto continue - end - ---@type string, string - local code, content = line:match("([^\t]+)\t([^\t]+)") - if not content or not code then - goto continue - end - content = string.sub(content, 1, -2) - env.fixed[content] = code - ::continue:: + -- 定义固定词典文件的路径 + local paths = { + rime_api.get_user_data_dir() .. "/jm_dicts/custom_phrase_super_1jian.txt", + rime_api.get_user_data_dir() .. "/jm_dicts/custom_phrase_super_2jian.txt", + rime_api.get_user_data_dir() .. "/jm_dicts/custom_phrase_super_3jian.txt" + } + + -- 遍历每个路径,加载固定词典 + for _, path in ipairs(paths) do + local file = io.open(path, "r") + if not file then + log.info("stick path not file") + return + end + -- 逐行读取文件内容 + for line in file:lines() do + if string.sub(line, 1, 1) == "#" then + goto continue + end + -- 匹配编码和词条内容 + local code, content = line:match("([^\t]+)\t([^\t]+)") + if content and code then + content = string.sub(content, 1, -2) -- 去除词条内容末尾的换行符 + env.fixed[content] = code -- 将编码和词条内容存储到 env.fixed 表中 + end + ::continue:: + end + file:close() -- 关闭文件 end - file:close() - end end -function isAllLetters(str) +-- 检查字符串是否全为字母 +local function isAllLetters(str) return not (string.find(str, "[^%a]")) end -function M.func(input,env) - -- log.info("stick M.func") - local first_cand = nil - for cand in input:iter() do - local preedit_str = cand.preedit - local preedit_len = utf8.len(preedit_str) - if first_cand == nil then - first_cand = cand - if preedit_len <=3 and isAllLetters(preedit_str) then +-- 创建候选词 +local function create_candidate(text, comment) + return Candidate("word", 0, string.len(text), text, comment) +end + +function M.func(input, env) + local first_cand = nil + local found = false + + -- 遍历输入的候选词 + for cand in input:iter() do + if not first_cand then + first_cand = cand + local preedit_str = cand.preedit + if utf8.len(preedit_str) <= 3 and isAllLetters(preedit_str) then + local stick_phrase = env.fixed[preedit_str] or "" + if stick_phrase ~= "" and first_cand.text ~= stick_phrase then + first_cand.comment = stick_phrase .. "⚡" -- 在注释后面加上闪电符号,表示快速输入,不想要置空 + end + yield(first_cand) + found = true + end + end + yield(cand) + found = true + end + + -- 如果没有找到匹配的候选词,显示固定词典内容作为第一候选词 + if not found then + local preedit_str = env.engine.context.input local stick_phrase = env.fixed[preedit_str] or "" - if stick_phrase ~= nil and first_cand.text ~= stick_phrase then - -- first_cand.comment=first_cand.comment .. stick_phrase - first_cand.comment = stick_phrase + if stick_phrase ~= "" then + yield(create_candidate(stick_phrase, "⚡")) -- 在注释中加上闪电符号,表示快速输入,不想要置空 end - yield(first_cand) - end end - yield(cand) - end end return M From 8562aee041311f06535cd083b8dd4658ce119b1d Mon Sep 17 00:00:00 2001 From: amzxyz <129564993+amzxyz@users.noreply.github.com> Date: Fri, 12 Jul 2024 08:29:28 +0800 Subject: [PATCH 2/2] Update stick.lua --- lua/stick.lua | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lua/stick.lua b/lua/stick.lua index 0cef8a4..e7061b0 100644 --- a/lua/stick.lua +++ b/lua/stick.lua @@ -14,9 +14,9 @@ function M.init(env) -- 定义固定词典文件的路径 local paths = { - rime_api.get_user_data_dir() .. "/jm_dicts/custom_phrase_super_1jian.txt", - rime_api.get_user_data_dir() .. "/jm_dicts/custom_phrase_super_2jian.txt", - rime_api.get_user_data_dir() .. "/jm_dicts/custom_phrase_super_3jian.txt" + rime_api.get_user_data_dir() .. "/custom_phrase/custom_phrase_super_1jian.txt", + rime_api.get_user_data_dir() .. "/custom_phrase/custom_phrase_super_2jian.txt", + rime_api.get_user_data_dir() .. "/custom_phrase/custom_phrase_super_3jian.txt" } -- 遍历每个路径,加载固定词典