Guest User

more jdtls hacks

a guest
Jul 19th, 2025
17
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.83 KB | None | 0 0
  1. -- assuming you have a function "register" to wire up keymaps and crap depending on server capabilities
  2. -- for jdtls, it isn't enough to just do this on LspAttach like the neovim docs suggest
  3. -- you need to also handle "dynamic registration".. which jdtls seems to use heavily?
  4.  
  5. -- Static registration of server's capabilities
  6. vim.api.nvim_create_autocmd('LspAttach', {
  7.   callback = function(args)
  8.     local client = assert(vim.lsp.get_client_by_id(args.data.client_id))
  9.     register(client, args.buf)
  10.   end,
  11. })
  12.  
  13. --- original handler that sets Neovim defaults
  14. local register_capability_handler = vim.lsp.handlers[ms.client_registerCapability]
  15.  
  16. --- Dynamic registration of server's capabilities
  17. --- @param err lsp.ResponseError?
  18. --- @param params lsp.RegistrationParams
  19. --- @param ctx lsp.HandlerContext
  20. --- @return any
  21. vim.lsp.handlers[ms.client_registerCapability] = function(err, params, ctx)
  22.   --- @type any
  23.   local res = register_capability_handler(err, params, ctx)
  24.   local client = assert(vim.lsp.get_client_by_id(ctx.client_id))
  25.   for bufnr in pairs(client.attached_buffers) do
  26.     register(client, bufnr)
  27.   end
  28.   return res
  29. end
  30.  
  31. --- VSCode command: show references in the quickfix list
  32. --- this one is really expected to exist by the language servers
  33. --- you need it e.g. to execute code lens commands to show references/implementations
  34. vim.lsp.commands['editor.action.showReferences'] = function(command, context)
  35.   --- @type lsp.Location[]
  36.   local locations = command.arguments[3]
  37.   local client = assert(vim.lsp.get_client_by_id(context.client_id))
  38.   if locations and #locations > 0 then
  39.     local items = vim.lsp.util.locations_to_items(locations, client.offset_encoding)
  40.     vim.fn.setqflist({}, ' ', { title = 'Code Lens: ' .. command.title, items = items, context = context })
  41.     vim.api.nvim_command(':copen')
  42.   end
  43. end
Advertisement
Add Comment
Please, Sign In to add comment