init.lua 11 KB

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