1
0

feat: install plugin lspinstaller

This commit is contained in:
Rokas Puzonas 2021-10-31 18:05:09 +02:00
parent 7459b2c0f7
commit 7f07f9707e
3 changed files with 78 additions and 72 deletions

View File

@ -1,33 +1,40 @@
local lspconfig = require('lspconfig')
local M = {} local M = {}
-- debounce_text_changes = delay, between changing something and lsp updating -- debounce_text_changes = delay, between changing something and lsp updating
M.flags = { debounce_text_changes = 150 } M.flags = { debounce_text_changes = 150 }
M.default_settings = { local general_settings = { }
Lua = { M.server_settings = {
runtime = { sumneko_lua = {
-- Tell the language server which version of Lua you're using (most likely LuaJIT in the case of Neovim) Lua = {
version = 'LuaJIT', runtime = {
-- Setup your lua path -- Tell the language server which version of Lua you're using (most likely LuaJIT in the case of Neovim)
path = vim.split(package.path, ';') version = 'LuaJIT',
}, -- Setup your lua path
diagnostics = { path = vim.split(package.path, ';')
-- Get the language server to recognize the `vim` global },
globals = {'vim', 'packer_plugins'} diagnostics = {
}, -- Get the language server to recognize the `vim` global
workspace = { globals = {'vim', 'packer_plugins'}
-- Make the server aware of Neovim runtime files },
library = vim.api.nvim_get_runtime_file("", true) workspace = {
}, -- Make the server aware of Neovim runtime files
telemetry = { enable = false } 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 -- Use an on_attach function to only map the following keys
-- after the language server attaches to the current buffer -- after the language server attaches to the current buffer
---@diagnostic disable-next-line: unused-local ---@diagnostic disable-next-line: unused-local
local function on_attach(client, bufnr) function M.on_attach(client, bufnr)
-- Mappings. -- Mappings.
local opts = { noremap = true, silent = true } local opts = { noremap = true, silent = true }
@ -43,7 +50,10 @@ local function on_attach(client, bufnr)
buf_set_keymap('n', 'gr', '<cmd>lua vim.lsp.buf.references()<CR>') buf_set_keymap('n', 'gr', '<cmd>lua vim.lsp.buf.references()<CR>')
buf_set_keymap('n', 'gi', '<cmd>lua vim.lsp.buf.implementation()<CR>') buf_set_keymap('n', 'gi', '<cmd>lua vim.lsp.buf.implementation()<CR>')
buf_set_keymap('n', 'K', '<cmd>lua vim.lsp.buf.hover()<CR>') buf_set_keymap('n', 'K', '<cmd>lua vim.lsp.buf.hover()<CR>')
buf_set_keymap('n', '<C-k>', '<cmd>lua vim.lsp.buf.signature_help()<CR>')
-- Conflicts with movement between panes
-- buf_set_keymap('n', '<C-k>', '<cmd>lua vim.lsp.buf.signature_help()<CR>')
-- buf_set_keymap('n', '<space>wa', '<cmd>lua vim.lsp.buf.add_workspace_folder()<CR>') -- buf_set_keymap('n', '<space>wa', '<cmd>lua vim.lsp.buf.add_workspace_folder()<CR>')
-- buf_set_keymap('n', '<space>wr', '<cmd>lua vim.lsp.buf.remove_workspace_folder()<CR>') -- buf_set_keymap('n', '<space>wr', '<cmd>lua vim.lsp.buf.remove_workspace_folder()<CR>')
-- buf_set_keymap('n', '<space>wl', '<cmd>lua print(vim.inspect(vim.lsp.buf.list_workspace_folders()))<CR>') -- buf_set_keymap('n', '<space>wl', '<cmd>lua print(vim.inspect(vim.lsp.buf.list_workspace_folders()))<CR>')
@ -62,64 +72,28 @@ local function on_attach(client, bufnr)
-- autocmd BufWritePre *.py lua vim.lsp.buf.formatting_sync(nil, 100) -- autocmd BufWritePre *.py lua vim.lsp.buf.formatting_sync(nil, 100)
end 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
if packer_plugins['cmp-nvim-lsp'] and packer_plugins['cmp-nvim-lsp'].loaded then capabilities = require('cmp_nvim_lsp').update_capabilities(capabilities)
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
end end
lspconfig.sumneko_lua.setup { return capabilities
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
}
end end
setupLuaServer() function M.on_init(initialize_params, config)
-- print("lsp init")
end
-- Use a loop to conveniently call 'setup' on multiple servers and function M.get_server_settings(name)
-- map buffer local keybindings when the language server attaches return M.server_settings[name]
local servers = { 'tsserver' } end
for _, name in ipairs(servers) do
lspconfig[name].setup { ---@diagnostic disable-next-line: empty-block
on_attach = on_attach, if packer_plugins['nvim-lsp-installer'] and packer_plugins['nvim-lsp-installer'].loaded then
capabilities = capabilities, -- local lspconfig = require('lspconfig')
on_init = on_init, -- TODO: Manually do "server:setup{}" if lspinstaller is not loaded
flags = M.flags,
settings = M.default_settings
}
end end
return M return M

View File

@ -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

View File

@ -80,6 +80,11 @@ local function usePlugins(use)
'neovim/nvim-lspconfig', 'neovim/nvim-lspconfig',
config = [[require 'config.lspconfig']], config = [[require 'config.lspconfig']],
} }
use {
'williamboman/nvim-lsp-installer',
requires = 'neovim/nvim-lspconfig',
config = [[require 'config.lspinstaller']]
}
-- Completion -- Completion
use { 'onsails/lspkind-nvim' } use { 'onsails/lspkind-nvim' }