service.lua 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. local M = {}
  2. local function lsp_highlight_document(client)
  3. if lvim.lsp.document_highlight == false then
  4. return -- we don't need further
  5. end
  6. -- Set autocommands conditional on server_capabilities
  7. if client.resolved_capabilities.document_highlight then
  8. vim.api.nvim_exec(
  9. [[
  10. hi LspReferenceRead cterm=bold ctermbg=red guibg=#464646
  11. hi LspReferenceText cterm=bold ctermbg=red guibg=#464646
  12. hi LspReferenceWrite cterm=bold ctermbg=red guibg=#464646
  13. augroup lsp_document_highlight
  14. autocmd! * <buffer>
  15. autocmd CursorHold <buffer> lua vim.lsp.buf.document_highlight()
  16. autocmd CursorMoved <buffer> lua vim.lsp.buf.clear_references()
  17. augroup END
  18. ]],
  19. false
  20. )
  21. end
  22. end
  23. function M.lsp_highlight_document(client)
  24. lsp_highlight_document(client)
  25. end
  26. -- Taken from https://www.reddit.com/r/neovim/comments/gyb077/nvimlsp_peek_defination_javascript_ttserver/
  27. function M.preview_location(location, context, before_context)
  28. -- location may be LocationLink or Location (more useful for the former)
  29. context = context or 15
  30. before_context = before_context or 0
  31. local uri = location.targetUri or location.uri
  32. if uri == nil then
  33. return
  34. end
  35. local bufnr = vim.uri_to_bufnr(uri)
  36. if not vim.api.nvim_buf_is_loaded(bufnr) then
  37. vim.fn.bufload(bufnr)
  38. end
  39. local range = location.targetRange or location.range
  40. local contents = vim.api.nvim_buf_get_lines(
  41. bufnr,
  42. range.start.line - before_context,
  43. range["end"].line + 1 + context,
  44. false
  45. )
  46. local filetype = vim.api.nvim_buf_get_option(bufnr, "filetype")
  47. return vim.lsp.util.open_floating_preview(contents, filetype, { border = lvim.lsp.popup_border })
  48. end
  49. function M.preview_location_callback(_, method, result)
  50. local context = 15
  51. if result == nil or vim.tbl_isempty(result) then
  52. print("No location found: " .. method)
  53. return nil
  54. end
  55. if vim.tbl_islist(result) then
  56. M.floating_buf, M.floating_win = M.preview_location(result[1], context)
  57. else
  58. M.floating_buf, M.floating_win = M.preview_location(result, context)
  59. end
  60. end
  61. function M.PeekDefinition()
  62. if vim.tbl_contains(vim.api.nvim_list_wins(), M.floating_win) then
  63. vim.api.nvim_set_current_win(M.floating_win)
  64. else
  65. local params = vim.lsp.util.make_position_params()
  66. return vim.lsp.buf_request(0, "textDocument/definition", params, M.preview_location_callback)
  67. end
  68. end
  69. function M.PeekTypeDefinition()
  70. if vim.tbl_contains(vim.api.nvim_list_wins(), M.floating_win) then
  71. vim.api.nvim_set_current_win(M.floating_win)
  72. else
  73. local params = vim.lsp.util.make_position_params()
  74. return vim.lsp.buf_request(0, "textDocument/typeDefinition", params, M.preview_location_callback)
  75. end
  76. end
  77. function M.PeekImplementation()
  78. if vim.tbl_contains(vim.api.nvim_list_wins(), M.floating_win) then
  79. vim.api.nvim_set_current_win(M.floating_win)
  80. else
  81. local params = vim.lsp.util.make_position_params()
  82. return vim.lsp.buf_request(0, "textDocument/implementation", params, M.preview_location_callback)
  83. end
  84. end
  85. function M.common_on_attach(client, bufnr)
  86. if lvim.lsp.on_attach_callback then
  87. lvim.lsp.on_attach_callback(client, bufnr)
  88. end
  89. lsp_highlight_document(client)
  90. end
  91. function M.no_formatter_on_attach(client, bufnr)
  92. if lvim.lsp.on_attach_callback then
  93. lvim.lsp.on_attach_callback(client, bufnr)
  94. end
  95. lsp_highlight_document(client)
  96. client.resolved_capabilities.document_formatting = false
  97. end
  98. function M.common_capabilities()
  99. local capabilities = vim.lsp.protocol.make_client_capabilities()
  100. capabilities.textDocument.completion.completionItem.snippetSupport = true
  101. capabilities.textDocument.completion.completionItem.resolveSupport = {
  102. properties = {
  103. "documentation",
  104. "detail",
  105. "additionalTextEdits",
  106. },
  107. }
  108. return capabilities
  109. end
  110. return M