From fef956e36df8b98c3ff485db4253d282f7e2b363 Mon Sep 17 00:00:00 2001 From: JuanZoran <1430359574@qq.com> Date: Mon, 9 Jan 2023 18:57:20 +0800 Subject: [PATCH] feat: add some bugs --- lua/.luarc.json | 5 ++ lua/Trans/api/READMD.md | 7 +++ lua/Trans/api/init.lua | 8 +++ .../{database/init.lua => api/query.lua} | 27 +++++----- lua/Trans/conf/default.lua | 3 +- lua/Trans/conf/loader.lua | 21 +++----- lua/Trans/core/READMD.md | 6 +-- lua/Trans/core/query.lua | 22 +++++++- lua/Trans/database/online.lua | 0 lua/Trans/init.lua | 6 ++- lua/Trans/test.lua | 33 ------------ lua/Trans/wrapper/translate.lua | 52 +++++++++++++++++++ 12 files changed, 125 insertions(+), 65 deletions(-) create mode 100644 lua/Trans/api/READMD.md create mode 100644 lua/Trans/api/init.lua rename lua/Trans/{database/init.lua => api/query.lua} (97%) delete mode 100644 lua/Trans/database/online.lua delete mode 100644 lua/Trans/test.lua create mode 100644 lua/Trans/wrapper/translate.lua diff --git a/lua/.luarc.json b/lua/.luarc.json index 97534e4..cb3ed2f 100644 --- a/lua/.luarc.json +++ b/lua/.luarc.json @@ -3,5 +3,10 @@ "Lua.diagnostics.disable": [ "empty-block", "trailing-space" + ], + "Lua.diagnostics.globals": [ + "vim", + "user_conf", + "default_conf" ] } \ No newline at end of file diff --git a/lua/Trans/api/READMD.md b/lua/Trans/api/READMD.md new file mode 100644 index 0000000..bdc0653 --- /dev/null +++ b/lua/Trans/api/READMD.md @@ -0,0 +1,7 @@ +# API说明 + +## 概述 +- 翻译查询 + - `` +- 字段处理 +- 窗口显示 diff --git a/lua/Trans/api/init.lua b/lua/Trans/api/init.lua new file mode 100644 index 0000000..6efad78 --- /dev/null +++ b/lua/Trans/api/init.lua @@ -0,0 +1,8 @@ +local M = {} + +local query_warpper = require 'Trans.api.query' + +M.query = query_warpper.query + + +return M diff --git a/lua/Trans/database/init.lua b/lua/Trans/api/query.lua similarity index 97% rename from lua/Trans/database/init.lua rename to lua/Trans/api/query.lua index 82c2a39..65ee90f 100644 --- a/lua/Trans/database/init.lua +++ b/lua/Trans/api/query.lua @@ -5,18 +5,6 @@ if not _ then end local type_check = require("Trans.util.debug").type_check -local query_field = { - 'word', - 'phonetic', - 'definition', - 'translation', - 'pos', - 'collins', - 'oxford', - 'tag', - 'exchange', -} - -- INFO : init database local path = require("Trans.conf.loader").loaded_conf.base.db_path local dict = db:open(path) @@ -31,6 +19,20 @@ vim.api.nvim_create_autocmd('VimLeavePre', { end }) + +local query_field = { + 'word', + 'phonetic', + 'definition', + 'translation', + 'pos', + 'collins', + 'oxford', + 'tag', + 'exchange', +} + +-- NOTE : local query M.query = function(arg) -- TODO : more opts type_check { @@ -45,4 +47,5 @@ M.query = function(arg) return res[1] end + return M diff --git a/lua/Trans/conf/default.lua b/lua/Trans/conf/default.lua index 7cc2649..2677f0b 100644 --- a/lua/Trans/conf/default.lua +++ b/lua/Trans/conf/default.lua @@ -4,7 +4,7 @@ M.conf = { style = { ui = { input = 'float', - cursor = 'cursor', + normal = 'cursor', select = 'cursor' }, window = { @@ -115,4 +115,5 @@ M.replace_rules = { 'ui.highlight.*', } + return M diff --git a/lua/Trans/conf/loader.lua b/lua/Trans/conf/loader.lua index 67a5e08..90f0803 100644 --- a/lua/Trans/conf/loader.lua +++ b/lua/Trans/conf/loader.lua @@ -13,6 +13,7 @@ if def and usr then end ]] + local plain_format = [[ default_conf.%s = user_conf.%s or default_conf.%s ]] @@ -36,21 +37,15 @@ local function pre_process() end end - - M.load_conf = function(conf) - if #M.loaded_conf == 0 then - user_conf = conf or {} - default_conf = require("Trans.conf.default").conf - pre_process() - M.loaded_conf = vim.tbl_deep_extend('force', default_conf, user_conf) - user_conf = nil - default_conf = nil - else - vim.notify('Configuration has been loaded...') - end + user_conf = conf or {} + default_conf = require("Trans.conf.default").conf + pre_process() + M.loaded_conf = vim.tbl_deep_extend('force', default_conf, user_conf) + user_conf = nil + default_conf = nil end -M.loaded_conf = {} +M.loaded_conf = nil return M diff --git a/lua/Trans/core/READMD.md b/lua/Trans/core/READMD.md index 47232dd..e86391d 100644 --- a/lua/Trans/core/READMD.md +++ b/lua/Trans/core/READMD.md @@ -79,14 +79,14 @@ ```lua vim.keymap.set('n', 'mi', function () require('Trans').translate({ - method = 'input', + method = 'input', -- 不填则自动判断mode获取查询的单词 engine = { -- 异步查询所有的引擎, 按照列表 'local', 'youdao', 'baidu' }, - -- view = 'cursor' - view = { + -- win = 'cursor' + win = { style = 'cursor', height = 50, width = 30, diff --git a/lua/Trans/core/query.lua b/lua/Trans/core/query.lua index e8f8a49..ed53437 100644 --- a/lua/Trans/core/query.lua +++ b/lua/Trans/core/query.lua @@ -15,6 +15,27 @@ local function get_select() end +local function get_word(method) + if not method then + local mode = vim.api.nvim_get_mode() + if mode == 'n' then + return vim.fn.expand('') + elseif mode == 'v' then + return get_select() + else + error('invalid mode') + end + end + + if method == 'input' then + return vim.fn.input('请输入您要查询的单词:') -- TODO Use Telescope with fuzzy finder + + -- TODO : other method + else + error('invalid method') + end +end + M.get_query_res = function(method) type_check { method = { method, 'string' }, @@ -25,7 +46,6 @@ M.get_query_res = function(method) elseif method == 'select' then word = get_select():match('%S+') elseif method == 'input' then - word = vim.fn.input('请输入您要查询的单词:') -- TODO Use Telescope with fuzzy finder else error('unknown method') end diff --git a/lua/Trans/database/online.lua b/lua/Trans/database/online.lua deleted file mode 100644 index e69de29..0000000 diff --git a/lua/Trans/init.lua b/lua/Trans/init.lua index 5b4853a..5090cb7 100644 --- a/lua/Trans/init.lua +++ b/lua/Trans/init.lua @@ -1,7 +1,9 @@ local M = {} -M.conf = {} - M.setup = require('Trans.conf.loader').load_conf +M.translate = require('Tran.wrapper.translate') + + + return M diff --git a/lua/Trans/test.lua b/lua/Trans/test.lua deleted file mode 100644 index 6430e03..0000000 --- a/lua/Trans/test.lua +++ /dev/null @@ -1,33 +0,0 @@ -local M = {} - -M.test = { - 'test1', - 'test1', - 'test1', - 'test1', -} - - -function M.tmp (start, stop) - if start > stop then - return - end - - local value = M.test[start] - start = start + 1 - return function () - - return start - end -end --- function M:tmp(index) --- end - -for v in M.tmp, 1, #M.test do - print(v) -end - --- for i,n in square,3,0 --- do --- print(i,n) --- end diff --git a/lua/Trans/wrapper/translate.lua b/lua/Trans/wrapper/translate.lua new file mode 100644 index 0000000..e1e9a8b --- /dev/null +++ b/lua/Trans/wrapper/translate.lua @@ -0,0 +1,52 @@ +local type_check = require("Trans.util.debug").type_check + +local window = require("Trans.conf.window") +-- Default conf +local core = require("Trasn.core") + +local function get_opts(opts) + local default_conf = { + method = vim.api.nvim_get_mode(), + engine = { + 'local', + -- TODO : other engine + }, + win = window.cursor_win, + } + + -- TODO :process win height and width + if type(opts.engine) == 'string' then + opts.engine = { opts.engine } + end + + if opts.win then + opts.win = window.process(opts.win) + end + return vim.tbl_extend('force', default_conf, opts) +end + +local function translate(opts) + type_check { + opts = { opts, 'table' } + } + opts = get_opts(opts or {}) + + + local field = core.query(opts) + + opts = { + field = field, + } + + local content = core.process(opts) + + opts = { + style = opts.style, + height = opts.height, + width = opts.width, + content = content, + } + core.show(opts) +end + +return translate