From a2394cd91c007f59099f152d5d0c9cfbb5ec98b6 Mon Sep 17 00:00:00 2001 From: WantChane <143467549+WantChane@users.noreply.github.com> Date: Thu, 23 Oct 2025 08:59:20 +0000 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BB=A5=E8=AF=8D=E5=AE=9A=E5=AD=97?= =?UTF-8?q?=E9=95=BF=E5=8F=A5=E8=BE=93=E5=85=A5=E6=94=AF=E6=8C=81=20(#1252?= =?UTF-8?q?)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit close #1251 --- lua/select_character.lua | 50 ++++++++++++++++++++++++---------------- 1 file changed, 30 insertions(+), 20 deletions(-) diff --git a/lua/select_character.lua b/lua/select_character.lua index d5fe342..55c9769 100644 --- a/lua/select_character.lua +++ b/lua/select_character.lua @@ -4,7 +4,9 @@ -- 20230526195910 不再错误地获取commit_text,而是直接获取get_selected_candidate().text -- 20240128141207 重写:将读取设置移动到 init 方法中;简化中文取字方法;预先判断候选存在与否,无候选取 input -- 20240508111725 当候选字数为 1 时,快捷键使该字上屏 - +-- 20250515093039 以词定字支持长句输入 +-- 20250516231523 当候选字数为 1 且还有未处理的输入时,快捷键使该字上屏, 保留未处理部分 +-- 20250524151149 当光标位置不在 input 末尾时,保留光标右侧部分 local select = {} function select.init(env) @@ -22,27 +24,35 @@ function select.func(key, env) and (context:is_composing() or context:has_menu()) and (env.first_key or env.last_key) then - local text = context.input - if context:get_selected_candidate() then - text = context:get_selected_candidate().text - end - if utf8.len(text) > 1 then - if (key:repr() == env.first_key) then - engine:commit_text(text:sub(1, utf8.offset(text, 2) - 1)) - context:clear() - return 1 - elseif (key:repr() == env.last_key) then - engine:commit_text(text:sub(utf8.offset(text, -1))) - context:clear() - return 1 - end + local input = context.input + local selected_candidate = context:get_selected_candidate() + selected_candidate = selected_candidate and selected_candidate.text or input + + local selected_char = "" + if (key:repr() == env.first_key) then + selected_char = selected_candidate:sub(1, utf8.offset(selected_candidate, 2) - 1) + elseif (key:repr() == env.last_key) then + selected_char = selected_candidate:sub(utf8.offset(selected_candidate, -1)) else - if key:repr() == env.first_key or key:repr() == env.last_key then - engine:commit_text(text) - context:clear() - return 1 - end + return 2 end + + local commit_text = context:get_commit_text() + local _, end_pos = commit_text:find(selected_candidate) + local caret_pos = context.caret_pos + + local part1 = commit_text:sub(1, end_pos):gsub(selected_candidate, selected_char) + local part2 = commit_text:sub(end_pos + 1) + + engine:commit_text(part1) + context:clear() + if caret_pos ~= #input then + part2 = part2 .. input:sub(caret_pos + 1) + end + if part2 ~= "" then + context:push_input(part2) + end + return 1 end return 2 end