comparison 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
comparison
equal deleted inserted replaced
329:111f178824b9 330:471701051c92
187 }, 187 },
188 comment = {}, 188 comment = {},
189 } 189 }
190 } 190 }
191 191
192 -- Copied and modified from https://github.com/chengzeyi/.vim_runtime/blob/8a47981c81d31f88d1138211908e58fd58e4decc/lua/lsp_ext.lua
193
194 function preview_location(location, context, before_context)
195 -- location may be LocationLink or Location (more useful for the former)
196 context = context or 15
197 before_context = before_context or 0
198 local uri = location.targetUri or location.uri
199 if uri == nil then
200 return
201 end
202 local bufnr = vim.uri_to_bufnr(uri)
203 if not vim.api.nvim_buf_is_loaded(bufnr) then
204 vim.fn.bufload(bufnr)
205 end
206 local range = location.targetRange or location.range
207 local contents =
208 vim.api.nvim_buf_get_lines(bufnr, range.start.line - before_context, range['end'].line + 1 + context, false)
209 local filetype = vim.api.nvim_buf_get_option(bufnr, 'filetype')
210 return vim.lsp.util.open_floating_preview(contents, filetype)
211 end
212
213 function preview_location_callback(_, method, result)
214 local context = 15
215 if result == nil or vim.tbl_isempty(result) then
216 return nil
217 end
218 if vim.tbl_islist(result) then
219 preview_location(result[1], context, 5)
220 else
221 preview_location(result, context, 5)
222 end
223 end
224
225 function peek_declaration()
226 local params = vim.lsp.util.make_position_params()
227 return vim.lsp.buf_request(0, 'textDocument/declaration', params, preview_location_callback)
228 end
229
230 function peek_definition()
231 local params = vim.lsp.util.make_position_params()
232 return vim.lsp.buf_request(0, 'textDocument/definition', params, preview_location_callback)
233 end
234
235 function peek_type_definition()
236 local params = vim.lsp.util.make_position_params()
237 return vim.lsp.buf_request(0, 'textDocument/typeDefinition', params, preview_location_callback)
238 end
239
240 function peek_implementation()
241 local params = vim.lsp.util.make_position_params()
242 return vim.lsp.buf_request(0, 'textDocument/implementation', params, preview_location_callback)
243 end
244
192 local on_attach = function(client) 245 local on_attach = function(client)
193 require'completion'.on_attach({ 246 require'completion'.on_attach({
194 sorting = 'alphabet', 247 sorting = 'alphabet',
195 matching_strategy_list = {'exact', 'fuzzy'}, 248 matching_strategy_list = {'exact', 'fuzzy'},
196 chain_complete_list = chain_complete_list, 249 chain_complete_list = chain_complete_list,
215 mapper('n', '<leader>f', '<cmd>lua vim.lsp.buf.code_action()<CR>') 268 mapper('n', '<leader>f', '<cmd>lua vim.lsp.buf.code_action()<CR>')
216 mapper('n', '<c-p>', '<cmd>lua vim.lsp.buf.formatting()<CR>') 269 mapper('n', '<c-p>', '<cmd>lua vim.lsp.buf.formatting()<CR>')
217 mapper("i", "<c-n>", "<Plug>(completion_trigger)", false) 270 mapper("i", "<c-n>", "<Plug>(completion_trigger)", false)
218 mapper("i", "<c-j>", "<Plug>(completion_next_source)", false) 271 mapper("i", "<c-j>", "<Plug>(completion_next_source)", false)
219 mapper("i", "<c-k>", "<Plug>(completion_prev_source)", false) 272 mapper("i", "<c-k>", "<Plug>(completion_prev_source)", false)
273 mapper("n", "gp", "<cmd>lua peek_definition()<CR>")
220 end 274 end
221 275
222 276
223 vim.lsp.handlers["textDocument/formatting"] = function(err, _, result, _, bufnr) 277 vim.lsp.handlers["textDocument/formatting"] = function(err, _, result, _, bufnr)
224 if err ~= nil or result == nil then 278 if err ~= nil or result == nil then