From efec62d144ecb632b2a60b7e675e15fe48c3ff66 Mon Sep 17 00:00:00 2001 From: JuanZoran <1430359574@qq.com> Date: Wed, 15 Mar 2023 22:27:30 +0800 Subject: [PATCH] fix: return the latest instance instead of the first instance --- lua/Trans/backend/youdao.lua | 9 ++++++++- lua/Trans/core/backend.lua | 11 ++++++++++- lua/Trans/core/buffer.lua | 28 ++++++++++++++-------------- lua/Trans/core/conf.lua | 1 + lua/Trans/core/frontend.lua | 5 ++++- lua/Trans/frontend/hover/init.lua | 2 +- 6 files changed, 38 insertions(+), 18 deletions(-) diff --git a/lua/Trans/backend/youdao.lua b/lua/Trans/backend/youdao.lua index 5106249..8141436 100644 --- a/lua/Trans/backend/youdao.lua +++ b/lua/Trans/backend/youdao.lua @@ -1,5 +1,12 @@ --- local GET = require("Trans.util.curl").GET +---@class Youdao: TransBackend +---@field uri string api uri +---@field salt string +---@field app_id string +---@field app_passwd string +---@field disable boolean + +-- local GET = require("Trans.util.curl").GET -- return function(word) -- local isEn = word:isEn() -- local result = {} diff --git a/lua/Trans/core/backend.lua b/lua/Trans/core/backend.lua index ffd034a..ba882cf 100644 --- a/lua/Trans/core/backend.lua +++ b/lua/Trans/core/backend.lua @@ -20,11 +20,20 @@ if file then file:close() end +local all_name = {} +for name, opts in pairs(result) do + if opts.disable then + result[name] = nil + else + all_name[#all_name + 1] = name + end +end + ---@class Trans ---@field backend table return setmetatable({ - all_name = vim.tbl_keys(result), + all_name = Trans.conf.backend_order or all_name, }, { __index = function(self, name) ---@type TransBackend diff --git a/lua/Trans/core/buffer.lua b/lua/Trans/core/buffer.lua index 4eb5df9..5fc58bc 100644 --- a/lua/Trans/core/buffer.lua +++ b/lua/Trans/core/buffer.lua @@ -79,20 +79,20 @@ function buffer:lines(i, j) return api.nvim_buf_get_lines(self.bufnr, i, j, false) end ----Add Extmark to buffer ----@param linenr number line number should be set[one index] ----@param col_start number column start ----@param col_end number column end ----@param hl_group string highlight group ----@param ns number? highlight namespace -function buffer:add_extmark(linenr, col_start, col_end, hl_group, ns) - linenr = linenr and linenr - 1 or -1 - api.nvim_buf_set_extmark(self.bufnr, ns or -1, linenr, col_start, { - end_line = linenr, - end_col = col_end, - hl_group = hl_group, - }) -end +-- ---Add Extmark to buffer +-- ---@param linenr number line number should be set[one index] +-- ---@param col_start number column start +-- ---@param col_end number column end +-- ---@param hl_group string highlight group +-- ---@param ns number? highlight namespace +-- function buffer:add_extmark(linenr, col_start, col_end, hl_group, ns) +-- linenr = linenr and linenr - 1 or -1 +-- api.nvim_buf_set_extmark(self.bufnr, ns or -1, linenr, col_start, { +-- end_line = linenr, +-- end_col = col_end, +-- hl_group = hl_group, +-- }) +-- end ---Add highlight to buffer ---@param linenr number line number should be set[one index] diff --git a/lua/Trans/core/conf.lua b/lua/Trans/core/conf.lua index 3428ae7..2b7e37a 100644 --- a/lua/Trans/core/conf.lua +++ b/lua/Trans/core/conf.lua @@ -16,6 +16,7 @@ return { ---@type string the directory for database file and password file dir = os.getenv('HOME') .. '/.vim/dict', query = 'fallback', + -- backend_order = {}, ---@type 'default' | 'dracula' | 'tokyonight' global Trans theme [see lua/Trans/style/theme.lua] theme = 'default', -- default | tokyonight | dracula strategy = { diff --git a/lua/Trans/core/frontend.lua b/lua/Trans/core/frontend.lua index 920a134..169af0c 100644 --- a/lua/Trans/core/frontend.lua +++ b/lua/Trans/core/frontend.lua @@ -7,7 +7,10 @@ local frontend_opts = conf.frontend ---@param frontend TransFrontend local function set_frontend_keymap(frontend) local set = vim.keymap.set - local keymap_opts = { silent = true, expr = false, } + local keymap_opts = { + silent = true, + -- expr = true, + } for action, key in pairs(frontend.opts.keymaps) do set('n', key, function() diff --git a/lua/Trans/frontend/hover/init.lua b/lua/Trans/frontend/hover/init.lua index 038f0a1..beae0a3 100644 --- a/lua/Trans/frontend/hover/init.lua +++ b/lua/Trans/frontend/hover/init.lua @@ -34,7 +34,7 @@ end ---@return TransHover function M.get_active_instance() M.clear_dead_instance() - return M.queue[1] + return M.queue[#M.queue] end ---Clear dead instance