init.lua 8.9 KB

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