style: remove nest
This commit is contained in:
parent
6f75ba057b
commit
1205b11d97
82
README.md
82
README.md
@ -1,6 +1,7 @@
|
|||||||
# Trans.nvim
|
# Trans.nvim
|
||||||
|
|
||||||
<!--toc:start-->
|
<!--toc:start-->
|
||||||
|
|
||||||
- [Trans.nvim](#transnvim)
|
- [Trans.nvim](#transnvim)
|
||||||
- [特点](#特点)
|
- [特点](#特点)
|
||||||
- [屏幕截图](#屏幕截图)
|
- [屏幕截图](#屏幕截图)
|
||||||
@ -14,15 +15,17 @@
|
|||||||
- [感谢](#感谢)
|
- [感谢](#感谢)
|
||||||
- [贡献](#贡献)
|
- [贡献](#贡献)
|
||||||
- [待办 (画大饼)](#待办-画大饼)
|
- [待办 (画大饼)](#待办-画大饼)
|
||||||
<!--toc:end-->
|
<!--toc:end-->
|
||||||
|
|
||||||
|
|
||||||
## 特点
|
## 特点
|
||||||
- 使用纯lua编写, 速度极快
|
|
||||||
> `Lazy.nvim`的记录: <font color="#0099FF">`➜ Trans.nvim 0.82ms`</font>
|
- 使用纯 lua 编写, 速度极快
|
||||||
|
|
||||||
|
> `Lazy.nvim`的记录: <font color="#0099FF">`➜ Trans.nvim 0.82ms`</font>
|
||||||
|
|
||||||
- **可以定义快捷键读英文单词**
|
- **可以定义快捷键读英文单词**
|
||||||
> 见wiki
|
|
||||||
|
> 见 wiki
|
||||||
|
|
||||||
- 大部分功能可以自定义:
|
- 大部分功能可以自定义:
|
||||||
- 高亮
|
- 高亮
|
||||||
@ -34,7 +37,7 @@
|
|||||||
- **完全离线** 的单词翻译体验 (可能后面会支持在线翻译)
|
- **完全离线** 的单词翻译体验 (可能后面会支持在线翻译)
|
||||||
- 支持显示:
|
- 支持显示:
|
||||||
- 柯林斯星级
|
- 柯林斯星级
|
||||||
- 牛津3000词汇
|
- 牛津 3000 词汇
|
||||||
- 中文翻译
|
- 中文翻译
|
||||||
- 英文翻译 (不是英译中, 而是用英文解释)
|
- 英文翻译 (不是英译中, 而是用英文解释)
|
||||||
- 词根
|
- 词根
|
||||||
@ -42,11 +45,12 @@
|
|||||||
- 舒服的排版和`动画`
|
- 舒服的排版和`动画`
|
||||||
- 支持 `normal`和 `visual`模式
|
- 支持 `normal`和 `visual`模式
|
||||||
> <font color='#FF9900'>不支持 visual-block mode</font>
|
> <font color='#FF9900'>不支持 visual-block mode</font>
|
||||||
|
|
||||||
- 本地词库单词量: `430w`
|
- 本地词库单词量: `430w`
|
||||||
|
|
||||||
## 屏幕截图
|
## 屏幕截图
|
||||||
|
|
||||||
### 演示
|
### 演示
|
||||||
|
|
||||||
https://user-images.githubusercontent.com/107862700/213752097-2eee026a-ddee-4531-bf80-ba2cbc8b44ef.mp4
|
https://user-images.githubusercontent.com/107862700/213752097-2eee026a-ddee-4531-bf80-ba2cbc8b44ef.mp4
|
||||||
|
|
||||||
> 视频演示的在线查询, 查询速度取决于你的网络状况
|
> 视频演示的在线查询, 查询速度取决于你的网络状况
|
||||||
@ -55,22 +59,23 @@ https://user-images.githubusercontent.com/107862700/213752097-2eee026a-ddee-4531
|
|||||||
https://user-images.githubusercontent.com/107862700/215941500-3293c571-20a1-44e2-b202-77079f158ce9.mp4
|
https://user-images.githubusercontent.com/107862700/215941500-3293c571-20a1-44e2-b202-77079f158ce9.mp4
|
||||||
|
|
||||||
### 主题
|
### 主题
|
||||||
> 如果你有更美观或者更适合的配色, 欢迎提PR
|
|
||||||
|
> 如果你有更美观或者更适合的配色, 欢迎提 PR
|
||||||
> 主题配色在: `lua/Trans/theme.lua`文件中,你只需要添加你主题的表就可以了
|
> 主题配色在: `lua/Trans/theme.lua`文件中,你只需要添加你主题的表就可以了
|
||||||
|
|
||||||
|
|
||||||
- `default`
|
- `default`
|
||||||

|

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

|

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

|

|
||||||
|
|
||||||
|
|
||||||
## 安装
|
## 安装
|
||||||
*安装之前, 首先需要明确本插件的依赖:*
|
|
||||||
|
_安装之前, 首先需要明确本插件的依赖:_
|
||||||
|
|
||||||
- [ECDICT](https://github.com/skywind3000/ECDICT): 插件所用的离线单词数据库
|
- [ECDICT](https://github.com/skywind3000/ECDICT): 插件所用的离线单词数据库
|
||||||
- sqlite.lua: 操作数据库所用的库
|
- sqlite.lua: 操作数据库所用的库
|
||||||
- sqlite3: 数据库
|
- sqlite3: 数据库
|
||||||
@ -92,7 +97,8 @@ use {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
**如果你想要使用Packer的惰性加载,这里有一个例子**
|
**如果你想要使用 Packer 的惰性加载,这里有一个例子**
|
||||||
|
|
||||||
```lua
|
```lua
|
||||||
use {
|
use {
|
||||||
"JuanZoran/Trans.nvim",
|
"JuanZoran/Trans.nvim",
|
||||||
@ -111,6 +117,7 @@ use {
|
|||||||
end
|
end
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
</details>
|
</details>
|
||||||
|
|
||||||
<details>
|
<details>
|
||||||
@ -133,38 +140,46 @@ use {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
</details>
|
</details>
|
||||||
|
|
||||||
<font color="#FF9900">**注意事项**: </font>
|
<font color="#FF9900">**注意事项**: </font>
|
||||||
|
|
||||||
- `install.sh`
|
- `install.sh`
|
||||||
- 使用了 `wget`下载词库, 安装请确保你的环境变量中存在wget
|
|
||||||
|
- 使用了 `wget`下载词库, 安装请确保你的环境变量中存在 wget
|
||||||
- install.sh 下载后会自动将词库解压, 并移动到 `$HOME/.vim/dict`文件夹下
|
- install.sh 下载后会自动将词库解压, 并移动到 `$HOME/.vim/dict`文件夹下
|
||||||
- 目前仅在 `Ubuntu22.04`的环境下测试通过
|
- 目前仅在 `Ubuntu22.04`的环境下测试通过
|
||||||
> 如果上述条件不符合, 请删掉 `run = 'install.sh'`部分, 考虑手动安装词库
|
> 如果上述条件不符合, 请删掉 `run = 'install.sh'`部分, 考虑手动安装词库
|
||||||
> 如果上述条件满足, 仍出现问题, 欢迎在issue里向我反馈,我会及时尝试解决
|
> 如果上述条件满足, 仍出现问题, 欢迎在 issue 里向我反馈,我会及时尝试解决
|
||||||
|
|
||||||
- 下载词典的过程中, 需要能够 `流畅的访问github下载`
|
- 下载词典的过程中, 需要能够 `流畅的访问github下载`
|
||||||
|
|
||||||
> 词库文件压缩包大小为: **281M**
|
> 词库文件压缩包大小为: **281M**
|
||||||
> 解压缩后的大小大概为: 1.2G
|
> 解压缩后的大小大概为: 1.2G
|
||||||
|
|
||||||
- 安装后如果不能正常运行, 请尝试检查一下问题:
|
- 安装后如果不能正常运行, 请尝试检查一下问题:
|
||||||
|
|
||||||
- 本机是否已经安装了 `sqlite3`
|
- 本机是否已经安装了 `sqlite3`
|
||||||
> Linux下安装:
|
> Linux 下安装:
|
||||||
> `sudo pacman -S sqlite # Arch`
|
> `sudo pacman -S sqlite # Arch`
|
||||||
> `sudo apt-get install sqlite3 libsqlite3-dev # Ubuntu`
|
> `sudo apt-get install sqlite3 libsqlite3-dev # Ubuntu`
|
||||||
|
|
||||||
> 后续会增加 `healthcheck` 进行检查
|
> 后续会增加 `healthcheck` 进行检查
|
||||||
|
|
||||||
- **`auto_play`** 使用步骤:
|
- **`auto_play`** 使用步骤:
|
||||||
|
|
||||||
> linux 只需要安装`festival`
|
> linux 只需要安装`festival`
|
||||||
> sudo apt-get install festival festvox-kallpc16k
|
> sudo apt-get install festival festvox-kallpc16k
|
||||||
> ***如果你想要设置音色,发音可以访问:*** [Festival官方](https://www.cstr.ed.ac.uk/projects/festival/morevoices.html)
|
> **_如果你想要设置音色,发音可以访问:_** [Festival 官方](https://www.cstr.ed.ac.uk/projects/festival/morevoices.html)
|
||||||
> 可以选择英音、美音、男声、女声
|
> 可以选择英音、美音、男声、女声
|
||||||
|
|
||||||
> 其他操作系统
|
> 其他操作系统
|
||||||
|
|
||||||
- 需要确保安装了`nodejs`
|
- 需要确保安装了`nodejs`
|
||||||
- 进入插件的`tts`目录运行`npm install`
|
- 进入插件的`tts`目录运行`npm install`
|
||||||
> 如果`install.sh`运行正常则自动安装,如果安装失败,请尝试手动安装
|
> 如果`install.sh`运行正常则自动安装,如果安装失败,请尝试手动安装
|
||||||
|
|
||||||
- `title`的配置,只对`neovim 0.9`版本有效
|
- `title`的配置,只对`neovim 0.9`版本有效
|
||||||
|
|
||||||
<details>
|
<details>
|
||||||
@ -174,34 +189,41 @@ use {
|
|||||||
- 用户配置: `~/.festivalrc`
|
- 用户配置: `~/.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))
|
- 试听[在这里](https://www.cstr.ed.ac.uk/projects/festival/morevoices.html))
|
||||||
- 下载[在这里](http://festvox.org/packed/festival/2.5/voices/))
|
- 下载[在这里](http://festvox.org/packed/festival/2.5/voices/))
|
||||||
> 假设下载的文件在`Downloads`文件夹, 下载的文件为:`festvox_cmu_us_aew_cg.tar.gz`
|
> 假设下载的文件在`Downloads`文件夹, 下载的文件为:`festvox_cmu_us_aew_cg.tar.gz`
|
||||||
|
|
||||||
示例:
|
示例:
|
||||||
|
|
||||||
> `cd ~/Downloads && tar -xf festvox_cmu_us_aew_cg.tar.gz`
|
> `cd ~/Downloads && tar -xf festvox_cmu_us_aew_cg.tar.gz`
|
||||||
|
|
||||||
- 将音频文件拷贝到festival文件夹
|
- 将音频文件拷贝到 festival 文件夹
|
||||||
示例:
|
示例:
|
||||||
|
|
||||||
> `sudo cp -r festival/lib/voices/us/cmu_us_aew_cg/ /usr/share/festival/voices/my_voices/`
|
> `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)`到配置文件
|
> 加入`(set! voice_default voice_cmu_indic_hin_ab_cg)`到配置文件
|
||||||
|
|
||||||
- 安装完成
|
- 安装完成
|
||||||
|
|
||||||
- 相关说明网站
|
- 相关说明网站
|
||||||
> 可能需要
|
> 可能需要
|
||||||
- [wiki](https://archlinux.org/packages/community/any/festival-us/) 查看更多详细配置
|
- [wiki](https://archlinux.org/packages/community/any/festival-us/) 查看更多详细配置
|
||||||
- [官方网站](http://festvox.org/dbs/index.html)
|
- [官方网站](http://festvox.org/dbs/index.html)
|
||||||
- [用户手册](http://www.festvox.org/docs/manual-2.4.0/festival_toc.html)
|
- [用户手册](http://www.festvox.org/docs/manual-2.4.0/festival_toc.html)
|
||||||
@ -209,6 +231,7 @@ use {
|
|||||||
</details>
|
</details>
|
||||||
|
|
||||||
## 配置
|
## 配置
|
||||||
|
|
||||||
```lua
|
```lua
|
||||||
require'Trans'.setup {
|
require'Trans'.setup {
|
||||||
view = {
|
view = {
|
||||||
@ -318,8 +341,11 @@ require'Trans'.setup {
|
|||||||
```
|
```
|
||||||
|
|
||||||
## 快捷键绑定
|
## 快捷键绑定
|
||||||
|
|
||||||
**示例:**
|
**示例:**
|
||||||
|
|
||||||
> 示例中展示, 将`mm`映射成快捷键
|
> 示例中展示, 将`mm`映射成快捷键
|
||||||
|
|
||||||
```lua
|
```lua
|
||||||
vim.keymap.set({'n', 'x'}, 'mm', '<Cmd>Translate<CR>')
|
vim.keymap.set({'n', 'x'}, 'mm', '<Cmd>Translate<CR>')
|
||||||
vim.keymap.set({'n', 'x'}, 'mk', '<Cmd>TransPlay<CR>') -- 自动发音选中或者光标下的单词
|
vim.keymap.set({'n', 'x'}, 'mk', '<Cmd>TransPlay<CR>') -- 自动发音选中或者光标下的单词
|
||||||
@ -328,7 +354,9 @@ vim.keymap.set('n', 'mi', '<Cmd>TranslateInput<CR>')
|
|||||||
```
|
```
|
||||||
|
|
||||||
## 高亮组
|
## 高亮组
|
||||||
|
|
||||||
> 默认定义
|
> 默认定义
|
||||||
|
|
||||||
```lua
|
```lua
|
||||||
{
|
{
|
||||||
TransWord = {
|
TransWord = {
|
||||||
@ -378,18 +406,22 @@ 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) 本地词典的提供
|
- [ECDICT](https://github.com/skywind3000/ECDICT) 本地词典的提供
|
||||||
- [sqlite.lua](https://github.com/kharji/sqlite.lua) 数据库访问
|
- [sqlite.lua](https://github.com/kharji/sqlite.lua) 数据库访问
|
||||||
- [T.vim](https://github.com/sicong-li/T.vim) 灵感来源
|
- [T.vim](https://github.com/sicong-li/T.vim) 灵感来源
|
||||||
|
|
||||||
## 贡献
|
## 贡献
|
||||||
|
|
||||||
> 更新比较频繁, 文档先鸽了
|
> 更新比较频繁, 文档先鸽了
|
||||||
> 如果你想要参加这个项目, 可以提issue, 我会把文档补齐
|
> 如果你想要参加这个项目, 可以提 issue, 我会把文档补齐
|
||||||
|
|
||||||
## 待办 (画大饼)
|
## 待办 (画大饼)
|
||||||
|
|
||||||
- [x] 多风格样式查询
|
- [x] 多风格样式查询
|
||||||
- [x] 重新录制屏幕截图示例
|
- [x] 重新录制屏幕截图示例
|
||||||
- [x] 快捷键定义
|
- [x] 快捷键定义
|
||||||
|
@ -5,7 +5,6 @@ if test -e "$HOME/.vim/dict/ultimate.db"; then
|
|||||||
exit
|
exit
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
|
||||||
mkdir -p "$HOME/.vim/dict"
|
mkdir -p "$HOME/.vim/dict"
|
||||||
|
|
||||||
wget https://github.com/skywind3000/ECDICT-ultimate/releases/download/1.0.0/ecdict-ultimate-sqlite.zip -O /tmp/dict.zip
|
wget https://github.com/skywind3000/ECDICT-ultimate/releases/download/1.0.0/ecdict-ultimate-sqlite.zip -O /tmp/dict.zip
|
||||||
@ -13,7 +12,7 @@ wget https://github.com/skywind3000/ECDICT-ultimate/releases/download/1.0.0/ecdi
|
|||||||
unzip /tmp/dict.zip -d "$HOME/.vim/dict" && rm -rf /tmp/dict.zip
|
unzip /tmp/dict.zip -d "$HOME/.vim/dict" && rm -rf /tmp/dict.zip
|
||||||
|
|
||||||
uNames=$(uname -s)
|
uNames=$(uname -s)
|
||||||
osName=${uNames: 0: 4}
|
osName=${uNames:0:4}
|
||||||
if [ "$osName" != "Linux" ];then
|
if [ "$osName" != "Linux" ]; then
|
||||||
cd ./tts/ && npm install
|
cd ./tts/ && npm install
|
||||||
fi
|
fi
|
||||||
|
@ -1,28 +1,121 @@
|
|||||||
local api = vim.api
|
local api, fn = vim.api, vim.fn
|
||||||
local fn = vim.fn
|
|
||||||
|
|
||||||
local buffer = {
|
---@class buf
|
||||||
addline = function(self, nodes, index)
|
---@field bufnr integer buffer handle
|
||||||
local size = self.size
|
---@field size integer buffer line count
|
||||||
if index then
|
local buffer = {}
|
||||||
assert(index <= size + 1)
|
|
||||||
index = index
|
---Clear all content in buffer
|
||||||
|
function buffer:wipe()
|
||||||
|
api.nvim_buf_set_lines(self.bufnr, 0, -1, false, {})
|
||||||
|
self.size = 0
|
||||||
|
end
|
||||||
|
|
||||||
|
---delete buffer [_start, _end] line content [one index]
|
||||||
|
---@param _start integer start line index
|
||||||
|
---@param _end integer end line index
|
||||||
|
function buffer:del(_start, _end)
|
||||||
|
if not _start then
|
||||||
|
fn.deletebufline(self.bufnr, '$')
|
||||||
else
|
else
|
||||||
index = size + 1
|
_end = _end or _start
|
||||||
|
fn.deletebufline(self.bufnr, _start, _end)
|
||||||
end
|
end
|
||||||
local append = index == size + 1
|
self.size = api.nvim_buf_line_count(self.bufnr)
|
||||||
local line = index - 1
|
end
|
||||||
|
|
||||||
|
---Set buffer option
|
||||||
|
---@param name string option name
|
||||||
|
---@param value any option value
|
||||||
|
function buffer:set(name, value)
|
||||||
|
api.nvim_buf_set_option(self.bufnr, name, value)
|
||||||
|
end
|
||||||
|
|
||||||
|
---get buffer option
|
||||||
|
---@param name string option name
|
||||||
|
---@return any
|
||||||
|
function buffer:option(name)
|
||||||
|
return api.nvim_buf_get_option(self.bufnr, name)
|
||||||
|
end
|
||||||
|
|
||||||
|
function buffer:delete()
|
||||||
|
api.nvim_buf_delete(self.bufnr, { force = true })
|
||||||
|
end
|
||||||
|
|
||||||
|
---Set buffer load keymap
|
||||||
|
---@param key string
|
||||||
|
---@param operation function | string
|
||||||
|
function buffer:map(key, operation)
|
||||||
|
vim.keymap.set('n', key, operation, {
|
||||||
|
buffer = self.bufnr,
|
||||||
|
silent = true,
|
||||||
|
})
|
||||||
|
end
|
||||||
|
|
||||||
|
---Execute normal keycode in this buffer[no recursive]
|
||||||
|
---@param key string key code
|
||||||
|
function buffer:normal(key)
|
||||||
|
api.nvim_buf_call(self.bufnr, function()
|
||||||
|
vim.cmd([[normal! ]] .. key)
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
|
||||||
|
---@return boolean
|
||||||
|
---@nodiscard
|
||||||
|
function buffer:is_valid()
|
||||||
|
return api.nvim_buf_is_valid(self.bufnr)
|
||||||
|
end
|
||||||
|
|
||||||
|
---Get buffer [i, j] line content
|
||||||
|
---@param i integer? start line index
|
||||||
|
---@param j integer? end line index
|
||||||
|
---@return string[]
|
||||||
|
function buffer:lines(i, j)
|
||||||
|
i = i and i - 1 or 0
|
||||||
|
j = j and j - 1 or -1
|
||||||
|
return api.nvim_buf_get_lines(self.bufnr, i, j, false)
|
||||||
|
end
|
||||||
|
|
||||||
|
---Calculate buffer content display height
|
||||||
|
---@param width integer
|
||||||
|
---@return integer height
|
||||||
|
function buffer:height(width)
|
||||||
|
local size = self.size
|
||||||
|
local lines = self:lines()
|
||||||
|
local height = 0
|
||||||
|
for i = 1, size do
|
||||||
|
height = height + math.max(1, (math.ceil(lines[i]:width() / width)))
|
||||||
|
end
|
||||||
|
return height
|
||||||
|
end
|
||||||
|
|
||||||
|
---Add|Set line content
|
||||||
|
---@param nodes string|table|table[] string -> as line content | table -> as a node | table[] -> as node[]
|
||||||
|
---@param index number? line number should be set[one index]
|
||||||
|
function buffer:addline(nodes, index)
|
||||||
|
local newsize = self.size + 1
|
||||||
|
assert(index == nil or index <= newsize)
|
||||||
|
index = index or newsize
|
||||||
|
if index == newsize then
|
||||||
|
self.size = newsize
|
||||||
|
end
|
||||||
|
|
||||||
if type(nodes) == 'string' then
|
if type(nodes) == 'string' then
|
||||||
self[index] = nodes
|
self[index] = nodes
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
else
|
|
||||||
|
local line = index - 1
|
||||||
local bufnr = self.bufnr
|
local bufnr = self.bufnr
|
||||||
local col = 0
|
local col = 0
|
||||||
if type(nodes[1]) == 'string' then
|
if type(nodes[1]) == 'string' then
|
||||||
self[index] = nodes[1]
|
self[index] = nodes[1]
|
||||||
nodes:load(bufnr, line, col)
|
nodes:load(bufnr, line, col)
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
else
|
|
||||||
local strs = {}
|
local strs = {}
|
||||||
local num = #nodes
|
local num = #nodes
|
||||||
for i = 1, num do
|
for i = 1, num do
|
||||||
@ -35,89 +128,16 @@ local buffer = {
|
|||||||
node:load(bufnr, line, col)
|
node:load(bufnr, line, col)
|
||||||
col = col + #node[1]
|
col = col + #node[1]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
|
||||||
if append then
|
|
||||||
self.size = self.size + 1
|
|
||||||
end
|
|
||||||
end,
|
|
||||||
|
|
||||||
wipe = function(self)
|
function buffer:init()
|
||||||
api.nvim_buf_set_lines(self.bufnr, 0, -1, false, {})
|
|
||||||
self.size = 0
|
|
||||||
end,
|
|
||||||
|
|
||||||
del = function(self, _start, _end)
|
|
||||||
if not _start then
|
|
||||||
fn.deletebufline(self.bufnr, '$')
|
|
||||||
else
|
|
||||||
_end = _end or _start
|
|
||||||
fn.deletebufline(self.bufnr, _start, _end)
|
|
||||||
end
|
|
||||||
self.size = api.nvim_buf_line_count(self.bufnr)
|
|
||||||
end,
|
|
||||||
|
|
||||||
set = function(self, name, option)
|
|
||||||
api.nvim_buf_set_option(self.bufnr, name, option)
|
|
||||||
end,
|
|
||||||
|
|
||||||
option = function(self, name)
|
|
||||||
return api.nvim_buf_get_option(self.bufnr, name)
|
|
||||||
end,
|
|
||||||
|
|
||||||
is_valid = function(self)
|
|
||||||
return api.nvim_buf_is_valid(self.bufnr)
|
|
||||||
end,
|
|
||||||
|
|
||||||
delete = function(self)
|
|
||||||
api.nvim_buf_delete(self.bufnr, { force = true })
|
|
||||||
end,
|
|
||||||
|
|
||||||
len = function(self)
|
|
||||||
return api.nvim_buf_line_count(self.bufnr) - 1
|
|
||||||
end,
|
|
||||||
|
|
||||||
map = function(self, key, operation)
|
|
||||||
vim.keymap.set('n', key, operation, {
|
|
||||||
buffer = self.bufnr,
|
|
||||||
silent = true,
|
|
||||||
})
|
|
||||||
end,
|
|
||||||
|
|
||||||
normal = function(self, key)
|
|
||||||
api.nvim_buf_call(self.bufnr, function()
|
|
||||||
vim.cmd([[normal! ]] .. key)
|
|
||||||
end)
|
|
||||||
end,
|
|
||||||
|
|
||||||
lines = function(self, i, j)
|
|
||||||
i = i and i - 1 or 0
|
|
||||||
j = j and j - 1 or -1
|
|
||||||
return api.nvim_buf_get_lines(self.bufnr, i, j, false)
|
|
||||||
end,
|
|
||||||
|
|
||||||
height = function(self, width)
|
|
||||||
local size = self.size
|
|
||||||
if width then
|
|
||||||
local lines = self:lines()
|
|
||||||
local height = 0
|
|
||||||
for i = 1, size do
|
|
||||||
height = height + math.max(1, (math.ceil(lines[i]:width() / width)))
|
|
||||||
end
|
|
||||||
return height
|
|
||||||
else
|
|
||||||
return size
|
|
||||||
end
|
|
||||||
end,
|
|
||||||
|
|
||||||
init = function(self)
|
|
||||||
self.bufnr = api.nvim_create_buf(false, false)
|
self.bufnr = api.nvim_create_buf(false, false)
|
||||||
self:set('filetype', 'Trans')
|
self:set('filetype', 'Trans')
|
||||||
self:set('buftype', 'nofile')
|
self:set('buftype', 'nofile')
|
||||||
self.size = 0
|
self.size = 0
|
||||||
end,
|
end
|
||||||
}
|
|
||||||
|
|
||||||
|
---@private
|
||||||
buffer.__index = function(self, key)
|
buffer.__index = function(self, key)
|
||||||
local res = buffer[key]
|
local res = buffer[key]
|
||||||
if res then
|
if res then
|
||||||
@ -131,12 +151,14 @@ buffer.__index = function(self, key)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
---@private
|
||||||
buffer.__newindex = function(self, key, text)
|
buffer.__newindex = function(self, key, text)
|
||||||
assert(key <= self.size + 1)
|
assert(key <= self.size + 1)
|
||||||
fn.setbufline(self.bufnr, key, text)
|
fn.setbufline(self.bufnr, key, text)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
---buffer constructor
|
||||||
|
---@return buf
|
||||||
return function()
|
return function()
|
||||||
return setmetatable({
|
return setmetatable({
|
||||||
bufnr = -1,
|
bufnr = -1,
|
||||||
|
@ -1,6 +1,9 @@
|
|||||||
local M = {}
|
local M = {}
|
||||||
local api = vim.api
|
local api, fn = vim.api, vim.fn
|
||||||
local fn = vim.fn
|
|
||||||
|
if fn.executable('sqlite3') ~= 1 then
|
||||||
|
error('Please check out sqlite3')
|
||||||
|
end
|
||||||
|
|
||||||
local win_title = fn.has('nvim-0.9') == 1 and {
|
local win_title = fn.has('nvim-0.9') == 1 and {
|
||||||
{ '', 'TransTitleRound' },
|
{ '', 'TransTitleRound' },
|
||||||
@ -17,7 +20,6 @@ local win_title = fn.has('nvim-0.9') == 1 and {
|
|||||||
-- " ╚═╝ ╚═╝ ╚═╝╚═╝ ╚═╝╚═╝ ╚═══╝╚══════╝",
|
-- " ╚═╝ ╚═╝ ╚═╝╚═╝ ╚═╝╚═╝ ╚═══╝╚══════╝",
|
||||||
--}
|
--}
|
||||||
|
|
||||||
|
|
||||||
string.width = api.nvim_strwidth
|
string.width = api.nvim_strwidth
|
||||||
string.isEn = function(self)
|
string.isEn = function(self)
|
||||||
local char = { self:byte(1, -1) }
|
local char = { self:byte(1, -1) }
|
||||||
@ -29,7 +31,6 @@ string.isEn = function(self)
|
|||||||
return true
|
return true
|
||||||
end
|
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)
|
local cmd = ([[echo "%s" | festival --tts]]):format(self)
|
||||||
fn.jobstart(cmd)
|
fn.jobstart(cmd)
|
||||||
@ -39,7 +40,6 @@ end or function(self)
|
|||||||
fn.jobstart('node ' .. file .. ' ' .. self)
|
fn.jobstart('node ' .. file .. ' ' .. self)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
M.conf = {
|
M.conf = {
|
||||||
view = {
|
view = {
|
||||||
i = 'float',
|
i = 'float',
|
||||||
@ -121,7 +121,6 @@ M.conf = {
|
|||||||
-- theme = 'tokyonight',
|
-- theme = 'tokyonight',
|
||||||
|
|
||||||
db_path = '$HOME/.vim/dict/ultimate.db',
|
db_path = '$HOME/.vim/dict/ultimate.db',
|
||||||
|
|
||||||
engine = {
|
engine = {
|
||||||
-- baidu = {
|
-- baidu = {
|
||||||
-- appid = '',
|
-- appid = '',
|
||||||
@ -132,7 +131,6 @@ M.conf = {
|
|||||||
-- appPasswd = '',
|
-- appPasswd = '',
|
||||||
-- },
|
-- },
|
||||||
},
|
},
|
||||||
|
|
||||||
-- TODO :
|
-- TODO :
|
||||||
-- register word
|
-- register word
|
||||||
-- history = {
|
-- history = {
|
||||||
@ -158,41 +156,33 @@ M.setup = function(opts)
|
|||||||
end
|
end
|
||||||
|
|
||||||
local engines = {}
|
local engines = {}
|
||||||
|
local i = 1
|
||||||
for k, _ in pairs(conf.engine) do
|
for k, _ in pairs(conf.engine) do
|
||||||
table.insert(engines, k)
|
engines[i] = k
|
||||||
|
i = i + 1
|
||||||
end
|
end
|
||||||
conf.engines = engines
|
|
||||||
|
|
||||||
|
conf.engines = engines
|
||||||
times = times + 1
|
times = times + 1
|
||||||
if times == 1 then
|
if times == 1 then
|
||||||
|
---@format disable
|
||||||
local get_mode = api.nvim_get_mode
|
|
||||||
local set_hl = api.nvim_set_hl
|
|
||||||
local new_command = api.nvim_create_user_command
|
local new_command = api.nvim_create_user_command
|
||||||
|
new_command('Translate' , function() M.translate() end, { desc = ' 单词翻译',})
|
||||||
if fn.executable('sqlite3') ~= 1 then
|
new_command('TranslateInput' , function() M.translate('i') end, { desc = ' 搜索翻译',})
|
||||||
error('Please check out sqlite3')
|
new_command('TransPlay' , function()
|
||||||
end
|
local word = M.get_word(api.nvim_get_mode().mode)
|
||||||
|
|
||||||
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(get_mode().mode)
|
|
||||||
if word ~= '' and word:isEn() then
|
if word ~= '' and word:isEn() then
|
||||||
word:play()
|
word:play()
|
||||||
end
|
end
|
||||||
end, { desc = ' 自动发音' })
|
end, { desc = ' 自动发音' })
|
||||||
|
|
||||||
|
|
||||||
|
local set_hl = api.nvim_set_hl
|
||||||
local hls = require('Trans.ui.theme')[conf.theme]
|
local hls = require('Trans.ui.theme')[conf.theme]
|
||||||
for hl, opt in pairs(hls) do
|
for hl, opt in pairs(hls) do
|
||||||
set_hl(0, hl, opt)
|
set_hl(0, hl, opt)
|
||||||
end
|
end
|
||||||
|
---@format enable
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -203,10 +193,8 @@ local function get_select()
|
|||||||
if _start[2] > _end[2] or (_start[3] > _end[3] and _start[2] == _end[2]) then
|
if _start[2] > _end[2] or (_start[3] > _end[3] and _start[2] == _end[2]) then
|
||||||
_start, _end = _end, _start
|
_start, _end = _end, _start
|
||||||
end
|
end
|
||||||
local s_row = _start[2]
|
local s_row, s_col = _start[2], _start[3]
|
||||||
local e_row = _end[2]
|
local e_row, e_col = _end[2], _end[3]
|
||||||
local s_col = _start[3]
|
|
||||||
local e_col = _end[3]
|
|
||||||
|
|
||||||
-- print(s_row, e_row, s_col, e_col)
|
-- print(s_row, e_row, s_col, e_col)
|
||||||
---@type string
|
---@type string
|
||||||
@ -217,6 +205,7 @@ local function get_select()
|
|||||||
|
|
||||||
if s_row == e_row then
|
if s_row == e_row then
|
||||||
return line:sub(s_col, e_col)
|
return line:sub(s_col, e_col)
|
||||||
|
|
||||||
else
|
else
|
||||||
local lines = fn.getline(s_row, e_row)
|
local lines = fn.getline(s_row, e_row)
|
||||||
local i = #lines
|
local i = #lines
|
||||||
@ -237,9 +226,8 @@ M.get_word = function(mode)
|
|||||||
|
|
||||||
elseif mode == 'i' then
|
elseif mode == 'i' then
|
||||||
-- TODO Use Telescope with fuzzy finder
|
-- TODO Use Telescope with fuzzy finder
|
||||||
vim.ui.input({ prompt = '请输入需要查询的单词: ' }, function(input)
|
---@diagnostic disable-next-line: param-type-mismatch
|
||||||
word = input
|
word = fn.input('请输入需要查询的单词:')
|
||||||
end)
|
|
||||||
else
|
else
|
||||||
error('invalid mode: ' .. mode)
|
error('invalid mode: ' .. mode)
|
||||||
end
|
end
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
local api = vim.api
|
local api = vim.api
|
||||||
local add_hl = api.nvim_buf_add_highlight
|
|
||||||
local ns = require('Trans').ns
|
local ns = require('Trans').ns
|
||||||
|
local add_hl = api.nvim_buf_add_highlight
|
||||||
|
|
||||||
local item_meta = {
|
local item_meta = {
|
||||||
load = function(self, bufnr, line, col)
|
load = function(self, bufnr, line, col)
|
||||||
|
@ -19,9 +19,7 @@ vim.api.nvim_create_autocmd('VimLeavePre', {
|
|||||||
|
|
||||||
return function(word)
|
return function(word)
|
||||||
local res = (dict:select('stardict', {
|
local res = (dict:select('stardict', {
|
||||||
where = {
|
where = { word = word, },
|
||||||
word = word,
|
|
||||||
},
|
|
||||||
keys = {
|
keys = {
|
||||||
'word',
|
'word',
|
||||||
'phonetic',
|
'phonetic',
|
||||||
@ -43,10 +41,6 @@ return function(word)
|
|||||||
collins = res.collins,
|
collins = res.collins,
|
||||||
phonetic = res.phonetic,
|
phonetic = res.phonetic,
|
||||||
}
|
}
|
||||||
res.word = nil
|
|
||||||
res.oxford = nil
|
|
||||||
res.collins = nil
|
|
||||||
res.phonetic = nil
|
|
||||||
end
|
end
|
||||||
|
|
||||||
return res
|
return res
|
||||||
|
@ -63,5 +63,4 @@ curl.POST = function(uri, opts)
|
|||||||
vim.fn.jobstart(table.concat(cmd, ' '), option)
|
vim.fn.jobstart(table.concat(cmd, ' '), option)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
return curl
|
return curl
|
||||||
|
@ -5,9 +5,7 @@ local buffer = require('Trans.buffer')()
|
|||||||
local error_msg = conf.icon.notfound .. ' 没有找到相关的翻译'
|
local error_msg = conf.icon.notfound .. ' 没有找到相关的翻译'
|
||||||
|
|
||||||
local node = require('Trans.node')
|
local node = require('Trans.node')
|
||||||
local it = node.item
|
local it, t, f = node.item, node.text, node.format
|
||||||
local t = node.text
|
|
||||||
local f = node.format
|
|
||||||
|
|
||||||
local function handle_result(result)
|
local function handle_result(result)
|
||||||
local icon = conf.icon
|
local icon = conf.icon
|
||||||
@ -19,7 +17,6 @@ local function handle_result(result)
|
|||||||
string.play(word:isEn() and word or result.definition)
|
string.play(word:isEn() and word or result.definition)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
local addtitle = function(title)
|
local addtitle = function(title)
|
||||||
buffer:addline {
|
buffer:addline {
|
||||||
it('', 'TransTitleRound'),
|
it('', 'TransTitleRound'),
|
||||||
@ -197,8 +194,6 @@ local function open_window(opts)
|
|||||||
title = hover.title,
|
title = hover.title,
|
||||||
border = hover.border,
|
border = hover.border,
|
||||||
animation = hover.animation,
|
animation = hover.animation,
|
||||||
zindex = 80,
|
|
||||||
enter = false,
|
|
||||||
ns = require('Trans').ns,
|
ns = require('Trans').ns,
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
@ -232,8 +227,7 @@ local function handle_keymap(win, word)
|
|||||||
lock = true
|
lock = true
|
||||||
end
|
end
|
||||||
pcall(api.nvim_del_autocmd, cmd_id)
|
pcall(api.nvim_del_autocmd, cmd_id)
|
||||||
local width = win.width
|
local width, height = win.width, win.height
|
||||||
local height = win.height
|
|
||||||
local col = vim.o.columns - width - 3
|
local col = vim.o.columns - width - 3
|
||||||
local buf = buffer.bufnr
|
local buf = buffer.bufnr
|
||||||
local run = win:try_close()
|
local run = win:try_close()
|
||||||
@ -312,7 +306,9 @@ local function online_query(win, word)
|
|||||||
local error_line = it(error_msg, 'TransFailed')
|
local error_line = it(error_msg, 'TransFailed')
|
||||||
if size == 0 then
|
if size == 0 then
|
||||||
buffer:addline(error_line)
|
buffer:addline(error_line)
|
||||||
else
|
return
|
||||||
|
end
|
||||||
|
|
||||||
for i = 1, size do
|
for i = 1, size do
|
||||||
lists[size] = require('Trans.query.' .. engines[i])(word)
|
lists[size] = require('Trans.query.' .. engines[i])(word)
|
||||||
end
|
end
|
||||||
@ -324,23 +320,21 @@ local function online_query(win, word)
|
|||||||
local win_width = win.width
|
local win_width = win.width
|
||||||
|
|
||||||
local s = '%s %s'
|
local s = '%s %s'
|
||||||
local width = hover.width
|
local width, height = hover.width, hover.height
|
||||||
local height = hover.height
|
local function waitting_result(this, times)
|
||||||
local function waitting_result(self, times)
|
|
||||||
for i = 1, size do
|
for i = 1, size do
|
||||||
local res = lists[i][1]
|
local res = lists[i][1]
|
||||||
if res then
|
if res then
|
||||||
buffer:wipe()
|
buffer:wipe()
|
||||||
win:set_width(width)
|
win:set_width(width)
|
||||||
handle_result(res)
|
handle_result(res)
|
||||||
local actual_height = buffer:height(width)
|
height = math.min(height, buffer:height(width))
|
||||||
height = math.min(height, actual_height)
|
|
||||||
|
|
||||||
win:expand {
|
win:expand {
|
||||||
field = 'height',
|
field = 'height',
|
||||||
target = height,
|
target = height,
|
||||||
}
|
}
|
||||||
self.run = false
|
this.run = false
|
||||||
return
|
return
|
||||||
elseif res == false then
|
elseif res == false then
|
||||||
lists:remove(i)
|
lists:remove(i)
|
||||||
@ -350,7 +344,7 @@ local function online_query(win, word)
|
|||||||
|
|
||||||
if size == 0 or times == win_width then
|
if size == 0 or times == win_width then
|
||||||
buffer:addline(error_line, 1)
|
buffer:addline(error_line, 1)
|
||||||
self.run = false
|
this.run = false
|
||||||
else
|
else
|
||||||
buffer:addline(it(s:format(spinner[times % range + 1], cell:rep(times)), 'MoreMsg'), 1)
|
buffer:addline(it(s:format(spinner[times % range + 1], cell:rep(times)), 'MoreMsg'), 1)
|
||||||
end
|
end
|
||||||
@ -366,7 +360,6 @@ local function online_query(win, word)
|
|||||||
run(function()
|
run(function()
|
||||||
buffer:set('modifiable', false)
|
buffer:set('modifiable', false)
|
||||||
end)
|
end)
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
---处理不同hover模式的窗口
|
---处理不同hover模式的窗口
|
||||||
|
@ -1,34 +1,59 @@
|
|||||||
local api = vim.api
|
local api = vim.api
|
||||||
local display = require('Trans.util.display')
|
local display = require('Trans.util.display')
|
||||||
|
|
||||||
local window = {
|
---@class win
|
||||||
set_buf = function(self, buf)
|
---@field winid integer window handle
|
||||||
api.nvim_win_set_buf(self.winid, buf)
|
---@field width integer
|
||||||
end,
|
---@field height integer
|
||||||
|
---@field ns integer namespace for highlight
|
||||||
|
---@field animation table window animation
|
||||||
|
---@field buf buf buffer for attached
|
||||||
|
local window = {}
|
||||||
|
|
||||||
is_valid = function(self)
|
---Change window attached buffer
|
||||||
|
---@param buf buf
|
||||||
|
function window:set_buf(buf)
|
||||||
|
api.nvim_win_set_buf(self.winid, buf.bufnr)
|
||||||
|
self.buf = buf
|
||||||
|
end
|
||||||
|
|
||||||
|
---Check window valid
|
||||||
|
---@return boolean
|
||||||
|
function window:is_valid()
|
||||||
return api.nvim_win_is_valid(self.winid)
|
return api.nvim_win_is_valid(self.winid)
|
||||||
end,
|
end
|
||||||
|
|
||||||
set = function(self, option, value)
|
---Set window option
|
||||||
|
---@param option string option name
|
||||||
|
---@param value any
|
||||||
|
function window:set(option, value)
|
||||||
api.nvim_win_set_option(self.winid, option, value)
|
api.nvim_win_set_option(self.winid, option, value)
|
||||||
end,
|
end
|
||||||
|
|
||||||
option = function(self, name)
|
---@param name string option name
|
||||||
|
---@return any
|
||||||
|
function window:option(name)
|
||||||
return api.nvim_win_get_option(self.winid, name)
|
return api.nvim_win_get_option(self.winid, name)
|
||||||
end,
|
end
|
||||||
|
|
||||||
set_height = function(self, height)
|
---@param height integer
|
||||||
|
function window:set_height(height)
|
||||||
api.nvim_win_set_height(self.winid, height)
|
api.nvim_win_set_height(self.winid, height)
|
||||||
self.height = height
|
self.height = height
|
||||||
end,
|
end
|
||||||
|
|
||||||
set_width = function(self, width)
|
---@param width integer
|
||||||
|
function window:set_width(width)
|
||||||
api.nvim_win_set_width(self.winid, width)
|
api.nvim_win_set_width(self.winid, width)
|
||||||
self.width = width
|
self.width = width
|
||||||
end,
|
end
|
||||||
|
|
||||||
expand = function(self, opts)
|
---Expand window [width | height] value
|
||||||
|
---@param opts table 窗口的配置
|
||||||
|
---|'field'string [width | height]
|
||||||
|
---|'target'integer
|
||||||
|
---@return function
|
||||||
|
function window:expand(opts)
|
||||||
self:lock()
|
self:lock()
|
||||||
local field = opts.field
|
local field = opts.field
|
||||||
local target = opts.target
|
local target = opts.target
|
||||||
@ -38,12 +63,13 @@ local window = {
|
|||||||
local wrap = self:option('wrap')
|
local wrap = self:option('wrap')
|
||||||
self:set('wrap', false)
|
self:set('wrap', false)
|
||||||
local interval = opts.interval or self.animation.interval
|
local interval = opts.interval or self.animation.interval
|
||||||
local method = 'set_' .. field
|
local method = api['nvim_win_set_' .. field]
|
||||||
|
|
||||||
|
local winid = self.winid
|
||||||
local frame = target > cur and function(_, cur_times)
|
local frame = target > cur and function(_, cur_times)
|
||||||
self[method](self, cur + cur_times)
|
method(winid, cur + cur_times)
|
||||||
end or function(_, cur_times)
|
end or function(_, cur_times)
|
||||||
self[method](self, cur - cur_times)
|
method(winid, cur - cur_times)
|
||||||
end
|
end
|
||||||
|
|
||||||
local run = display {
|
local run = display {
|
||||||
@ -54,14 +80,15 @@ local window = {
|
|||||||
|
|
||||||
run(function()
|
run(function()
|
||||||
self:set('wrap', wrap)
|
self:set('wrap', wrap)
|
||||||
|
self[field] = target
|
||||||
self:unlock()
|
self:unlock()
|
||||||
end)
|
end)
|
||||||
return run
|
return run
|
||||||
end,
|
end
|
||||||
|
|
||||||
try_close = function(self)
|
---Close window
|
||||||
if self:is_valid() then
|
---@return function run run until close done
|
||||||
local winid = self.winid
|
function window:try_close()
|
||||||
local field = ({
|
local field = ({
|
||||||
slid = 'width',
|
slid = 'width',
|
||||||
fold = 'height',
|
fold = 'height',
|
||||||
@ -73,56 +100,76 @@ local window = {
|
|||||||
target = 1,
|
target = 1,
|
||||||
}
|
}
|
||||||
run(function()
|
run(function()
|
||||||
api.nvim_win_close(winid, true)
|
api.nvim_win_close(self.winid, true)
|
||||||
end)
|
end)
|
||||||
return run
|
return run
|
||||||
end
|
end
|
||||||
end,
|
|
||||||
|
|
||||||
lock = function(self)
|
---lock window [open | close] operation
|
||||||
|
function window:lock()
|
||||||
while self.busy do
|
while self.busy do
|
||||||
vim.wait(50)
|
vim.wait(50)
|
||||||
end
|
end
|
||||||
self.busy = true
|
self.busy = true
|
||||||
|
end
|
||||||
|
|
||||||
end,
|
function window:unlock()
|
||||||
|
|
||||||
unlock = function(self)
|
|
||||||
self.busy = false
|
self.busy = false
|
||||||
end,
|
end
|
||||||
|
|
||||||
set_hl = function(self, name, opts)
|
---设置窗口本地的高亮组
|
||||||
|
---@param name string 高亮组的名称
|
||||||
|
---@param opts table 高亮选项
|
||||||
|
function window:set_hl(name, opts)
|
||||||
api.nvim_set_hl(self.ns, name, opts)
|
api.nvim_set_hl(self.ns, name, opts)
|
||||||
end,
|
end
|
||||||
|
|
||||||
center = function(self, node)
|
---buffer:addline() helper function
|
||||||
|
---@param node table
|
||||||
|
---@return table node formatted node
|
||||||
|
function window:center(node)
|
||||||
local text = node[1]
|
local text = node[1]
|
||||||
local width = text:width()
|
local width = text:width()
|
||||||
local win_width = self.width
|
local win_width = self.width
|
||||||
local space = math.max(math.floor((win_width - width) / 2), 0)
|
local space = math.max(math.floor((win_width - width) / 2), 0)
|
||||||
node[1] = (' '):rep(space) .. text
|
node[1] = (' '):rep(space) .. text
|
||||||
return node
|
return node
|
||||||
end,
|
end
|
||||||
}
|
|
||||||
|
---@private
|
||||||
window.__index = window
|
window.__index = window
|
||||||
|
|
||||||
---window的构造函数
|
---@class win_opts
|
||||||
---@param opts table
|
---@field buf buf buffer for attached
|
||||||
|
---@field height integer
|
||||||
|
---@field width integer
|
||||||
|
---@field col integer
|
||||||
|
---@field row integer
|
||||||
|
---@field border string
|
||||||
|
---@field title string | nil | table
|
||||||
|
---@field relative string
|
||||||
|
---@field ns integer namespace for highlight
|
||||||
|
---@field zindex? integer
|
||||||
|
---@field enter? boolean cursor should [enter] window
|
||||||
|
---@field animation table window animation
|
||||||
|
|
||||||
|
---window constructor
|
||||||
|
---@param opts win_opts
|
||||||
---@return table
|
---@return table
|
||||||
---@return function
|
---@return function
|
||||||
return function(opts)
|
return function(opts)
|
||||||
assert(type(opts) == 'table')
|
assert(type(opts) == 'table')
|
||||||
|
local ns = opts.ns
|
||||||
local buf = opts.buf
|
local buf = opts.buf
|
||||||
local height = opts.height
|
|
||||||
local width = opts.width
|
|
||||||
local col = opts.col
|
local col = opts.col
|
||||||
local row = opts.row
|
local row = opts.row
|
||||||
local border = opts.border
|
|
||||||
local title = opts.title
|
local title = opts.title
|
||||||
|
local width = opts.width
|
||||||
|
local enter = opts.enter or false
|
||||||
|
local height = opts.height
|
||||||
|
local border = opts.border
|
||||||
|
local zindex = opts.zindex
|
||||||
local relative = opts.relative
|
local relative = opts.relative
|
||||||
local zindex = opts.zindex or 100
|
|
||||||
local enter = opts.enter
|
|
||||||
local ns = opts.ns
|
|
||||||
local animation = opts.animation
|
local animation = opts.animation
|
||||||
|
|
||||||
local open = animation.open
|
local open = animation.open
|
||||||
|
Loading…
x
Reference in New Issue
Block a user