diff options
author | kylo252 <[email protected]> | 2021-07-31 06:06:08 +0200 |
---|---|---|
committer | GitHub <[email protected]> | 2021-07-31 00:06:08 -0400 |
commit | 9d89929d9bb47d1f78c2d3945b761da2f24a5643 (patch) | |
tree | 7f5d281ddb4bbdcecffe01c0234d02be32ede1f7 /lua/lsp/keybinds.lua | |
parent | 6f4dd8471b24f71ed6712fb6b9acc0ac79b9137a (diff) |
Enable querying lang-server formatting capabilities (#1078)
Diffstat (limited to 'lua/lsp/keybinds.lua')
-rw-r--r-- | lua/lsp/keybinds.lua | 67 |
1 files changed, 66 insertions, 1 deletions
diff --git a/lua/lsp/keybinds.lua b/lua/lsp/keybinds.lua index 820ebce9..cc0d4ec9 100644 --- a/lua/lsp/keybinds.lua +++ b/lua/lsp/keybinds.lua @@ -1,5 +1,70 @@ local M = {} +-- Taken from https://www.reddit.com/r/neovim/comments/gyb077/nvimlsp_peek_defination_javascript_ttserver/ +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, { border = lvim.lsp.popup_border }) +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.PeekDefinition() + 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 + +function M.PeekTypeDefinition() + 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/typeDefinition", params, M.preview_location_callback) + end +end + +function M.PeekImplementation() + 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/implementation", params, M.preview_location_callback) + end +end + function M.setup() if lvim.lsp.default_keybinds then vim.cmd "nnoremap <silent> gd <cmd>lua vim.lsp.buf.definition()<CR>" @@ -13,7 +78,7 @@ function M.setup() { noremap = true, silent = true } ) - vim.cmd "nnoremap <silent> gp <cmd>lua require'lsp.service'.PeekDefinition()<CR>" + vim.cmd "nnoremap <silent> gp <cmd>lua require'lsp.keybinds'.PeekDefinition()<CR>" vim.cmd "nnoremap <silent> K :lua vim.lsp.buf.hover()<CR>" vim.cmd "nnoremap <silent> <C-p> :lua vim.lsp.diagnostic.goto_prev({popup_opts = {border = lvim.lsp.popup_border}})<CR>" vim.cmd "nnoremap <silent> <C-n> :lua vim.lsp.diagnostic.goto_next({popup_opts = {border = lvim.lsp.popup_border}})<CR>" |