From ff36cfd549d30f2ab38a29e19518432dbe9086b7 Mon Sep 17 00:00:00 2001 From: JuanZoran <1430359574@qq.com> Date: Mon, 9 Jan 2023 23:20:56 +0800 Subject: [PATCH] feat: add more bugs --- lua/Trans/api/query.lua | 3 +- lua/Trans/component/content.lua | 2 +- lua/Trans/component/offline/Title.lua | 4 +- lua/Trans/conf/base.lua | 12 - lua/Trans/conf/default.lua | 19 +- lua/Trans/conf/loader.lua | 8 +- lua/Trans/core/backup.lua | 294 +++++++++++++++++++ lua/Trans/core/process.lua | 28 +- lua/Trans/core/query.lua | 51 ++-- lua/Trans/core/show_win.lua | 23 +- lua/Trans/core/translate.lua | 390 +++++++------------------- lua/Trans/init.lua | 9 +- lua/Trans/setup.lua | 27 +- lua/Trans/util/debug.lua | 9 +- lua/Trans/util/format.lua | 9 +- lua/Trans/util/test/test.lua | 7 +- lua/Trans/wrapper/translate.lua | 67 ----- 17 files changed, 497 insertions(+), 465 deletions(-) delete mode 100644 lua/Trans/conf/base.lua create mode 100644 lua/Trans/core/backup.lua delete mode 100644 lua/Trans/wrapper/translate.lua diff --git a/lua/Trans/api/query.lua b/lua/Trans/api/query.lua index 65ee90f..d2645af 100644 --- a/lua/Trans/api/query.lua +++ b/lua/Trans/api/query.lua @@ -11,7 +11,7 @@ local dict = db:open(path) -- INFO :Auto Close vim.api.nvim_create_autocmd('VimLeavePre', { - group = require("Trans.conf.base").autogroup, + group = require("Trans").augroup, callback = function() if db:isopen() then db:close() @@ -19,7 +19,6 @@ vim.api.nvim_create_autocmd('VimLeavePre', { end }) - local query_field = { 'word', 'phonetic', diff --git a/lua/Trans/component/content.lua b/lua/Trans/component/content.lua index 7102f78..3c6f907 100644 --- a/lua/Trans/component/content.lua +++ b/lua/Trans/component/content.lua @@ -64,7 +64,7 @@ end --- end ---@return table line ---@return table highlight -function M:lines() +function M:data() -- NOTE 返回格式化的行,如果需要高亮,则第二个参数返回高亮 local lines = {} local highlights = {} diff --git a/lua/Trans/component/offline/Title.lua b/lua/Trans/component/offline/Title.lua index e8455aa..d02eb69 100644 --- a/lua/Trans/component/offline/Title.lua +++ b/lua/Trans/component/offline/Title.lua @@ -1,7 +1,7 @@ local M = {} -local display = require("Trans.conf.loader").loaded.conf.ui.display -local icon = require("Trans.conf.loader").loaded.conf.ui.icon +local display = require("Trans.conf.loader").loaded_conf.ui.display +local icon = require("Trans.conf.loader").loaded_conf.ui.icon -- { -- collins = 3, diff --git a/lua/Trans/conf/base.lua b/lua/Trans/conf/base.lua deleted file mode 100644 index c247c60..0000000 --- a/lua/Trans/conf/base.lua +++ /dev/null @@ -1,12 +0,0 @@ -local M = {} - -local buf_opts = { -} - -local buf = vim.api.nvim_create_buf(false, true) - -M.buf = buf - -M.augroup = vim.api.nvim_create_augroup('Trans', { clear = true }) - -return M diff --git a/lua/Trans/conf/default.lua b/lua/Trans/conf/default.lua index 845e0cc..5e92c18 100644 --- a/lua/Trans/conf/default.lua +++ b/lua/Trans/conf/default.lua @@ -29,11 +29,11 @@ M.conf = { order = { offline = { 'Title', - 'Tag', - 'Pos', - 'Exchange', - 'Translation', - 'Definition', + -- 'Tag', + -- 'Pos', + -- 'Exchange', + -- 'Translation', + -- 'Definition', }, -- online = { -- -- TODO @@ -61,10 +61,10 @@ M.conf = { TransPos = { link = 'TransTag', }, - TransZh = { + TransTranslation = { link = 'TransWord', }, - TransEn = { + TransDefinition = { fg = '#bc8cff', }, }, @@ -84,11 +84,6 @@ M.conf = { base = { db_path = '$HOME/.vim/dict/ultimate.db', auto_close = true, - debug = { - enable = true, - type_check = true, - unknown_conf = true, - }, engine = { -- TODO 'local', diff --git a/lua/Trans/conf/loader.lua b/lua/Trans/conf/loader.lua index ef8f53f..3b3684f 100644 --- a/lua/Trans/conf/loader.lua +++ b/lua/Trans/conf/loader.lua @@ -45,15 +45,15 @@ M.load_conf = function(conf) pre_process() M.loaded_conf = vim.tbl_deep_extend('force', default_conf, user_conf) - local width = M.loaded_conf.style.window.cursor.float.width - local height = M.loaded_conf.style.window.cursor.float.height + local width = M.loaded_conf.style.window.float.width + local height = M.loaded_conf.style.window.float.height if width > 0 and width <= 1 then - M.loaded_conf.style.window.cursor.float.width = math.floor(width * vim.o.columns) + M.loaded_conf.style.window.float.width = math.floor(width * vim.o.columns) end if height > 0 and height <= 1 then - M.loaded_conf.style.window.cursor.float.height = math.floor(height * (vim.o.lines - vim.o.cmdheight)) + M.loaded_conf.style.window.float.height = math.floor(height * (vim.o.lines - vim.o.cmdheight)) end user_conf = nil diff --git a/lua/Trans/core/backup.lua b/lua/Trans/core/backup.lua new file mode 100644 index 0000000..fb8cd0e --- /dev/null +++ b/lua/Trans/core/backup.lua @@ -0,0 +1,294 @@ +local M = {} + + +local opt = { + method = 'select', + view = 'cursor', +} + +M.Translate = function(opts) + local res = get_query_res(opts.method) + -- TODO <++> +end + +-- M.Translate_online = function () +-- -- TOOD +-- end + + +-- local win = 0 +-- local line = 0 +-- local pos_info = {} +-- +-- local handler = {} +-- api.nvim_buf_set_option(buf, 'filetype', 'Trans') +-- +-- local function show_win(width, height) +-- end +-- +-- -- NOTE title +-- handler.title = function(text, query_res) +-- local title = ('%s [%s] %s %s'):format( +-- query_res.word, +-- query_res.phonetic, +-- (display.oxford and (query_res.oxford == 1 and icon.isOxford or icon.notOxford) or ''), +-- ((display.collins_star and query_res.collins) and string.rep(icon.star, query_res.collins) or '') +-- ) +-- table.insert(text, title) +-- +-- pos_info.title = {} +-- pos_info.title.word = #query_res.word +-- pos_info.title.phonetic = query_res.phonetic and #query_res.phonetic or 3 +-- pos_info.title.line = line +-- line = line + 1 +-- end +-- +-- -- NOTE tag +-- handler.tag = function(text, query_res) +-- if query_res.tag and #query_res.tag > 0 then +-- local tag = query_res.tag:gsub('zk', '中考'):gsub('gk', '高考'):gsub('ky', '考研'):gsub('cet4', '四级'): +-- gsub('cet6', '六级'):gsub('ielts', '雅思'):gsub('toefl', '托福'):gsub('gre', 'GRE') +-- +-- table.insert(text, '标签:') +-- table.insert(text, ' ' .. tag) +-- table.insert(text, '') +-- +-- pos_info.tag = line +-- line = line + 3 +-- end +-- end +-- +-- -- NOTE pos 词性 +-- handler.pos = function(text, query_res) +-- if query_res.pos and #query_res.pos > 0 then +-- table.insert(text, '词性:') +-- +-- local content = 0 +-- for v in vim.gsplit(query_res.pos, [[/]]) do +-- table.insert(text, string.format(' %s', v .. '%')) +-- content = content + 1 +-- end +-- +-- table.insert(text, '') +-- +-- pos_info.pos = {} +-- pos_info.pos.line = line +-- pos_info.pos.content = content +-- line = line + content + 2 +-- end +-- end +-- +-- -- NOTE exchange +-- handler.exchange = function(text, query_res) +-- if query_res.exchange and #query_res.exchange > 0 then +-- table.insert(text, '词形变化:') +-- +-- local exchange_map = { +-- p = '过去式', +-- d = '过去分词', +-- i = '现在分词', +-- r = '形容词比较级', +-- t = '形容词最高级', +-- s = '名词复数形式', +-- O = '词干', +-- ['3'] = '第三人称单数', +-- } +-- +-- local content = 0 +-- for v in vim.gsplit(query_res.exchange, [[/]]) do +-- table.insert(text, string.format(' %s: %s', exchange_map[v:sub(1, 1)], v:sub(3))) +-- content = content + 1 +-- -- FIXME: 中文字符与字母位宽不一致, 暂时无法对齐 +-- end +-- table.insert(text, '') +-- +-- pos_info.exchange = {} +-- pos_info.exchange.line = line +-- pos_info.exchange.content = content +-- line = line + content + 2 +-- end +-- end +-- +-- -- NOTE 中文翻译 +-- handler.zh = function(text, query_res) +-- if query_res.translation then +-- table.insert(text, '中文翻译:') +-- +-- local content = 0 +-- for v in vim.gsplit(query_res.translation, '\n') do +-- table.insert(text, ' ' .. v) +-- content = content + 1 +-- end +-- table.insert(text, '') +-- +-- pos_info.zh = {} +-- pos_info.zh.line = line +-- pos_info.zh.content = content +-- line = content + line + 2 +-- end +-- end +-- +-- -- NOTE 英文翻译 +-- handler.en = function(text, query_res) +-- if query_res.definition and #query_res.definition > 0 then +-- table.insert(text, '英文翻译:') +-- +-- local content = 0 +-- for v in vim.gsplit(query_res.definition, '\n') do +-- table.insert(text, ' ' .. v) +-- content = content + 1 +-- end +-- table.insert(text, '') +-- +-- pos_info.en = {} +-- pos_info.en.line = line +-- pos_info.en.content = content +-- line = line + content + 2 +-- end +-- end +-- +-- -- @return string array +-- local function get_text(query_res) +-- local text = {} +-- for _, v in ipairs(order) do +-- handler[v](text, query_res) +-- end +-- return text +-- end +-- +-- local function set_text(query_res) +-- local text = query_res and get_text(query_res) or { '没有找到相关定义' } +-- +-- api.nvim_buf_set_lines(buf, 0, -1, false, text) +-- local width = 0 +-- for _, v in ipairs(text) do +-- if #v > width then +-- width = v:len() +-- end +-- end +-- return width, #text +-- end +-- +-- local hl_handler = {} +-- +-- hl_handler.title = function() +-- api.nvim_buf_add_highlight(buf, -1, hl.word, pos_info.title.line, 0, pos_info.title.word) +-- api.nvim_buf_add_highlight(buf, -1, hl.phonetic, pos_info.title.line, pos_info.title.word + 5, +-- pos_info.title.word + 5 + pos_info.title.phonetic) +-- end +-- +-- hl_handler.tag = function() +-- if pos_info.tag then +-- api.nvim_buf_add_highlight(buf, -1, hl.ref, pos_info.tag, 0, -1) +-- api.nvim_buf_add_highlight(buf, -1, hl.tag, pos_info.tag + 1, 0, -1) +-- end +-- end +-- +-- hl_handler.pos = function() +-- if pos_info.pos then +-- api.nvim_buf_add_highlight(buf, -1, hl.ref, pos_info.pos.line, 0, -1) +-- for i = 1, pos_info.pos.content, 1 do +-- api.nvim_buf_add_highlight(buf, -1, hl.pos, pos_info.pos.line + i, 0, -1) +-- end +-- end +-- end +-- +-- hl_handler.exchange = function() +-- if pos_info.exchange then +-- api.nvim_buf_add_highlight(buf, -1, hl.ref, pos_info.exchange.line, 0, -1) +-- for i = 1, pos_info.exchange.content, 1 do +-- api.nvim_buf_add_highlight(buf, -1, hl.exchange, pos_info.exchange.line + i, 0, -1) +-- end +-- end +-- end +-- +-- hl_handler.zh = function() +-- api.nvim_buf_add_highlight(buf, -1, hl.ref, pos_info.zh.line, 0, -1) +-- for i = 1, pos_info.zh.content, 1 do +-- api.nvim_buf_add_highlight(buf, -1, hl.zh, pos_info.zh.line + i, 0, -1) +-- end +-- end +-- +-- hl_handler.en = function() +-- if pos_info.en then +-- api.nvim_buf_add_highlight(buf, -1, hl.ref, pos_info.en.line, 0, -1) +-- for i = 1, pos_info.en.content, 1 do +-- api.nvim_buf_add_highlight(buf, -1, hl.en, pos_info.en.line + i, 0, -1) +-- end +-- end +-- end +-- +-- +-- local function set_hl() +-- for _, v in ipairs(order) do +-- hl_handler[v]() +-- end +-- end +-- +-- local function clear_tmp_info() +-- pos_info = {} +-- line = 0 +-- end +-- +-- +-- function M.query(mode) +-- assert(buf > 0) +-- local word = '' +-- if mode == 'n' then +-- word = vim.fn.expand('') +-- elseif mode == 'v' then +-- word = get_visual_selection() +-- elseif mode == 'I' then +-- word = vim.fn.input('请输入您要查询的单词: ') +-- -- vim.ui.input({prompt = '请输入您要查询的单词: '}, function (input) +-- -- word = input +-- -- end) +-- else +-- error('mode argument is invalid') +-- end +-- +-- local res = require("Trans.database").query(word) +-- local width, height = set_text(res) +-- show_win(width, height) +-- if res then +-- set_hl() +-- clear_tmp_info() +-- end +-- +-- if auto_close then +-- api.nvim_create_autocmd( +-- { 'InsertEnter', 'CursorMoved', 'BufLeave', }, { +-- buffer = 0, +-- once = true, +-- callback = M.close_win, +-- }) +-- end +-- end +-- +-- function M.query_cursor() +-- M.query('n') +-- end +-- +-- function M.query_select() +-- M.query('v') +-- end +-- +-- function M.query_input() +-- M.query('I') +-- end +-- +-- function M.close_win() +-- if win > 0 then +-- api.nvim_win_close(win, true) +-- win = 0 +-- end +-- end +-- +-- -- function M.enter_win() +-- -- if api.nvim_win_is_valid(win) then +-- -- else +-- -- error('current win is not valid') +-- -- end +-- -- end +-- +-- return M diff --git a/lua/Trans/core/process.lua b/lua/Trans/core/process.lua index f5ff007..71f91fa 100644 --- a/lua/Trans/core/process.lua +++ b/lua/Trans/core/process.lua @@ -1,21 +1,35 @@ local type_check = require("Trans.util.debug").type_check -local format = require("Trans.util.format") --- NOTE : 将请求得到的字段进行处理 --- local offline_dir = debug.getinfo(1, "S").source:sub(2):match('.*Trans') .. '/component/offline' +local function format(items, interval) + +end + + local function process (opts) type_check { - opts = { opts, 'table' } + opts = { opts, 'table' }, + ['opts.field'] = { opts.field, 'table' }, + ['opts.order'] = { opts.order, 'table' }, + ['opts.win'] = { opts.win, 'table' }, + ['opts.engine'] = { opts.engine, 'table' }, } + local content = require('Trans.component.content'):new() for _, v in ipairs(opts.order) do - local component = format.format(opts.win_style, require("Trans.component" .. opts.engine .. v)) - content:insert(component) + local items = format(require("Trans.component." .. opts.engine .. '.' .. v), 4) + content:insert(items) end - return content:lines() + local lines, __highlight = content:data() + vim.api.nvim_buf_set_lines(opts.bufnr, 0, lines) + + for line, l_hl in ipairs(__highlight) do + for _, hl in ipairs(l_hl) do + vim.api.nvim_buf_add_highlight(opts.bufnr, line, hl.name, hl._start, hl._end) + end + end end diff --git a/lua/Trans/core/query.lua b/lua/Trans/core/query.lua index ed53437..dac837a 100644 --- a/lua/Trans/core/query.lua +++ b/lua/Trans/core/query.lua @@ -1,7 +1,5 @@ -local M = {} - local type_check = require("Trans.util.debug").type_check -local query = require("Trans.database").query +local query = require("Trans.api").query local function get_select() local s_start = vim.fn.getpos("'<") @@ -14,44 +12,29 @@ local function get_select() return word 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) +local query_wrapper = function(opts) type_check { - method = { method, 'string' }, + opts = { opts, 'table' }, + ['opts.method'] = { opts.method, 'string' }, } + local word = '' - if method == 'cursor' then + + if opts.method == 'input' then + word = vim.fn.input('请输入您要查询的单词:') -- TODO Use Telescope with fuzzy finder + + elseif opts.method == 'n' then word = vim.fn.expand('') - elseif method == 'select' then - word = get_select():match('%S+') - elseif method == 'input' then + + elseif opts.mehotd == 'v' then + word = get_select() + -- TODO : other method + else - error('unknown method') + error('invalid method' .. opts.method) end return query(word) end - -return M +return query_wrapper diff --git a/lua/Trans/core/show_win.lua b/lua/Trans/core/show_win.lua index 7c17fa3..be06838 100644 --- a/lua/Trans/core/show_win.lua +++ b/lua/Trans/core/show_win.lua @@ -1,10 +1,5 @@ local type_check = require("Trans.util.debug").type_check -local buf_opts = { - filetype = 'Trans', - modifiable = false, -} - -- local win_opts = { -- winhl = 'Normal:TransWinNormal, FloatBorder:TransWinBorder' -- } @@ -15,25 +10,22 @@ local function caculate_format(height, width) return row, col end + local function show_win(opts) type_check { opts = { opts, 'table' }, win = { opts.win, 'table' }, + border = { opts.border, 'string' }, highlight = { opts.highlight, 'table', true }, } - local bufnr = vim.api.nvim_create_buf(false, true) - for k, v in pairs(buf_opts) do - vim.api.nvim_buf_set_option(bufnr, k, v) - end - local is_float = opts.style == 'float' local win_opts = { relative = opts.style == 'float' and 'editor' or 'cursor', width = opts.width, height = opts.height, style = 'minimal', - border = 'rounded', + border = opts.border, title = 'Trans', title_pos = 'center', focusable = true, @@ -45,8 +37,17 @@ local function show_win(opts) win_opts.row = 2 win_opts.col = 2 end + local winid = vim.api.nvim_open_win(bufnr, is_float, win_opts) + vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, opts.lines) + + for line, l_hl in ipairs(opts.highlight) do + for i, hl in ipairs(l_hl) do + vim.api.nvim_buf_add_highlight(bufnr, line, hl.name, i, hl._start, hl._end) + end + end + return bufnr, winid end diff --git a/lua/Trans/core/translate.lua b/lua/Trans/core/translate.lua index fb8cd0e..af88e91 100644 --- a/lua/Trans/core/translate.lua +++ b/lua/Trans/core/translate.lua @@ -1,294 +1,110 @@ -local M = {} +-- Default conf +local conf = require("Trans.conf.loader").loaded_conf +local core = require("Trans.core") -local opt = { - method = 'select', - view = 'cursor', -} +local function get_opts(opts) + local default_conf = { + method = vim.api.nvim_get_mode().mode, + engine = { + 'local', + -- TODO : other engine + }, + win = { + style = 'cursor', + width = conf.style.window.cursor.width, + height = conf.style.window.cursor.height + }, + } -M.Translate = function(opts) - local res = get_query_res(opts.method) - -- TODO <++> + if type(opts.engine) == 'string' then + opts.engine = { opts.engine } + end + + if opts.win then + local width, height = opts.win.width, opts.win.height + if width and width > 0 and width <= 1 then + opts.win.width = math.floor(vim.o.columns * width) + end + + if height and height > 0 and height <= 1 then + opts.win.height = math.floor(vim.o.lines * opts.win.height) + end + end + + return vim.tbl_extend('force', default_conf, opts) end --- M.Translate_online = function () --- -- TOOD --- end +-- EXAMPLE : +-- require('Trans').translate({ +-- method = 'input', -- 不填则自动判断mode获取查询的单词 +-- engine = { -- 异步查询所有的引擎, 按照列表 +-- 'offline', +-- 'youdao', +-- 'baidu' +-- }, +-- -- win = 'cursor' +-- win = { +-- style = 'cursor', +-- height = 50, +-- width = 30, +-- } +-- }) --- local win = 0 --- local line = 0 --- local pos_info = {} --- --- local handler = {} --- api.nvim_buf_set_option(buf, 'filetype', 'Trans') --- --- local function show_win(width, height) --- end --- --- -- NOTE title --- handler.title = function(text, query_res) --- local title = ('%s [%s] %s %s'):format( --- query_res.word, --- query_res.phonetic, --- (display.oxford and (query_res.oxford == 1 and icon.isOxford or icon.notOxford) or ''), --- ((display.collins_star and query_res.collins) and string.rep(icon.star, query_res.collins) or '') --- ) --- table.insert(text, title) --- --- pos_info.title = {} --- pos_info.title.word = #query_res.word --- pos_info.title.phonetic = query_res.phonetic and #query_res.phonetic or 3 --- pos_info.title.line = line --- line = line + 1 --- end --- --- -- NOTE tag --- handler.tag = function(text, query_res) --- if query_res.tag and #query_res.tag > 0 then --- local tag = query_res.tag:gsub('zk', '中考'):gsub('gk', '高考'):gsub('ky', '考研'):gsub('cet4', '四级'): --- gsub('cet6', '六级'):gsub('ielts', '雅思'):gsub('toefl', '托福'):gsub('gre', 'GRE') --- --- table.insert(text, '标签:') --- table.insert(text, ' ' .. tag) --- table.insert(text, '') --- --- pos_info.tag = line --- line = line + 3 --- end --- end --- --- -- NOTE pos 词性 --- handler.pos = function(text, query_res) --- if query_res.pos and #query_res.pos > 0 then --- table.insert(text, '词性:') --- --- local content = 0 --- for v in vim.gsplit(query_res.pos, [[/]]) do --- table.insert(text, string.format(' %s', v .. '%')) --- content = content + 1 --- end --- --- table.insert(text, '') --- --- pos_info.pos = {} --- pos_info.pos.line = line --- pos_info.pos.content = content --- line = line + content + 2 --- end --- end --- --- -- NOTE exchange --- handler.exchange = function(text, query_res) --- if query_res.exchange and #query_res.exchange > 0 then --- table.insert(text, '词形变化:') --- --- local exchange_map = { --- p = '过去式', --- d = '过去分词', --- i = '现在分词', --- r = '形容词比较级', --- t = '形容词最高级', --- s = '名词复数形式', --- O = '词干', --- ['3'] = '第三人称单数', --- } --- --- local content = 0 --- for v in vim.gsplit(query_res.exchange, [[/]]) do --- table.insert(text, string.format(' %s: %s', exchange_map[v:sub(1, 1)], v:sub(3))) --- content = content + 1 --- -- FIXME: 中文字符与字母位宽不一致, 暂时无法对齐 --- end --- table.insert(text, '') --- --- pos_info.exchange = {} --- pos_info.exchange.line = line --- pos_info.exchange.content = content --- line = line + content + 2 --- end --- end --- --- -- NOTE 中文翻译 --- handler.zh = function(text, query_res) --- if query_res.translation then --- table.insert(text, '中文翻译:') --- --- local content = 0 --- for v in vim.gsplit(query_res.translation, '\n') do --- table.insert(text, ' ' .. v) --- content = content + 1 --- end --- table.insert(text, '') --- --- pos_info.zh = {} --- pos_info.zh.line = line --- pos_info.zh.content = content --- line = content + line + 2 --- end --- end --- --- -- NOTE 英文翻译 --- handler.en = function(text, query_res) --- if query_res.definition and #query_res.definition > 0 then --- table.insert(text, '英文翻译:') --- --- local content = 0 --- for v in vim.gsplit(query_res.definition, '\n') do --- table.insert(text, ' ' .. v) --- content = content + 1 --- end --- table.insert(text, '') --- --- pos_info.en = {} --- pos_info.en.line = line --- pos_info.en.content = content --- line = line + content + 2 --- end --- end --- --- -- @return string array --- local function get_text(query_res) --- local text = {} --- for _, v in ipairs(order) do --- handler[v](text, query_res) --- end --- return text --- end --- --- local function set_text(query_res) --- local text = query_res and get_text(query_res) or { '没有找到相关定义' } --- --- api.nvim_buf_set_lines(buf, 0, -1, false, text) --- local width = 0 --- for _, v in ipairs(text) do --- if #v > width then --- width = v:len() --- end --- end --- return width, #text --- end --- --- local hl_handler = {} --- --- hl_handler.title = function() --- api.nvim_buf_add_highlight(buf, -1, hl.word, pos_info.title.line, 0, pos_info.title.word) --- api.nvim_buf_add_highlight(buf, -1, hl.phonetic, pos_info.title.line, pos_info.title.word + 5, --- pos_info.title.word + 5 + pos_info.title.phonetic) --- end --- --- hl_handler.tag = function() --- if pos_info.tag then --- api.nvim_buf_add_highlight(buf, -1, hl.ref, pos_info.tag, 0, -1) --- api.nvim_buf_add_highlight(buf, -1, hl.tag, pos_info.tag + 1, 0, -1) --- end --- end --- --- hl_handler.pos = function() --- if pos_info.pos then --- api.nvim_buf_add_highlight(buf, -1, hl.ref, pos_info.pos.line, 0, -1) --- for i = 1, pos_info.pos.content, 1 do --- api.nvim_buf_add_highlight(buf, -1, hl.pos, pos_info.pos.line + i, 0, -1) --- end --- end --- end --- --- hl_handler.exchange = function() --- if pos_info.exchange then --- api.nvim_buf_add_highlight(buf, -1, hl.ref, pos_info.exchange.line, 0, -1) --- for i = 1, pos_info.exchange.content, 1 do --- api.nvim_buf_add_highlight(buf, -1, hl.exchange, pos_info.exchange.line + i, 0, -1) --- end --- end --- end --- --- hl_handler.zh = function() --- api.nvim_buf_add_highlight(buf, -1, hl.ref, pos_info.zh.line, 0, -1) --- for i = 1, pos_info.zh.content, 1 do --- api.nvim_buf_add_highlight(buf, -1, hl.zh, pos_info.zh.line + i, 0, -1) --- end --- end --- --- hl_handler.en = function() --- if pos_info.en then --- api.nvim_buf_add_highlight(buf, -1, hl.ref, pos_info.en.line, 0, -1) --- for i = 1, pos_info.en.content, 1 do --- api.nvim_buf_add_highlight(buf, -1, hl.en, pos_info.en.line + i, 0, -1) --- end --- end --- end --- --- --- local function set_hl() --- for _, v in ipairs(order) do --- hl_handler[v]() --- end --- end --- --- local function clear_tmp_info() --- pos_info = {} --- line = 0 --- end --- --- --- function M.query(mode) --- assert(buf > 0) --- local word = '' --- if mode == 'n' then --- word = vim.fn.expand('') --- elseif mode == 'v' then --- word = get_visual_selection() --- elseif mode == 'I' then --- word = vim.fn.input('请输入您要查询的单词: ') --- -- vim.ui.input({prompt = '请输入您要查询的单词: '}, function (input) --- -- word = input --- -- end) --- else --- error('mode argument is invalid') --- end --- --- local res = require("Trans.database").query(word) --- local width, height = set_text(res) --- show_win(width, height) --- if res then --- set_hl() --- clear_tmp_info() --- end --- --- if auto_close then --- api.nvim_create_autocmd( --- { 'InsertEnter', 'CursorMoved', 'BufLeave', }, { --- buffer = 0, --- once = true, --- callback = M.close_win, --- }) --- end --- end --- --- function M.query_cursor() --- M.query('n') --- end --- --- function M.query_select() --- M.query('v') --- end --- --- function M.query_input() --- M.query('I') --- end --- --- function M.close_win() --- if win > 0 then --- api.nvim_win_close(win, true) --- win = 0 --- end --- end --- --- -- function M.enter_win() --- -- if api.nvim_win_is_valid(win) then --- -- else --- -- error('current win is not valid') --- -- end --- -- end --- --- return M +local function create_win(opts) + local bufnr = vim.api.nvim_create_buf(false, true) + vim.api.nvim_buf_set_option(bufnr, 'filetype', 'Trans') + vim.api.nvim_buf_set_option(bufnr, 'modifiable', false) + + local is_float = opts.style == 'float' + local win_opts = { + relative = is_float and 'editor' or 'cursor', + width = opts.width, + height = opts.height, + style = 'minimal', + border = conf.style.window[opts.win.style].border, + title = 'Trans', + title_pos = 'center', + focusable = true, + zindex = 100, + } + + if is_float then + win_opts.row = math.floor((vim.o.lines - win_opts.height - vim.o.cmdheight) / 2) + win_opts.col = math.floor((vim.o.columns - win_opts.width) / 2) + else + win_opts.row = 2 + win_opts.col = 2 + end + + local winid = vim.api.nvim_open_win(bufnr, is_float, win_opts) + + return bufnr, winid +end + +local function translate(opts) + vim.validate { + opts = { opts, 'table', true } + } + + --- TODO : 异步请求 + -- NOTE : 这里只处理了本地数据库查询 + opts = get_opts(opts or {}) + local field = core.query(opts) + + local bufnr, winid = create_win(opts.win) + + local proc_opts = { + bufnr = bufnr, + winid = winid, + field = field, + order = conf.order['offline'], + engine = { 'offline' }, + win_opts = opts.win, + } + + core.process(proc_opts) +end + +return translate diff --git a/lua/Trans/init.lua b/lua/Trans/init.lua index 380e4fb..317547b 100644 --- a/lua/Trans/init.lua +++ b/lua/Trans/init.lua @@ -1,7 +1,12 @@ local M = {} -M.setup = require('Trans.conf.loader').load_conf +M.setup = function(opts) + require('Trans.conf.loader').load_conf(opts) + require("Trans.setup") + M.translate = require('Trans.core.translate') +end -M.translate = require('Tran.core.translate') +M.translate = nil +M.augroup = vim.api.nvim_create_augroup('Trans', {clear = true}) return M diff --git a/lua/Trans/setup.lua b/lua/Trans/setup.lua index 9bf3250..2e0acb8 100644 --- a/lua/Trans/setup.lua +++ b/lua/Trans/setup.lua @@ -2,17 +2,24 @@ if vim.fn.executable('sqlite3') ~= 1 then error('Please check out sqlite3') end -vim.api.nvim_create_user_command('TranslateCursorWord', require("Trans.core").query_cursor, { - desc = '翻译光标下的单词', -}) -vim.api.nvim_create_user_command('TranslateSelectWord', require("Trans.core").query_select, { - desc = '翻译选中的单词', -}) -vim.api.nvim_create_user_command('TranslateInputWord', require("Trans.core").query_input, { - desc = '翻译输入的单词', +vim.api.nvim_create_user_command('Translate', function () + require("Trans").translate() +end, { + desc = '翻译单词', }) -local highlights = require("Trans.conf").ui.highligh +vim.api.nvim_create_user_command('TranslateInput', function () + require("Trans").translate { + method = 'input', + } +end, {desc = '翻译单词'}) + +-- TODO +-- vim.api.nvim_create_user_command('TranslateHistory', require("Trans.core").query_input, { +-- desc = '翻译输入的单词', +-- }) + +local highlights = require("Trans.conf.loader").loaded_conf.ui.highlight for highlight, opt in pairs(highlights) do - vim.nvim_set_hl(0, highlight, opt) + vim.api.nvim_set_hl(0, highlight, opt) end diff --git a/lua/Trans/util/debug.lua b/lua/Trans/util/debug.lua index 58cb855..3088acd 100644 --- a/lua/Trans/util/debug.lua +++ b/lua/Trans/util/debug.lua @@ -1,17 +1,14 @@ local M = {} -- INFO : get loaded debug conf -local base = require("Trans.conf.loader").loaded_conf.base -local debug = require("Trans.conf.default").conf.base.debug -if base and base.debug then - debug = vim.tbl_extend('force', debug, base) -end +local type_check = true M.type_check = function (types) - if debug.enable and debug.type_check then + if type_check then vim.validate(types) end end + -- local function dedent(lines) -- local ind_size = math.huge -- for i, _ in ipairs(lines) do diff --git a/lua/Trans/util/format.lua b/lua/Trans/util/format.lua index e3c520d..bdc4bec 100644 --- a/lua/Trans/util/format.lua +++ b/lua/Trans/util/format.lua @@ -143,10 +143,10 @@ end ---@alias formatted_items table ---将组件格式化成相应的vim支持的lines格式 ----@param style string 窗口的风格 +---@param win_size string 窗口的宽度和高度 ---@param component table 需要格式化的字段 ---@return formatted_items[] lines -M.format = function(style, component) +M.format = function(win_width, component) type_check { style = { style, { 'string' } }, component = { component, { 'table' } }, @@ -161,19 +161,16 @@ M.format = function(style, component) length = length + width end - m_win_width = style_width[style] - m_indent + m_win_width = win_width m_fields = fields m_size = #m_fields m_tot_width = length m_item_width = item_size - m_interval = m_win_width > 50 and 6 or 4 return formatted_lines() end - - ---合并多个数组, 第一个数组将会被使用 ---@param ... string[] 需要被合并的数组 ---@return table res 合并后的数组 diff --git a/lua/Trans/util/test/test.lua b/lua/Trans/util/test/test.lua index fa4d1e9..7fa1b76 100644 --- a/lua/Trans/util/test/test.lua +++ b/lua/Trans/util/test/test.lua @@ -1,5 +1,8 @@ -local a = nil +local a = 'test' +local b = a -print((' '):rep(a)) +b = b .. 'test' + +print(a, b) diff --git a/lua/Trans/wrapper/translate.lua b/lua/Trans/wrapper/translate.lua deleted file mode 100644 index 4cbffd3..0000000 --- a/lua/Trans/wrapper/translate.lua +++ /dev/null @@ -1,67 +0,0 @@ -local type_check = require("Trans.util.debug").type_check --- Default conf -local conf = require("Trans.conf.loader").loaded_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 = { - style = 'cursor', - width = conf.window.cursor.width, - height = conf.window.cursor.height - }, - } - - if type(opts.engine) == 'string' then - opts.engine = { opts.engine } - end - - if opts.win then - local width, height = opts.win.width, opts.win.height - if width and width > 0 and width <= 1 then - opts.win.width = math.floor(vim.o.columns * width) - end - - if height and height > 0 and height <= 1 then - opts.win.height = math.floor(vim.o.lines * opts.win.height) - end - end - - return vim.tbl_extend('force', default_conf, opts) -end - -local function translate(opts) - type_check { - opts = { opts, 'table' } - } - - --- TODO : 异步请求 - -- NOTE : 这里只处理了本地的请求 - opts = get_opts(opts or {}) - local field = core.query(opts) - - local proc_opts = { - field = field, - order = conf.order['offline'], - engine = 'offline', - win_style = opts.win.style, - } - - local content, highlight = core.process(proc_opts) - - local win_opts = { - win = opts.win, - content = content, - highlight = highlight, - } - - core.show_win(win_opts) -end - -return translate