fix: disable autocmd when buf is closed

This commit is contained in:
amrbashir
2022-05-15 10:28:13 +02:00
parent 4d491dc47f
commit 66cb6a423f
2 changed files with 39 additions and 35 deletions

View File

@@ -5,19 +5,6 @@ A neovim plugin to display lsp hover documentation in a side panel.
## Installation ## 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) Using [packer.nvim](https://github.com/wbthomason/packer.nvim)
```lua ```lua
@@ -28,12 +15,25 @@ use {
config = function() config = function()
require("docs-view").setup { require("docs-view").setup {
position = "right", position = "right",
width = 30, width = vim.api.nvim_get_option("columns") / 3,
} }
end 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 ## Usage
Use `:DocsViewToggle` to open/close the docs view side panel Use `:DocsViewToggle` to open/close the docs view side panel

View File

@@ -14,13 +14,12 @@ M.setup = function(conf)
end end
end end
local buf, win, prev_win local buf, win, prev_win, autocmd
M.toggle = function() M.toggle = function()
if win and vim.api.nvim_win_is_valid(win) then if win and vim.api.nvim_win_is_valid(win) then
vim.api.nvim_win_close(win, true) vim.api.nvim_win_close(win, true)
win = nil vim.api.nvim_del_autocmd(autocmd)
buf = nil buf, win, prev_win, autocmd = nil
prev_win = nil
else else
prev_win = vim.api.nvim_get_current_win() prev_win = vim.api.nvim_get_current_win()
@@ -44,25 +43,30 @@ M.toggle = function()
vim.api.nvim_set_current_win(prev_win) vim.api.nvim_set_current_win(prev_win)
vim.api.nvim_create_autocmd( autocmd = vim.api.nvim_create_autocmd(
{ "CursorHold" }, { "CursorHold", "CursorHoldI" },
{ pattern = "*", callback = function() { pattern = "*", callback = function()
local l,c = unpack(vim.api.nvim_win_get_cursor(0)) if win and vim.api.nvim_win_is_valid(win) then
vim.lsp.buf_request(0, "textDocument/hover", { local l, c = unpack(vim.api.nvim_win_get_cursor(0))
textDocument = { uri = "file://"..vim.api.nvim_buf_get_name(0) }, vim.lsp.buf_request(0, "textDocument/hover", {
position = { line = l - 1, character = c } textDocument = { uri = "file://"..vim.api.nvim_buf_get_name(0) },
}, function(err, result, ctx, config) position = { line = l - 1, character = c }
if not (result and result.contents) then return end }, function(err, result, ctx, config)
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
local md_lines = vim.lsp.util.convert_input_to_markdown_lines(result.contents) vim.api.nvim_buf_set_option(buf, "modifiable", true)
md_lines = vim.lsp.util.trim_empty_lines(md_lines) vim.lsp.util.stylize_markdown(buf, md_lines)
if vim.tbl_isempty(md_lines) then return end vim.api.nvim_buf_set_option(buf, "modifiable", false)
end
vim.api.nvim_buf_set_option(buf, "modifiable", true) end
vim.lsp.util.stylize_markdown(buf, md_lines) )
vim.api.nvim_buf_set_option(buf, "modifiable", false) else
end vim.api.nvim_del_autocmd(autocmd)
) buf, win, prev_win, autocmd = nil
end
end end
} }
) )