From e30462dff67bfaf5babacae31b0d8462a7c058e8 Mon Sep 17 00:00:00 2001 From: amrbashir Date: Sat, 14 May 2022 13:53:41 +0200 Subject: [PATCH] feat: toggle docs view instead of just showing it --- README.md | 6 ++- lua/docs-view.lua | 95 +++++++++++++++++++++++--------------------- plugin/docs-view.vim | 2 +- 3 files changed, 55 insertions(+), 48 deletions(-) diff --git a/README.md b/README.md index 2e4a60d..5ecbc5c 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ A neovim plugin to display lsp hover documentation in a side panel. Using [vim-plug](https://github.com/junegunn/vim-plug) ```viml -Plug 'amrbashir/nvim-docs-view' +Plug 'amrbashir/nvim-docs-view', { 'on': 'DocsViewToggle'} lua << EOF require("docs-view").setup { @@ -23,6 +23,8 @@ Using [packer.nvim](https://github.com/wbthomason/packer.nvim) ```lua use { "amrbashir/nvim-docs-view", + opt = true, + cmd = { "DocsViewToggle" }, config = function() require("docs-view").setup { position = "right", @@ -34,7 +36,7 @@ use { ## Usage -Use `:DocsViewShow` to open the docs view side panel +Use `:DocsViewToggle` to open/close the docs view side panel ## LICENSE diff --git a/lua/docs-view.lua b/lua/docs-view.lua index a09630b..85b513c 100644 --- a/lua/docs-view.lua +++ b/lua/docs-view.lua @@ -14,53 +14,58 @@ M.setup = function(conf) end end -local buf, win, start_win -M.show = function() - if win and vim.api.nvim_win_is_valid(win) then return end - - start_win = vim.api.nvim_get_current_win() - - if config.position == "left" then - vim.api.nvim_command("topleft vnew") +local buf, win, prev_win +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 else - vim.api.nvim_command("botright vnew") + prev_win = vim.api.nvim_get_current_win() + + if config.position == "left" then + vim.api.nvim_command("topleft vnew") + else + vim.api.nvim_command("botright vnew") + end + + win = vim.api.nvim_get_current_win() + buf = vim.api.nvim_get_current_buf() + + vim.api.nvim_win_set_width(win, math.ceil(config.width)) + + vim.api.nvim_buf_set_name(buf, "Docs View") + vim.api.nvim_buf_set_option(buf, "buftype", "nofile") + vim.api.nvim_buf_set_option(buf, "swapfile", false) + vim.api.nvim_buf_set_option(buf, "bufhidden", "wipe") + vim.api.nvim_buf_set_option(buf, "filetype", "nvim-docs-view") + + vim.api.nvim_set_current_win(prev_win) + + vim.api.nvim_create_autocmd( + { "CursorHold" }, + { pattern = "*", callback = function() + 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 + + 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 + + vim.api.nvim_buf_set_option(buf, "modifiable", true) + vim.lsp.util.stylize_markdown(buf, md_lines) + vim.api.nvim_buf_set_option(buf, "modifiable", false) + end + ) + end + } + ) end - - win = vim.api.nvim_get_current_win() - buf = vim.api.nvim_get_current_buf() - - vim.api.nvim_win_set_width(win, math.ceil(config.width)) - - vim.api.nvim_buf_set_name(buf, "Docs View") - vim.api.nvim_buf_set_option(buf, "buftype", "nofile") - vim.api.nvim_buf_set_option(buf, "swapfile", false) - vim.api.nvim_buf_set_option(buf, "bufhidden", "wipe") - vim.api.nvim_buf_set_option(buf, "filetype", "nvim-docs-view") - - vim.api.nvim_set_current_win(start_win) - - vim.api.nvim_create_autocmd( - { "CursorHold" }, - { pattern = "*", callback = function() - 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 - - 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 - - vim.api.nvim_buf_set_option(buf, "modifiable", true) - vim.lsp.util.stylize_markdown(buf, md_lines) - vim.api.nvim_buf_set_option(buf, "modifiable", false) - end - ) - end - } - ) end return M diff --git a/plugin/docs-view.vim b/plugin/docs-view.vim index 3b87504..295d8d4 100644 --- a/plugin/docs-view.vim +++ b/plugin/docs-view.vim @@ -3,5 +3,5 @@ if exists('g:loaded_docs_view') endif let g:loaded_docs_view = 1 -command! -nargs=0 DocsViewShow lua require("docs-view").show() +command! -nargs=0 DocsViewToggle lua require("docs-view").toggle()