minimal_lsp.lua 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. local on_windows = vim.loop.os_uname().version:match "Windows"
  2. local function join_paths(...)
  3. local path_sep = on_windows and "\\" or "/"
  4. local result = table.concat({ ... }, path_sep)
  5. return result
  6. end
  7. vim.cmd [[set runtimepath=$VIMRUNTIME]]
  8. local temp_dir = vim.loop.os_getenv "TEMP" or "/tmp"
  9. vim.cmd("set packpath=" .. join_paths(temp_dir, "nvim", "site"))
  10. local package_root = join_paths(temp_dir, "nvim", "site", "pack")
  11. local plugins_dir = join_paths(package_root, "lazy", "opt")
  12. local install_path = join_paths(package_root, "lazy.nvim")
  13. -- Choose whether to use the executable that's managed by mason
  14. local use_lsp_installer = true
  15. local function load_plugins()
  16. vim.opt.rtp:prepend(install_path)
  17. require("lazy").setup({
  18. "neovim/nvim-lspconfig",
  19. "williamboman/mason-lspconfig.nvim",
  20. "williamboman/mason.nvim",
  21. }, {
  22. root = plugins_dir,
  23. })
  24. end
  25. function _G.dump(...)
  26. local objects = vim.tbl_map(vim.inspect, { ... })
  27. print(unpack(objects))
  28. return ...
  29. end
  30. _G.load_config = function()
  31. vim.lsp.set_log_level "trace"
  32. require("vim.lsp.log").set_format_func(vim.inspect)
  33. local nvim_lsp = require "lspconfig"
  34. local on_attach = function(_, bufnr)
  35. local function buf_set_option(...)
  36. vim.api.nvim_buf_set_option(bufnr, ...)
  37. end
  38. buf_set_option("omnifunc", "v:lua.vim.lsp.omnifunc")
  39. -- Mappings.
  40. local opts = { buffer = bufnr, noremap = true, silent = true }
  41. vim.keymap.set("n", "gD", vim.lsp.buf.declaration, opts)
  42. vim.keymap.set("n", "gd", vim.lsp.buf.definition, opts)
  43. vim.keymap.set("n", "K", vim.lsp.buf.hover, opts)
  44. vim.keymap.set("n", "gi", vim.lsp.buf.implementation, opts)
  45. vim.keymap.set("n", "<C-k>", vim.lsp.buf.signature_help, opts)
  46. vim.keymap.set("n", "<space>wa", vim.lsp.buf.add_workspace_folder, opts)
  47. vim.keymap.set("n", "<space>wr", vim.lsp.buf.remove_workspace_folder, opts)
  48. vim.keymap.set("n", "<space>wl", function()
  49. print(vim.inspect(vim.lsp.buf.list_workspace_folders()))
  50. end, opts)
  51. vim.keymap.set("n", "<space>lD", vim.lsp.buf.type_definition, opts)
  52. vim.keymap.set("n", "<space>lr", vim.lsp.buf.rename, opts)
  53. vim.keymap.set("n", "gr", vim.lsp.buf.references, opts)
  54. vim.keymap.set("n", "gl", vim.diagnostic.open_float, opts)
  55. vim.keymap.set("n", "[d", vim.diagnostic.goto_prev, opts)
  56. vim.keymap.set("n", "]d", vim.diagnostic.goto_next, opts)
  57. vim.keymap.set("n", "<space>q", vim.diagnostic.setloclist, opts)
  58. vim.keymap.set("n", "<space>li", "<cmd>LspInfo<CR>", opts)
  59. vim.keymap.set("n", "<space>lI", "<cmd>Mason<CR>", opts)
  60. end
  61. -- Add the server that troubles you here, e.g. "clangd", "pyright", "tsserver"
  62. local name = "sumneko_lua"
  63. local setup_opts = {
  64. on_attach = on_attach,
  65. }
  66. if not name then
  67. print "You have not defined a server name, please edit minimal_init.lua"
  68. end
  69. if not nvim_lsp[name].document_config.default_config.cmd and not setup_opts.cmd then
  70. print [[You have not defined a server default cmd for a server
  71. that requires it please edit minimal_init.lua]]
  72. end
  73. nvim_lsp[name].setup(setup_opts)
  74. if use_lsp_installer then
  75. require("mason-lspconfig").setup { automatic_installation = true }
  76. end
  77. print [[You can find your log at $HOME/.cache/nvim/lsp.log. Please paste in a github issue under a details tag as described in the issue template.]]
  78. end
  79. if vim.fn.isdirectory(install_path) == 0 then
  80. print "Installing lazy.nvim"
  81. vim.fn.system {
  82. "git",
  83. "clone",
  84. "--filter=blob:none",
  85. "--single-branch",
  86. "https://github.com/folke/lazy.nvim.git",
  87. install_path,
  88. }
  89. load_plugins()
  90. vim.cmd [[autocmd User LazyDone ++once lua load_config()]]
  91. else
  92. load_plugins()
  93. require("lazy").sync()
  94. _G.load_config()
  95. end