init.lua 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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 lsp_highlight_document(client)
  105. if O.lsp.document_highlight == false then
  106. return -- we don't need further
  107. end
  108. -- Set autocommands conditional on server_capabilities
  109. if client.resolved_capabilities.document_highlight then
  110. vim.api.nvim_exec(
  111. [[
  112. hi LspReferenceRead cterm=bold ctermbg=red guibg=#464646
  113. hi LspReferenceText cterm=bold ctermbg=red guibg=#464646
  114. hi LspReferenceWrite cterm=bold ctermbg=red guibg=#464646
  115. augroup lsp_document_highlight
  116. autocmd! * <buffer>
  117. autocmd CursorHold <buffer> lua vim.lsp.buf.document_highlight()
  118. autocmd CursorMoved <buffer> lua vim.lsp.buf.clear_references()
  119. augroup END
  120. ]],
  121. false
  122. )
  123. end
  124. end
  125. local lsp_config = {}
  126. -- Taken from https://www.reddit.com/r/neovim/comments/gyb077/nvimlsp_peek_defination_javascript_ttserver/
  127. function lsp_config.preview_location(location, context, before_context)
  128. -- location may be LocationLink or Location (more useful for the former)
  129. context = context or 15
  130. before_context = before_context or 0
  131. local uri = location.targetUri or location.uri
  132. if uri == nil then
  133. return
  134. end
  135. local bufnr = vim.uri_to_bufnr(uri)
  136. if not vim.api.nvim_buf_is_loaded(bufnr) then
  137. vim.fn.bufload(bufnr)
  138. end
  139. local range = location.targetRange or location.range
  140. local contents = vim.api.nvim_buf_get_lines(
  141. bufnr,
  142. range.start.line - before_context,
  143. range["end"].line + 1 + context,
  144. false
  145. )
  146. local filetype = vim.api.nvim_buf_get_option(bufnr, "filetype")
  147. return vim.lsp.util.open_floating_preview(contents, filetype, { border = O.lsp.popup_border })
  148. end
  149. function lsp_config.preview_location_callback(_, method, result)
  150. local context = 15
  151. if result == nil or vim.tbl_isempty(result) then
  152. print("No location found: " .. method)
  153. return nil
  154. end
  155. if vim.tbl_islist(result) then
  156. lsp_config.floating_buf, lsp_config.floating_win = lsp_config.preview_location(result[1], context)
  157. else
  158. lsp_config.floating_buf, lsp_config.floating_win = lsp_config.preview_location(result, context)
  159. end
  160. end
  161. function lsp_config.PeekDefinition()
  162. if vim.tbl_contains(vim.api.nvim_list_wins(), lsp_config.floating_win) then
  163. vim.api.nvim_set_current_win(lsp_config.floating_win)
  164. else
  165. local params = vim.lsp.util.make_position_params()
  166. return vim.lsp.buf_request(0, "textDocument/definition", params, lsp_config.preview_location_callback)
  167. end
  168. end
  169. function lsp_config.PeekTypeDefinition()
  170. if vim.tbl_contains(vim.api.nvim_list_wins(), lsp_config.floating_win) then
  171. vim.api.nvim_set_current_win(lsp_config.floating_win)
  172. else
  173. local params = vim.lsp.util.make_position_params()
  174. return vim.lsp.buf_request(0, "textDocument/typeDefinition", params, lsp_config.preview_location_callback)
  175. end
  176. end
  177. function lsp_config.PeekImplementation()
  178. if vim.tbl_contains(vim.api.nvim_list_wins(), lsp_config.floating_win) then
  179. vim.api.nvim_set_current_win(lsp_config.floating_win)
  180. else
  181. local params = vim.lsp.util.make_position_params()
  182. return vim.lsp.buf_request(0, "textDocument/implementation", params, lsp_config.preview_location_callback)
  183. end
  184. end
  185. function lsp_config.common_on_attach(client, bufnr)
  186. if O.lsp.on_attach_callback then
  187. O.lsp.on_attach_callback(client, bufnr)
  188. end
  189. lsp_highlight_document(client)
  190. end
  191. function lsp_config.tsserver_on_attach(client, _)
  192. -- lsp_config.common_on_attach(client, bufnr)
  193. client.resolved_capabilities.document_formatting = false
  194. local ts_utils = require "nvim-lsp-ts-utils"
  195. -- defaults
  196. ts_utils.setup {
  197. debug = false,
  198. disable_commands = false,
  199. enable_import_on_completion = false,
  200. import_all_timeout = 5000, -- ms
  201. -- eslint
  202. eslint_enable_code_actions = true,
  203. eslint_enable_disable_comments = true,
  204. -- eslint_bin = O.lang.tsserver.linter,
  205. eslint_config_fallback = nil,
  206. eslint_enable_diagnostics = true,
  207. -- formatting
  208. enable_formatting = O.lang.tsserver.autoformat,
  209. formatter = O.lang.tsserver.formatter.exe,
  210. formatter_config_fallback = nil,
  211. -- parentheses completion
  212. complete_parens = false,
  213. signature_help_in_parens = false,
  214. -- update imports on file move
  215. update_imports_on_move = false,
  216. require_confirmation_on_move = false,
  217. watch_dir = nil,
  218. }
  219. -- required to fix code action ranges
  220. ts_utils.setup_client(client)
  221. -- TODO: keymap these?
  222. -- vim.api.nvim_buf_set_keymap(bufnr, "n", "gs", ":TSLspOrganize<CR>", {silent = true})
  223. -- vim.api.nvim_buf_set_keymap(bufnr, "n", "qq", ":TSLspFixCurrent<CR>", {silent = true})
  224. -- vim.api.nvim_buf_set_keymap(bufnr, "n", "gr", ":TSLspRenameFile<CR>", {silent = true})
  225. -- vim.api.nvim_buf_set_keymap(bufnr, "n", "gi", ":TSLspImportAll<CR>", {silent = true})
  226. end
  227. require("lv-utils").define_augroups {
  228. _general_lsp = {
  229. { "FileType", "lspinfo", "nnoremap <silent> <buffer> q :q<CR>" },
  230. },
  231. }
  232. -- Use a loop to conveniently both setup defined servers
  233. -- and map buffer local keybindings when the language server attaches
  234. -- local servers = {"pyright", "tsserver"}
  235. -- for _, lsp in ipairs(servers) do nvim_lsp[lsp].setup {on_attach = on_attach} end
  236. return lsp_config