Skip to content

Commit

Permalink
Merge pull request #7 from lost-melody/main
Browse files Browse the repository at this point in the history
  • Loading branch information
forFudan authored May 26, 2023
2 parents 84a83e7 + a41b67d commit 770bfca
Showing 1 changed file with 58 additions and 44 deletions.
102 changes: 58 additions & 44 deletions beta/schema/lua/yuhao/yuhao_embeded_cands.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
-- 将要被返回的過濾器對象
-- 將要被返回的過濾器對象
local embeded_cands_filter = {}

--[[
Expand All @@ -15,6 +15,15 @@ key_binder:
- { when: always, accept: "Control+Shift+E", toggle: embeded_cands }
--]]

-- 候選序號標記
local index_indicators = {"¹", "²", "³", "", "", "", "", "", "", ""}

-- 首選/非首選格式定義
-- seq: 候選序號; code: 編碼; 候選: 候選文本; comment: 候選提示
local first_format = "[候選code]comment"
local next_format = "seq候選comment"
local separator = ""

-- 讀取 schema.yaml 開關設置:
local option_name = "embeded_cands"
local embeded_cands = nil
Expand All @@ -25,7 +34,7 @@ function embeded_cands_filter.init(env)
if name == option_name then
embeded_cands = ctx:get_option(name)
if embeded_cands == nil then
-- 當選項不存在時默認爲啟用狀態
-- 當選項不存在時默認爲啓用狀態
embeded_cands = true
end
end
Expand All @@ -36,6 +45,35 @@ function embeded_cands_filter.init(env)
env.engine.context.option_update_notifier:connect(handler)
end

-- 渲染提示, 因爲提示經常有可能爲空, 抽取爲函數更昜操作
local function render_comment(comment)
if string.match(comment, "^[ ~]") then
-- 丟棄以空格和"~"開頭的提示串, 這通常是補全提示
comment = ""
elseif string.len(comment) ~= 0 then
comment = "["..comment.."]"
end
return comment
end

-- 渲染單個候選項
local function render_cand(seq, code, text, comment)
local cand = ""
-- 選擇渲染格式
if seq == 1 then
cand = first_format
else
cand = next_format
end
-- 渲染提示串
comment = render_comment(comment)
cand = string.gsub(cand, "seq", index_indicators[seq])
cand = string.gsub(cand, "code", code)
cand = string.gsub(cand, "候選", text)
cand = string.gsub(cand, "comment", comment)
return cand
end

-- 過濾器
function embeded_cands_filter.func(input, env)
if not embeded_cands then
Expand All @@ -48,10 +86,21 @@ function embeded_cands_filter.func(input, env)
-- 要顯示的候選數量
local page_size = env.engine.schema.page_size
-- 暫存當前頁候選, 然后批次送出
local page_cands = {}
local page_cands, page_rendered = {}, {}
-- 暫存索引, 首選和預編輯文本
local index, first_cand, preedit = 0, nil, ""

local function refresh_preedit()
first_cand.preedit = table.concat(page_rendered, separator)
-- 將暫存的一頁候選批次送出
for _, c in ipairs(page_cands) do
yield(c)
end
-- 清空暫存
first_cand, preedit = nil, ""
page_cands, page_rendered = {}, {}
end

-- 迭代器
local iter, obj = input:iter()
-- 迭代由翻譯器輸入的候選列表
Expand All @@ -64,62 +113,27 @@ function embeded_cands_filter.func(input, env)

if index == 1 then
-- 把首選捉出來
first_cand = cand:get_genuine()
first_cand = cand
end

-- 修改首選的預编輯文本, 這会作爲内嵌編碼顯示到輸入處
if index == 1 then
-- 首選和編碼
-- 這裏的if块可以直接改成一個 preedit = preedit..cand.text
if string.len(cand.text) <= string.len("四個漢字") then
-- 四字以内, "漢字code"
preedit = cand.text..first_cand.preedit
else
-- 四字以上, "code四字以上詞"
preedit = first_cand.preedit..cand.text
end
elseif index <= page_size then
-- 當前頁余下候選項, 形如 "2.漢字"
-- 組合顯示爲 "首選code 2.次選 3.三選 ..."
preedit = preedit.." "..tostring(index).."."..cand.text
end
preedit = render_cand(index, first_cand.preedit, cand.text, cand.comment)

-- 如果候選有提示且不以 "~" 開頭(补全提示), 識别爲反查提示
if string.len(cand.comment) ~= 0 and string.sub(cand.comment, 1, 1) ~= "~" then
if index == 1 then
-- 首選後額外增加一空格, 以将輸入編碼與反查分隔
preedit = preedit.." "
end
preedit = preedit..cand.comment
end
-- 存入首選
-- 存入候選
table.insert(page_cands, cand)
table.insert(page_rendered, preedit)

-- 遍歷完一頁候選後, 刷新預編輯文本
if index == page_size then
first_cand.preedit = preedit
-- 将暫存的一頁候選批次送出
for _, c in ipairs(page_cands) do
yield(c)
end
-- 清空暫存
first_cand, preedit = nil, ""
page_cands = {}
refresh_preedit()
end

-- 當前候選處理完畢, 查詢下一個
next = iter(obj)

-- 如果當前暫存候選不足page_size但没有更多候選, 則需要刷新預編輯並送出
if not next and index < page_size then
first_cand.preedit = preedit
-- 将暫存的前三候選批次送出
for _, c in ipairs(page_cands) do
yield(c)
end
-- 清空暫存
first_cand, preedit = nil, ""
page_cands = {}
refresh_preedit()
end

-- 下一頁, index歸零
Expand Down

0 comments on commit 770bfca

Please sign in to comment.