aboutsummaryrefslogtreecommitdiff
path: root/.config/nvim/lua/plugins/scripts/lsp-ext.lua
diff options
context:
space:
mode:
authorsrdusr <trevorgray@srdusr.com>2023-02-10 11:22:07 +0200
committersrdusr <trevorgray@srdusr.com>2023-02-10 11:22:07 +0200
commit7cee6e5a673defef72c803eca094d1247c31bb9c (patch)
tree1dbc97c5ece5f574ef5091d549fb434a8b59a625 /.config/nvim/lua/plugins/scripts/lsp-ext.lua
parentb91ee8da3ef2c1c154833b4b6e99250fe2c280e7 (diff)
parentf76f2c4bbc1ddde4b5c0882863060e4c58a11733 (diff)
downloaddotfiles-7cee6e5a673defef72c803eca094d1247c31bb9c.tar.gz
dotfiles-7cee6e5a673defef72c803eca094d1247c31bb9c.zip
Add '.config/nvim/' from commit 'e707f3abc83e0621eab64b4828defd0c80dff5c0'
git-subtree-dir: .config/nvim git-subtree-mainline: bb29321714929e1b7b962dd47b486325fd77e67a git-subtree-split: e707f3abc83e0621eab64b4828defd0c80dff5c0
Diffstat (limited to '.config/nvim/lua/plugins/scripts/lsp-ext.lua')
-rw-r--r--.config/nvim/lua/plugins/scripts/lsp-ext.lua48
1 files changed, 48 insertions, 0 deletions
diff --git a/.config/nvim/lua/plugins/scripts/lsp-ext.lua b/.config/nvim/lua/plugins/scripts/lsp-ext.lua
new file mode 100644
index 0000000..c4378c6
--- /dev/null
+++ b/.config/nvim/lua/plugins/scripts/lsp-ext.lua
@@ -0,0 +1,48 @@
+--
+-- lsp-ext.lua
+
+
+M = {}
+
+function M.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 M.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
+ M.floating_buf, M.floating_win = M.preview_location(result[1], context)
+ else
+ M.floating_buf, M.floating_win = M.preview_location(result, context)
+ end
+end
+
+function M.peek_definition()
+ if vim.tbl_contains(vim.api.nvim_list_wins(), M.floating_win) then
+ vim.api.nvim_set_current_win(M.floating_win)
+ else
+ local params = vim.lsp.util.make_position_params()
+ return vim.lsp.buf_request(0, "textDocument/definition", params, M.preview_location_callback)
+ end
+end
+
+return M