From 85bab070c6535163e684c60f94ea64990eae666c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Moritz=20B=C3=B6hme?= Date: Sun, 15 Sep 2024 15:32:43 +0200 Subject: [PATCH] feat(nvim): move conform to own file --- modules/programs/nvim/new_plugins/conform.nix | 73 +++++++++++++++++++ modules/programs/nvim/new_plugins/other.nix | 64 ---------------- 2 files changed, 73 insertions(+), 64 deletions(-) create mode 100644 modules/programs/nvim/new_plugins/conform.nix diff --git a/modules/programs/nvim/new_plugins/conform.nix b/modules/programs/nvim/new_plugins/conform.nix new file mode 100644 index 0000000..a8b94f3 --- /dev/null +++ b/modules/programs/nvim/new_plugins/conform.nix @@ -0,0 +1,73 @@ +{ config, lib, pkgs, inputs, ... }: + +let + inherit (lib) mkEnableOption mkIf readFile; +in +{ + home-manager.users.moritz.programs.nixvim = { + plugins.conform-nvim = { + enable = true; + settings.formatters_by_ft = { + "*" = [ "codespell" "trim_whitespace" ]; + elixir = [ "mix" ]; + gleam = [ "gleam" ]; + go = [ "gofmt" ]; + json = [ "jq" ]; + lua = [ "stylua" ]; + nix.__raw = ''{ "nixpkgs_fmt", "alejandra", stop_after_first=true }''; + python.__raw = '' + function(bufnr) + return { first("ruff_organize_imports", "isort"), first("ruff_format", "black")} + end + ''; + rust = [ "rustfmt" ]; + sh = [ "shfmt" ]; + tex = [ "latexindent" ]; + toml = [ "taplo" ]; + yaml = [ "yamlfix" ]; + }; + }; + opts.formatexpr = "v:lua.require'conform'.formatexpr()"; + extraConfigLuaPre = '' + ---@param bufnr integer + ---@param ... string + ---@return string + local function first(bufnr, ...) + local conform = require("conform") + for i = 1, select("#", ...) do + local formatter = select(i, ...) + if conform.get_formatter_info(formatter, bufnr).available then + return formatter + end + end + return select(1, ...) + end + + vim.api.nvim_create_user_command("Format", function(opts) + require("conform").format({ formatters = opts.fargs }) + end, { + nargs = "+", + complete = function() + local formatters_by_ft = require("conform").formatters_by_ft + local names = formatters_by_ft[vim.bo.filetype] or formatters_by_ft["_"] or {} + names = vim.list_extend(names, formatters_by_ft["*"] or {}) + names = vim.tbl_flatten(names) + local formatters = vim.tbl_map(require("conform").get_formatter_info, names) + formatters = vim.tbl_filter(function(formatter) + return formatter.available + end, formatters) + return vim.tbl_map(function(formatter_info) + return formatter_info.name + end, formatters) + end, + }) + ''; + performance.combinePlugins.standalonePlugins = [ + "conform.nvim" + ]; + keymaps = [ + { key = "cf"; action.__raw = ''function() require("conform").format() end''; options.desc = "Format current file"; } + ]; + }; + +} diff --git a/modules/programs/nvim/new_plugins/other.nix b/modules/programs/nvim/new_plugins/other.nix index b5d91a1..ac8ec77 100644 --- a/modules/programs/nvim/new_plugins/other.nix +++ b/modules/programs/nvim/new_plugins/other.nix @@ -22,71 +22,7 @@ in keymaps.todoTelescope.key = "fc"; }; } - { - plugins.conform-nvim = { - enable = true; - settings.formatters_by_ft = { - "*" = [ "codespell" "trim_whitespace" ]; - elixir = [ "mix" ]; - gleam = [ "gleam" ]; - go = [ "gofmt" ]; - json = [ "jq" ]; - lua = [ "stylua" ]; - nix.__raw = ''{ "nixpkgs_fmt", "alejandra", stop_after_first=true }''; - python.__raw = '' - function(bufnr) - return { first("ruff_organize_imports", "isort"), first("ruff_format", "black")} - end - ''; - rust = [ "rustfmt" ]; - sh = [ "shfmt" ]; - tex = [ "latexindent" ]; - toml = [ "taplo" ]; - yaml = [ "yamlfix" ]; - }; - }; - opts.formatexpr = "v:lua.require'conform'.formatexpr()"; - extraConfigLuaPre = '' - ---@param bufnr integer - ---@param ... string - ---@return string - local function first(bufnr, ...) - local conform = require("conform") - for i = 1, select("#", ...) do - local formatter = select(i, ...) - if conform.get_formatter_info(formatter, bufnr).available then - return formatter - end - end - return select(1, ...) - end - vim.api.nvim_create_user_command("Format", function(opts) - require("conform").format({ formatters = opts.fargs }) - end, { - nargs = "+", - complete = function() - local formatters_by_ft = require("conform").formatters_by_ft - local names = formatters_by_ft[vim.bo.filetype] or formatters_by_ft["_"] or {} - names = vim.list_extend(names, formatters_by_ft["*"] or {}) - names = vim.tbl_flatten(names) - local formatters = vim.tbl_map(require("conform").get_formatter_info, names) - formatters = vim.tbl_filter(function(formatter) - return formatter.available - end, formatters) - return vim.tbl_map(function(formatter_info) - return formatter_info.name - end, formatters) - end, - }) - ''; - performance.combinePlugins.standalonePlugins = [ - "conform.nvim" - ]; - keymaps = [ - { key = "cf"; action.__raw = ''function() require("conform").format() end''; options.desc = "Format current file"; } - ]; - } { keymaps = [ { key = ""; action = "noh"; options.desc = "Escape and clear hlsearch"; mode = [ "i" "n" ]; }