fix: 以词定字取字逻辑 (#293)

修复的问题:在翻页后按下取最后一个字的快捷键,取到的是拼音的最后一个字母。
This commit is contained in:
abcdefg233 2023-05-26 21:04:25 +08:00 committed by GitHub
parent 2093207504
commit cdbc79985a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,6 +1,7 @@
-- 以词定字
-- 来源 https://github.com/BlindingDark/rime-lua-select-character
-- 删除了默认按键 [ ],和方括号翻页冲突,需要在 key_binder 下指定才能生效
-- 20230526195910 不再错误地获取commit_text而是直接获取get_selected_candidate().text。
local function utf8_sub(s, i, j)
i = i or 1
j = j or -1
@ -44,14 +45,6 @@ local function utf8_sub(s, i, j)
end
end
local function first_character(s)
return utf8_sub(s, 1, 1)
end
local function last_character(s)
return utf8_sub(s, -1, -1)
end
local function select_character(key, env)
local engine = env.engine
local context = engine.context
@ -63,17 +56,19 @@ local function select_character(key, env)
local first_key = config:get_string('key_binder/select_first_character')
local last_key = config:get_string('key_binder/select_last_character')
if (key:repr() == first_key and commit_text ~= "") then
engine:commit_text(first_character(commit_text))
context:clear()
if (key:repr() == first_key) then
if(context:get_selected_candidate().text)then
engine:commit_text(utf8_sub(context:get_selected_candidate().text, 1, 1))
context:clear()
end
return 1 -- kAccepted
end
if (key:repr() == last_key and commit_text ~= "") then
engine:commit_text(last_character(commit_text))
context:clear()
if (key:repr() == last_key) then
if(context:get_selected_candidate().text)then
engine:commit_text(utf8_sub(context:get_selected_candidate().text,-1,-1))
context:clear()
end
return 1 -- kAccepted
end