init.lua 8.8 KB

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