diff dot_config/nvim/config.lua @ 330:471701051c92

Add peek_declaration to show in floating preview window
author Zeger Van de Vannet <zegervdv@me.com>
date Tue, 12 Jan 2021 16:53:05 +0100
parents 111f178824b9
children d43ba211f920
line wrap: on
line diff
--- 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", "<c-n>", "<Plug>(completion_trigger)", false)
   mapper("i", "<c-j>", "<Plug>(completion_next_source)", false)
   mapper("i", "<c-k>", "<Plug>(completion_prev_source)", false)
+  mapper("n", "gp", "<cmd>lua peek_definition()<CR>")
 end