From 7f07f9707ed3e4a1748d6cebdda91a846182562a Mon Sep 17 00:00:00 2001 From: Rokas Puzonas Date: Sun, 31 Oct 2021 18:05:09 +0200 Subject: [PATCH] feat: install plugin lspinstaller --- lua/config/lspconfig.lua | 118 ++++++++++++++---------------------- lua/config/lspinstaller.lua | 27 +++++++++ lua/plugins.lua | 5 ++ 3 files changed, 78 insertions(+), 72 deletions(-) create mode 100644 lua/config/lspinstaller.lua diff --git a/lua/config/lspconfig.lua b/lua/config/lspconfig.lua index 7f114b7..761490b 100644 --- a/lua/config/lspconfig.lua +++ b/lua/config/lspconfig.lua @@ -1,33 +1,40 @@ -local lspconfig = require('lspconfig') local M = {} -- debounce_text_changes = delay, between changing something and lsp updating M.flags = { debounce_text_changes = 150 } -M.default_settings = { - Lua = { - runtime = { - -- Tell the language server which version of Lua you're using (most likely LuaJIT in the case of Neovim) - version = 'LuaJIT', - -- Setup your lua path - path = vim.split(package.path, ';') - }, - diagnostics = { - -- Get the language server to recognize the `vim` global - globals = {'vim', 'packer_plugins'} - }, - workspace = { - -- Make the server aware of Neovim runtime files - library = vim.api.nvim_get_runtime_file("", true) - }, - telemetry = { enable = false } +local general_settings = { } +M.server_settings = { + sumneko_lua = { + Lua = { + runtime = { + -- Tell the language server which version of Lua you're using (most likely LuaJIT in the case of Neovim) + version = 'LuaJIT', + -- Setup your lua path + path = vim.split(package.path, ';') + }, + diagnostics = { + -- Get the language server to recognize the `vim` global + globals = {'vim', 'packer_plugins'} + }, + workspace = { + -- Make the server aware of Neovim runtime files + library = vim.api.nvim_get_runtime_file("", true) + }, + telemetry = { enable = false } + } } } +-- Apply general settings for each server +for _, settings in ipairs(M.server_settings) do + M.server_settings = vim.tbl_deep_extend("keep", settings, general_settings) +end + -- Use an on_attach function to only map the following keys -- after the language server attaches to the current buffer ---@diagnostic disable-next-line: unused-local -local function on_attach(client, bufnr) +function M.on_attach(client, bufnr) -- Mappings. local opts = { noremap = true, silent = true } @@ -43,7 +50,10 @@ local function on_attach(client, bufnr) buf_set_keymap('n', 'gr', 'lua vim.lsp.buf.references()') buf_set_keymap('n', 'gi', 'lua vim.lsp.buf.implementation()') buf_set_keymap('n', 'K', 'lua vim.lsp.buf.hover()') - buf_set_keymap('n', '', 'lua vim.lsp.buf.signature_help()') + + -- Conflicts with movement between panes + -- buf_set_keymap('n', '', 'lua vim.lsp.buf.signature_help()') + -- buf_set_keymap('n', 'wa', 'lua vim.lsp.buf.add_workspace_folder()') -- buf_set_keymap('n', 'wr', 'lua vim.lsp.buf.remove_workspace_folder()') -- buf_set_keymap('n', 'wl', 'lua print(vim.inspect(vim.lsp.buf.list_workspace_folders()))') @@ -62,64 +72,28 @@ local function on_attach(client, bufnr) -- autocmd BufWritePre *.py lua vim.lsp.buf.formatting_sync(nil, 100) end +function M.get_capabilities() + local capabilities = vim.lsp.protocol.make_client_capabilities() -local capabilities = vim.lsp.protocol.make_client_capabilities() -if packer_plugins['cmp-nvim-lsp'] and packer_plugins['cmp-nvim-lsp'].loaded then - capabilities = require('cmp_nvim_lsp').update_capabilities(capabilities) -end - -local function on_init(initialize_params, config) - -- print(vim.inspect(capabilities)) - -- print(vim.inspect(config)) -end - --- https://github.com/sumneko/lua-language-server/wiki/Build-and-Run-(Standalone) -local function getSumnekoPaths() - USER = vim.fn.expand('$USER') - - if vim.fn.has("mac") == 1 then - local sumneko_root_path = "/Users/" .. USER .. "/.config/nvim/lua-language-server" - local sumneko_binary = "/Users/" .. USER .. "/.config/nvim/lua-language-server/bin/macOS/lua-language-server" - return sumneko_root_path, sumneko_binary - elseif vim.fn.has("unix") == 1 then - local sumneko_root_path = "/home/" .. USER .. "/.config/nvim/lua-language-server" - local sumneko_binary = "/home/" .. USER .. "/.config/nvim/lua-language-server/bin/Linux/lua-language-server" - return sumneko_root_path, sumneko_binary - end -end - -local function setupLuaServer() - local sumneko_root_path, sumneko_binary = getSumnekoPaths() - - if not sumneko_root_path then - print("Unsupported system for sumneko") - return + if packer_plugins['cmp-nvim-lsp'] and packer_plugins['cmp-nvim-lsp'].loaded then + capabilities = require('cmp_nvim_lsp').update_capabilities(capabilities) end - lspconfig.sumneko_lua.setup { - cmd = {sumneko_binary, "-E", sumneko_root_path .. "/main.lua"}, - on_attach = on_attach, - filetypes = {"lua"}, - capabilities = capabilities, - on_init = on_init, - flags = M.flags, - settings = M.default_settings - } + return capabilities end -setupLuaServer() +function M.on_init(initialize_params, config) + -- print("lsp init") +end --- Use a loop to conveniently call 'setup' on multiple servers and --- map buffer local keybindings when the language server attaches -local servers = { 'tsserver' } -for _, name in ipairs(servers) do - lspconfig[name].setup { - on_attach = on_attach, - capabilities = capabilities, - on_init = on_init, - flags = M.flags, - settings = M.default_settings - } +function M.get_server_settings(name) + return M.server_settings[name] +end + +---@diagnostic disable-next-line: empty-block +if packer_plugins['nvim-lsp-installer'] and packer_plugins['nvim-lsp-installer'].loaded then + -- local lspconfig = require('lspconfig') + -- TODO: Manually do "server:setup{}" if lspinstaller is not loaded end return M diff --git a/lua/config/lspinstaller.lua b/lua/config/lspinstaller.lua new file mode 100644 index 0000000..5979663 --- /dev/null +++ b/lua/config/lspinstaller.lua @@ -0,0 +1,27 @@ +local lspconfig_config = require 'config.lspconfig' +local lsp_instller = require 'nvim-lsp-installer' +local lsp_installer_servers = require'nvim-lsp-installer.servers' +local M = {} + +local autoinstall_servers = {"sumneko_lua"} +for _, name in ipairs(autoinstall_servers) do + local ok, server = lsp_installer_servers.get_server(name) + if ok and not server:is_installed() then + server:install() + end +end + +local capabilities = lspconfig_config.get_capabilities() + +lsp_instller.on_server_ready(function(server) + server:setup{ + on_attach = lspconfig_config.on_attach, + on_init = lspconfig_config.on_init, + flags = lspconfig_config.flags, + capabilities = capabilities, + settings = lspconfig_config.get_server_settings(server.name) + } +end) + +return M + diff --git a/lua/plugins.lua b/lua/plugins.lua index 38ead0d..4887dc9 100644 --- a/lua/plugins.lua +++ b/lua/plugins.lua @@ -80,6 +80,11 @@ local function usePlugins(use) 'neovim/nvim-lspconfig', config = [[require 'config.lspconfig']], } + use { + 'williamboman/nvim-lsp-installer', + requires = 'neovim/nvim-lspconfig', + config = [[require 'config.lspinstaller']] + } -- Completion use { 'onsails/lspkind-nvim' }