style: code format use lua_ls

This commit is contained in:
JuanZoran
2023-03-24 00:56:36 +08:00
parent 9f5a81249c
commit 82fa2a008a
22 changed files with 227 additions and 247 deletions

View File

@ -1,4 +1,4 @@
local Trans = require('Trans')
local Trans = require 'Trans'
---@class TransBackend
@ -18,12 +18,12 @@ local Trans = require('Trans')
local conf = Trans.conf
--- INFO :Parse online engine keys config file
local path = conf.dir .. '/Trans.json'
local file = io.open(path, "r")
local file = io.open(path, 'r')
local user_conf = {}
if file then
local content = file:read("*a")
local content = file:read '*a'
user_conf = vim.json.decode(content) or user_conf
file:close()
end
@ -56,5 +56,5 @@ return setmetatable({
self[name] = backend
return backend
end
end,
})

View File

@ -5,7 +5,7 @@
---@class TransConf
return {
---@type string the directory for database file and password file
dir = require('Trans').relative_path({ 'extra' }, true),
dir = require 'Trans'.relative_path({ 'extra' }, true),
debug = true,
---@type 'default' | 'dracula' | 'tokyonight' global Trans theme [see lua/Trans/style/theme.lua]
theme = 'default', -- default | tokyonight | dracula
@ -23,7 +23,7 @@ return {
default = {
query = 'fallback',
border = 'rounded',
title = vim.fn.has('nvim-0.9') == 1 and {
title = vim.fn.has 'nvim-0.9' == 1 and {
{ '', 'TransTitleRound' },
{ ' Trans', 'TransTitle' },
{ '', 'TransTitleRound' },
@ -49,7 +49,7 @@ return {
fallback_message = '{{notfound}} 翻译超时或没有找到相关的翻译',
auto_resize = true,
split_width = 60,
padding = 10, -- padding for hover window width
padding = 10, -- padding for hover window width
keymaps = {
pageup = '[[',
pagedown = ']]',
@ -84,9 +84,8 @@ return {
'translation',
'definition',
'web',
}
},
},
---@type table<string, string>
icon = {
-- or use emoji
list = '', -- ● | ○ | ◉ | ◯ | ◇ | ◆ | ▪ | ▫ | ⬤ | 🟢 | 🟡 | 🟣 | 🟤 | 🟠| 🟦 | 🟨 | 🟧 | 🟥 | 🟪 | 🟫 | 🟩 | 🟦

View File

@ -1,4 +1,4 @@
local Trans = require('Trans')
local Trans = require 'Trans'
---@class TransData
@ -21,8 +21,8 @@ function M.new(opts)
local mode = opts.mode
local str = opts.str
local strategy = Trans.conf.strategy[mode]
local strategy = Trans.conf.strategy[mode]
local data = setmetatable({
str = str,
mode = mode,

View File

@ -1,4 +1,4 @@
local Trans = require('Trans')
local Trans = require 'Trans'
local conf = Trans.conf
local frontend_opts = conf.frontend
@ -50,5 +50,5 @@ return setmetatable({}, {
set_frontend_keymap(frontend)
return frontend
end
end,
})

View File

@ -4,22 +4,22 @@ local function trans()
-- TODO : trim empty lines in the beginning and the end
for index, line in ipairs(lines) do
if line:match('%S+') then
if line:match '%S+' then
table.insert(paragraphs, { index - 1, line })
end
end
local Trans = require('Trans')
local Trans = require 'Trans'
local baidu = Trans.backend.baidu
---@cast baidu Baidu
for _, line in ipairs(paragraphs) do
local query = baidu.get_query({
local query = baidu.get_query {
str = line[2],
from = "en",
to = "zh",
})
from = 'en',
to = 'zh',
}
Trans.curl.get(baidu.uri, {
query = query,
@ -27,9 +27,9 @@ local function trans()
-- vim.print(output)
local body = output.body
local status, ret = pcall(vim.json.decode, body)
assert(status and ret, "Failed to parse json:" .. vim.inspect(body))
assert(status and ret, 'Failed to parse json:' .. vim.inspect(body))
local result = ret.trans_result
assert(result, "Failed to get result: " .. vim.inspect(ret))
assert(result, 'Failed to get result: ' .. vim.inspect(ret))
result = result[1]
@ -38,7 +38,7 @@ local function trans()
})
end
local ns = vim.api.nvim_create_namespace("Trans")
local ns = vim.api.nvim_create_namespace 'Trans'
for _, line in ipairs(paragraphs) do
local index = line[1]
local co = coroutine.running()
@ -60,12 +60,12 @@ local function trans()
Trans.util.main_loop(function()
vim.api.nvim_buf_set_extmark(0, ns, index, #line[2], {
virt_lines = {
{ { translation, "MoreMsg" } }
{ { translation, 'MoreMsg' } },
},
})
end)
print('done')
print 'done'
end
-- TODO :双语翻译
end

View File

@ -1,7 +1,7 @@
---@class Trans
---@field install fun() Download database and tts dependencies
return function()
local Trans = require('Trans')
local Trans = require 'Trans'
-- INFO :Check ultimate.db exists
local dir = Trans.conf.dir
local path = dir .. 'ultimate.db'
@ -26,7 +26,7 @@ return function()
local handle = function(output)
if output.exit == 0 and fn.filereadable(zip) then
if fn.executable('unzip') == 0 then
if fn.executable 'unzip' == 0 then
vim.notify('unzip not found, Please unzip ' .. zip .. 'manually', vim.log.ERROR)
return
end
@ -50,7 +50,7 @@ return function()
})
-- INFO : Install tts dependencies
if fn.has('linux') == 0 and fn.has('mac') == 0 then
os.execute('cd ./tts && npm install')
if fn.has 'linux' == 0 and fn.has 'mac' == 0 then
os.execute 'cd ./tts && npm install'
end
end

View File

@ -1,4 +1,4 @@
local Trans = require('Trans')
local Trans = require 'Trans'
local function set_strategy_opts(conf)
local all_backends = Trans.backend.all_name
@ -21,7 +21,7 @@ local function set_strategy_opts(conf)
setmetatable(conf.strategy, {
__index = function()
return default_strategy
end
end,
})
for _, strategy in pairs(conf.strategy) do

View File

@ -1,4 +1,4 @@
local Trans = require('Trans')
local Trans = require 'Trans'
local util = Trans.util
local function init_opts(opts)
@ -19,7 +19,7 @@ local function do_query(data, backend)
local method = backend.method
local formatter = backend.formatter
local query = backend.get_query(data)
local header = type(backend.header) == "function" and backend.header(data) or backend.header
local header = type(backend.header) == 'function' and backend.header(data) or backend.header
local function handle(output)
local status, body = pcall(vim.json.decode, output.body)

View File

@ -1,14 +1,14 @@
local fn, api = vim.fn, vim.api
---@class TransUtil
local M = require('Trans').metatable('util')
local M = require 'Trans'.metatable 'util'
---Get selected text
---@return string
function M.get_select()
local _start = fn.getpos("v")
local _end = fn.getpos('.')
local _start = fn.getpos 'v'
local _end = fn.getpos '.'
if _start[2] > _end[2] or (_start[3] > _end[3] and _start[2] == _end[2]) then
_start, _end = _end, _start
@ -42,19 +42,19 @@ end
function M.get_str(mode)
return ({
n = function()
return fn.expand('<cword>')
return fn.expand '<cword>'
end,
v = function()
api.nvim_input('<Esc>')
api.nvim_input '<Esc>'
return M.get_select()
end,
i = function()
return fn.input('需要翻译的字符串: ')
return fn.input '需要翻译的字符串: '
end,
V = function()
print('TODO')
end
})[mode]():match('^%s*(.-)%s*$')
print 'TODO'
end,
})[mode]():match '^%s*(.-)%s*$'
end
---Puase coroutine for {ms} milliseconds
@ -103,7 +103,6 @@ function M.display_width(lines)
return width
end
---Center node utility function
---@param node string -- TODO :Node
---@param win_width integer window width
@ -175,7 +174,7 @@ end
---@param str string
---@return boolean
function M.is_word(str)
return str:match('%w+') == str
return str:match '%w+' == str
end
---@param list any[]

View File

@ -1,6 +1,6 @@
local api = vim.api
---@class Trans
local Trans = require("Trans")
local Trans = require 'Trans'
---@class TransWindow
local window = {}
@ -61,10 +61,10 @@ function window:adjust_height(height)
local display_height = Trans.util.display_height(self.buffer:lines(), self:width())
height = height and math.min(display_height, height) or display_height
self:smooth_expand({
self:smooth_expand {
field = 'height',
to = height
})
to = height,
}
end
---Expand window [width | height] value
@ -82,7 +82,7 @@ function window:smooth_expand(opts)
local method = api['nvim_win_set_' .. field]
local wrap = self:option('wrap')
local wrap = self:option 'wrap'
local interval = self.animation.interval
for i = from + 1, to, (from < to and 1 or -1) do
self:set('wrap', false)
@ -99,14 +99,14 @@ function window:resize(opts)
if opts.height and self:height() ~= opts.height then
self:smooth_expand {
field = 'height',
to = opts.height
to = opts.height,
}
end
if opts.width and self:width() ~= opts.width then
self:smooth_expand {
field = 'width',
to = opts.width
to = opts.width,
}
end
end
@ -120,10 +120,10 @@ function window:try_close()
fold = 'height',
})[close_animation]
self:smooth_expand({
self:smooth_expand {
field = field,
to = 1,
})
}
end
api.nvim_win_close(self.winid, true)
@ -150,10 +150,10 @@ function window:open()
local to = win_opts[field]
win_opts[field] = 1
self.winid = api.nvim_open_win(self.buffer.bufnr, self.enter, win_opts)
self:smooth_expand({
self:smooth_expand {
field = field,
to = to,
})
}
else
self.winid = api.nvim_open_win(self.buffer.bufnr, self.enter, win_opts)
end