From 121566a267e0a9f4030e50838b1ba4c38cd6c6c5 Mon Sep 17 00:00:00 2001 From: traxys Date: Sat, 15 Apr 2023 15:21:25 +0200 Subject: [PATCH] editorconfig: Migrate to builtin plugin (#335) --- modules/editorconfig.nix | 61 +++++++++++++++++++ plugins/default.nix | 1 - plugins/utils/editorconfig.nix | 59 ------------------ .../utils => modules}/editorconfig.nix | 9 ++- 4 files changed, 65 insertions(+), 65 deletions(-) create mode 100644 modules/editorconfig.nix delete mode 100644 plugins/utils/editorconfig.nix rename tests/test-sources/{plugins/utils => modules}/editorconfig.nix (56%) diff --git a/modules/editorconfig.nix b/modules/editorconfig.nix new file mode 100644 index 00000000..0ee4df01 --- /dev/null +++ b/modules/editorconfig.nix @@ -0,0 +1,61 @@ +{ + lib, + config, + ... +}: +with lib; { + imports = [ + (lib.mkRenamedOptionModule ["plugins" "editorconfig" "enable"] ["editorconfig" "enable"]) + (lib.mkRemovedOptionModule ["plugins" "editorconfig" "package"] "editorconfig is now builtin, no plugin required") + ]; + + options.editorconfig = { + enable = mkOption { + type = types.bool; + default = true; + description = "editorconfig plugin for neovim"; + }; + + properties = mkOption { + type = types.attrsOf types.str; + default = {}; + description = '' + The table key is a property name and the value is a callback function which accepts the + number of the buffer to be modified, the value of the property in the .editorconfig file, + and (optionally) a table containing all of the other properties and their values (useful + for properties which depend on other properties). The value is always a string and must be + coerced if necessary. + ''; + example = { + foo = '' + function(bufnr, val, opts) + if opts.charset and opts.charset ~= "utf-8" then + error("foo can only be set when charset is utf-8", 0) + end + vim.b[bufnr].foo = val + end + ''; + }; + }; + }; + + config = let + cfg = config.editorconfig; + in { + globals.editorconfig = mkIf (!cfg.enable) false; + + extraConfigLua = let + mkProperty = name: function: '' + __editorconfig.properties.${name} = ${function} + ''; + propertiesString = lib.concatLines (lib.mapAttrsToList mkProperty cfg.properties); + in + mkIf (propertiesString != "" && cfg.enable) '' + do + local __editorconfig = require('editorconfig') + + ${propertiesString} + end + ''; + }; +} diff --git a/plugins/default.nix b/plugins/default.nix index fd2d1280..c81e4ecb 100644 --- a/plugins/default.nix +++ b/plugins/default.nix @@ -68,7 +68,6 @@ ./utils/comment-nvim.nix ./utils/commentary.nix ./utils/easyescape.nix - ./utils/editorconfig.nix ./utils/endwise.nix ./utils/floaterm.nix ./utils/goyo.nix diff --git a/plugins/utils/editorconfig.nix b/plugins/utils/editorconfig.nix deleted file mode 100644 index 4b59ff45..00000000 --- a/plugins/utils/editorconfig.nix +++ /dev/null @@ -1,59 +0,0 @@ -{ - lib, - config, - pkgs, - ... -} @ args: -with lib; let - # TODO: Migrate to builtin editorconfig in neovim 0.9 - helpers = import ../helpers.nix args; -in { - options.plugins.editorconfig = { - enable = mkEnableOption "editorconfig plugin for neovim"; - - package = helpers.mkPackageOption "editorconfig.nvim" pkgs.vimPlugins.editorconfig-nvim; - - properties = mkOption { - type = types.attrsOf types.str; - default = {}; - description = '' - The table key is a property name and the value is a callback function which accepts the - number of the buffer to be modified, the value of the property in the .editorconfig file, - and (optionally) a table containing all of the other properties and their values (useful - for properties which depend on other properties). The value is always a string and must be - coerced if necessary. - ''; - example = { - foo = '' - function(bufnr, val, opts) - if opts.charset and opts.charset ~= "utf-8" then - error("foo can only be set when charset is utf-8", 0) - end - vim.b[bufnr].foo = val - end - ''; - }; - }; - }; - - config = let - cfg = config.plugins.editorconfig; - in - mkIf cfg.enable { - extraPlugins = [cfg.package]; - - extraConfigLua = let - mkProperty = name: function: '' - __editorconfig.properties.${name} = ${function} - ''; - propertiesString = lib.concatLines (lib.mapAttrsToList mkProperty cfg.properties); - in - mkIf (propertiesString != "" && cfg.enable) '' - do - local __editorconfig = require('editorconfig') - - ${propertiesString} - end - ''; - }; -} diff --git a/tests/test-sources/plugins/utils/editorconfig.nix b/tests/test-sources/modules/editorconfig.nix similarity index 56% rename from tests/test-sources/plugins/utils/editorconfig.nix rename to tests/test-sources/modules/editorconfig.nix index 8f25a5e6..3503e777 100644 --- a/tests/test-sources/plugins/utils/editorconfig.nix +++ b/tests/test-sources/modules/editorconfig.nix @@ -1,12 +1,11 @@ { - # TODO: convert to disable test in neovim 0.9 - empty = { - plugins.editorconfig.enable = true; + disable = { + editorconfig.enable = false; }; example = { - plugins.editorconfig.enable = true; - plugins.editorconfig.properties.foo = '' + editorconfig.enable = true; + editorconfig.properties.foo = '' function(bufnr, val, opts) if opts.charset and opts.charset ~= "utf-8" then error("foo can only be set when charset is utf-8", 0)