feat: embrace neovim lsp

main
Ricard Illa 2023-07-22 00:33:57 +02:00
parent eeb0baf8b0
commit 23fbc255a2
No known key found for this signature in database
GPG Key ID: F69A672B72E54902
2 changed files with 236 additions and 108 deletions

View File

@ -8,17 +8,27 @@
vimdiffAlias = true;
withPython3 = true;
extraPackages = with pkgs; [
ansible-language-server
beancount-language-server
black
docker-compose-language-service
efm-langserver
gcc
hlint
jq
ltex-ls
lua-language-server
lua54Packages.luacheck
nixfmt
nixd
nixpkgs-fmt
nodePackages.dockerfile-language-server-nodejs
nodePackages.pyright
pylint
ruff
ruff-lsp
shellcheck
shfmt
stylua
sqlfluff
terraform-ls
tflint
vscode-langservers-extracted
];
extraPython3Packages = pyPkgs: with pyPkgs; [ pylint ];
plugins = [ pkgs.vimPlugins.packer-nvim ];

View File

@ -7,9 +7,6 @@ require("packer").startup(function(use)
use("freitass/todo.txt-vim")
use("ledger/vim-ledger")
use("Shougo/deoplete.nvim")
use("dense-analysis/ale")
use("psliwka/vim-smoothie")
-- navigation
@ -62,14 +59,22 @@ require("packer").startup(function(use)
use("tpope/vim-haml")
use({ "psf/black", branch = "stable" })
use("elzr/vim-json")
use("z0mbix/vim-shfmt")
use("ckipp01/stylua-nvim")
use("jpalardy/vim-slime")
use("neovim/nvim-lspconfig")
use("hrsh7th/nvim-cmp") -- Autocompletion plugin
use("hrsh7th/cmp-nvim-lsp") -- LSP source for nvim-cmp
use("hrsh7th/cmp-buffer")
use("hrsh7th/cmp-path")
use("saadparwaiz1/cmp_luasnip") -- Snippets source for nvim-cmp
use("L3MON4D3/LuaSnip") -- Snippets plugin
use("lukas-reineke/lsp-format.nvim")
use("sheerun/vim-polyglot")
use("nvim-treesitter/nvim-treesitter")
use("nvim-orgmode/orgmode")
end)
local o = vim.opt
@ -192,7 +197,7 @@ augroup my-glyph-palette
augroup END
]])
nmap("<leader>t", ":Fern . -drawer -toggle<cr>")
nmap("<leader>f", ":Files<cr>")
-- nmap("<leader>f", ":Files<cr>")
nmap("<leader>rg", ":Rg<cr>")
nmap("<leader><Bs>", ": cd ..<cr>")
nmap("<leader>b", ": Buffers<cr>")
@ -213,50 +218,163 @@ vim.g["airline_theme"] = "base16_gruvbox_dark_hard"
vim.g["airline#extensions#tabline#enabled"] = 1
vim.g["airline#extensions#ale#enabled"] = 1
vim.g["ale_lint_on_text_changed"] = "never"
vim.g["ale_lint_on_insert_leave"] = 0
vim.g["ale_lint_on_enter"] = 0
vim.g["deoplete#enable_at_startup"] = 1
-- vim.g["black_virtualenv"] = "/Users/rilla/virtualenvs/black"
vim.g["shfmt_extra_args"] = "-i 4"
vim.g["NERDDefaultAlign"] = "left"
vim.g["UltiSnipsExpandTrigger"] = "<tab>"
vim.g["UltiSnipsJumpForwardTrigger"] = "<c-b>"
vim.g["UltiSnipsJumpBackwardTrigger"] = "<c-z>"
local formatters = {
go = ":GoFmt<CR>",
json = ":%!jq --indent 4 .<CR>",
nix = ":%!nixfmt < %<CR>",
-- python = ":Black<CR>",
python = [[<cmd>lua vim.lsp.buf.format()<CR>]],
sh = ":Shfmt<CR>",
sql = ":%!sqlfluff fix -<CR>",
terraform = ":TerraformFmt<CR>",
lua = [[<cmd>lua require("stylua-nvim").format_file()<CR>]],
local lspconfig = require("lspconfig")
local capabilities = require("cmp_nvim_lsp").default_capabilities()
lspconfig.pyright.setup { capabilities = capabilities }
lspconfig.ruff_lsp.setup { capabilities = capabilities }
lspconfig.nixd.setup { capabilities = capabilities }
lspconfig.jsonls.setup { capabilities = capabilities }
lspconfig.ansiblels.setup { capabilities = capabilities }
lspconfig.beancount.setup { capabilities = capabilities }
lspconfig.ltex.setup { capabilities = capabilities }
lspconfig.terraformls.setup { capabilities = capabilities }
lspconfig.tflint.setup { capabilities = capabilities }
lspconfig.docker_compose_language_service.setup {
root_dir = lspconfig.util.root_pattern("docker-compose.yaml", "docker-compose.yml"),
filetypes = { "yaml", "yaml.docker-compose" },
capabilities = capabilities,
}
lspconfig.dockerls.setup { capabilities = capabilities }
-- lspconfig.sqlls.setup {capabilities = capabilities}
lspconfig.lua_ls.setup {
settings = {
Lua = {
runtime = {
-- Tell the language server which version of Lua you're using (most likely LuaJIT in the case of Neovim)
version = "LuaJIT",
},
diagnostics = {
-- Get the language server to recognize the `vim` global
globals = { "vim" },
},
workspace = {
-- Make the server aware of Neovim runtime files
library = vim.api.nvim_get_runtime_file("", true),
},
-- Do not send telemetry data containing a randomized but unique identifier
telemetry = {
enable = false,
},
},
},
capabilities = capabilities
}
for pattern, command in pairs(formatters) do
vim.api.nvim_create_autocmd("FileType", {
pattern = pattern,
callback = function()
nmap("<C-f>", command)
end,
})
end
require("lspconfig").pyright.setup({})
require("lspconfig").efm.setup({
lspconfig.efm.setup {
init_options = { documentFormatting = true },
filetypes = { "python", "sh" },
settings = {
rootMarkers = { ".git/", "pyproject.toml" },
languages = {
python = { { formatCommand = "black -", formatStdin = true } },
sql = { {
formatCommand = "sqlfluff fix --dialect bigquery -",
formatStdin = true
} },
sh = { {
formatCommand = "shfmt -i 4 -bn -sr -p -ci",
formatStdin = true,
lintCommand = "shellcheck -f gcc -x",
lintSource = "shellcheck",
lintFormats = {
"%f:%l:%c: %trror: %m",
"%f:%l:%c: %tarning: %m",
"%f:%l:%c: %tote: %m",
}
} },
},
},
capabilities = capabilities
}
local luasnip = require 'luasnip'
local cmp = require 'cmp'
cmp.setup {
snippet = {
expand = function(args)
luasnip.lsp_expand(args.body)
end,
},
mapping = cmp.mapping.preset.insert({
['<C-u>'] = cmp.mapping.scroll_docs(-4), -- Up
['<C-d>'] = cmp.mapping.scroll_docs(4), -- Down
-- C-b (back) C-f (forward) for snippet placeholder navigation.
['<C-Space>'] = cmp.mapping.complete(),
['<CR>'] = cmp.mapping.confirm {
behavior = cmp.ConfirmBehavior.Replace,
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()
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' }),
}),
sources = {
{ name = 'nvim_lsp' },
{ name = 'luasnip' },
{ name = 'buffer' },
{ name = 'path' },
{ name = 'orgmode' },
},
}
local orgmode = require("orgmode")
orgmode.setup_ts_grammar()
require("nvim-treesitter.configs").setup {
-- If TS highlights are not enabled at all, or disabled via `disable` prop,
-- highlighting will fallback to default Vim syntax highlighting
highlight = {
enable = true,
-- Required for spellcheck, some LaTex highlights and
-- code block highlights that do not have ts grammar
additional_vim_regex_highlighting = { "org" },
},
ensure_installed = { "org" }, -- Or run :TSUpdate org
}
orgmode.setup {
org_agenda_files = { "~/Nextcloud/org/*" },
org_default_notes_file = "~/Nextcloud/org/refile.org",
}
vim.api.nvim_create_autocmd("LspAttach", {
group = vim.api.nvim_create_augroup("UserLspConfig", {}),
callback = function(ev)
-- Enable completion triggered by <c-x><c-o>
vim.bo[ev.buf].omnifunc = "v:lua.vim.lsp.omnifunc"
local opts = { buffer = ev.buf }
vim.keymap.set("n", "<C-f>", function()
vim.lsp.buf.format({ async = true })
end, opts)
end,
})