feat: add update_mode option and :DocsViewUpdate, closes #6

This commit is contained in:
amrbashir
2023-02-12 00:41:55 +02:00
parent 6981376235
commit 601d7f6a2e
2 changed files with 93 additions and 52 deletions

View File

@@ -1,6 +1,7 @@
# nvim-docs-view
A neovim plugin to display lsp hover documentation in a side panel.
> Inspired by the VSCode extension [Docs View](https://marketplace.visualstudio.com/items?itemName=bierner.docs-view).
<img alt="doc-view-example" src="demo.gif" width="500" />
@@ -36,9 +37,31 @@ lua << EOF
EOF
```
## Usage
## Options
Use `:DocsViewToggle` to open/close the docs view panel
- `position`:
- description: Determines where to open the docs view panel.
- type: `string`
- default: `right`
- possible: `right` | `left` | `top` | `bottom`
- `height`:
- description: Hieght of the docs view panel when position is set to `top` or `bottom`
- type: `number`
- default: `10`
- `width`:
- description: Width of the docs view panel when position is set to `right` or `left`
- type: `number`
- default: `60`
- `update_mode`:
- description: Determines the mechanism used to update the docs view panel content. If `auto`, the content will update upon cursor move. If `manual`, the content will only update once `:DocsViewUpdate` is called.
- type: `string`
- default: `auto`
- possible: `auto` | `manual`
## Commands
- `:DocsViewToggle` to open/close the docs view panel.
- `:DocsViewUpdate` to manually update the docs view panel (will open the docs view panel if necessary).
## LICENSE

View File

@@ -6,11 +6,17 @@ local get_clients
local function toggle()
if win and vim.api.nvim_win_is_valid(win) then
vim.api.nvim_win_close(win, false)
if autocmd then
vim.api.nvim_del_autocmd(autocmd)
end
buf, win, prev_win, autocmd = nil, nil, nil, nil
else
local height = cfg["height"]
local width = cfg["width"]
local update_mode = cfg["update_mode"]
if update_mode ~= "manual" then
update_mode = "auto"
end
prev_win = vim.api.nvim_get_current_win()
@@ -43,20 +49,27 @@ local function toggle()
vim.api.nvim_set_current_win(prev_win)
if vim.fn.has("nvim-0.8.0") then
get_clients = function()
return vim.lsp.get_active_clients()
end
else
get_clients = function()
return vim.lsp.buf_get_clients(0)
end
end
if update_mode == "auto" then
autocmd = vim.api.nvim_create_autocmd({ "CursorHold", "CursorHoldI" }, {
pattern = "*",
callback = function()
if win and vim.api.nvim_win_is_valid(win) then
update()
else
vim.api.nvim_del_autocmd(autocmd)
buf, win, prev_win, autocmd = nil, nil, nil, nil
end
end,
})
end
end
end
local function update()
if not win or not vim.api.nvim_win_is_valid(win) then
toggle()
end
local clients = get_clients()
local gotHover = false
for i = 1, #clients do
@@ -86,13 +99,6 @@ local function toggle()
vim.api.nvim_buf_set_option(buf, "modifiable", false)
end
end)
else
vim.api.nvim_del_autocmd(autocmd)
buf, win, prev_win, autocmd = nil, nil, nil, nil
end
end,
})
end
end
M.setup = function(user_cfg)
@@ -100,11 +106,23 @@ M.setup = function(user_cfg)
position = "right",
height = 10,
width = 60,
update_mode = "auto",
}
cfg = vim.tbl_extend("force", default_cfg, user_cfg)
if vim.fn.has("nvim-0.8.0") then
get_clients = function()
return vim.lsp.get_active_clients()
end
else
get_clients = function()
return vim.lsp.buf_get_clients(0)
end
end
vim.api.nvim_create_user_command("DocsViewToggle", toggle, { nargs = 0 })
vim.api.nvim_create_user_command("DocsViewUpdate", update, { nargs = 0 })
end
return M