minimal_lsp.lua 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 install_path = join_paths(package_root, "packer", "start", "packer.nvim")
  12. local compile_path = join_paths(install_path, "plugin", "packer_compiled.lua")
  13. -- Choose whether to use the executable that's managed by mason
  14. local use_lsp_installer = true
  15. local function load_plugins()
  16. require("packer").startup {
  17. {
  18. "wbthomason/packer.nvim",
  19. "neovim/nvim-lspconfig",
  20. "williamboman/mason-lspconfig.nvim",
  21. "williamboman/mason.nvim",
  22. },
  23. config = {
  24. package_root = package_root,
  25. compile_path = compile_path,
  26. },
  27. }
  28. end
  29. function _G.dump(...)
  30. local objects = vim.tbl_map(vim.inspect, { ... })
  31. print(unpack(objects))
  32. return ...
  33. end
  34. _G.load_config = function()
  35. vim.lsp.set_log_level "trace"
  36. require("vim.lsp.log").set_format_func(vim.inspect)
  37. local nvim_lsp = require "lspconfig"
  38. local on_attach = function(_, bufnr)
  39. local function buf_set_option(...)
  40. vim.api.nvim_buf_set_option(bufnr, ...)
  41. end
  42. buf_set_option("omnifunc", "v:lua.vim.lsp.omnifunc")
  43. -- Mappings.
  44. local opts = { buffer = bufnr, noremap = true, silent = true }
  45. vim.keymap.set("n", "gD", vim.lsp.buf.declaration, opts)
  46. vim.keymap.set("n", "gd", vim.lsp.buf.definition, opts)
  47. vim.keymap.set("n", "K", vim.lsp.buf.hover, opts)
  48. vim.keymap.set("n", "gi", vim.lsp.buf.implementation, opts)
  49. vim.keymap.set("n", "<C-k>", vim.lsp.buf.signature_help, opts)
  50. vim.keymap.set("n", "<space>wa", vim.lsp.buf.add_workspace_folder, opts)
  51. vim.keymap.set("n", "<space>wr", vim.lsp.buf.remove_workspace_folder, opts)
  52. vim.keymap.set("n", "<space>wl", function()
  53. print(vim.inspect(vim.lsp.buf.list_workspace_folders()))
  54. end, opts)
  55. vim.keymap.set("n", "<space>lD", vim.lsp.buf.type_definition, opts)
  56. vim.keymap.set("n", "<space>lr", vim.lsp.buf.rename, opts)
  57. vim.keymap.set("n", "gr", vim.lsp.buf.references, opts)
  58. vim.keymap.set("n", "gl", vim.diagnostic.open_float, opts)
  59. vim.keymap.set("n", "[d", vim.diagnostic.goto_prev, opts)
  60. vim.keymap.set("n", "]d", vim.diagnostic.goto_next, opts)
  61. vim.keymap.set("n", "<space>q", vim.diagnostic.setloclist, opts)
  62. vim.keymap.set("n", "<space>li", "<cmd>LspInfo<CR>", opts)
  63. vim.keymap.set("n", "<space>lI", "<cmd>Mason<CR>", opts)
  64. end
  65. -- Add the server that troubles you here, e.g. "clangd", "pyright", "tsserver"
  66. local name = "sumneko_lua"
  67. local setup_opts = {
  68. on_attach = on_attach,
  69. }
  70. if not name then
  71. print "You have not defined a server name, please edit minimal_init.lua"
  72. end
  73. if not nvim_lsp[name].document_config.default_config.cmd and not setup_opts.cmd then
  74. print [[You have not defined a server default cmd for a server
  75. that requires it please edit minimal_init.lua]]
  76. end
  77. nvim_lsp[name].setup(setup_opts)
  78. if use_lsp_installer then
  79. require("mason-lspconfig").setup { automatic_installation = true }
  80. end
  81. 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.]]
  82. end
  83. if vim.fn.isdirectory(install_path) == 0 then
  84. vim.fn.system { "git", "clone", "https://github.com/wbthomason/packer.nvim", install_path }
  85. load_plugins()
  86. require("packer").sync()
  87. vim.cmd [[autocmd User PackerComplete ++once lua load_config()]]
  88. else
  89. load_plugins()
  90. require("packer").sync()
  91. _G.load_config()
  92. end