feat: add some bugs

This commit is contained in:
JuanZoran 2023-01-09 18:57:20 +08:00
parent 32cba43aa2
commit fef956e36d
12 changed files with 125 additions and 65 deletions

View File

@ -3,5 +3,10 @@
"Lua.diagnostics.disable": [
"empty-block",
"trailing-space"
],
"Lua.diagnostics.globals": [
"vim",
"user_conf",
"default_conf"
]
}

7
lua/Trans/api/READMD.md Normal file
View File

@ -0,0 +1,7 @@
# API说明
## 概述
- 翻译查询
- ``
- 字段处理
- 窗口显示

8
lua/Trans/api/init.lua Normal file
View File

@ -0,0 +1,8 @@
local M = {}
local query_warpper = require 'Trans.api.query'
M.query = query_warpper.query
return M

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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,

View File

@ -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('<cword>')
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

View File

@ -1,7 +1,9 @@
local M = {}
M.conf = {}
M.setup = require('Trans.conf.loader').load_conf
M.translate = require('Tran.wrapper.translate')
return M

View File

@ -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

View File

@ -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