2023-01-14 01:57:12 +08:00

60 lines
1.8 KiB
Lua

local M = {}
local conf = require('Trans').conf
local api = require('Trans.api')
local win = require('Trans.core.window')
local handler = require('Trans.core.handler')
local c = require('Trans.core.content')
local function get_select()
local s_start = vim.fn.getpos("v")
local s_end = vim.fn.getpos(".")
local n_lines = math.abs(s_end[2] - s_start[2]) + 1
local lines = vim.api.nvim_buf_get_lines(0, s_start[2] - 1, s_end[2], false)
lines[1] = string.sub(lines[1], s_start[3], -1)
if n_lines == 1 then
lines[n_lines] = string.sub(lines[n_lines], 1, s_end[3] - s_start[3] + 1)
else
lines[n_lines] = string.sub(lines[n_lines], 1, s_end[3])
end
return table.concat(lines, '')
end
M.translate = function(method, view)
method = method or vim.api.nvim_get_mode().mode
view = view or conf.view[method]
local word
if method == 'input' then
---@diagnostic disable-next-line: param-type-mismatch
word = vim.fn.input('请输入您要查询的单词:') -- TODO Use Telescope with fuzzy finder
elseif method == 'n' then
word = vim.fn.expand('<cword>')
elseif method == 'v' then
word = get_select()
end
win.init(view)
local result = api.query('offline', word)
if result then
local content = c:new(win.width)
for i = 1, #conf.order do
handler[conf.order[i]](result, content)
end
win.draw(content)
else
local line = { '⚠️ 本地没有找到相应的结果' }
vim.api.nvim_buf_set_lines(win.bufnr, 0, -1, false, line)
local wid = vim.fn.strdisplaywidth(line[1])
vim.api.nvim_win_set_width(win.id, wid)
vim.api.nvim_win_set_height(win.id, #line)
win.auto_close()
end
end
return M