minimal_lsp.lua 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 lsp-installer
  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/nvim-lsp-installer", disable = not use_lsp_installer },
  21. },
  22. config = {
  23. package_root = package_root,
  24. compile_path = compile_path,
  25. },
  26. }
  27. end
  28. function _G.dump(...)
  29. local objects = vim.tbl_map(vim.inspect, { ... })
  30. print(unpack(objects))
  31. return ...
  32. end
  33. _G.load_config = function()
  34. vim.lsp.set_log_level "trace"
  35. require("vim.lsp.log").set_format_func(vim.inspect)
  36. local nvim_lsp = require "lspconfig"
  37. local on_attach = function(_, bufnr)
  38. local function buf_set_keymap(...)
  39. vim.api.nvim_buf_set_keymap(bufnr, ...)
  40. end
  41. local function buf_set_option(...)
  42. vim.api.nvim_buf_set_option(bufnr, ...)
  43. end
  44. buf_set_option("omnifunc", "v:lua.vim.lsp.omnifunc")
  45. -- Mappings.
  46. local opts = { noremap = true, silent = true }
  47. buf_set_keymap("n", "gD", "<Cmd>lua vim.lsp.buf.declaration()<CR>", opts)
  48. buf_set_keymap("n", "gd", "<Cmd>lua vim.lsp.buf.definition()<CR>", opts)
  49. buf_set_keymap("n", "K", "<Cmd>lua vim.lsp.buf.hover()<CR>", opts)
  50. buf_set_keymap("n", "gi", "<cmd>lua vim.lsp.buf.implementation()<CR>", opts)
  51. buf_set_keymap("n", "<C-k>", "<cmd>lua vim.lsp.buf.signature_help()<CR>", opts)
  52. buf_set_keymap("n", "<space>wa", "<cmd>lua vim.lsp.buf.add_workspace_folder()<CR>", opts)
  53. buf_set_keymap("n", "<space>wr", "<cmd>lua vim.lsp.buf.remove_workspace_folder()<CR>", opts)
  54. buf_set_keymap("n", "<space>wl", "<cmd>lua print(vim.inspect(vim.lsp.buf.list_workspace_folders()))<CR>", opts)
  55. buf_set_keymap("n", "<space>lD", "<cmd>lua vim.lsp.buf.type_definition()<CR>", opts)
  56. buf_set_keymap("n", "<space>lr", "<cmd>lua vim.lsp.buf.rename()<CR>", opts)
  57. buf_set_keymap("n", "gr", "<cmd>lua vim.lsp.buf.references()<CR>", opts)
  58. buf_set_keymap("n", "gl", "<cmd>lua vim.diagnostic.open_float(0,{scope='line'})<CR>", opts)
  59. buf_set_keymap("n", "<space>lk", "<cmd>lua vim.diagnostic.goto_prev()<CR>", opts)
  60. buf_set_keymap("n", "<space>lj", "<cmd>lua vim.diagnostic.goto_next()<CR>", opts)
  61. buf_set_keymap("n", "<space>lq", "<cmd>lua vim.diagnostic.setloclist()<CR>", opts)
  62. buf_set_keymap("n", "<space>li", "<cmd>LspInfo<CR>", opts)
  63. buf_set_keymap("n", "<space>lI", "<cmd>LspInstallInfo<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 use_lsp_installer then
  71. local server_available, server = require("nvim-lsp-installer.servers").get_server(name)
  72. if not server_available then
  73. server:install()
  74. end
  75. local default_opts = server:get_default_options()
  76. setup_opts.cmd_env = default_opts.cmd_env
  77. end
  78. if not name then
  79. print "You have not defined a server name, please edit minimal_init.lua"
  80. end
  81. if not nvim_lsp[name].document_config.default_config.cmd and not setup_opts.cmd then
  82. print [[You have not defined a server default cmd for a server
  83. that requires it please edit minimal_init.lua]]
  84. end
  85. nvim_lsp[name].setup(setup_opts)
  86. 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.]]
  87. end
  88. if vim.fn.isdirectory(install_path) == 0 then
  89. vim.fn.system { "git", "clone", "https://github.com/wbthomason/packer.nvim", install_path }
  90. load_plugins()
  91. require("packer").sync()
  92. vim.cmd [[autocmd User PackerComplete ++once lua load_config()]]
  93. else
  94. load_plugins()
  95. require("packer").sync()
  96. _G.load_config()
  97. end