init.lua 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. local lsp_config = {}
  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 lvim.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 = lvim.lsp.popup_border}})<CR>"
  51. vim.cmd "nnoremap <silent> <C-n> :lua vim.lsp.diagnostic.goto_next({popup_opts = {border = lvim.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. function lsp_config.setup_handlers()
  60. vim.lsp.handlers["textDocument/publishDiagnostics"] = vim.lsp.with(vim.lsp.diagnostic.on_publish_diagnostics, {
  61. virtual_text = lvim.lsp.diagnostics.virtual_text,
  62. signs = lvim.lsp.diagnostics.signs,
  63. underline = lvim.lsp.document_highlight,
  64. })
  65. vim.lsp.handlers["textDocument/hover"] = vim.lsp.with(vim.lsp.handlers.hover, {
  66. border = lvim.lsp.popup_border,
  67. })
  68. vim.lsp.handlers["textDocument/signatureHelp"] = vim.lsp.with(vim.lsp.handlers.signature_help, {
  69. border = lvim.lsp.popup_border,
  70. })
  71. end
  72. -- symbols for autocomplete
  73. vim.lsp.protocol.CompletionItemKind = {
  74. "  (Text) ",
  75. "  (Method)",
  76. "  (Function)",
  77. "  (Constructor)",
  78. " ﴲ (Field)",
  79. "[] (Variable)",
  80. "  (Class)",
  81. " ﰮ (Interface)",
  82. "  (Module)",
  83. " 襁 (Property)",
  84. "  (Unit)",
  85. "  (Value)",
  86. " 練 (Enum)",
  87. "  (Keyword)",
  88. "  (Snippet)",
  89. "  (Color)",
  90. "  (File)",
  91. "  (Reference)",
  92. "  (Folder)",
  93. "  (EnumMember)",
  94. " ﲀ (Constant)",
  95. " ﳤ (Struct)",
  96. "  (Event)",
  97. "  (Operator)",
  98. "  (TypeParameter)",
  99. }
  100. --[[ " autoformat
  101. autocmd BufWritePre *.js lua vim.lsp.buf.formatting_sync(nil, 100)
  102. autocmd BufWritePre *.jsx lua vim.lsp.buf.formatting_sync(nil, 100)
  103. autocmd BufWritePre *.lua lua vim.lsp.buf.formatting_sync(nil, 100) ]]
  104. -- Java
  105. -- autocmd FileType java nnoremap ca <Cmd>lua require('jdtls').code_action()<CR>
  106. local function lsp_highlight_document(client)
  107. if lvim.lsp.document_highlight == false then
  108. return -- we don't need further
  109. end
  110. -- Set autocommands conditional on server_capabilities
  111. if client.resolved_capabilities.document_highlight then
  112. vim.api.nvim_exec(
  113. [[
  114. hi LspReferenceRead cterm=bold ctermbg=red guibg=#464646
  115. hi LspReferenceText cterm=bold ctermbg=red guibg=#464646
  116. hi LspReferenceWrite cterm=bold ctermbg=red guibg=#464646
  117. augroup lsp_document_highlight
  118. autocmd! * <buffer>
  119. autocmd CursorHold <buffer> lua vim.lsp.buf.document_highlight()
  120. autocmd CursorMoved <buffer> lua vim.lsp.buf.clear_references()
  121. augroup END
  122. ]],
  123. false
  124. )
  125. end
  126. end
  127. -- Taken from https://www.reddit.com/r/neovim/comments/gyb077/nvimlsp_peek_defination_javascript_ttserver/
  128. function lsp_config.preview_location(location, context, before_context)
  129. -- location may be LocationLink or Location (more useful for the former)
  130. context = context or 15
  131. before_context = before_context or 0
  132. local uri = location.targetUri or location.uri
  133. if uri == nil then
  134. return
  135. end
  136. local bufnr = vim.uri_to_bufnr(uri)
  137. if not vim.api.nvim_buf_is_loaded(bufnr) then
  138. vim.fn.bufload(bufnr)
  139. end
  140. local range = location.targetRange or location.range
  141. local contents = vim.api.nvim_buf_get_lines(
  142. bufnr,
  143. range.start.line - before_context,
  144. range["end"].line + 1 + context,
  145. false
  146. )
  147. local filetype = vim.api.nvim_buf_get_option(bufnr, "filetype")
  148. return vim.lsp.util.open_floating_preview(contents, filetype, { border = lvim.lsp.popup_border })
  149. end
  150. function lsp_config.preview_location_callback(_, method, result)
  151. local context = 15
  152. if result == nil or vim.tbl_isempty(result) then
  153. print("No location found: " .. method)
  154. return nil
  155. end
  156. if vim.tbl_islist(result) then
  157. lsp_config.floating_buf, lsp_config.floating_win = lsp_config.preview_location(result[1], context)
  158. else
  159. lsp_config.floating_buf, lsp_config.floating_win = lsp_config.preview_location(result, context)
  160. end
  161. end
  162. function lsp_config.PeekDefinition()
  163. if vim.tbl_contains(vim.api.nvim_list_wins(), lsp_config.floating_win) then
  164. vim.api.nvim_set_current_win(lsp_config.floating_win)
  165. else
  166. local params = vim.lsp.util.make_position_params()
  167. return vim.lsp.buf_request(0, "textDocument/definition", params, lsp_config.preview_location_callback)
  168. end
  169. end
  170. function lsp_config.PeekTypeDefinition()
  171. if vim.tbl_contains(vim.api.nvim_list_wins(), lsp_config.floating_win) then
  172. vim.api.nvim_set_current_win(lsp_config.floating_win)
  173. else
  174. local params = vim.lsp.util.make_position_params()
  175. return vim.lsp.buf_request(0, "textDocument/typeDefinition", params, lsp_config.preview_location_callback)
  176. end
  177. end
  178. function lsp_config.PeekImplementation()
  179. if vim.tbl_contains(vim.api.nvim_list_wins(), lsp_config.floating_win) then
  180. vim.api.nvim_set_current_win(lsp_config.floating_win)
  181. else
  182. local params = vim.lsp.util.make_position_params()
  183. return vim.lsp.buf_request(0, "textDocument/implementation", params, lsp_config.preview_location_callback)
  184. end
  185. end
  186. function lsp_config.common_on_attach(client, bufnr)
  187. if lvim.lsp.on_attach_callback then
  188. lvim.lsp.on_attach_callback(client, bufnr)
  189. end
  190. lsp_highlight_document(client)
  191. end
  192. function lsp_config.common_capabilities()
  193. local capabilities = vim.lsp.protocol.make_client_capabilities()
  194. capabilities.textDocument.completion.completionItem.snippetSupport = true
  195. capabilities.textDocument.completion.completionItem.resolveSupport = {
  196. properties = {
  197. "documentation",
  198. "detail",
  199. "additionalTextEdits",
  200. },
  201. }
  202. return capabilities
  203. end
  204. function lsp_config.tsserver_on_attach(client, _)
  205. -- lsp_config.common_on_attach(client, bufnr)
  206. client.resolved_capabilities.document_formatting = false
  207. local ts_utils = require "nvim-lsp-ts-utils"
  208. -- defaults
  209. ts_utils.setup {
  210. debug = false,
  211. disable_commands = false,
  212. enable_import_on_completion = false,
  213. import_all_timeout = 5000, -- ms
  214. -- eslint
  215. eslint_enable_code_actions = true,
  216. eslint_enable_disable_comments = true,
  217. -- eslint_bin = lvim.lang.tsserver.linter,
  218. eslint_config_fallback = nil,
  219. eslint_enable_diagnostics = true,
  220. -- formatting
  221. enable_formatting = lvim.lang.tsserver.autoformat,
  222. formatter = lvim.lang.tsserver.formatter.exe,
  223. formatter_config_fallback = nil,
  224. -- parentheses completion
  225. complete_parens = false,
  226. signature_help_in_parens = false,
  227. -- update imports on file move
  228. update_imports_on_move = false,
  229. require_confirmation_on_move = false,
  230. watch_dir = nil,
  231. }
  232. -- required to fix code action ranges
  233. ts_utils.setup_client(client)
  234. -- TODO: keymap these?
  235. -- vim.api.nvim_buf_set_keymap(bufnr, "n", "gs", ":TSLspOrganize<CR>", {silent = true})
  236. -- vim.api.nvim_buf_set_keymap(bufnr, "n", "qq", ":TSLspFixCurrent<CR>", {silent = true})
  237. -- vim.api.nvim_buf_set_keymap(bufnr, "n", "gr", ":TSLspRenameFile<CR>", {silent = true})
  238. -- vim.api.nvim_buf_set_keymap(bufnr, "n", "gi", ":TSLspImportAll<CR>", {silent = true})
  239. end
  240. require("core.autocmds").define_augroups {
  241. _general_lsp = {
  242. { "FileType", "lspinfo", "nnoremap <silent> <buffer> q :q<CR>" },
  243. },
  244. }
  245. local function is_table(t)
  246. return type(t) == "table"
  247. end
  248. local function is_string(t)
  249. return type(t) == "string"
  250. end
  251. local function has_value(tab, val)
  252. for _, value in ipairs(tab) do
  253. if value == val then
  254. return true
  255. end
  256. end
  257. return false
  258. end
  259. function lsp_config.setup(lang)
  260. local lang_server = lvim.lang[lang].lsp
  261. local provider = lang_server.provider
  262. if require("lv-utils").check_lsp_client_active(provider) then
  263. return
  264. end
  265. local overrides = lvim.lsp.override
  266. if is_table(overrides) then
  267. if has_value(overrides, lang) then
  268. return
  269. end
  270. end
  271. if is_string(overrides) then
  272. if overrides == lang then
  273. return
  274. end
  275. end
  276. require("lspconfig")[provider].setup(lang_server.setup)
  277. require("lsp.null-ls").setup(lang)
  278. end
  279. return lsp_config