minimal_lsp.lua 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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
  9. if on_windows then
  10. temp_dir = vim.loop.os_getenv "TEMP"
  11. else
  12. temp_dir = "/tmp"
  13. end
  14. vim.cmd("set packpath=" .. join_paths(temp_dir, "nvim", "site"))
  15. local package_root = join_paths(temp_dir, "nvim", "site", "pack")
  16. local install_path = join_paths(package_root, "packer", "start", "packer.nvim")
  17. local compile_path = join_paths(install_path, "plugin", "packer_compiled.lua")
  18. -- Choose whether to use the executable that's managed by lsp-installer
  19. local use_lsp_installer = true
  20. local function load_plugins()
  21. require("packer").startup {
  22. {
  23. "wbthomason/packer.nvim",
  24. "neovim/nvim-lspconfig",
  25. { "williamboman/nvim-lsp-installer", disable = not use_lsp_installer },
  26. },
  27. config = {
  28. package_root = package_root,
  29. compile_path = compile_path,
  30. },
  31. }
  32. end
  33. function _G.dump(...)
  34. local objects = vim.tbl_map(vim.inspect, { ... })
  35. print(unpack(objects))
  36. return ...
  37. end
  38. _G.load_config = function()
  39. vim.lsp.set_log_level "trace"
  40. if vim.fn.has "nvim-0.5.1" == 1 then
  41. require("vim.lsp.log").set_format_func(vim.inspect)
  42. end
  43. local nvim_lsp = require "lspconfig"
  44. local on_attach = function(_, bufnr)
  45. local function buf_set_keymap(...)
  46. vim.api.nvim_buf_set_keymap(bufnr, ...)
  47. end
  48. local function buf_set_option(...)
  49. vim.api.nvim_buf_set_option(bufnr, ...)
  50. end
  51. buf_set_option("omnifunc", "v:lua.vim.lsp.omnifunc")
  52. -- Mappings.
  53. local opts = { noremap = true, silent = true }
  54. buf_set_keymap("n", "gD", "<Cmd>lua vim.lsp.buf.declaration()<CR>", opts)
  55. buf_set_keymap("n", "gd", "<Cmd>lua vim.lsp.buf.definition()<CR>", opts)
  56. buf_set_keymap("n", "K", "<Cmd>lua vim.lsp.buf.hover()<CR>", opts)
  57. buf_set_keymap("n", "gi", "<cmd>lua vim.lsp.buf.implementation()<CR>", opts)
  58. buf_set_keymap("n", "<C-k>", "<cmd>lua vim.lsp.buf.signature_help()<CR>", opts)
  59. buf_set_keymap("n", "<space>wa", "<cmd>lua vim.lsp.buf.add_workspace_folder()<CR>", opts)
  60. buf_set_keymap("n", "<space>wr", "<cmd>lua vim.lsp.buf.remove_workspace_folder()<CR>", opts)
  61. buf_set_keymap("n", "<space>wl", "<cmd>lua print(vim.inspect(vim.lsp.buf.list_workspace_folders()))<CR>", opts)
  62. buf_set_keymap("n", "<space>lD", "<cmd>lua vim.lsp.buf.type_definition()<CR>", opts)
  63. buf_set_keymap("n", "<space>lr", "<cmd>lua vim.lsp.buf.rename()<CR>", opts)
  64. buf_set_keymap("n", "gr", "<cmd>lua vim.lsp.buf.references()<CR>", opts)
  65. buf_set_keymap("n", "gl", "<cmd>lua vim.lsp.diagnostic.show_line_diagnostics()<CR>", opts)
  66. buf_set_keymap("n", "<space>lk", "<cmd>lua vim.lsp.diagnostic.goto_prev()<CR>", opts)
  67. buf_set_keymap("n", "<space>lj", "<cmd>lua vim.lsp.diagnostic.goto_next()<CR>", opts)
  68. buf_set_keymap("n", "<space>lq", "<cmd>lua vim.lsp.diagnostic.set_loclist()<CR>", opts)
  69. buf_set_keymap("n", "<space>li", "<cmd>LspInfo<CR>", opts)
  70. buf_set_keymap("n", "<space>lI", "<cmd>LspInstallInfo<CR>", opts)
  71. end
  72. -- Add the server that troubles you here, e.g. "sumneko_lua", "pyright", "tsserver"
  73. local name = "clangd"
  74. -- You need to specify the server's command manually
  75. local cmd
  76. if use_lsp_installer then
  77. local server_available, server = require("nvim-lsp-installer.servers").get_server(name)
  78. if not server_available then
  79. server:install()
  80. end
  81. local default_opts = server:get_default_options()
  82. cmd = default_opts.cmd
  83. end
  84. if not name then
  85. print "You have not defined a server name, please edit minimal_init.lua"
  86. end
  87. if not nvim_lsp[name].document_config.default_config.cmd and not cmd then
  88. print [[You have not defined a server default cmd for a server
  89. that requires it please edit minimal_init.lua]]
  90. end
  91. nvim_lsp[name].setup {
  92. cmd = cmd,
  93. on_attach = on_attach,
  94. }
  95. 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.]]
  96. end
  97. if vim.fn.isdirectory(install_path) == 0 then
  98. vim.fn.system { "git", "clone", "https://github.com/wbthomason/packer.nvim", install_path }
  99. load_plugins()
  100. require("packer").sync()
  101. vim.cmd [[autocmd User PackerComplete ++once lua load_config()]]
  102. else
  103. load_plugins()
  104. require("packer").sync()
  105. _G.load_config()
  106. end