From 1015f7e587953651b5b5c0bfff210fa2448fabf3 Mon Sep 17 00:00:00 2001 From: billy4479 Date: Sun, 29 Dec 2024 13:49:22 +0100 Subject: [PATCH] Add formatting in nvim --- .../applications/editor/nvim/default.nix | 1 + .../editor/nvim/plugins/format.nix | 120 ++++++++++-------- 2 files changed, 70 insertions(+), 51 deletions(-) diff --git a/user/modules/applications/editor/nvim/default.nix b/user/modules/applications/editor/nvim/default.nix index 6d0be90..b466549 100644 --- a/user/modules/applications/editor/nvim/default.nix +++ b/user/modules/applications/editor/nvim/default.nix @@ -7,6 +7,7 @@ { imports = [ ./plugins/catppuccin.nix + ./plugins/format.nix ./plugins/telescope.nix ./plugins/treesitter.nix ]; diff --git a/user/modules/applications/editor/nvim/plugins/format.nix b/user/modules/applications/editor/nvim/plugins/format.nix index 99fc573..d55c084 100644 --- a/user/modules/applications/editor/nvim/plugins/format.nix +++ b/user/modules/applications/editor/nvim/plugins/format.nix @@ -1,54 +1,72 @@ { pkgs, ... }: { - programs.neovim.plugins = [ - { - plugin = pkgs.vimPlugins.conform-nvim; - type = "lua"; - config = # lua - '' - require("conform").setup({ - formatters_by_ft = { - lua = { "stylua" }, - go = { "gofmt" }, - nix = { "nixfmt" }, - python = { "ruff" }, - rust = { "rustfmt", lsp_format = "fallback" }, - javascript = { "prettierd", "prettier", stop_after_first = true }, - }, - }) - - vim.api.nvim_create_autocmd("BufWritePre", { - pattern = "*", - callback = function(args) - -- Disable "format_on_save lsp_fallback" for languages that don't - -- have a well standardized coding style. You can add additional - -- languages here or re-enable it for the disabled ones. - local disable_filetypes = { c = true, cpp = true } - local lsp_format_opt - if disable_filetypes[args.buf.filetype] then - lsp_format_opt = 'never' - else - lsp_format_opt = 'fallback' - end - require("conform").format({ bufnr = args.buf, lsp_format = lsp_format_opt }) - end, - }) - - vim.o.formatexpr = "v:lua.require'conform'.formatexpr()" - - vim.keymap.set("n", "f", function() - local disable_filetypes = { c = true, cpp = true } - local lsp_format_opt - if disable_filetypes[args.buf.filetype] then - lsp_format_opt = 'never' - else - lsp_format_opt = 'fallback' - end - - require('conform').format { async = true, lsp_format = lsp_format_opt} - end, {} - ) - ''; - } - ]; + programs.neovim = { + # TODO: move this to flake.nix mkShell + # each project should add their own + extraPackages = with pkgs; [ + stylua + ]; + + plugins = [ + { + plugin = pkgs.vimPlugins.conform-nvim; + type = "lua"; + config = # lua + '' + -- https://github.com/nvim-lua/kickstart.nvim/blob/master/init.lua#L677 + -- https://github.com/tjdevries/config.nvim/blob/master/lua/custom/autoformat.lua + local conform = require("conform") + + conform.setup({ + formatters_by_ft = { + lua = { "stylua" }, + go = { "gofmt" }, + nix = { "nixfmt", "injected" }, + python = { "ruff" }, + rust = { "rustfmt", lsp_format = "fallback" }, + javascript = { "prettierd", "prettier", stop_after_first = true }, + }, + + formatters = { injected = { options = { ignore_errors = false } } }, + + -- log_level = vim.log.levels.DEBUG, + }) + + local format_buf = function(bufnr, async) + -- Disable "format_on_save lsp_fallback" for languages that don't + -- have a well standardized coding style. You can add additional + -- languages here or re-enable it for the disabled ones. + local disable_filetypes = { c = true, cpp = true } + local lsp_format_opt + if disable_filetypes[vim.bo[bufnr].filetype] then + lsp_format_opt = "never" + else + lsp_format_opt = "fallback" + end + + require("conform").format({ + async = async, + bufnr = bufnr, + lsp_format = lsp_format_opt, + }) + end + + vim.api.nvim_create_autocmd("BufWritePre", { + group = vim.api.nvim_create_augroup("custom-conform", { clear = true }), + pattern = "*", + callback = function(args) + format_buf(args.buf, false) + end, + }) + + vim.o.formatexpr = "v:lua.require'conform'.formatexpr()" + + vim.keymap.set("n", "f", function() + local bufnr = vim.api.nvim_buf_get_number(0) + format_buf(bufnr, true) + end, {}) + ''; + } + ]; + }; }