feat: add setup function

This commit is contained in:
amrbashir
2022-05-13 21:00:25 +02:00
parent f49bcc50f0
commit f5768fa866
3 changed files with 52 additions and 28 deletions

View File

@@ -9,12 +9,27 @@ Using [vim-plug](https://github.com/junegunn/vim-plug)
```viml ```viml
Plug 'amrbashir/nvim-docs-view' Plug 'amrbashir/nvim-docs-view'
lua << EOF
require("docs-view").setup {
position = "right",
width = 300,
}
EOF
``` ```
Using [packer.nvim](https://github.com/wbthomason/packer.nvim) Using [packer.nvim](https://github.com/wbthomason/packer.nvim)
```lua ```lua
use { 'amrbashir/nvim-docs-view' } use {
"amrbashir/nvim-docs-view",
config = function()
require("docs-view").setup {
position = "right",
width = 300,
}
end
}
``` ```
## Usage ## Usage

View File

@@ -1,42 +1,53 @@
local M = {}
local config = {
position = "right",
width = 300,
}
M.setup = function(conf)
config.position = conf.position
config.width = conf.width
end
local buf, win, start_win local buf, win, start_win
local api = vim.api M.show = function()
local lsp = vim.lsp if win and vim.api.nvim_win_is_valid(win) then return end
local function show() start_win = vim.api.nvim_get_current_win()
if win and api.nvim_win_is_valid(win) then return end
start_win = api.nvim_get_current_win() vim.api.nvim_command("bot"..config.position.." vnew")
api.nvim_command("botright vnew") win = vim.api.nvim_get_current_win()
buf = vim.api.nvim_get_current_buf()
win = api.nvim_get_current_win() vim.api.nvim_win_set_width(win, config.position)
buf = api.nvim_get_current_buf()
api.nvim_buf_set_name(buf, "Docs View") vim.api.nvim_buf_set_name(buf, "Docs View")
api.nvim_buf_set_option(buf, "buftype", "nofile") vim.api.nvim_buf_set_option(buf, "buftype", "nofile")
api.nvim_buf_set_option(buf, "swapfile", false) vim.api.nvim_buf_set_option(buf, "swapfile", false)
api.nvim_buf_set_option(buf, "bufhidden", "wipe") vim.api.nvim_buf_set_option(buf, "bufhidden", "wipe")
api.nvim_buf_set_option(buf, "filetype", "nvim-docs-view") vim.api.nvim_buf_set_option(buf, "filetype", "nvim-docs-view")
api.nvim_set_current_win(start_win) vim.api.nvim_set_current_win(start_win)
api.nvim_create_autocmd( vim.api.nvim_create_autocmd(
{ "CursorHold" }, { "CursorHold" },
{ pattern = "*", callback = function() { pattern = "*", callback = function()
local l,c = unpack(api.nvim_win_get_cursor(0)) local l,c = unpack(vim.api.nvim_win_get_cursor(0))
lsp.buf_request(0, "textDocument/hover", { vim.lsp.buf_request(0, "textDocument/hover", {
textDocument = { uri = "file://"..api.nvim_buf_get_name(0) }, textDocument = { uri = "file://"..vim.api.nvim_buf_get_name(0) },
position = { line = l - 1, character = c } position = { line = l - 1, character = c }
}, function(err, result, ctx, config) }, function(err, result, ctx, config)
if not (result and result.contents) then return end if not (result and result.contents) then return end
local md_lines = lsp.util.convert_input_to_markdown_lines(result.contents) local md_lines = vim.lsp.util.convert_input_to_markdown_lines(result.contents)
md_lines = lsp.util.trim_empty_lines(md_lines) md_lines = vim.lsp.util.trim_empty_lines(md_lines)
if vim.tbl_isempty(md_lines) then return end if vim.tbl_isempty(md_lines) then return end
api.nvim_buf_set_option(buf, "modifiable", true) vim.api.nvim_buf_set_option(buf, "modifiable", true)
lsp.util.stylize_markdown(buf, md_lines) vim.lsp.util.stylize_markdown(buf, md_lines)
api.nvim_buf_set_option(buf, "modifiable", false) vim.api.nvim_buf_set_option(buf, "modifiable", false)
end end
) )
end end
@@ -44,6 +55,4 @@ local function show()
) )
end end
return { return M
show = show,
}

View File

@@ -3,5 +3,5 @@ if exists('g:loaded_docs_view')
endif endif
let g:loaded_docs_view = 1 let g:loaded_docs_view = 1
command! DocsViewShow lua require("docs-view").show() command! -nargs=0 DocsViewShow lua require("docs-view").show()