init.lua 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. -- TODO: figure out why this don't work
  2. vim.fn.sign_define(
  3. "LspDiagnosticsSignError",
  4. { texthl = "LspDiagnosticsSignError", text = "", numhl = "LspDiagnosticsSignError" }
  5. )
  6. vim.fn.sign_define(
  7. "LspDiagnosticsSignWarning",
  8. { texthl = "LspDiagnosticsSignWarning", text = "", numhl = "LspDiagnosticsSignWarning" }
  9. )
  10. vim.fn.sign_define(
  11. "LspDiagnosticsSignHint",
  12. { texthl = "LspDiagnosticsSignHint", text = "", numhl = "LspDiagnosticsSignHint" }
  13. )
  14. vim.fn.sign_define(
  15. "LspDiagnosticsSignInformation",
  16. { texthl = "LspDiagnosticsSignInformation", text = "", numhl = "LspDiagnosticsSignInformation" }
  17. )
  18. vim.cmd "nnoremap <silent> gd <cmd>lua vim.lsp.buf.definition()<CR>"
  19. vim.cmd "nnoremap <silent> gD <cmd>lua vim.lsp.buf.declaration()<CR>"
  20. vim.cmd "nnoremap <silent> gr <cmd>lua vim.lsp.buf.references()<CR>"
  21. vim.cmd "nnoremap <silent> gi <cmd>lua vim.lsp.buf.implementation()<CR>"
  22. vim.cmd "nnoremap <silent> gp <cmd>lua require'lsp'.PeekDefinition()<CR>"
  23. vim.cmd "nnoremap <silent> K :lua vim.lsp.buf.hover()<CR>"
  24. -- vim.cmd('nnoremap <silent> <C-k> <cmd>lua vim.lsp.buf.signature_help()<CR>')
  25. vim.cmd "nnoremap <silent> <C-p> :lua vim.lsp.diagnostic.goto_prev({popup_opts = {border = O.lsp.popup_border}})<CR>"
  26. vim.cmd "nnoremap <silent> <C-n> :lua vim.lsp.diagnostic.goto_next({popup_opts = {border = O.lsp.popup_border}})<CR>"
  27. -- scroll down hover doc or scroll in definition preview
  28. -- scroll up hover doc
  29. vim.cmd 'command! -nargs=0 LspVirtualTextToggle lua require("lsp/virtual_text").toggle()'
  30. -- Set Default Prefix.
  31. -- Note: You can set a prefix per lsp server in the lv-globals.lua file
  32. vim.lsp.handlers["textDocument/publishDiagnostics"] = vim.lsp.with(vim.lsp.diagnostic.on_publish_diagnostics, {
  33. virtual_text = {
  34. prefix = "",
  35. spacing = 0,
  36. },
  37. signs = true,
  38. underline = true,
  39. })
  40. vim.lsp.handlers["textDocument/hover"] = vim.lsp.with(vim.lsp.handlers.hover, {
  41. border = O.lsp.popup_border,
  42. })
  43. vim.lsp.handlers["textDocument/signatureHelp"] = vim.lsp.with(vim.lsp.handlers.signature_help, {
  44. border = O.lsp.popup_border,
  45. })
  46. -- symbols for autocomplete
  47. vim.lsp.protocol.CompletionItemKind = {
  48. "  (Text) ",
  49. "  (Method)",
  50. "  (Function)",
  51. "  (Constructor)",
  52. " ﴲ (Field)",
  53. "[] (Variable)",
  54. "  (Class)",
  55. " ﰮ (Interface)",
  56. "  (Module)",
  57. " 襁 (Property)",
  58. "  (Unit)",
  59. "  (Value)",
  60. " 練 (Enum)",
  61. "  (Keyword)",
  62. "  (Snippet)",
  63. "  (Color)",
  64. "  (File)",
  65. "  (Reference)",
  66. "  (Folder)",
  67. "  (EnumMember)",
  68. " ﲀ (Constant)",
  69. " ﳤ (Struct)",
  70. "  (Event)",
  71. "  (Operator)",
  72. "  (TypeParameter)",
  73. }
  74. --[[ " autoformat
  75. autocmd BufWritePre *.js lua vim.lsp.buf.formatting_sync(nil, 100)
  76. autocmd BufWritePre *.jsx lua vim.lsp.buf.formatting_sync(nil, 100)
  77. autocmd BufWritePre *.lua lua vim.lsp.buf.formatting_sync(nil, 100) ]]
  78. -- Java
  79. -- autocmd FileType java nnoremap ca <Cmd>lua require('jdtls').code_action()<CR>
  80. local function documentHighlight(client, bufnr)
  81. -- Set autocommands conditional on server_capabilities
  82. if client.resolved_capabilities.document_highlight then
  83. vim.api.nvim_exec(
  84. [[
  85. hi LspReferenceRead cterm=bold ctermbg=red guibg=#464646
  86. hi LspReferenceText cterm=bold ctermbg=red guibg=#464646
  87. hi LspReferenceWrite cterm=bold ctermbg=red guibg=#464646
  88. augroup lsp_document_highlight
  89. autocmd! * <buffer>
  90. autocmd CursorHold <buffer> lua vim.lsp.buf.document_highlight()
  91. autocmd CursorMoved <buffer> lua vim.lsp.buf.clear_references()
  92. augroup END
  93. ]],
  94. false
  95. )
  96. end
  97. end
  98. local lsp_config = {}
  99. -- Taken from https://www.reddit.com/r/neovim/comments/gyb077/nvimlsp_peek_defination_javascript_ttserver/
  100. function lsp_config.preview_location(location, context, before_context)
  101. -- location may be LocationLink or Location (more useful for the former)
  102. context = context or 15
  103. before_context = before_context or 0
  104. local uri = location.targetUri or location.uri
  105. if uri == nil then
  106. return
  107. end
  108. local bufnr = vim.uri_to_bufnr(uri)
  109. if not vim.api.nvim_buf_is_loaded(bufnr) then
  110. vim.fn.bufload(bufnr)
  111. end
  112. local range = location.targetRange or location.range
  113. local contents = vim.api.nvim_buf_get_lines(
  114. bufnr,
  115. range.start.line - before_context,
  116. range["end"].line + 1 + context,
  117. false
  118. )
  119. local filetype = vim.api.nvim_buf_get_option(bufnr, "filetype")
  120. return vim.lsp.util.open_floating_preview(contents, filetype, { border = O.lsp.popup_border })
  121. end
  122. function lsp_config.preview_location_callback(_, method, result)
  123. local context = 15
  124. if result == nil or vim.tbl_isempty(result) then
  125. print("No location found: " .. method)
  126. return nil
  127. end
  128. if vim.tbl_islist(result) then
  129. lsp_config.floating_buf, lsp_config.floating_win = lsp_config.preview_location(result[1], context)
  130. else
  131. lsp_config.floating_buf, lsp_config.floating_win = lsp_config.preview_location(result, context)
  132. end
  133. end
  134. function lsp_config.PeekDefinition()
  135. if vim.tbl_contains(vim.api.nvim_list_wins(), lsp_config.floating_win) then
  136. vim.api.nvim_set_current_win(lsp_config.floating_win)
  137. else
  138. local params = vim.lsp.util.make_position_params()
  139. return vim.lsp.buf_request(0, "textDocument/definition", params, lsp_config.preview_location_callback)
  140. end
  141. end
  142. function lsp_config.PeekTypeDefinition()
  143. if vim.tbl_contains(vim.api.nvim_list_wins(), lsp_config.floating_win) then
  144. vim.api.nvim_set_current_win(lsp_config.floating_win)
  145. else
  146. local params = vim.lsp.util.make_position_params()
  147. return vim.lsp.buf_request(0, "textDocument/typeDefinition", params, lsp_config.preview_location_callback)
  148. end
  149. end
  150. function lsp_config.PeekImplementation()
  151. if vim.tbl_contains(vim.api.nvim_list_wins(), lsp_config.floating_win) then
  152. vim.api.nvim_set_current_win(lsp_config.floating_win)
  153. else
  154. local params = vim.lsp.util.make_position_params()
  155. return vim.lsp.buf_request(0, "textDocument/implementation", params, lsp_config.preview_location_callback)
  156. end
  157. end
  158. if O.lsp.document_highlight then
  159. function lsp_config.common_on_attach(client, bufnr)
  160. documentHighlight(client, bufnr)
  161. end
  162. end
  163. function lsp_config.tsserver_on_attach(client, bufnr)
  164. -- lsp_config.common_on_attach(client, bufnr)
  165. client.resolved_capabilities.document_formatting = false
  166. local ts_utils = require "nvim-lsp-ts-utils"
  167. -- defaults
  168. ts_utils.setup {
  169. debug = false,
  170. disable_commands = false,
  171. enable_import_on_completion = false,
  172. import_all_timeout = 5000, -- ms
  173. -- eslint
  174. eslint_enable_code_actions = true,
  175. eslint_enable_disable_comments = true,
  176. eslint_bin = O.lang.tsserver.linter,
  177. eslint_config_fallback = nil,
  178. eslint_enable_diagnostics = true,
  179. -- formatting
  180. enable_formatting = O.lang.tsserver.autoformat,
  181. formatter = O.lang.tsserver.formatter.exe,
  182. formatter_config_fallback = nil,
  183. -- parentheses completion
  184. complete_parens = false,
  185. signature_help_in_parens = false,
  186. -- update imports on file move
  187. update_imports_on_move = false,
  188. require_confirmation_on_move = false,
  189. watch_dir = nil,
  190. }
  191. -- required to fix code action ranges
  192. ts_utils.setup_client(client)
  193. -- TODO: keymap these?
  194. -- vim.api.nvim_buf_set_keymap(bufnr, "n", "gs", ":TSLspOrganize<CR>", {silent = true})
  195. -- vim.api.nvim_buf_set_keymap(bufnr, "n", "qq", ":TSLspFixCurrent<CR>", {silent = true})
  196. -- vim.api.nvim_buf_set_keymap(bufnr, "n", "gr", ":TSLspRenameFile<CR>", {silent = true})
  197. -- vim.api.nvim_buf_set_keymap(bufnr, "n", "gi", ":TSLspImportAll<CR>", {silent = true})
  198. end
  199. require("lv-utils").define_augroups {
  200. _general_lsp = {
  201. { "FileType", "lspinfo", "nnoremap <silent> <buffer> q :q<CR>" },
  202. },
  203. }
  204. -- Use a loop to conveniently both setup defined servers
  205. -- and map buffer local keybindings when the language server attaches
  206. -- local servers = {"pyright", "tsserver"}
  207. -- for _, lsp in ipairs(servers) do nvim_lsp[lsp].setup {on_attach = on_attach} end
  208. return lsp_config