init.lua 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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. local function no_formatter_on_attach(client, bufnr)
  193. if lvim.lsp.on_attach_callback then
  194. lvim.lsp.on_attach_callback(client, bufnr)
  195. end
  196. lsp_highlight_document(client)
  197. client.resolved_capabilities.document_formatting = false
  198. end
  199. function lsp_config.common_capabilities()
  200. local capabilities = vim.lsp.protocol.make_client_capabilities()
  201. capabilities.textDocument.completion.completionItem.snippetSupport = true
  202. capabilities.textDocument.completion.completionItem.resolveSupport = {
  203. properties = {
  204. "documentation",
  205. "detail",
  206. "additionalTextEdits",
  207. },
  208. }
  209. return capabilities
  210. end
  211. function lsp_config.tsserver_on_attach(client, _)
  212. -- lsp_config.common_on_attach(client, bufnr)
  213. client.resolved_capabilities.document_formatting = false
  214. local ts_utils = require "nvim-lsp-ts-utils"
  215. -- defaults
  216. ts_utils.setup {
  217. debug = false,
  218. disable_commands = false,
  219. enable_import_on_completion = false,
  220. import_all_timeout = 5000, -- ms
  221. -- eslint
  222. eslint_enable_code_actions = true,
  223. eslint_enable_disable_comments = true,
  224. -- eslint_bin = lvim.lang.tsserver.linter,
  225. eslint_config_fallback = nil,
  226. eslint_enable_diagnostics = true,
  227. -- formatting
  228. enable_formatting = lvim.lang.tsserver.autoformat,
  229. formatter = lvim.lang.tsserver.formatter.exe,
  230. formatter_config_fallback = nil,
  231. -- parentheses completion
  232. complete_parens = false,
  233. signature_help_in_parens = false,
  234. -- update imports on file move
  235. update_imports_on_move = false,
  236. require_confirmation_on_move = false,
  237. watch_dir = nil,
  238. }
  239. -- required to fix code action ranges
  240. ts_utils.setup_client(client)
  241. -- TODO: keymap these?
  242. -- vim.api.nvim_buf_set_keymap(bufnr, "n", "gs", ":TSLspOrganize<CR>", {silent = true})
  243. -- vim.api.nvim_buf_set_keymap(bufnr, "n", "qq", ":TSLspFixCurrent<CR>", {silent = true})
  244. -- vim.api.nvim_buf_set_keymap(bufnr, "n", "gr", ":TSLspRenameFile<CR>", {silent = true})
  245. -- vim.api.nvim_buf_set_keymap(bufnr, "n", "gi", ":TSLspImportAll<CR>", {silent = true})
  246. end
  247. require("core.autocmds").define_augroups {
  248. _general_lsp = {
  249. { "FileType", "lspinfo", "nnoremap <silent> <buffer> q :q<CR>" },
  250. },
  251. }
  252. local function is_table(t)
  253. return type(t) == "table"
  254. end
  255. local function is_string(t)
  256. return type(t) == "string"
  257. end
  258. local function has_value(tab, val)
  259. for _, value in ipairs(tab) do
  260. if value == val then
  261. return true
  262. end
  263. end
  264. return false
  265. end
  266. function lsp_config.setup(lang)
  267. local lang_server = lvim.lang[lang].lsp
  268. local provider = lang_server.provider
  269. if require("utils").check_lsp_client_active(provider) then
  270. return
  271. end
  272. local overrides = lvim.lsp.override
  273. if is_table(overrides) then
  274. if has_value(overrides, lang) then
  275. return
  276. end
  277. end
  278. if is_string(overrides) then
  279. if overrides == lang then
  280. return
  281. end
  282. end
  283. local sources = require("lsp.null-ls").setup(lang)
  284. for _, source in pairs(sources) do
  285. local method = source.method
  286. local format_method = "NULL_LS_FORMATTING"
  287. if is_table(method) then
  288. if has_value(method, format_method) then
  289. lang_server.setup.on_attach = no_formatter_on_attach
  290. end
  291. end
  292. if is_string(method) then
  293. if method == format_method then
  294. lang_server.setup.on_attach = no_formatter_on_attach
  295. end
  296. end
  297. end
  298. require("lspconfig")[provider].setup(lang_server.setup)
  299. end
  300. return lsp_config