rework(preview): Ask providers for hover info

Now lsp checks if client supports hover
Addresses #82
This commit is contained in:
simrat39
2021-11-24 21:44:23 -08:00
parent 552b67993e
commit f4f2b5edfb
5 changed files with 251 additions and 207 deletions

View File

@@ -1,7 +1,7 @@
local vim = vim
local main = require('symbols-outline')
local config = require('symbols-outline.config')
local buf_request = require('symbols-outline.utils.lsp_utils').request
local main = require("symbols-outline")
local config = require("symbols-outline.config")
local buf_request = require("symbols-outline.utils.lsp_utils").request
local M = {}
@@ -9,7 +9,7 @@ local state = {
preview_buf = nil,
preview_win = nil,
hover_buf = nil,
hover_win = nil
hover_win = nil,
}
local function is_current_win_outline()
@@ -19,7 +19,9 @@ end
local function has_code_win()
local isWinValid = vim.api.nvim_win_is_valid(main.state.code_win)
if not isWinValid then return false end
if not isWinValid then
return false
end
local bufnr = vim.api.nvim_win_get_buf(main.state.code_win)
local isBufValid = vim.api.nvim_buf_is_valid(bufnr)
return isBufValid
@@ -31,10 +33,10 @@ local function get_offset()
local height = 0
if config.has_numbers() then
width = width + 4;
width = width + 4
end
if config.options.position == 'right' then
if config.options.position == "right" then
width = 0 - width
else
width = vim.api.nvim_win_get_width(outline_winnr) + 1
@@ -57,13 +59,14 @@ local function update_preview(code_buf)
code_buf = code_buf or vim.api.nvim_win_get_buf(main.state.code_win)
local node = get_hovered_node()
if not node then return end
if not node then
return
end
local lines = vim.api.nvim_buf_get_lines(code_buf, 0, -1, false)
if state.preview_buf ~= nil then
vim.api.nvim_buf_set_lines(state.preview_buf, 0, -1, 0, lines)
vim.api.nvim_win_set_cursor(state.preview_win,
{node.line + 1, node.character})
vim.api.nvim_win_set_cursor(state.preview_win, { node.line + 1, node.character })
end
end
@@ -72,7 +75,7 @@ local function setup_preview_buf()
local ft = vim.api.nvim_buf_get_option(code_buf, "filetype")
local function treesitter_attach()
local ts_highlight = require('nvim-treesitter.highlight')
local ts_highlight = require("nvim-treesitter.highlight")
ts_highlight.attach(state.preview_buf, ft)
end
@@ -93,24 +96,30 @@ local function get_hover_params(node, winnr)
return {
textDocument = { uri = uri },
position = { line = node.line, character = node.character },
bufnr = bufnr
bufnr = bufnr,
}
end
local function update_hover()
if not has_code_win() then return end
if not has_code_win() then
return
end
local node = get_hovered_node()
if not node then return end
if not node then
return
end
local provider = _G._symbols_outline_current_provider
local params = get_hover_params(node, main.state.code_win)
buf_request(params.bufnr, "textDocument/hover", params,
function(err, result)
if err then print(vim.inspect(err)) end
provider.hover_info(params.bufnr, params, function(err, result)
if err then
print(vim.inspect(err))
end
local markdown_lines = {}
if result ~= nil then
markdown_lines = vim.lsp.util.convert_input_to_markdown_lines(
result.contents)
markdown_lines = vim.lsp.util.convert_input_to_markdown_lines(result.contents)
end
markdown_lines = vim.lsp.util.trim_empty_lines(markdown_lines)
if vim.tbl_isempty(markdown_lines) then
@@ -126,7 +135,9 @@ local function update_hover()
end
local function setup_hover_buf()
if not has_code_win() then return end
if not has_code_win() then
return
end
local code_buf = vim.api.nvim_win_get_buf(main.state.code_win)
local ft = vim.api.nvim_buf_get_option(code_buf, "filetype")
vim.api.nvim_buf_set_option(state.hover_buf, "syntax", ft)
@@ -149,17 +160,17 @@ local function show_preview()
on_detach = function()
state.preview_buf = nil
state.preview_win = nil
end
end,
})
local offsets = get_offset()
state.preview_win = vim.api.nvim_open_win(state.preview_buf, false, {
relative = 'win',
relative = "win",
width = 50,
height = get_height(),
bufpos = { 0, 0 },
row = offsets[1],
col = offsets[2],
border = 'single'
border = "single",
})
setup_preview_buf()
else
@@ -174,18 +185,18 @@ local function show_hover()
on_detach = function()
state.hover_buf = nil
state.hover_win = nil
end
end,
})
local offsets = get_offset()
local height = get_height()
state.hover_win = vim.api.nvim_open_win(state.hover_buf, false, {
relative = 'win',
relative = "win",
width = 50,
height = height,
bufpos = { 0, 0 },
row = offsets[1] + height + 2,
col = offsets[2],
border = 'single'
border = "single",
})
setup_hover_buf()
else
@@ -194,8 +205,7 @@ local function show_hover()
end
function M.show()
if not is_current_win_outline() or
#vim.api.nvim_list_wins() < 2 then
if not is_current_win_outline() or #vim.api.nvim_list_wins() < 2 then
return
end
@@ -206,12 +216,10 @@ end
function M.close()
if has_code_win() then
if state.preview_win ~= nil and
vim.api.nvim_win_is_valid(state.preview_win) then
if state.preview_win ~= nil and vim.api.nvim_win_is_valid(state.preview_win) then
vim.api.nvim_win_close(state.preview_win, true)
end
if state.hover_win ~= nil and
vim.api.nvim_win_is_valid(state.hover_win) then
if state.hover_win ~= nil and vim.api.nvim_win_is_valid(state.hover_win) then
vim.api.nvim_win_close(state.hover_win, true)
end
end

View File

@@ -3,19 +3,28 @@ local M = {}
function M.should_use_provider(_)
local coc_installed = vim.fn.exists("*CocActionAsync")
if coc_installed == 0 then return end
if coc_installed == 0 then
return
end
local coc_attached = vim.fn.call('CocAction', {'ensureDocument'})
local has_symbols = vim.fn.call('CocHasProvider', {'documentSymbol'})
local coc_attached = vim.fn.call("CocAction", { "ensureDocument" })
local has_symbols = vim.fn.call("CocHasProvider", { "documentSymbol" })
return coc_attached and has_symbols;
return coc_attached and has_symbols
end
function M.hover_info(_, _, on_info)
on_info(nil, { contents = { kind = "markdown", contents = { "No extra information availaible!" } } })
end
---@param on_symbols function
function M.request_symbols(on_symbols)
vim.fn.call('CocActionAsync', {'documentSymbols', function (_, symbols)
on_symbols{[1000000]={result=symbols}}
end})
vim.fn.call("CocActionAsync", {
"documentSymbols",
function(_, symbols)
on_symbols({ [1000000] = { result = symbols } })
end,
})
end
return M

View File

@@ -1,19 +1,19 @@
local M = {}
local providers = {
'symbols-outline/providers/nvim-lsp',
'symbols-outline/providers/coc',
'symbols-outline/providers/markdown'
"symbols-outline/providers/nvim-lsp",
"symbols-outline/providers/coc",
"symbols-outline/providers/markdown",
}
local current_provider;
_G._symbols_outline_current_provider = nil
function M.has_provider()
local ret = false;
local ret = false
for _, value in ipairs(providers) do
local provider = require(value)
if provider.should_use_provider(0) then
ret = true;
ret = true
break
end
end
@@ -25,6 +25,7 @@ function M.request_symbols(on_symbols)
for _, value in ipairs(providers) do
local provider = require(value)
if provider.should_use_provider(0) then
_G._symbols_outline_current_provider = provider
provider.request_symbols(on_symbols)
break
end

View File

@@ -1,10 +1,14 @@
local md_parser = require('symbols-outline.markdown')
local md_parser = require("symbols-outline.markdown")
local M = {}
-- probably change this
function M.should_use_provider(bufnr)
return vim.api.nvim_buf_get_option(bufnr, 'ft') == 'markdown'
return vim.api.nvim_buf_get_option(bufnr, "ft") == "markdown"
end
function M.hover_info(_, _, on_info)
on_info(nil, { contents = { kind = "markdown", contents = { "No extra information availaible!" } } })
end
---@param on_symbols function

View File

@@ -1,4 +1,4 @@
local config = require('symbols-outline.config')
local config = require("symbols-outline.config")
local M = {}
@@ -6,6 +6,29 @@ local function getParams()
return { textDocument = vim.lsp.util.make_text_document_params() }
end
function M.hover_info(bufnr, params, on_info)
local clients = vim.lsp.buf_get_clients(bufnr)
local used_client
for id, client in pairs(clients) do
if config.is_client_blacklisted(id) then
goto continue
else
if client.server_capabilities.hoverProvider then
used_client = client
break
end
end
::continue::
end
if not used_client then
on_info(nil, { contents = { kind = "markdown", content = { "No extra information availaible!" } } })
end
used_client.request("textDocument/hover", params, on_info, bufnr)
end
-- probably change this
function M.should_use_provider(bufnr)
local clients = vim.lsp.buf_get_clients(bufnr)
@@ -28,8 +51,7 @@ end
---@param on_symbols function
function M.request_symbols(on_symbols)
vim.lsp.buf_request_all(0, "textDocument/documentSymbol", getParams(),
on_symbols)
vim.lsp.buf_request_all(0, "textDocument/documentSymbol", getParams(), on_symbols)
end
return M