init.lua 5.4 KB

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