init.lua 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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> K :lua vim.lsp.buf.hover()<CR>")
  23. -- vim.cmd('nnoremap <silent> <C-k> <cmd>lua vim.lsp.buf.signature_help()<CR>')
  24. vim.cmd("nnoremap <silent> <C-p> :lua vim.lsp.diagnostic.goto_prev({popup_opts = {border = O.lsp.popup_border}})<CR>")
  25. vim.cmd("nnoremap <silent> <C-n> :lua vim.lsp.diagnostic.goto_next({popup_opts = {border = O.lsp.popup_border}})<CR>")
  26. -- scroll down hover doc or scroll in definition preview
  27. -- scroll up hover doc
  28. vim.cmd('command! -nargs=0 LspVirtualTextToggle lua require("lsp/virtual_text").toggle()')
  29. -- Set Default Prefix.
  30. -- Note: You can set a prefix per lsp server in the lv-globals.lua file
  31. vim.lsp.handlers["textDocument/publishDiagnostics"] = vim.lsp.with(
  32. vim.lsp.diagnostic.on_publish_diagnostics, {
  33. virtual_text = {
  34. prefix = "",
  35. spacing = 0,
  36. },
  37. signs = true,
  38. underline = true,
  39. }
  40. )
  41. vim.lsp.handlers["textDocument/hover"] = vim.lsp.with(
  42. vim.lsp.handlers.hover, {
  43. border = O.lsp.popup_border
  44. }
  45. )
  46. vim.lsp.handlers["textDocument/signatureHelp"] = vim.lsp.with(
  47. vim.lsp.handlers.signature_help, {
  48. border = O.lsp.popup_border
  49. }
  50. )
  51. -- symbols for autocomplete
  52. vim.lsp.protocol.CompletionItemKind = {
  53. "  (Text) ",
  54. "  (Method)",
  55. "  (Function)",
  56. "  (Constructor)",
  57. " ﴲ (Field)",
  58. "[] (Variable)",
  59. "  (Class)",
  60. " ﰮ (Interface)",
  61. "  (Module)",
  62. " 襁 (Property)",
  63. "  (Unit)",
  64. "  (Value)",
  65. " 練 (Enum)",
  66. "  (Keyword)",
  67. "  (Snippet)",
  68. "  (Color)",
  69. "  (File)",
  70. "  (Reference)",
  71. "  (Folder)",
  72. "  (EnumMember)",
  73. " ﲀ (Constant)",
  74. " ﳤ (Struct)",
  75. "  (Event)",
  76. "  (Operator)",
  77. "  (TypeParameter)"
  78. }
  79. --[[ " autoformat
  80. autocmd BufWritePre *.js lua vim.lsp.buf.formatting_sync(nil, 100)
  81. autocmd BufWritePre *.jsx lua vim.lsp.buf.formatting_sync(nil, 100)
  82. autocmd BufWritePre *.lua lua vim.lsp.buf.formatting_sync(nil, 100) ]]
  83. -- Java
  84. -- autocmd FileType java nnoremap ca <Cmd>lua require('jdtls').code_action()<CR>
  85. local function documentHighlight(client, bufnr)
  86. -- Set autocommands conditional on server_capabilities
  87. if client.resolved_capabilities.document_highlight then
  88. vim.api.nvim_exec(
  89. [[
  90. hi LspReferenceRead cterm=bold ctermbg=red guibg=#464646
  91. hi LspReferenceText cterm=bold ctermbg=red guibg=#464646
  92. hi LspReferenceWrite cterm=bold ctermbg=red guibg=#464646
  93. augroup lsp_document_highlight
  94. autocmd! * <buffer>
  95. autocmd CursorHold <buffer> lua vim.lsp.buf.document_highlight()
  96. autocmd CursorMoved <buffer> lua vim.lsp.buf.clear_references()
  97. augroup END
  98. ]],
  99. false
  100. )
  101. end
  102. end
  103. local lsp_config = {}
  104. if O.document_highlight then
  105. function lsp_config.common_on_attach(client, bufnr)
  106. documentHighlight(client, bufnr)
  107. end
  108. end
  109. function lsp_config.tsserver_on_attach(client, bufnr)
  110. -- lsp_config.common_on_attach(client, bufnr)
  111. client.resolved_capabilities.document_formatting = false
  112. local ts_utils = require("nvim-lsp-ts-utils")
  113. -- defaults
  114. ts_utils.setup {
  115. debug = false,
  116. disable_commands = false,
  117. enable_import_on_completion = false,
  118. import_all_timeout = 5000, -- ms
  119. -- eslint
  120. eslint_enable_code_actions = true,
  121. eslint_enable_disable_comments = true,
  122. eslint_bin = O.lang.tsserver.linter,
  123. eslint_config_fallback = nil,
  124. eslint_enable_diagnostics = true,
  125. -- formatting
  126. enable_formatting = O.lang.tsserver.autoformat,
  127. formatter = O.lang.tsserver.formatter,
  128. formatter_config_fallback = nil,
  129. -- parentheses completion
  130. complete_parens = false,
  131. signature_help_in_parens = false,
  132. -- update imports on file move
  133. update_imports_on_move = false,
  134. require_confirmation_on_move = false,
  135. watch_dir = nil,
  136. }
  137. -- required to fix code action ranges
  138. ts_utils.setup_client(client)
  139. -- TODO: keymap these?
  140. -- vim.api.nvim_buf_set_keymap(bufnr, "n", "gs", ":TSLspOrganize<CR>", {silent = true})
  141. -- vim.api.nvim_buf_set_keymap(bufnr, "n", "qq", ":TSLspFixCurrent<CR>", {silent = true})
  142. -- vim.api.nvim_buf_set_keymap(bufnr, "n", "gr", ":TSLspRenameFile<CR>", {silent = true})
  143. -- vim.api.nvim_buf_set_keymap(bufnr, "n", "gi", ":TSLspImportAll<CR>", {silent = true})
  144. end
  145. require('lv-utils').define_augroups({
  146. _general_lsp = {
  147. {'FileType', 'lspinfo', 'nnoremap <silent> <buffer> q :q<CR>'},
  148. }
  149. })
  150. -- Use a loop to conveniently both setup defined servers
  151. -- and map buffer local keybindings when the language server attaches
  152. -- local servers = {"pyright", "tsserver"}
  153. -- for _, lsp in ipairs(servers) do nvim_lsp[lsp].setup {on_attach = on_attach} end
  154. return lsp_config