Compare commits
13 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
6a9d887db7 | ||
|
4d547a0397 | ||
|
b2851cffd8 | ||
|
c6ad825dac | ||
|
c6c5bf4f7c | ||
|
ce43dbd489 | ||
|
adfbe7f50c | ||
|
5355d9c97e | ||
|
2172d29f08 | ||
|
c35cfbb0f5 | ||
|
e723f4177f | ||
|
b62478cf2d | ||
|
acfde8e4f5 |
7
.deepsource.toml
Normal file
7
.deepsource.toml
Normal file
@ -0,0 +1,7 @@
|
||||
version = 1
|
||||
|
||||
[[analyzers]]
|
||||
name = "shell"
|
||||
|
||||
[[analyzers]]
|
||||
name = "javascript"
|
249
README.md
249
README.md
@ -2,83 +2,93 @@
|
||||
|
||||
<!--toc:start-->
|
||||
|
||||
- [Trans.nvim](#transnvim)
|
||||
- [特点](#特点)
|
||||
- [屏幕截图](#屏幕截图)
|
||||
- [演示](#演示)
|
||||
- [主题](#主题)
|
||||
- [安装](#安装)
|
||||
- [配置](#配置)
|
||||
- [快捷键绑定](#快捷键绑定)
|
||||
- [高亮组](#高亮组)
|
||||
- [声明](#声明)
|
||||
- [感谢](#感谢)
|
||||
- [贡献](#贡献)
|
||||
- [待办 (画大饼)](#待办-画大饼)
|
||||
<!--toc:end-->
|
||||
- [Trans.nvim](#transnvim)
|
||||
- [特点](#特点)
|
||||
- [屏幕截图](#屏幕截图)
|
||||
- [演示](#演示)
|
||||
- [主题](#主题)
|
||||
- [安装](#安装)
|
||||
- [配置](#配置)
|
||||
- [快捷键绑定](#快捷键绑定)
|
||||
- [高亮组](#高亮组)
|
||||
- [声明](#声明)
|
||||
- [感谢](#感谢)
|
||||
- [贡献](#贡献)
|
||||
- [待办 (画大饼)](#待办-画大饼)
|
||||
<!--toc:end-->
|
||||
|
||||
## 注意: 此分支已经不再打算维护, 新版本目前在expermental分支, 稳定后会设置成默认分支
|
||||
|
||||
## 特点
|
||||
|
||||
- 使用纯 lua 编写, 速度极快
|
||||
- 使用纯 lua 编写, 速度极快
|
||||
|
||||
> `Lazy.nvim`的记录: <font color="#0099FF">`➜ Trans.nvim 0.82ms`</font>
|
||||
> `Lazy.nvim`的记录: <font color="#0099FF">`➜ Trans.nvim 0.82ms`</font>
|
||||
|
||||
- **可以定义快捷键读英文单词**
|
||||
- **可以定义快捷键读英文单词**
|
||||
|
||||
> 见 wiki
|
||||
> 见 wiki
|
||||
|
||||
- 大部分功能可以自定义:
|
||||
- 高亮
|
||||
- 悬浮大小
|
||||
- 排版顺序
|
||||
- 弹窗大小
|
||||
- `舒服窗口动画`
|
||||
- etc (更多可以查看[配置](#配置))
|
||||
- **完全离线** 的单词翻译体验 (可能后面会支持在线翻译)
|
||||
- 支持显示:
|
||||
- 柯林斯星级
|
||||
- 牛津 3000 词汇
|
||||
- 中文翻译
|
||||
- 英文翻译 (不是英译中, 而是用英文解释)
|
||||
- 词根
|
||||
- etc
|
||||
- 舒服的排版和`动画`
|
||||
- 支持 `normal`和 `visual`模式
|
||||
> <font color='#FF9900'>不支持 visual-block mode</font>
|
||||
- 本地词库单词量: `430w`
|
||||
- 大部分功能可以自定义:
|
||||
|
||||
- 高亮
|
||||
- 悬浮大小
|
||||
- 排版顺序
|
||||
- 弹窗大小
|
||||
- `舒服窗口动画`
|
||||
- etc (更多可以查看[配置](#配置))
|
||||
|
||||
- **完全离线** 的单词翻译体验 (可能后面会支持在线翻译)
|
||||
|
||||
- 支持显示:
|
||||
|
||||
- 柯林斯星级
|
||||
- 牛津 3000 词汇
|
||||
- 中文翻译
|
||||
- 英文翻译 (不是英译中, 而是用英文解释)
|
||||
- 词根
|
||||
- etc
|
||||
|
||||
- 舒服的排版和`动画`
|
||||
|
||||
- 支持 `normal`和 `visual`模式
|
||||
|
||||
> <font color='#FF9900'>不支持 visual-block mode</font>
|
||||
|
||||
- 本地词库单词量: `430w`
|
||||
|
||||
## 屏幕截图
|
||||
|
||||
### 演示
|
||||
|
||||
https://user-images.githubusercontent.com/107862700/213752097-2eee026a-ddee-4531-bf80-ba2cbc8b44ef.mp4
|
||||
|
||||
> 视频演示的在线查询, 查询速度取决于你的网络状况
|
||||
> 可以打开音量查看自动读音
|
||||
|
||||
https://user-images.githubusercontent.com/107862700/215941500-3293c571-20a1-44e2-b202-77079f158ce9.mp4
|
||||
|
||||
https://user-images.githubusercontent.com/107862700/213752097-2eee026a-ddee-4531-bf80-ba2cbc8b44ef.mp4
|
||||
|
||||
### 主题
|
||||
|
||||
> 如果你有更美观或者更适合的配色, 欢迎提 PR
|
||||
> 主题配色在: `lua/Trans/theme.lua`文件中,你只需要添加你主题的表就可以了
|
||||
|
||||
- `default`
|
||||

|
||||
- `default`
|
||||

|
||||
|
||||
- `dracula`
|
||||

|
||||
- `dracula`
|
||||

|
||||
|
||||
- `tokyonight`
|
||||

|
||||
- `tokyonight`
|
||||

|
||||
|
||||
## 安装
|
||||
|
||||
_安装之前, 首先需要明确本插件的依赖:_
|
||||
|
||||
- [ECDICT](https://github.com/skywind3000/ECDICT): 插件所用的离线单词数据库
|
||||
- sqlite.lua: 操作数据库所用的库
|
||||
- sqlite3: 数据库
|
||||
- [ECDICT](https://github.com/skywind3000/ECDICT): 插件所用的离线单词数据库
|
||||
- sqlite.lua: 操作数据库所用的库
|
||||
- sqlite3: 数据库
|
||||
|
||||
<details>
|
||||
<summary>Packer.nvim</summary>
|
||||
@ -145,42 +155,47 @@ use {
|
||||
|
||||
<font color="#FF9900">**注意事项**: </font>
|
||||
|
||||
- `install.sh`
|
||||
- **如果插件无法正常工作, 请运行**`:check Trans`, 查看插件是否安装正确并且处于正常工作环境
|
||||
|
||||
- 使用了 `wget`下载词库, 安装请确保你的环境变量中存在 wget
|
||||
- install.sh 下载后会自动将词库解压, 并移动到 `$HOME/.vim/dict`文件夹下
|
||||
- 目前仅在 `Ubuntu22.04`的环境下测试通过
|
||||
> 如果上述条件不符合, 请删掉 `run = 'install.sh'`部分, 考虑手动安装词库
|
||||
> 如果上述条件满足, 仍出现问题, 欢迎在 issue 里向我反馈,我会及时尝试解决
|
||||
- `install.sh`
|
||||
|
||||
- 下载词典的过程中, 需要能够 `流畅的访问github下载`
|
||||
- 使用了 `wget`下载词库, 安装请确保你的环境变量中存在 wget
|
||||
|
||||
> 词库文件压缩包大小为: **281M**
|
||||
> 解压缩后的大小大概为: 1.2G
|
||||
- install.sh 下载后会自动将词库解压, 并移动到 `$HOME/.vim/dict`文件夹下
|
||||
|
||||
- 安装后如果不能正常运行, 请尝试检查一下问题:
|
||||
- 目前仅在 `Ubuntu22.04`的环境下测试通过
|
||||
> 如果上述条件不符合, 请删掉 `run = 'install.sh'`部分, 考虑手动安装词库
|
||||
> 如果上述条件满足, 仍出现问题, 欢迎在 issue 里向我反馈,我会及时尝试解决
|
||||
|
||||
- 本机是否已经安装了 `sqlite3`
|
||||
> Linux 下安装:
|
||||
> `sudo pacman -S sqlite # Arch`
|
||||
> `sudo apt-get install sqlite3 libsqlite3-dev # Ubuntu`
|
||||
- 下载词典的过程中, 需要能够 `流畅的访问github下载`
|
||||
|
||||
> 后续会增加 `healthcheck` 进行检查
|
||||
> 词库文件压缩包大小为: **281M**
|
||||
> 解压缩后的大小大概为: **1.2G**
|
||||
|
||||
- **`auto_play`** 使用步骤:
|
||||
- 安装后如果不能正常运行, 请尝试检查一下问题:
|
||||
|
||||
> linux 只需要安装`festival`
|
||||
> sudo apt-get install festival festvox-kallpc16k
|
||||
> **_如果你想要设置音色,发音可以访问:_** [Festival 官方](https://www.cstr.ed.ac.uk/projects/festival/morevoices.html)
|
||||
> 可以选择英音、美音、男声、女声
|
||||
- 本机是否已经安装了 `sqlite3`
|
||||
|
||||
> 其他操作系统
|
||||
> Linux 下安装:
|
||||
> `sudo pacman -S sqlite # Arch`
|
||||
> `sudo apt-get install sqlite3 libsqlite3-dev # Ubuntu`
|
||||
|
||||
- 需要确保安装了`nodejs`
|
||||
- 进入插件的`tts`目录运行`npm install`
|
||||
> 如果`install.sh`运行正常则自动安装,如果安装失败,请尝试手动安装
|
||||
- **`auto_play`** 使用步骤:
|
||||
|
||||
- `title`的配置,只对`neovim 0.9`版本有效
|
||||
> linux 只需要安装`festival`
|
||||
> sudo apt-get install festival festvox-kallpc16k
|
||||
> **_如果你想要设置音色,发音可以访问:_** [Festival 官方](https://www.cstr.ed.ac.uk/projects/festival/morevoices.html)
|
||||
> 可以选择英音、美音、男声、女声
|
||||
|
||||
> mac 系统使用`say` (感谢[@happysmile12321](https://github.com/happysmile12321) )
|
||||
|
||||
|
||||
> 其他操作系统
|
||||
- 需要确保安装了`nodejs`
|
||||
- 进入插件的`tts`目录运行`npm install`
|
||||
> 如果`install.sh`运行正常则自动安装,如果安装失败,请尝试手动安装
|
||||
|
||||
- `title`的配置,只对`neovim 0.9`版本有效
|
||||
|
||||
<details>
|
||||
<summary>Festival配置(仅针对linux用户)</summary>
|
||||
@ -188,45 +203,45 @@ use {
|
||||
- 全局配置: `/usr/share/festival/siteinit.scm`
|
||||
- 用户配置: `~/.festivalrc`
|
||||
|
||||
- 更改声音
|
||||
- 更改声音
|
||||
|
||||
- 在 festival 的 voices 文件内建立自己的文件夹
|
||||
- 在 festival 的 voices 文件内建立自己的文件夹
|
||||
|
||||
> 一般其默认配置目录在`/usr/share/festival/voices`
|
||||
> 一般其默认配置目录在`/usr/share/festival/voices`
|
||||
|
||||
示例:
|
||||
示例:
|
||||
|
||||
> `sudo mkdir /usr/share/festival/voices/my_voices`
|
||||
> `sudo mkdir /usr/share/festival/voices/my_voices`
|
||||
|
||||
- 下载想要的 voices 文件并解压
|
||||
- 下载想要的 voices 文件并解压
|
||||
|
||||
> 可能需要
|
||||
|
||||
- 试听[在这里](https://www.cstr.ed.ac.uk/projects/festival/morevoices.html))
|
||||
- 下载[在这里](http://festvox.org/packed/festival/2.5/voices/))
|
||||
> 假设下载的文件在`Downloads`文件夹, 下载的文件为:`festvox_cmu_us_aew_cg.tar.gz`
|
||||
|
||||
示例:
|
||||
|
||||
> `cd ~/Downloads && tar -xf festvox_cmu_us_aew_cg.tar.gz`
|
||||
|
||||
- 将音频文件拷贝到 festival 文件夹
|
||||
示例:
|
||||
|
||||
> `sudo cp -r festival/lib/voices/us/cmu_us_aew_cg/ /usr/share/festival/voices/my_voices/`
|
||||
|
||||
- 在配置文件中设置默认的声音
|
||||
示例:
|
||||
|
||||
> 加入`(set! voice_default voice_cmu_indic_hin_ab_cg)`到配置文件
|
||||
|
||||
- 安装完成
|
||||
|
||||
- 相关说明网站
|
||||
> 可能需要
|
||||
|
||||
- 试听[在这里](https://www.cstr.ed.ac.uk/projects/festival/morevoices.html))
|
||||
- 下载[在这里](http://festvox.org/packed/festival/2.5/voices/))
|
||||
> 假设下载的文件在`Downloads`文件夹, 下载的文件为:`festvox_cmu_us_aew_cg.tar.gz`
|
||||
|
||||
示例:
|
||||
|
||||
> `cd ~/Downloads && tar -xf festvox_cmu_us_aew_cg.tar.gz`
|
||||
|
||||
- 将音频文件拷贝到 festival 文件夹
|
||||
示例:
|
||||
|
||||
> `sudo cp -r festival/lib/voices/us/cmu_us_aew_cg/ /usr/share/festival/voices/my_voices/`
|
||||
|
||||
- 在配置文件中设置默认的声音
|
||||
示例:
|
||||
|
||||
> 加入`(set! voice_default voice_cmu_indic_hin_ab_cg)`到配置文件
|
||||
|
||||
- 安装完成
|
||||
|
||||
- 相关说明网站
|
||||
> 可能需要
|
||||
- [wiki](https://archlinux.org/packages/community/any/festival-us/) 查看更多详细配置
|
||||
- [官方网站](http://festvox.org/dbs/index.html)
|
||||
- [用户手册](http://www.festvox.org/docs/manual-2.4.0/festival_toc.html)
|
||||
- [wiki](https://archlinux.org/packages/community/any/festival-us/) 查看更多详细配置
|
||||
- [官方网站](http://festvox.org/dbs/index.html)
|
||||
- [用户手册](http://www.festvox.org/docs/manual-2.4.0/festival_toc.html)
|
||||
|
||||
</details>
|
||||
|
||||
@ -407,13 +422,13 @@ vim.keymap.set('n', 'mi', '<Cmd>TranslateInput<CR>')
|
||||
|
||||
## 声明
|
||||
|
||||
- 本插件词典基于[ECDICT](https://github.com/skywind3000/ECDICT)
|
||||
- 本插件词典基于[ECDICT](https://github.com/skywind3000/ECDICT)
|
||||
|
||||
## 感谢
|
||||
|
||||
- [ECDICT](https://github.com/skywind3000/ECDICT) 本地词典的提供
|
||||
- [sqlite.lua](https://github.com/kharji/sqlite.lua) 数据库访问
|
||||
- [T.vim](https://github.com/sicong-li/T.vim) 灵感来源
|
||||
- [ECDICT](https://github.com/skywind3000/ECDICT) 本地词典的提供
|
||||
- [sqlite.lua](https://github.com/kharji/sqlite.lua) 数据库访问
|
||||
- [T.vim](https://github.com/sicong-li/T.vim) 灵感来源
|
||||
|
||||
## 贡献
|
||||
|
||||
@ -422,11 +437,11 @@ vim.keymap.set('n', 'mi', '<Cmd>TranslateInput<CR>')
|
||||
|
||||
## 待办 (画大饼)
|
||||
|
||||
- [x] 多风格样式查询
|
||||
- [x] 重新录制屏幕截图示例
|
||||
- [x] 快捷键定义
|
||||
- [x] 自动读音
|
||||
- [ ] 变量命名的支持
|
||||
- [ ] 历史查询结果保存
|
||||
- [ ] 在线多引擎异步查询
|
||||
- [ ] `句子翻译` | `中翻英` 的支持
|
||||
- [x] 多风格样式查询
|
||||
- [x] 重新录制屏幕截图示例
|
||||
- [x] 快捷键定义
|
||||
- [x] 自动读音
|
||||
- [ ] 变量命名的支持
|
||||
- [ ] 历史查询结果保存
|
||||
- [ ] 在线多引擎异步查询
|
||||
- [ ] `句子翻译` | `中翻英` 的支持
|
||||
|
@ -142,10 +142,8 @@ buffer.__index = function(self, key)
|
||||
local res = buffer[key]
|
||||
if res then
|
||||
return res
|
||||
|
||||
elseif type(key) == 'number' then
|
||||
return fn.getbufoneline(self.bufnr, key)
|
||||
|
||||
else
|
||||
error('invalid key' .. key)
|
||||
end
|
||||
@ -161,7 +159,7 @@ end
|
||||
---@return buf
|
||||
return function()
|
||||
return setmetatable({
|
||||
bufnr = -1,
|
||||
size = 0,
|
||||
}, buffer)
|
||||
bufnr = -1,
|
||||
size = 0,
|
||||
}, buffer)
|
||||
end
|
||||
|
63
lua/Trans/health.lua
Normal file
63
lua/Trans/health.lua
Normal file
@ -0,0 +1,63 @@
|
||||
local M = {}
|
||||
|
||||
M.check = function()
|
||||
local health = vim.health
|
||||
local ok = health.report_ok
|
||||
local warn = health.report_warn
|
||||
local error = health.report_error
|
||||
|
||||
|
||||
local has = vim.fn.has
|
||||
local executable = vim.fn.executable
|
||||
|
||||
-- INFO :Check neovim version
|
||||
if has('nvim-0.9') == 1 then
|
||||
ok [[
|
||||
[PASS]: you have Trans.nvim with full features in neovim-nightly
|
||||
]]
|
||||
else
|
||||
warn [[
|
||||
[WARN]: Trans Title requires Neovim 0.9 or newer
|
||||
See neovim-nightly: https://github.com/neovim/neovim/releases/tag/nightly
|
||||
]]
|
||||
end
|
||||
|
||||
-- INFO :Check Sqlite
|
||||
local has_sqlite = pcall(require, 'sqlite')
|
||||
if has_sqlite then
|
||||
ok [[
|
||||
[PASS]: Dependency sqlite.lua is installed
|
||||
]]
|
||||
else
|
||||
error [[
|
||||
[ERROR]: Dependency sqlite.lua can't work correctly
|
||||
Please Read the doc in github carefully
|
||||
]]
|
||||
end
|
||||
|
||||
if executable('sqlite3') then
|
||||
ok [[
|
||||
[PASS]: Dependency sqlite3 found
|
||||
]]
|
||||
else
|
||||
error [[
|
||||
[ERROR]: Dependency sqlite3 not found
|
||||
]]
|
||||
end
|
||||
|
||||
|
||||
-- INFO :Check stardict
|
||||
local db_path = vim.fn.expand(require('Trans').conf.db_path)
|
||||
if vim.fn.filereadable(db_path) == 1 then
|
||||
ok [[
|
||||
[PASS]: Stardict database found
|
||||
]]
|
||||
else
|
||||
error [[
|
||||
[PASS]: Stardict database not found
|
||||
Please check the doc in github
|
||||
]]
|
||||
end
|
||||
end
|
||||
|
||||
return M
|
@ -1,14 +1,14 @@
|
||||
local M = {}
|
||||
local api, fn = vim.api, vim.fn
|
||||
|
||||
if fn.executable('sqlite3') ~= 1 then
|
||||
error('Please check out sqlite3')
|
||||
if fn.executable 'sqlite3' ~= 1 then
|
||||
error 'Please check out sqlite3'
|
||||
end
|
||||
|
||||
local win_title = fn.has('nvim-0.9') == 1 and {
|
||||
{ '', 'TransTitleRound' },
|
||||
local win_title = fn.has 'nvim-0.9' == 1 and {
|
||||
{ '', 'TransTitleRound' },
|
||||
{ ' Trans', 'TransTitle' },
|
||||
{ '', 'TransTitleRound' },
|
||||
{ '', 'TransTitleRound' },
|
||||
} or nil
|
||||
|
||||
-- local title = {
|
||||
@ -24,23 +24,27 @@ string.width = api.nvim_strwidth
|
||||
string.isEn = function(self)
|
||||
local char = { self:byte(1, -1) }
|
||||
for i = 1, #self do
|
||||
if char[i] > 127 then
|
||||
if char[i] > 128 then
|
||||
return false
|
||||
end
|
||||
end
|
||||
return true
|
||||
end
|
||||
|
||||
string.play = fn.has('linux') == 1 and function(self)
|
||||
string.play = fn.has 'linux' == 1 and function(self)
|
||||
local cmd = ([[echo "%s" | festival --tts]]):format(self)
|
||||
fn.jobstart(cmd)
|
||||
end or fn.has 'mac' == 1 and function(self)
|
||||
local cmd = ([[say "%s"]]):format(self)
|
||||
fn.jobstart(cmd)
|
||||
end or function(self)
|
||||
local seperator = fn.has('unix') and '/' or '\\'
|
||||
local file = debug.getinfo(1, "S").source:sub(2):match('(.*)lua') .. seperator .. 'tts' .. seperator .. 'say.js'
|
||||
local seperator = fn.has 'unix' and '/' or '\\'
|
||||
local file = debug.getinfo(1, 'S').source:sub(2):match '(.*)lua' .. seperator .. 'tts' .. seperator .. 'say.js'
|
||||
fn.jobstart('node ' .. file .. ' ' .. self)
|
||||
end
|
||||
|
||||
M.conf = {
|
||||
warning = true,
|
||||
view = {
|
||||
i = 'float',
|
||||
n = 'hover',
|
||||
@ -132,16 +136,8 @@ M.conf = {
|
||||
-- appPasswd = '',
|
||||
-- },
|
||||
},
|
||||
-- TODO :
|
||||
-- register word
|
||||
-- history = {
|
||||
-- -- TOOD
|
||||
-- }
|
||||
|
||||
-- TODO :add online translate engine
|
||||
}
|
||||
|
||||
local times = 0
|
||||
M.setup = function(opts)
|
||||
if opts then
|
||||
M.conf = vim.tbl_deep_extend('force', M.conf, opts)
|
||||
@ -162,34 +158,27 @@ M.setup = function(opts)
|
||||
engines[i] = k
|
||||
i = i + 1
|
||||
end
|
||||
|
||||
conf.engines = engines
|
||||
times = times + 1
|
||||
if times == 1 then
|
||||
---@format disable
|
||||
local new_command = api.nvim_create_user_command
|
||||
new_command('Translate' , function() M.translate() end, { desc = ' 单词翻译',})
|
||||
new_command('TranslateInput' , function() M.translate('i') end, { desc = ' 搜索翻译',})
|
||||
new_command('TransPlay' , function()
|
||||
local word = M.get_word(api.nvim_get_mode().mode)
|
||||
if word ~= '' and word:isEn() then
|
||||
word:play()
|
||||
end
|
||||
end, { desc = ' 自动发音' })
|
||||
|
||||
local set_hl = api.nvim_set_hl
|
||||
local hls = require 'Trans.ui.theme'[conf.theme]
|
||||
for hl, opt in pairs(hls) do
|
||||
set_hl(0, hl, opt)
|
||||
end
|
||||
|
||||
|
||||
local set_hl = api.nvim_set_hl
|
||||
local hls = require('Trans.ui.theme')[conf.theme]
|
||||
for hl, opt in pairs(hls) do
|
||||
set_hl(0, hl, opt)
|
||||
end
|
||||
---@format enable
|
||||
if M.conf.warning then
|
||||
vim.notify([[
|
||||
新版本v2已经发布, 见:
|
||||
https://github.com/JuanZoran/Trans.nvim
|
||||
该分支已经不再维护, 请尝试迁移到新分支
|
||||
]], vim.log.levels.WARN)
|
||||
end
|
||||
end
|
||||
|
||||
local function 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
|
||||
@ -206,7 +195,6 @@ local function get_select()
|
||||
|
||||
if s_row == e_row then
|
||||
return line:sub(s_col, e_col)
|
||||
|
||||
else
|
||||
local lines = fn.getline(s_row, e_row)
|
||||
local i = #lines
|
||||
@ -219,16 +207,14 @@ end
|
||||
M.get_word = function(mode)
|
||||
local word
|
||||
if mode == 'n' then
|
||||
word = fn.expand('<cword>')
|
||||
|
||||
word = fn.expand '<cword>'
|
||||
elseif mode == 'v' then
|
||||
api.nvim_input('<ESC>')
|
||||
api.nvim_input '<ESC>'
|
||||
word = get_select()
|
||||
|
||||
elseif mode == 'i' then
|
||||
-- TODO Use Telescope with fuzzy finder
|
||||
---@diagnostic disable-next-line: param-type-mismatch
|
||||
word = fn.input('请输入需要查询的单词:')
|
||||
word = fn.input '请输入需要查询的单词:'
|
||||
else
|
||||
error('invalid mode: ' .. mode)
|
||||
end
|
||||
@ -237,9 +223,17 @@ end
|
||||
|
||||
|
||||
M.translate = function(mode, view)
|
||||
if M.conf.warning then
|
||||
vim.notify([[
|
||||
新版本已经发布, 见:
|
||||
https://github.com/JuanZoran/Trans.nvim
|
||||
该分支已经不再维护, 请尝试迁移到新分支
|
||||
]], vim.log.levels.WARN)
|
||||
end
|
||||
|
||||
vim.validate {
|
||||
mode = { mode, 's', true },
|
||||
view = { view, 's', true }
|
||||
view = { view, 's', true },
|
||||
}
|
||||
|
||||
mode = mode or api.nvim_get_mode().mode
|
||||
@ -253,6 +247,6 @@ M.translate = function(mode, view)
|
||||
end
|
||||
end
|
||||
|
||||
M.ns = api.nvim_create_namespace('Trans')
|
||||
M.ns = api.nvim_create_namespace 'Trans'
|
||||
|
||||
return M
|
||||
|
@ -82,6 +82,7 @@ local function process()
|
||||
end
|
||||
|
||||
return function(word)
|
||||
buffer:init()
|
||||
-- TODO :online query
|
||||
-- local float = conf.float
|
||||
vim.notify([[
|
||||
|
@ -1,9 +1,10 @@
|
||||
local api = vim.api
|
||||
local conf = require('Trans').conf
|
||||
local hover = conf.hover
|
||||
local buffer = require('Trans.buffer')()
|
||||
local error_msg = conf.icon.notfound .. ' 没有找到相关的翻译'
|
||||
|
||||
local buffer = require('Trans.buffer')()
|
||||
|
||||
local node = require('Trans.node')
|
||||
local it, t, f = node.item, node.text, node.format
|
||||
|
||||
@ -33,7 +34,6 @@ local function handle_result(result)
|
||||
|
||||
if not phonetic and not collins and not oxford then
|
||||
buffer:addline(it(word, 'TransWord'))
|
||||
|
||||
else
|
||||
buffer:addline(f {
|
||||
width = hover.width,
|
||||
@ -50,7 +50,6 @@ local function handle_result(result)
|
||||
})
|
||||
end
|
||||
end,
|
||||
|
||||
tag = function(tag)
|
||||
addtitle('标签')
|
||||
local tag_map = {
|
||||
@ -78,7 +77,7 @@ local function handle_result(result)
|
||||
it(
|
||||
indent .. tags[i] ..
|
||||
(tags[i + 1] and interval .. tags[i + 1] ..
|
||||
(tags[i + 2] and interval .. tags[i + 2] or '') or ''),
|
||||
(tags[i + 2] and interval .. tags[i + 2] or '') or ''),
|
||||
'TransTag'
|
||||
)
|
||||
)
|
||||
@ -86,7 +85,6 @@ local function handle_result(result)
|
||||
|
||||
buffer:addline('')
|
||||
end,
|
||||
|
||||
pos = function(pos)
|
||||
addtitle('词性')
|
||||
local pos_map = {
|
||||
@ -114,7 +112,6 @@ local function handle_result(result)
|
||||
|
||||
buffer:addline('')
|
||||
end,
|
||||
|
||||
exchange = function(exchange)
|
||||
addtitle('词形变化')
|
||||
local exchange_map = {
|
||||
@ -138,7 +135,6 @@ local function handle_result(result)
|
||||
|
||||
buffer:addline('')
|
||||
end,
|
||||
|
||||
translation = function(translation)
|
||||
addtitle('中文翻译')
|
||||
|
||||
@ -150,7 +146,6 @@ local function handle_result(result)
|
||||
|
||||
buffer:addline('')
|
||||
end,
|
||||
|
||||
definition = function(definition)
|
||||
addtitle('英文注释')
|
||||
|
||||
@ -176,7 +171,7 @@ local function handle_result(result)
|
||||
end
|
||||
|
||||
local function open_window(opts)
|
||||
opts = opts or {}
|
||||
opts = opts or {}
|
||||
|
||||
local col = opts.col or 1
|
||||
local row = opts.row or 1
|
||||
@ -215,11 +210,9 @@ local function handle_keymap(win, word)
|
||||
pageup = function()
|
||||
buffer:normal('gg')
|
||||
end,
|
||||
|
||||
pagedown = function()
|
||||
buffer:normal('G')
|
||||
end,
|
||||
|
||||
pin = function()
|
||||
if lock then
|
||||
error('请先关闭窗口')
|
||||
@ -256,7 +249,6 @@ local function handle_keymap(win, word)
|
||||
})
|
||||
end)
|
||||
end,
|
||||
|
||||
close = function()
|
||||
pcall(api.nvim_del_autocmd, cmd_id)
|
||||
local run = win:try_close()
|
||||
@ -265,7 +257,6 @@ local function handle_keymap(win, word)
|
||||
end)
|
||||
try_del_keymap()
|
||||
end,
|
||||
|
||||
toggle_entry = function()
|
||||
if lock and win:is_valid() then
|
||||
local prev = api.nvim_get_current_win()
|
||||
@ -275,7 +266,6 @@ local function handle_keymap(win, word)
|
||||
del('n', keymap.toggle_entry)
|
||||
end
|
||||
end,
|
||||
|
||||
play = function()
|
||||
if word then
|
||||
word:play()
|
||||
@ -312,14 +302,14 @@ local function online_query(win, word)
|
||||
for i = 1, size do
|
||||
lists[i] = require('Trans.query.' .. engines[i])(word)
|
||||
end
|
||||
local cell = icon.cell
|
||||
local timeout = hover.timeout
|
||||
local spinner = require('Trans.ui.spinner')[hover.spinner]
|
||||
local range = #spinner
|
||||
local interval = math.floor(timeout / (win.width - spinner[1]:width()))
|
||||
local win_width = win.width
|
||||
local cell = icon.cell
|
||||
local timeout = hover.timeout
|
||||
local spinner = require('Trans.ui.spinner')[hover.spinner]
|
||||
local range = #spinner
|
||||
local interval = math.floor(timeout / (win.width - spinner[1]:width()))
|
||||
local win_width = win.width
|
||||
|
||||
local s = '%s %s'
|
||||
local s = '%s %s'
|
||||
local width, height = hover.width, hover.height
|
||||
local function waitting_result(this, times)
|
||||
for i = size, 1, -1 do
|
||||
@ -379,7 +369,6 @@ return function(word)
|
||||
win:set('wrap', true)
|
||||
handle_keymap(win, word)
|
||||
end)
|
||||
|
||||
else
|
||||
local win, run = open_window {
|
||||
width = error_msg:width(),
|
||||
|
@ -27,7 +27,9 @@ end
|
||||
---@param option string option name
|
||||
---@param value any
|
||||
function window:set(option, value)
|
||||
api.nvim_win_set_option(self.winid, option, value)
|
||||
if self:is_valid() then
|
||||
api.nvim_win_set_option(self.winid, option, value)
|
||||
end
|
||||
end
|
||||
|
||||
---@param name string option name
|
||||
@ -99,6 +101,7 @@ function window:try_close()
|
||||
field = field,
|
||||
target = 1,
|
||||
}
|
||||
|
||||
run(function()
|
||||
api.nvim_win_close(self.winid, true)
|
||||
end)
|
||||
|
12
plugin/Trans.lua
Normal file
12
plugin/Trans.lua
Normal file
@ -0,0 +1,12 @@
|
||||
local M = require("Trans")
|
||||
local api = vim.api
|
||||
local command = api.nvim_create_user_command
|
||||
|
||||
command('Translate', function() M.translate() end, { desc = ' 单词翻译', })
|
||||
command('TranslateInput', function() M.translate('i') end, { desc = ' 搜索翻译', })
|
||||
command('TransPlay', function()
|
||||
local word = M.get_word(api.nvim_get_mode().mode)
|
||||
if word ~= '' and word:isEn() then
|
||||
word:play()
|
||||
end
|
||||
end, { desc = ' 自动发音' })
|
Loading…
x
Reference in New Issue
Block a user