# HG changeset patch # User Zeger Van de Vannet # Date 1610466785 -3600 # Node ID 471701051c92b032ca6b8a55d5b4d48ac6e1b45d # Parent 111f178824b995155633ae62948774a65c2fde9c Add peek_declaration to show in floating preview window diff -r 111f178824b9 -r 471701051c92 dot_config/nvim/config.lua --- a/dot_config/nvim/config.lua Tue Jan 12 16:35:09 2021 +0100 +++ b/dot_config/nvim/config.lua Tue Jan 12 16:53:05 2021 +0100 @@ -189,6 +189,59 @@ } } +-- Copied and modified from https://github.com/chengzeyi/.vim_runtime/blob/8a47981c81d31f88d1138211908e58fd58e4decc/lua/lsp_ext.lua + +function preview_location(location, context, before_context) + -- location may be LocationLink or Location (more useful for the former) + context = context or 15 + before_context = before_context or 0 + local uri = location.targetUri or location.uri + if uri == nil then + return + end + local bufnr = vim.uri_to_bufnr(uri) + if not vim.api.nvim_buf_is_loaded(bufnr) then + vim.fn.bufload(bufnr) + end + local range = location.targetRange or location.range + local contents = + vim.api.nvim_buf_get_lines(bufnr, range.start.line - before_context, range['end'].line + 1 + context, false) + local filetype = vim.api.nvim_buf_get_option(bufnr, 'filetype') + return vim.lsp.util.open_floating_preview(contents, filetype) +end + +function preview_location_callback(_, method, result) + local context = 15 + if result == nil or vim.tbl_isempty(result) then + return nil + end + if vim.tbl_islist(result) then + preview_location(result[1], context, 5) + else + preview_location(result, context, 5) + end +end + +function peek_declaration() + local params = vim.lsp.util.make_position_params() + return vim.lsp.buf_request(0, 'textDocument/declaration', params, preview_location_callback) +end + +function peek_definition() + local params = vim.lsp.util.make_position_params() + return vim.lsp.buf_request(0, 'textDocument/definition', params, preview_location_callback) +end + +function peek_type_definition() + local params = vim.lsp.util.make_position_params() + return vim.lsp.buf_request(0, 'textDocument/typeDefinition', params, preview_location_callback) +end + +function peek_implementation() + local params = vim.lsp.util.make_position_params() + return vim.lsp.buf_request(0, 'textDocument/implementation', params, preview_location_callback) +end + local on_attach = function(client) require'completion'.on_attach({ sorting = 'alphabet', @@ -217,6 +270,7 @@ mapper("i", "", "(completion_trigger)", false) mapper("i", "", "(completion_next_source)", false) mapper("i", "", "(completion_prev_source)", false) + mapper("n", "gp", "lua peek_definition()") end