feat: improve repository framework

This commit is contained in:
JuanZoran
2023-01-05 16:24:50 +08:00
parent 409d85f615
commit 033622ed85
16 changed files with 357 additions and 177 deletions

17
lua/Trans/conf/base.lua Normal file
View File

@@ -0,0 +1,17 @@
local M = {}
local buf_opts = {
filetype = 'Trans'
}
local buf = vim.api.nvim_create_buf(false, true)
for k, v in pairs(buf_opts) do
vim.api.nvim_buf_set_options(buf, k, v)
end
M.buf = buf
M.group = vim.api.nvim_create_augroup('Trans', { clear = true })
return M

110
lua/Trans/conf/default.lua Normal file
View File

@@ -0,0 +1,110 @@
local M = {}
-- INFO :加载的规则 [LuaRule]
M.replace_rules = {
'order',
'Trans.+',
}
M.conf = {
style = {
window = {
input = 'float',
cursor = 'cursor',
select = 'cursor'
},
order = {
'title',
'tag',
'pos',
'exchange',
'zh',
'en',
},
conf = {
-- NOTE :可选的风格:['fixed', 'relative', .. TODO]
-- width 和 height说明
-- 大于1
-- 如果style为fixed , 则为固定的长宽
-- 如果style为relative , 则为最大长宽
-- 小于1:
-- 如果style为fixed , 则为默认
-- 如果style为relative , 则为无限制
-- 0 ~ 1:
-- 相对长宽
cursor = {
style = 'fixed',
border = 'rounded',
width = 30,
height = 30,
},
float = {
style = 'fixed',
border = 'rounded',
width = 0.8,
height = 0.9,
},
},
},
ui = {
highligh = {
TransWord = {
fg = '#7ee787',
bold = true,
},
TransPhonetic = {
fg = '#8b949e',
},
TransRef = {
fg = '#75beff',
bold = true,
},
TransTag = {
fg = '#e5c07b',
},
TransExchange = {
link = 'TransTag',
},
TransPos = {
link = 'TransTag',
},
TransZh = {
link = 'TransWord',
},
TransEn = {
fg = '#bc8cff',
},
},
icon = {
star = '',
isOxford = '',
notOxford = ''
},
display = {
phnoetic = true,
collins_star = true,
oxford = true,
-- TODO
-- history = false,
},
},
base = {
db_path = '$HOME/.vim/dict/ultimate.db',
auto_close = true,
lazy_load = false,
debug = {
enable = true,
type_check = true,
unknown_conf = true,
},
},
-- TODO add online translate engine
-- online_search = {
-- enable = false,
-- engine = {},
-- }
-- TODO register word
}
return M

1
lua/Trans/conf/init.lua Normal file
View File

@@ -0,0 +1 @@
return require("Trans.conf.loader").get_conf()

52
lua/Trans/conf/loader.lua Normal file
View File

@@ -0,0 +1,52 @@
---@diagnostic disable: unused-local, unused-function
local M = {}
local replace_rules = require("Trans.conf.default").replace_rules
local conf = require("Trans.conf.default").conf
local user_conf = require("Trans").conf
local type_check = require("Trans.util.debug").type_check
local is_loaded = false
local function need_extend(name)
type_check {
name = { name, 'string' }
}
for _, rule in ipairs(replace_rules) do
if name:match(rule) then
return false
end
end
return true
end
-- 加载用户自定义的配置
---@param t1 table
---@param t2 table
local function extend(t1, t2)
type_check {
t1 = { t1, 'table' },
t2 = { t2, 'table' },
}
for k, v in pairs(t2) do
if type(v) == 'table' and need_extend(k) then
extend(t1[k], v)
else
t1[k] = v
end
end
end
M.get_conf = function()
if not is_loaded then
M.load_conf()
end
return conf
end
M.load_conf = function()
-- loaded_conf = default_conf:extend(user_conf)
extend(conf, user_conf)
is_loaded = true
end
return M

52
lua/Trans/conf/window.lua Normal file
View File

@@ -0,0 +1,52 @@
local M = {}
local conf = require("Trans").conf.view
local get_float_opts = function(float_conf)
local columns = vim.o.columns
local height = vim.o.lines - vim.o.cmdheight - float_conf.top_offset
local width = math.floor(columns * float_conf.relative_width)
return {
relative = 'editor',
col = math.floor((columns - width) / 2), -- 两侧的宽度
row = float_conf.top_offset,
title = 'Trans',
title_pos = 'center',
style = 'minimal',
width = width,
height = height,
border = float_conf.border,
zindex = 50,
}
end
local get_cursor_opts = function(cursor_conf)
local opts = {
relative = 'cursor',
col = 2,
row = 2,
title = 'Trans',
title_pos = 'center',
style = 'minimal',
border = cursor_conf.border,
-- TODO keymap to convert style to Float
focusable = false,
zindex = 100,
}
if cursor_conf.style == 'fixed' then
opts.width = cursor_conf.width
opts.height = cursor_conf.height
elseif cursor_conf.style == 'relative' then
opts.width = (cursor_conf.width > 0 and conf.width < conf.max_width) and conf.width or conf.max_width
opts.height = (cursor_conf.height > 0 and conf.height < conf.max_height) and conf.height or conf.max_height
else
error('unknown style!')
end
return opts
end
M.get_float_opts = get_float_opts(conf.float)
M.cursor_opts = get_cursor_opts(conf.cursor)
return M