From 71b231bb242383cc27cb3af19854ec0812db9404 Mon Sep 17 00:00:00 2001 From: amrbashir Date: Fri, 13 May 2022 17:38:49 +0200 Subject: [PATCH] init --- README.md | 22 +++++++++++++++++ lua/docs_view.lua | 59 ++++++++++++++++++++++++++++++++++++++++++++ plugin/docs_view.vim | 7 ++++++ 3 files changed, 88 insertions(+) create mode 100644 README.md create mode 100644 lua/docs_view.lua create mode 100644 plugin/docs_view.vim diff --git a/README.md b/README.md new file mode 100644 index 0000000..d606c3e --- /dev/null +++ b/README.md @@ -0,0 +1,22 @@ +# 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). + +### Installation + +Using [vim-plug](https://github.com/junegunn/vim-plug) + +```viml +Plug 'amrbashir/nvim-docs-view' +``` + +Using [packer.nvim](https://github.com/wbthomason/packer.nvim) + +```lua +use { 'amrbashir/nvim-docs-view' } +``` + +### Usage + +Use `:DocsViewOpen` to open the docs view side panel diff --git a/lua/docs_view.lua b/lua/docs_view.lua new file mode 100644 index 0000000..81394fc --- /dev/null +++ b/lua/docs_view.lua @@ -0,0 +1,59 @@ +local buf, win, start_win +local api = vim.api +local lsp = vim.lsp + +local function split(s, delimiter) + result = {}; + for match in (s..delimiter):gmatch("(.-)"..delimiter) do + table.insert(result, match); + end + return result; +end + +local function open() + if win and api.nvim_win_is_valid(win) then return end + + start_win = api.nvim_get_current_win() + + api.nvim_command("botright vnew") + + win = api.nvim_get_current_win() + buf = api.nvim_get_current_buf() + + api.nvim_buf_set_name(buf, "Docs View") + api.nvim_buf_set_option(buf, "buftype", "nofile") + api.nvim_buf_set_option(buf, "swapfile", false) + api.nvim_buf_set_option(buf, "bufhidden", "wipe") + api.nvim_buf_set_option(buf, "filetype", "nvim-docs-view") + + -- api.nvim_win_set_option(win, "wrap", false) + + api.nvim_set_current_win(start_win) + + api.nvim_create_autocmd( + { "CursorHold" }, + { pattern = "*", callback = function() + local l,c = unpack(api.nvim_win_get_cursor(0)) + lsp.buf_request(0, "textDocument/hover", { + textDocument = { uri = "file://"..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 = lsp.util.convert_input_to_markdown_lines(result.contents) + md_lines = lsp.util.trim_empty_lines(md_lines) + if vim.tbl_isempty(md_lines) then return end + + api.nvim_buf_set_option(buf, "modifiable", true) + lsp.util.stylize_markdown(buf, md_lines) + api.nvim_buf_set_option(buf, "modifiable", false) + end + ) + end + } + ) +end + +return { + open = open +} diff --git a/plugin/docs_view.vim b/plugin/docs_view.vim new file mode 100644 index 0000000..bbe1447 --- /dev/null +++ b/plugin/docs_view.vim @@ -0,0 +1,7 @@ +if exists('g:loaded_docs_view') + finish +endif +let g:loaded_docs_view = 1 + +command! DocsViewOpen lua require'docs_view'.open() +