From 4a9a3c1f6cea52d756d406d6467b48898cdbff07 Mon Sep 17 00:00:00 2001 From: Gaetan Lepage Date: Wed, 28 Jun 2023 22:00:55 +0200 Subject: [PATCH] plugins/neorg: refactor + test + neorg-telescope --- plugins/utils/neorg.nix | 127 ++++++++++++--------- tests/test-sources/plugins/utils/neorg.nix | 75 ++++++++++++ 2 files changed, 147 insertions(+), 55 deletions(-) create mode 100644 tests/test-sources/plugins/utils/neorg.nix diff --git a/plugins/utils/neorg.nix b/plugins/utils/neorg.nix index 5dc3dc8a..a303725d 100644 --- a/plugins/utils/neorg.nix +++ b/plugins/utils/neorg.nix @@ -43,7 +43,7 @@ in }; fatal = { hl = "ErrorMsg"; - level = "5"; + level = 5; }; }; @@ -70,88 +70,105 @@ in ''; modes = - helpers.mkNullOrOption - (types.submodule { - options = - mapAttrs - (mode: defaults: { - hl = helpers.defaultNullOpts.mkStr defaults.hl '' - Highlight for mode ${mode} - ''; - level = mkOption { - type = with types; either int (enum levelNames); - default = defaults.level; - description = "Level for mode ${mode}"; - }; - }) - modes; + mapAttrs + (mode: defaults: { + hl = helpers.defaultNullOpts.mkStr defaults.hl '' + Highlight for mode ${mode} + ''; + level = mkOption { + type = with types; either int (enum levelNames); + default = defaults.level; + description = "Level for mode ${mode}"; + }; }) - "Level configuration"; + modes; floatPrecision = helpers.defaultNullOpts.mkNullable types.float "0.01" '' Can limit the number of decimals displayed for floats ''; }; - modules = helpers.mkNullOrOption (types.attrsOf types.attrs) '' - Modules configuration. - - Example: - - modules = { - "core.defaults" = {}; - "core.norg.dirman" = { + modules = mkOption { + type = with types; attrsOf attrs; + description = "Modules configuration."; + default = {}; + example = { + "core.defaults" = {__empty = null;}; + "core.dirman" = { config = { workspaces = { - work = "~/notes/work"; - home = "~/notes/home"; + work = "~/notes/work"; + home = "~/notes/home"; }; }; }; }; - ''; + }; }; config = let - options = + setupOptions = with cfg; { - lazy_loading = cfg.lazyLoading; + lazy_loading = lazyLoading; - logger = { - inherit (cfg.logger) plugin; - use_console = cfg.logger.useConsole; - inherit (cfg.logger) highlights; - use_file = cfg.logger.useFile; - inherit (cfg.logger) level; + logger = with logger; { + inherit plugin; + use_console = useConsole; + inherit highlights; + use_file = useFile; + inherit level; modes = - if (cfg.logger.modes == null) - then null - else - attrsets.mapAttrsToList - (mode: modeConfig: { - name = mode; - inherit (modeConfig) hl; - level = let - inherit (modeConfig) level; - in - if (isInt level) - then level - else helpers.mkRaw "vim.log.levels.${strings.toUpper level}"; - }) - cfg.logger.modes; - float_precision = cfg.logger.floatPrecision; + mapAttrsToList + (mode: modeConfig: { + name = mode; + inherit (modeConfig) hl; + level = let + inherit (modeConfig) level; + in + if (isInt level) + then level + else helpers.mkRaw "vim.log.levels.${strings.toUpper level}"; + }) + modes; + float_precision = floatPrecision; }; - load = cfg.modules; + load = modules; } // cfg.extraOptions; + + telescopeSupport = hasAttr "core.integrations.telescope" cfg.modules; in mkIf cfg.enable { - extraPlugins = [cfg.package]; + warnings = + ( + optional + (telescopeSupport && (!config.plugins.telescope.enable)) + '' + Telescope support for neorg (`core.integrations.telescope`) is enabled but the + telescope plugin is not. + '' + ) + ++ ( + optional + ((hasAttr "core.defaults" cfg.modules) && (!config.plugins.treesitter.enable)) + '' + Neorg's `core.defaults` module is enabled but `plugins.treesitter` is not. + Treesitter is required when using the `core.defaults`. + '' + ); + + extraPlugins = + [cfg.package] + ++ ( + optional + telescopeSupport + pkgs.vimPlugins.neorg-telescope + ); extraConfigLua = '' - require('neorg').setup(${helpers.toLuaObject options}) + require('neorg').setup(${helpers.toLuaObject setupOptions}) ''; }; } diff --git a/tests/test-sources/plugins/utils/neorg.nix b/tests/test-sources/plugins/utils/neorg.nix new file mode 100644 index 00000000..d6cb8979 --- /dev/null +++ b/tests/test-sources/plugins/utils/neorg.nix @@ -0,0 +1,75 @@ +{ + empty = { + plugins.neorg.enable = true; + }; + + example = { + plugins = { + # Treesitter is required when using the "core.defaults" module. + treesitter.enable = true; + + neorg = { + enable = true; + + lazyLoading = false; + logger = { + plugin = "neorg"; + useConsole = true; + highlights = true; + useFile = true; + level = "warn"; + modes = { + trace = { + hl = "Comment"; + level = "trace"; + }; + debug = { + hl = "Comment"; + level = "debug"; + }; + info = { + hl = "None"; + level = "info"; + }; + warn = { + hl = "WarningMsg"; + level = "warn"; + }; + error = { + hl = "ErrorMsg"; + level = "error"; + }; + fatal = { + hl = "ErrorMsg"; + level = 5; + }; + }; + floatPrecision = 0.01; + }; + + modules = { + "core.defaults" = {__empty = null;}; + "core.dirman" = { + config = { + workspaces = { + work = "~/notes/work"; + home = "~/notes/home"; + }; + }; + }; + }; + }; + }; + }; + + telescope-integration = { + plugins = { + telescope.enable = false; + + neorg = { + enable = false; + modules."core.integrations.telescope".__empty = null; + }; + }; + }; +}