From 8772f38aed3d7bafaa1fa60342be17ead6f358a5 Mon Sep 17 00:00:00 2001 From: Pim Kunis Date: Fri, 10 Nov 2023 11:20:35 +0100 Subject: [PATCH] add lsp-format and efm-langserver to autoformat files autoformat lua files on save --- home-manager/neovim/bufferline.lua | 13 +++--- home-manager/neovim/cmp.lua | 17 +++----- home-manager/neovim/default.nix | 3 ++ home-manager/neovim/lspconfig.lua | 64 ++++++++++++++++++++---------- home-manager/neovim/telescope.lua | 24 ++++------- home-manager/neovim/treesitter.lua | 5 ++- 6 files changed, 66 insertions(+), 60 deletions(-) diff --git a/home-manager/neovim/bufferline.lua b/home-manager/neovim/bufferline.lua index 34fa5b8..5d13d9b 100644 --- a/home-manager/neovim/bufferline.lua +++ b/home-manager/neovim/bufferline.lua @@ -1,16 +1,13 @@ -require("bufferline").setup{ +require("bufferline").setup { options = { diagnostics = 'nvim_lsp', diagnostics_indicator = function(count, level, diagnostics_dict, context) - local icon = level:match("error") and " " or " " - return " " .. icon .. count + local icon = level:match("error") and " " or " " + return " " .. icon .. count end, separator_style = "slant", - hover = { - enabled = true, - reveal = {'close'}, - }, - }, + hover = {enabled = true, reveal = {'close'}} + } } vim.keymap.set('n', 'ft', ":BufferLinePick", {}) diff --git a/home-manager/neovim/cmp.lua b/home-manager/neovim/cmp.lua index cf2b7a7..a42f7ab 100644 --- a/home-manager/neovim/cmp.lua +++ b/home-manager/neovim/cmp.lua @@ -5,11 +5,7 @@ require('luasnip.loaders.from_vscode').lazy_load() luasnip.config.setup {} cmp.setup { - snippet = { - expand = function(args) - luasnip.lsp_expand(args.body) - end, - }, + snippet = {expand = function(args) luasnip.lsp_expand(args.body) end}, mapping = cmp.mapping.preset.insert { [''] = cmp.mapping.select_next_item(), [''] = cmp.mapping.select_prev_item(), @@ -18,7 +14,7 @@ cmp.setup { [''] = cmp.mapping.complete {}, [''] = cmp.mapping.confirm { behavior = cmp.ConfirmBehavior.Replace, - select = true, + select = true }, [''] = cmp.mapping(function(fallback) if cmp.visible() then @@ -28,7 +24,7 @@ cmp.setup { else fallback() end - end, { 'i', 's' }), + end, {'i', 's'}), [''] = cmp.mapping(function(fallback) if cmp.visible() then cmp.select_prev_item() @@ -37,10 +33,7 @@ cmp.setup { else fallback() end - end, { 'i', 's' }), - }, - sources = { - { name = 'nvim_lsp' }, - { name = 'luasnip' }, + end, {'i', 's'}) }, + sources = {{name = 'nvim_lsp'}, {name = 'luasnip'}} } diff --git a/home-manager/neovim/default.nix b/home-manager/neovim/default.nix index ac36944..35130f3 100644 --- a/home-manager/neovim/default.nix +++ b/home-manager/neovim/default.nix @@ -14,6 +14,8 @@ neofetch gopls terraform-ls + luaformatter + efm-langserver ]; plugins = with pkgs.vimPlugins; [ @@ -65,6 +67,7 @@ config = builtins.readFile ./bufferline.lua; } nvim-web-devicons + lsp-format-nvim ]; }; diff --git a/home-manager/neovim/lspconfig.lua b/home-manager/neovim/lspconfig.lua index abe6e13..40a5b70 100644 --- a/home-manager/neovim/lspconfig.lua +++ b/home-manager/neovim/lspconfig.lua @@ -1,33 +1,55 @@ -local on_attach = function(_, bufnr) +require("lsp-format").setup {} - local bufmap = function(keys, func) - vim.keymap.set('n', keys, func, { buffer = bufnr }) - end +local on_attach = function(client, bufnr) + local bufmap = function(keys, func) + vim.keymap.set('n', keys, func, {buffer = bufnr}) + end - bufmap('r', vim.lsp.buf.rename) - bufmap('a', vim.lsp.buf.code_action) + bufmap('r', vim.lsp.buf.rename) + bufmap('a', vim.lsp.buf.code_action) - bufmap('gd', vim.lsp.buf.definition) - bufmap('gD', vim.lsp.buf.declaration) - bufmap('gI', vim.lsp.buf.implementation) - bufmap('D', vim.lsp.buf.type_definition) + bufmap('gd', vim.lsp.buf.definition) + bufmap('gD', vim.lsp.buf.declaration) + bufmap('gI', vim.lsp.buf.implementation) + bufmap('D', vim.lsp.buf.type_definition) - bufmap('gr', require('telescope.builtin').lsp_references) - bufmap('s', require('telescope.builtin').lsp_document_symbols) - bufmap('S', require('telescope.builtin').lsp_dynamic_workspace_symbols) + bufmap('gr', require('telescope.builtin').lsp_references) + bufmap('s', require('telescope.builtin').lsp_document_symbols) + bufmap('S', + require('telescope.builtin').lsp_dynamic_workspace_symbols) - bufmap('K', vim.lsp.buf.hover) + bufmap('K', vim.lsp.buf.hover) - vim.api.nvim_buf_create_user_command(bufnr, 'Format', function(_) - vim.lsp.buf.format() - end, {}) + vim.api.nvim_buf_create_user_command(bufnr, 'Format', + function(_) vim.lsp.buf.format() end, + {}) end local capabilities = vim.lsp.protocol.make_client_capabilities() capabilities = require('cmp_nvim_lsp').default_capabilities(capabilities) require('neodev').setup() -require'lspconfig'.nil_ls.setup{ on_attach = on_attach, capabilities = capabilities } -require'lspconfig'.pyright.setup{ on_attach = on_attach, capabilities = capabilities } -require'lspconfig'.gopls.setup{ on_attach = on_attach, capabilities = capabilities } -require'lspconfig'.terraformls.setup{ on_attach = on_attach, capabilities = capabilities } +require'lspconfig'.nil_ls.setup { + on_attach = on_attach, + capabilities = capabilities +} +require'lspconfig'.pyright.setup { + on_attach = on_attach, + capabilities = capabilities +} +require'lspconfig'.gopls.setup { + on_attach = on_attach, + capabilities = capabilities +} +require'lspconfig'.terraformls.setup { + on_attach = on_attach, + capabilities = capabilities +} + +local luaformat = {formatCommand = "lua-format -i", formatStdin = true} +require'lspconfig'.efm.setup { + on_attach = require("lsp-format").on_attach, + init_options = {documentFormatting = true}, + settings = {languages = {lua = {luaformat}}}, + filetypes = {"lua"} +} diff --git a/home-manager/neovim/telescope.lua b/home-manager/neovim/telescope.lua index 59c0ffc..66b8346 100644 --- a/home-manager/neovim/telescope.lua +++ b/home-manager/neovim/telescope.lua @@ -6,22 +6,12 @@ vim.keymap.set('n', 'fb', builtin.buffers, {}) vim.keymap.set('n', 'fr', builtin.lsp_references, {}) vim.keymap.set('n', 'fs', builtin.lsp_document_symbols, {}) -require('telescope').setup{ - pickers = { - find_files = { - theme = "dropdown" - }, - live_grep = { - theme = "dropdown" - }, - buffers = { - theme = "dropdown" - }, - lsp_references = { - theme = "dropdown" - }, - lsp_document_symbols = { - theme = "dropdown" +require('telescope').setup { + pickers = { + find_files = {theme = "dropdown"}, + live_grep = {theme = "dropdown"}, + buffers = {theme = "dropdown"}, + lsp_references = {theme = "dropdown"}, + lsp_document_symbols = {theme = "dropdown"} } - } } diff --git a/home-manager/neovim/treesitter.lua b/home-manager/neovim/treesitter.lua index 2edb953..d73c59a 100644 --- a/home-manager/neovim/treesitter.lua +++ b/home-manager/neovim/treesitter.lua @@ -3,7 +3,8 @@ require('nvim-treesitter.configs').setup { auto_install = false, - highlight = { enable = true }, + highlight = {enable = true}, - indent = { enable = true }, + indent = {enable = true} } +