feat: use nvim-lsp-setup to configure language servers

This commit is contained in:
mandlm 2022-04-25 20:18:25 +02:00
parent ae4c29d906
commit 5f032adc7a
Signed by: mandlm
GPG Key ID: 4AA25D647AA54CC7
5 changed files with 158 additions and 248 deletions

View File

@ -2,4 +2,4 @@
mkdir --parents ~/.config/nvim mkdir --parents ~/.config/nvim
ln -svrf init.lua ~/.config/nvim/init.lua ln -svrf init.lua ~/.config/nvim/init.lua
ln -svrf lua ~/.config/nvim/lua ln -svrf lua ~/.config/nvim/

View File

@ -23,7 +23,7 @@ vim.cmd([[packadd packer.nvim]])
-- initialize plugins -- initialize plugins
return require('packer').startup(function(use) return require('packer').startup(function(use)
-- let packer manage itself -- let packer manage itself
use({'wbthomason/packer.nvim', opt = true}) use({ 'wbthomason/packer.nvim', opt = true })
-- theme -- theme
use("ishan9299/nvim-solarized-lua") use("ishan9299/nvim-solarized-lua")
@ -42,25 +42,25 @@ return require('packer').startup(function(use)
use { use {
'kdheepak/tabline.nvim', 'kdheepak/tabline.nvim',
config = function() config = function()
require'tabline'.setup { require 'tabline'.setup {
enable = true, enable = true,
options = {show_bufnr = true, show_filename_only = true} options = { show_bufnr = true, show_filename_only = true }
} }
end, end,
requires = { requires = {
{'hoob3rt/lualine.nvim'}, { 'hoob3rt/lualine.nvim' },
{'kyazdani42/nvim-web-devicons', opt = true} { 'kyazdani42/nvim-web-devicons', opt = true }
} }
} }
-- status line -- status line
use { use {
'nvim-lualine/lualine.nvim', 'nvim-lualine/lualine.nvim',
requires = {'kyazdani42/nvim-web-devicons', opt = true}, requires = { 'kyazdani42/nvim-web-devicons', opt = true },
config = function() config = function()
require('lualine').setup({ require('lualine').setup({
options = {globalstatus = true}, options = { globalstatus = true },
extensions = {"toggleterm"} extensions = { "toggleterm" }
}) })
end end
} }
@ -71,8 +71,8 @@ return require('packer').startup(function(use)
config = function() config = function()
require("indent_blankline").setup { require("indent_blankline").setup {
char = "", char = "",
buftype_exclude = {"terminal", "help", "nofile"}, buftype_exclude = { "terminal", "help", "nofile" },
filetype_exclude = {'help', 'packer'}, filetype_exclude = { 'help', 'packer' },
show_trailing_blankline_indent = false show_trailing_blankline_indent = false
} }
end end
@ -82,7 +82,7 @@ return require('packer').startup(function(use)
use('tpope/vim-fugitive') use('tpope/vim-fugitive')
use({ use({
'lewis6991/gitsigns.nvim', 'lewis6991/gitsigns.nvim',
requires = {'nvim-lua/plenary.nvim'}, requires = { 'nvim-lua/plenary.nvim' },
config = function() require('gitsigns').setup() end config = function() require('gitsigns').setup() end
}) })
use("junegunn/gv.vim") use("junegunn/gv.vim")
@ -90,7 +90,7 @@ return require('packer').startup(function(use)
-- autocompletion -- autocompletion
use({ use({
"L3MON4D3/LuaSnip", "L3MON4D3/LuaSnip",
requires = {"rafamadriz/friendly-snippets"}, requires = { "rafamadriz/friendly-snippets" },
config = function() config = function()
require("luasnip.loaders.from_vscode").lazy_load() require("luasnip.loaders.from_vscode").lazy_load()
end end
@ -104,81 +104,24 @@ return require('packer').startup(function(use)
"davidsierradz/cmp-conventionalcommits", "hrsh7th/cmp-nvim-lua", "davidsierradz/cmp-conventionalcommits", "hrsh7th/cmp-nvim-lua",
"hrsh7th/cmp-calc" "hrsh7th/cmp-calc"
}, },
config = function() config = function() require('plugins.nvim-cmp') end,
local has_words_before = function()
local line, col = unpack(vim.api.nvim_win_get_cursor(0))
return col ~= 0 and
vim.api.nvim_buf_get_lines(0, line - 1, line, true)[1]:sub(
col, col):match("%s") == nil
end
local cmp = require('cmp')
local luasnip = require("luasnip")
cmp.setup({
snippet = {
expand = function(args)
require("luasnip").lsp_expand(args.body)
end
},
sources = require("cmp").config.sources({
{name = "nvim_lsp"}, {name = "luasnip"}, {name = "path"},
{name = "buffer"}, {name = "git"},
{name = 'nvim_lsp_signature_help'},
{name = "conventionalcommits"}, {name = "nvim-lua"},
{name = "calc"}
}),
mapping = {
['<C-Space>'] = cmp.mapping(cmp.mapping.complete(),
{'i', 'c'}),
['<CR>'] = cmp.mapping.confirm({select = true}),
["<Tab>"] = cmp.mapping(function(fallback)
if cmp.visible() then
cmp.select_next_item()
elseif luasnip.expand_or_jumpable() then
luasnip.expand_or_jump()
elseif has_words_before() then
cmp.complete()
else
fallback()
end
end, {"i", "s"}),
["<S-Tab>"] = cmp.mapping(function(fallback)
if cmp.visible() then
cmp.select_prev_item()
elseif luasnip.jumpable(-1) then
luasnip.jump(-1)
else
fallback()
end
end, {"i", "s"})
},
experimental = {ghost_text = true}
})
-- `/` cmdline setup.
cmp.setup.cmdline('/', {sources = {{name = 'buffer'}}})
-- `:` cmdline setup.
cmp.setup.cmdline(':', {
sources = cmp.config
.sources({{name = 'path'}}, {{name = 'cmdline'}})
})
end
}) })
-- highlight current symbol
use({"RRethy/vim-illuminate"})
-- language server -- language server
use({ use {
"neovim/nvim-lspconfig", 'junnplus/nvim-lsp-setup',
config = function() require("plugins.lspconfig") end requires = {
}) 'neovim/nvim-lspconfig',
'williamboman/nvim-lsp-installer',
use('williamboman/nvim-lsp-installer') -- additional rust tools
"simrat39/rust-tools.nvim",
-- highlight current symbol
"RRethy/vim-illuminate",
},
config = function() require('plugins.nvim-lsp-setup') end,
}
-- treesitter -- treesitter
use({ use({
@ -190,33 +133,30 @@ return require('packer').startup(function(use)
-- Telescope -- Telescope
use({ use({
'nvim-telescope/telescope.nvim', 'nvim-telescope/telescope.nvim',
requires = {{'nvim-lua/plenary.nvim'}}, requires = { { 'nvim-lua/plenary.nvim' } },
config = function() require('plugins.telescope') end config = function() require('plugins.telescope') end
}) })
use({'nvim-telescope/telescope-fzf-native.nvim', run = 'make'}) use({ 'nvim-telescope/telescope-fzf-native.nvim', run = 'make' })
-- automatic pairs -- automatic pairs
use({"Raimondi/delimitMate"}) use({ "Raimondi/delimitMate" })
-- rust tools
use({"simrat39/rust-tools.nvim", requires = {{"neovim/nvim-lspconfig"}}})
-- markdown preview -- markdown preview
use({'iamcco/markdown-preview.nvim'}) use({ 'iamcco/markdown-preview.nvim' })
-- terminal -- terminal
use({ use({
"akinsho/nvim-toggleterm.lua", "akinsho/nvim-toggleterm.lua",
config = function() config = function()
require("toggleterm").setup({size = 32, open_mapping = [[<F4>]]}) require("toggleterm").setup({ size = 32, open_mapping = [[<F4>]] })
end end
}) })
-- buffer closing -- buffer closing
use({"sar/bbye.nvim"}) use({ "sar/bbye.nvim" })
-- ansible filetype -- ansible filetype
use({"pearofducks/ansible-vim"}) use({ "pearofducks/ansible-vim" })
if packer_bootstrap then require('packer').sync() end if packer_bootstrap then require('packer').sync() end
end) end)

View File

@ -1,154 +0,0 @@
local lsp_installer = require("nvim-lsp-installer")
local nvim_runtime_path = vim.split(package.path, ';')
table.insert(nvim_runtime_path, "lua/?.lua")
table.insert(nvim_runtime_path, "lua/?/init.lua")
local language_servers = {
"ansiblels", "bashls", "clangd", "dockerls", "efm", "eslint", "html",
"jsonls", "pyright", "rust_analyzer", "sumneko_lua", "svelte", "taplo",
"terraformls", "tflint", "tsserver", "volar"
}
for _, server_name in pairs(language_servers) do
local server_found, server = lsp_installer.get_server(server_name)
if server_found and not server:is_installed() then
print("Installing " .. server_name)
server:install()
end
end
local extra_server_opts = {
["efm"] = function(opts)
opts.filetypes = {
"lua", "html", "javascript", "markdown", "typescript",
"typescriptreact"
}
opts.init_options = {documentFormatting = true}
opts.settings = {
rootMarkers = {".git/"},
languages = {
lua = {{formatCommand = "lua-format -i", formatStdin = true}},
html = {
{
formatCommand = "yarn run --silent prettier --stdin-filepath ${INPUT} --parser html",
formatStdin = true
}
},
javascript = {
{
formatCommand = "prettier --stdin-filepath ${INPUT}",
formatStdin = true
}
},
typescript = {
{
formatCommand = "yarn run --silent prettier --stdin-filepath ${INPUT} --parser typescript",
formatStdin = true
}
},
typescriptreact = {
{
formatCommand = "yarn run --silent prettier --stdin-filepath ${INPUT} --parser typescript",
formatStdin = true
}
},
markdown = {
{
formatCommand = "yarn run --silent prettier --stdin-filepath ${INPUT} --parser markdown",
formatStdin = true
}
}
}
-- prettier-parser
-- flow|babel|babel-flow|babel-ts|typescript|espree|meriyah|css|
-- less|scss|json|json5|json-stringify|graphql|markdown|mdx|vue|yaml|glimmer|html|angular|lwc
}
end,
["rust_analyzer"] = function(opts)
opts.settings = {
["rust-analyzer"] = {checkOnSave = {command = "clippy"}}
}
end,
["sumneko_lua"] = function(opts)
opts.settings = {
Lua = {
runtime = {version = 'LuaJIT', path = nvim_runtime_path},
diagnostics = {globals = {'vim'}},
workspace = {library = vim.api.nvim_get_runtime_file("", true)},
telemetry = {enable = false}
}
}
end
}
local function custom_on_attach(client, buffer_nr)
-- onmifunc
vim.api.nvim_buf_set_option(buffer_nr, "omnifunc", "v:lua.vim.lsp.omnifunc")
-- Helper function
local opts = {noremap = true, silent = true}
local function bufnnoremap(key, action)
vim.api.nvim_buf_set_keymap(buffer_nr, 'n', key, action, opts)
end
-- Inspect function
bufnnoremap("K", "<Cmd>lua vim.lsp.buf.hover()<CR>")
bufnnoremap("<C-k>", "<Cmd>lua vim.lsp.buf.signature_help()<CR>")
-- Navigation
bufnnoremap("gd", "<Cmd>lua vim.lsp.buf.definition()<CR>")
bufnnoremap("gD", "<Cmd>lua vim.lsp.buf.declaration()<CR>")
bufnnoremap("gi", "<Cmd>lua vim.lsp.buf.implementation()<CR>")
bufnnoremap("gr", "<Cmd>lua vim.lsp.buf.references()<CR>")
bufnnoremap("ga", "<Cmd>Telescope lsp_code_actions theme=cursor<CR>")
-- Rename all references of symbol
bufnnoremap("<leader>r", "<Cmd>lua vim.lsp.buf.rename()<CR>")
-- Format buffer
bufnnoremap("<leader>F", "<Cmd>lua vim.lsp.buf.formatting_sync()<CR>")
-- Navigate diagnostics
bufnnoremap("<C-n>", "<Cmd>lua vim.diagnostic.goto_next()<CR>")
bufnnoremap("<C-p>", "<Cmd>lua vim.diagnostic.goto_prev()<CR>")
-- Open diagnostics
bufnnoremap("<leader>d", "<Cmd>Telescope diagnostics<CR>")
-- disable conflicting formatters
if client.name == "tsserver" or client.name == "html" then
client.resolved_capabilities.document_formatting = false
end
if client.resolved_capabilities.document_formatting then
vim.cmd("autocmd BufWritePre <buffer> lua vim.lsp.buf.formatting_sync()")
end
-- vim-illuminate
require("illuminate").on_attach(client)
end
lsp_installer.on_server_ready(function(server)
local cmp = require("cmp_nvim_lsp")
local opts = {
on_attach = custom_on_attach,
capabilities = cmp.update_capabilities(vim.lsp.protocol
.make_client_capabilities())
}
if extra_server_opts[server.name] then
extra_server_opts[server.name](opts)
end
if server.name == "rust_analyzer" then
require("rust-tools").setup({
server = vim.tbl_deep_extend("force", server:get_default_options(),
opts)
})
server:attach_buffers()
else
server:setup(opts)
end
end)

View File

@ -0,0 +1,60 @@
local has_words_before = function()
local line, col = unpack(vim.api.nvim_win_get_cursor(0))
return col ~= 0 and
vim.api.nvim_buf_get_lines(0, line - 1, line, true)[1]:sub(
col, col):match("%s") == nil
end
local cmp = require('cmp')
local luasnip = require("luasnip")
cmp.setup({
snippet = {
expand = function(args)
require("luasnip").lsp_expand(args.body)
end
},
sources = require("cmp").config.sources({
{ name = "nvim_lsp" }, { name = "luasnip" }, { name = "path" },
{ name = "buffer" }, { name = "git" },
{ name = 'nvim_lsp_signature_help' },
{ name = "conventionalcommits" }, { name = "nvim-lua" },
{ name = "calc" }
}),
mapping = {
['<C-Space>'] = cmp.mapping(cmp.mapping.complete(),
{ 'i', 'c' }),
['<CR>'] = cmp.mapping.confirm({ select = true }),
["<Tab>"] = cmp.mapping(function(fallback)
if cmp.visible() then
cmp.select_next_item()
elseif luasnip.expand_or_jumpable() then
luasnip.expand_or_jump()
elseif has_words_before() then
cmp.complete()
else
fallback()
end
end, { "i", "s" }),
["<S-Tab>"] = cmp.mapping(function(fallback)
if cmp.visible() then
cmp.select_prev_item()
elseif luasnip.jumpable(-1) then
luasnip.jump(-1)
else
fallback()
end
end, { "i", "s" })
},
experimental = { ghost_text = true }
})
-- `/` cmdline setup.
cmp.setup.cmdline('/', { sources = { { name = 'buffer' } } })
-- `:` cmdline setup.
cmp.setup.cmdline(':', {
sources = cmp.config
.sources({ { name = 'path' } }, { { name = 'cmdline' } })
})

View File

@ -0,0 +1,64 @@
local utils = require("nvim-lsp-setup.utils")
local nvim_runtime_path = vim.split(package.path, ";")
table.insert(nvim_runtime_path, "lua/?.lua")
table.insert(nvim_runtime_path, "lua/?/init.lua")
require("nvim-lsp-setup").setup({
default_mappings = false,
mappings = {
gD = "lua vim.lsp.buf.declaration()",
gd = "Telescope lsp_definitions",
gt = "Telescope lsp_type_definitions",
gi = "Telescope lsp_implementations",
gr = "Telescope lsp_references",
K = "lua vim.lsp.buf.hover()",
["<C-k>"] = "lua vim.lsp.buf.signature_help()",
["<leader>rn"] = "lua vim.lsp.buf.rename()",
["<leader>ca"] = "Telescope lsp_code_actions theme=cursor",
["<leader>f"] = "lua vim.lsp.buf.formatting()",
["<leader>e"] = "lua vim.lsp.diagnostic.show_line_diagnostics()",
["<leader>d"] = "Telescope diagnostics",
["<C-p>"] = "lua vim.diagnostic.goto_prev()",
["<C-n>"] = "lua vim.diagnostic.goto_next()",
},
on_attach = function(client)
utils.format_on_save(client)
require("illuminate").on_attach(client)
end,
servers = {
ansiblels = {},
bashls = {},
dockerls = {},
eslint = {},
jsonls = {},
pylsp = {},
rust_analyzer = require("nvim-lsp-setup.rust-tools").setup({
server = {
settings = {
["rust-analyzer"] = {
cargo = {
loadOutDirsFromCheck = true,
},
checkOnSave = { command = "clippy" },
procMacro = {
enable = true,
},
},
},
},
}),
sumneko_lua = {
settings = {
Lua = {
runtime = { version = "LuaJIT", path = nvim_runtime_path },
diagnostics = { globals = { "vim" } },
workspace = { library = vim.api.nvim_get_runtime_file("", true) },
telemetry = { enable = false }
}
}
},
terraformls = {},
volar = {},
},
})