mirror of
https://github.com/tiyn/nvim-docs-view.git
synced 2025-11-13 13:59:46 +01:00
fix: disable autocmd when buf is closed
This commit is contained in:
28
README.md
28
README.md
@@ -5,19 +5,6 @@ A neovim plugin to display lsp hover documentation in a side panel.
|
||||
|
||||
## Installation
|
||||
|
||||
Using [vim-plug](https://github.com/junegunn/vim-plug)
|
||||
|
||||
```viml
|
||||
Plug 'amrbashir/nvim-docs-view', { 'on': 'DocsViewToggle'}
|
||||
|
||||
lua << EOF
|
||||
require("docs-view").setup {
|
||||
position = "right",
|
||||
width = 30,
|
||||
}
|
||||
EOF
|
||||
```
|
||||
|
||||
Using [packer.nvim](https://github.com/wbthomason/packer.nvim)
|
||||
|
||||
```lua
|
||||
@@ -28,12 +15,25 @@ use {
|
||||
config = function()
|
||||
require("docs-view").setup {
|
||||
position = "right",
|
||||
width = 30,
|
||||
width = vim.api.nvim_get_option("columns") / 3,
|
||||
}
|
||||
end
|
||||
}
|
||||
```
|
||||
|
||||
Using [vim-plug](https://github.com/junegunn/vim-plug)
|
||||
|
||||
```viml
|
||||
Plug 'amrbashir/nvim-docs-view', { 'on': 'DocsViewToggle'}
|
||||
|
||||
lua << EOF
|
||||
require("docs-view").setup {
|
||||
position = "right",
|
||||
width = vim.api.nvim_get_option("columns") / 3,
|
||||
}
|
||||
EOF
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
Use `:DocsViewToggle` to open/close the docs view side panel
|
||||
|
||||
@@ -14,13 +14,12 @@ M.setup = function(conf)
|
||||
end
|
||||
end
|
||||
|
||||
local buf, win, prev_win
|
||||
local buf, win, prev_win, autocmd
|
||||
M.toggle = function()
|
||||
if win and vim.api.nvim_win_is_valid(win) then
|
||||
vim.api.nvim_win_close(win, true)
|
||||
win = nil
|
||||
buf = nil
|
||||
prev_win = nil
|
||||
vim.api.nvim_del_autocmd(autocmd)
|
||||
buf, win, prev_win, autocmd = nil
|
||||
else
|
||||
prev_win = vim.api.nvim_get_current_win()
|
||||
|
||||
@@ -44,16 +43,16 @@ M.toggle = function()
|
||||
|
||||
vim.api.nvim_set_current_win(prev_win)
|
||||
|
||||
vim.api.nvim_create_autocmd(
|
||||
{ "CursorHold" },
|
||||
autocmd = vim.api.nvim_create_autocmd(
|
||||
{ "CursorHold", "CursorHoldI" },
|
||||
{ pattern = "*", callback = function()
|
||||
if win and vim.api.nvim_win_is_valid(win) then
|
||||
local l, c = unpack(vim.api.nvim_win_get_cursor(0))
|
||||
vim.lsp.buf_request(0, "textDocument/hover", {
|
||||
textDocument = { uri = "file://"..vim.api.nvim_buf_get_name(0) },
|
||||
position = { line = l - 1, character = c }
|
||||
}, function(err, result, ctx, config)
|
||||
if not (result and result.contents) then return end
|
||||
|
||||
if win and vim.api.nvim_win_is_valid(win) and result and result.contents then
|
||||
local md_lines = vim.lsp.util.convert_input_to_markdown_lines(result.contents)
|
||||
md_lines = vim.lsp.util.trim_empty_lines(md_lines)
|
||||
if vim.tbl_isempty(md_lines) then return end
|
||||
@@ -62,7 +61,12 @@ M.toggle = function()
|
||||
vim.lsp.util.stylize_markdown(buf, md_lines)
|
||||
vim.api.nvim_buf_set_option(buf, "modifiable", false)
|
||||
end
|
||||
end
|
||||
)
|
||||
else
|
||||
vim.api.nvim_del_autocmd(autocmd)
|
||||
buf, win, prev_win, autocmd = nil
|
||||
end
|
||||
end
|
||||
}
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user