changeset 446:9c575d146453

Add mapping to open definition in split
author Zeger Van de Vannet <zegervdv@me.com>
date Wed, 01 Sep 2021 11:14:31 +0200
parents 34fb1bd9986f
children aa15d8b06f04
files dot_config/nvim/init.lua
diffstat 1 files changed, 30 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/dot_config/nvim/init.lua	Wed Sep 01 11:13:53 2021 +0200
+++ b/dot_config/nvim/init.lua	Wed Sep 01 11:14:31 2021 +0200
@@ -569,6 +569,36 @@
   local inoremap = vim.keymap.inoremap
   nnoremap { 'gd', vim.lsp.buf.declaration, silent = true }
   nnoremap { '<c-]>', vim.lsp.buf.definition, silent = true }
+  nnoremap { 
+    'g<c-]>',
+    function ()
+      local params = vim.lsp.util.make_position_params()
+      opts = {}
+      local results_lsp = vim.lsp.buf_request_sync(0, "textDocument/definition", params, opts.timeout or 10000)
+          if not results_lsp or vim.tbl_isempty(results_lsp) then
+              print("No results from textDocument/definition")
+              return
+          end
+        for _, lsp_data in pairs(results_lsp) do
+          if lsp_data ~= nil and lsp_data.result ~= nil and not vim.tbl_isempty(lsp_data.result) then
+            for _, value in pairs(lsp_data.result) do
+              local range = value.range or value.targetRange
+              if range ~= nil then
+                local file = value.uri or value.targetUri
+                if file ~=nil then
+                  vim.api.nvim_command [[split]]
+                  vim.lsp.util.jump_to_location(value)
+                  return
+                end
+              end
+            end
+          end
+        end
+        -- try to call default lsp function
+        vim.lsp.buf.definition()
+    end,
+    silent = true
+  }
   nnoremap { 'K', vim.lsp.buf.hover, silent = true }
   nnoremap { 'gD', vim.lsp.buf.implementation, silent = true }
   nnoremap { '1gD', vim.lsp.buf.type_definition, silent = true }