From 7731c6c8cbbb70ca86187e679fea4e6a3ab1c295 Mon Sep 17 00:00:00 2001 From: JuanZoran <1430359574@qq.com> Date: Mon, 13 Mar 2023 19:50:28 +0800 Subject: [PATCH] refactor: remove wrapper folder --- lua/Trans/README.md | 6 ++-- lua/Trans/backend/baidu.lua | 2 +- lua/Trans/{wrapper => core}/buffer.lua | 7 +++-- lua/Trans/{wrapper => core}/curl.lua | 0 lua/Trans/core/data.lua | 42 ++++++++++++++++++++++++++ lua/Trans/core/install.lua | 2 +- lua/Trans/core/translate.lua | 40 ++---------------------- lua/Trans/{wrapper => core}/window.lua | 20 +++--------- lua/Trans/frontend/hover/init.lua | 18 ++++++----- lua/Trans/init.lua | 9 +++--- lua/Trans/util/node.lua | 7 +---- 11 files changed, 74 insertions(+), 79 deletions(-) rename lua/Trans/{wrapper => core}/buffer.lua (96%) rename lua/Trans/{wrapper => core}/curl.lua (100%) create mode 100644 lua/Trans/core/data.lua rename lua/Trans/{wrapper => core}/window.lua (92%) diff --git a/lua/Trans/README.md b/lua/Trans/README.md index 24ab2b7..272100c 100644 --- a/lua/Trans/README.md +++ b/lua/Trans/README.md @@ -5,9 +5,9 @@ - [TODO](#todo) -- [ ] Refactor query engine to 'Backend' and 'Frontend' +- [x] Refactor query engine to 'Backend' and 'Frontend' - [x] Use `Trans.install` instead of `install.sh` - [ ] Check if str is a word -- [ ] init frontend window +- [x] waitting animation +- [x] init frontend window - [ ] build frontend window format logic -- [ ] waitting animation diff --git a/lua/Trans/backend/baidu.lua b/lua/Trans/backend/baidu.lua index dd799b8..de53ac4 100644 --- a/lua/Trans/backend/baidu.lua +++ b/lua/Trans/backend/baidu.lua @@ -56,7 +56,7 @@ function M.query(data) end - Trans.wrapper.curl.get(M.uri, { + Trans.curl.get(M.uri, { query = M.get_content(data), callback = handle, }) diff --git a/lua/Trans/wrapper/buffer.lua b/lua/Trans/core/buffer.lua similarity index 96% rename from lua/Trans/wrapper/buffer.lua rename to lua/Trans/core/buffer.lua index 92929aa..cd02b5c 100644 --- a/lua/Trans/wrapper/buffer.lua +++ b/lua/Trans/core/buffer.lua @@ -91,13 +91,14 @@ end ---Add highlight to buffer ---@param linenr number line number should be set[one index] ----@param col_start number column start +---@param col_start number column start [zero index] ---@param col_end number column end ---@param hl_group string highlight group ---@param ns number? highlight namespace -function buffer:add_highlight(linenr, col_start, col_end, hl_group, ns) +function buffer:add_highlight(linenr, hl_group, col_start, col_end, ns) linenr = linenr and linenr - 1 or -1 - api.nvim_buf_add_highlight(self.bufnr, ns or -1, hl_group, linenr, col_start, col_end) + col_start = col_start or 0 + api.nvim_buf_add_highlight(self.bufnr, ns or -1, hl_group, linenr, col_start, col_end or -1) end ---Calculate buffer content display height diff --git a/lua/Trans/wrapper/curl.lua b/lua/Trans/core/curl.lua similarity index 100% rename from lua/Trans/wrapper/curl.lua rename to lua/Trans/core/curl.lua diff --git a/lua/Trans/core/data.lua b/lua/Trans/core/data.lua new file mode 100644 index 0000000..f478b01 --- /dev/null +++ b/lua/Trans/core/data.lua @@ -0,0 +1,42 @@ +local Trans = require('Trans') + +local M = {} +M.__index = M + + + +function M.new(opts) + local mode = opts.mode + local str = opts.str + + + local strategy = Trans.conf.strategy[mode] + local data = { + str = str, + mode = mode, + result = {}, + } + + data.frontend = Trans.frontend[strategy.frontend].new() + + data.backend = {} + for i, name in ipairs(strategy.backend) do + data.backend[i] = Trans.backend[name] + end + + + if Trans.util.is_English(str) then + data.from = 'en' + data.to = 'zh' + else + data.from = 'zh' + data.to = 'en' + end + + -- FIXME : Check if the str is a word + data.is_word = true + + return setmetatable(data, M) +end + +return M diff --git a/lua/Trans/core/install.lua b/lua/Trans/core/install.lua index 211cdbe..ea01db3 100644 --- a/lua/Trans/core/install.lua +++ b/lua/Trans/core/install.lua @@ -36,7 +36,7 @@ return function() end - Trans.wrapper.curl.get(uri, { + Trans.curl.get(uri, { output = loc, callback = handle, }) diff --git a/lua/Trans/core/translate.lua b/lua/Trans/core/translate.lua index 67a00c7..d27f12e 100644 --- a/lua/Trans/core/translate.lua +++ b/lua/Trans/core/translate.lua @@ -13,41 +13,6 @@ local function init_opts(opts) return opts end - -local function new_data(opts) - local mode = opts.mode - local str = opts.str - - - local strategy = Trans.conf.strategy[mode] - local data = { - str = str, - mode = mode, - result = {}, - } - - data.frontend = Trans.frontend[strategy.frontend].new() - - data.backend = {} - for i, name in ipairs(strategy.backend) do - data.backend[i] = Trans.backend[name] - end - - - if util.is_English(str) then - data.from = 'en' - data.to = 'zh' - else - data.from = 'zh' - data.to = 'en' - end - - -- FIXME : Check if the str is a word - data.is_word = true - - return data -end - local function set_result(data) -- HACK :Rewrite this function to support multi requests local frontend = data.frontend @@ -80,9 +45,10 @@ local function process(opts) end - - local data = new_data(opts) + local data = Trans.data.new(opts) set_result(data) + + local success = false for _, v in pairs(data.result) do if type(v) == "table" then diff --git a/lua/Trans/wrapper/window.lua b/lua/Trans/core/window.lua similarity index 92% rename from lua/Trans/wrapper/window.lua rename to lua/Trans/core/window.lua index 9fa6bff..f2fba72 100644 --- a/lua/Trans/wrapper/window.lua +++ b/lua/Trans/core/window.lua @@ -54,6 +54,11 @@ function window:width() return api.nvim_win_get_width(self.winid) end + +function window:highlight_line(linenr, highlight) + self.buffer:highlight_line(linenr, highlight, self.ns) +end + ---Get window height function window:height() return api.nvim_win_get_height(self.winid) @@ -149,7 +154,6 @@ end window.__index = window local default_opts = { - ns = api.nvim_create_namespace('TransHoverWin'), enter = false, winid = -1, win_opts = { @@ -174,20 +178,6 @@ end return window ---@class win_opts ---@field buf buf buffer for attached ---@field height integer ---@field width integer ---@field col integer ---@field row integer ---@field border string ---@field title string | nil | table ---@field relative string ---@field ns integer namespace for highlight ---@field zindex? integer ---@field enter? boolean cursor should [enter] window ---@field animation table window animation - -- local ns = opts.ns -- local buf = opts.buf -- local col = opts.col diff --git a/lua/Trans/frontend/hover/init.lua b/lua/Trans/frontend/hover/init.lua index b2fe01e..3910d53 100644 --- a/lua/Trans/frontend/hover/init.lua +++ b/lua/Trans/frontend/hover/init.lua @@ -21,6 +21,7 @@ local Trans = require('Trans') local M = Trans.metatable('frontend.hover', { + ns = vim.api.nvim_create_namespace('TransHoverWin'), queue = {}, }) M.__index = M @@ -29,7 +30,7 @@ M.__index = M ---@return hover new_instance function M.new() local new_instance = { - buffer = Trans.wrapper.buffer.new(), + buffer = Trans.buffer.new(), destroy_funcs = {}, } M.queue[#M.queue + 1] = new_instance @@ -75,6 +76,7 @@ function M:init_window(opts) local m_opts = self.opts + opts.ns = self.ns opts.buffer = self.buffer win_opts.col = 1 win_opts.row = 1 @@ -89,7 +91,7 @@ function M:init_window(opts) - self.window = Trans.wrapper.window.new(opts) + self.window = Trans.window.new(opts) return self.window end @@ -98,8 +100,8 @@ end ---@param name string @key to be checked ---@param timeout number @timeout for waiting function M:wait(tbl, name, timeout) - local msg = self.opts.fallback_message - local wid = msg:width() + local opts = self.opts + local width = opts.width local spinner = Trans.style.spinner[self.opts.spinner] local size = #spinner local cell = self.opts.icon.cell @@ -112,15 +114,16 @@ function M:wait(tbl, name, timeout) self:init_window({ win_opts = { height = 1, - width = wid, + width = width, } }) - local interval = math.floor(timeout / wid) + local interval = math.floor(timeout / width) local pause = Trans.util.pause - for i = 1, wid do + for i = 1, width do if tbl[name] ~= nil then break end self.buffer[1] = update_text(i) + self.buffer:add_highlight(1, 'MoreMsg') pause(interval) end @@ -141,7 +144,6 @@ function M:is_available() end return M - -- local error_msg = conf.icon.notfound .. ' 没有找到相关的翻译' -- local buffer = require('Trans.buffer')() diff --git a/lua/Trans/init.lua b/lua/Trans/init.lua index 880eb40..6a0b309 100644 --- a/lua/Trans/init.lua +++ b/lua/Trans/init.lua @@ -7,9 +7,7 @@ local function metatable(folder_name, origin) __index = function(tbl, key) local status, result = pcall(require, ('Trans.%s.%s'):format(folder_name, key)) - if not status then - error('fail to load: ' .. key .. '\n' .. result) - end + if not status then error('fail to load: ' .. key .. '\n' .. result) end tbl[key] = result return result @@ -17,12 +15,13 @@ local function metatable(folder_name, origin) }) end + local M = metatable('core') M.metatable = metatable M.style = metatable("style") -M.wrapper = metatable("wrapper") -M.cache = {} +M.cache = {} + return M diff --git a/lua/Trans/util/node.lua b/lua/Trans/util/node.lua index 38a6d15..9c0bf9c 100644 --- a/lua/Trans/util/node.lua +++ b/lua/Trans/util/node.lua @@ -25,12 +25,7 @@ local text_meta = { item_meta.__index = item_meta text_meta.__index = function(self, key) - local res = text_meta[key] - if res then - return res - elseif key == 1 then - return table.concat(self.strs, self.step) - end + return text_meta[key] or (key == 1 and table.concat(self.strs, self.step) or nil) end return {