minimal_lsp.lua 4.0 KB

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