summaryrefslogtreecommitdiff
path: root/lua/lsp
diff options
context:
space:
mode:
authorRafael <[email protected]>2021-07-12 04:21:07 +0000
committerGitHub <[email protected]>2021-07-12 00:21:07 -0400
commitc41d900bb9e2ac153dd36efcf6964a013b9ec1cc (patch)
tree085bda09ded3c11a382528aee19e971ae82b0a85 /lua/lsp
parentc26c09fadd54a9c6753a6f3c2eb11666b16b9264 (diff)
add function to preview definitions (#883)
* add function to preview definitions * add funciton to preview types and implementations * add borders Co-authored-by: Abouzar Parvan <[email protected]>
Diffstat (limited to 'lua/lsp')
-rw-r--r--lua/lsp/init.lua66
1 files changed, 66 insertions, 0 deletions
diff --git a/lua/lsp/init.lua b/lua/lsp/init.lua
index 218f8065..01f82737 100644
--- a/lua/lsp/init.lua
+++ b/lua/lsp/init.lua
@@ -20,6 +20,7 @@ vim.cmd "nnoremap <silent> gd <cmd>lua vim.lsp.buf.definition()<CR>"
vim.cmd "nnoremap <silent> gD <cmd>lua vim.lsp.buf.declaration()<CR>"
vim.cmd "nnoremap <silent> gr <cmd>lua vim.lsp.buf.references()<CR>"
vim.cmd "nnoremap <silent> gi <cmd>lua vim.lsp.buf.implementation()<CR>"
+vim.cmd "nnoremap <silent> gp <cmd>lua require'lsp'.PeekDefinition()<CR>"
vim.cmd "nnoremap <silent> K :lua vim.lsp.buf.hover()<CR>"
-- vim.cmd('nnoremap <silent> <C-k> <cmd>lua vim.lsp.buf.signature_help()<CR>')
vim.cmd "nnoremap <silent> <C-p> :lua vim.lsp.diagnostic.goto_prev({popup_opts = {border = O.lsp.popup_border}})<CR>"
@@ -103,6 +104,71 @@ local function documentHighlight(client, bufnr)
end
local lsp_config = {}
+-- Taken from https://www.reddit.com/r/neovim/comments/gyb077/nvimlsp_peek_defination_javascript_ttserver/
+function lsp_config.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, { border = O.lsp.popup_border })
+end
+
+function lsp_config.preview_location_callback(_, method, result)
+ local context = 15
+ if result == nil or vim.tbl_isempty(result) then
+ print("No location found: " .. method)
+ return nil
+ end
+ if vim.tbl_islist(result) then
+ lsp_config.floating_buf, lsp_config.floating_win = lsp_config.preview_location(result[1], context)
+ else
+ lsp_config.floating_buf, lsp_config.floating_win = lsp_config.preview_location(result, context)
+ end
+end
+
+function lsp_config.PeekDefinition()
+ if vim.tbl_contains(vim.api.nvim_list_wins(), lsp_config.floating_win) then
+ vim.api.nvim_set_current_win(lsp_config.floating_win)
+ else
+ local params = vim.lsp.util.make_position_params()
+ return vim.lsp.buf_request(0, "textDocument/definition", params, lsp_config.preview_location_callback)
+ end
+end
+
+function lsp_config.PeekTypeDefinition()
+ if vim.tbl_contains(vim.api.nvim_list_wins(), lsp_config.floating_win) then
+ vim.api.nvim_set_current_win(lsp_config.floating_win)
+ else
+ local params = vim.lsp.util.make_position_params()
+ return vim.lsp.buf_request(0, "textDocument/typeDefinition", params, lsp_config.preview_location_callback)
+ end
+end
+
+function lsp_config.PeekImplementation()
+ if vim.tbl_contains(vim.api.nvim_list_wins(), lsp_config.floating_win) then
+ vim.api.nvim_set_current_win(lsp_config.floating_win)
+ else
+ local params = vim.lsp.util.make_position_params()
+ return vim.lsp.buf_request(0, "textDocument/implementation", params, lsp_config.preview_location_callback)
+ end
+end
+
if O.lsp.document_highlight then
function lsp_config.common_on_attach(client, bufnr)
documentHighlight(client, bufnr)