From 0b87e5b70c7da4b3a5a33197afeca1b308a3b916 Mon Sep 17 00:00:00 2001 From: traxys Date: Mon, 16 Oct 2023 23:54:41 +0200 Subject: [PATCH] plugin/persistence: init + tests (#645) --- lib/helpers.nix | 2 + plugins/default.nix | 1 + plugins/utils/persistence.nix | 68 +++++++++++++++++++ .../plugins/utils/persistence.nix | 16 +++++ 4 files changed, 87 insertions(+) create mode 100644 plugins/utils/persistence.nix create mode 100644 tests/test-sources/plugins/utils/persistence.nix diff --git a/lib/helpers.nix b/lib/helpers.nix index 6c30776154..95f3d67704 100644 --- a/lib/helpers.nix +++ b/lib/helpers.nix @@ -107,6 +107,8 @@ with lib; rec { then null else y; + mkRawIfNonNull = v: ifNonNull' v (mkRaw v); + mkCompositeOption = desc: options: mkNullOrOption (types.submodule {inherit options;}) desc; diff --git a/plugins/default.nix b/plugins/default.nix index 0a9555de66..c632e13231 100644 --- a/plugins/default.nix +++ b/plugins/default.nix @@ -126,6 +126,7 @@ ./utils/nvim-osc52.nix ./utils/nvim-ufo.nix ./utils/oil.nix + ./utils/persistence.nix ./utils/project-nvim.nix ./utils/presence-nvim.nix ./utils/quickmath.nix diff --git a/plugins/utils/persistence.nix b/plugins/utils/persistence.nix new file mode 100644 index 0000000000..be42c3a95e --- /dev/null +++ b/plugins/utils/persistence.nix @@ -0,0 +1,68 @@ +{ + config, + lib, + pkgs, + ... +}: +with lib; let + helpers = import ../helpers.nix {inherit lib;}; +in { + options.plugins.persistence = + helpers.extraOptionsOptions + // { + enable = mkEnableOption "persistence.nvim"; + + package = helpers.mkPackageOption "persistence.nvim" pkgs.vimPlugins.persistence-nvim; + + dir = + helpers.defaultNullOpts.mkNullable (with types; either str helpers.rawType) + ''vim.fn.expand(vim.fn.stdpath("state") .. "/sessions/")'' + "directory where session files are saved"; + + options = let + # https://neovim.io/doc/user/options.html#'sessionoptions' + sessionOpts = [ + "blank" + "buffers" + "curdir" + "folds" + "globals" + "help" + "localoptions" + "options" + "skiprtp" + "resize" + "sesdir" + "tabpages" + "terminal" + "winpos" + "winsize" + ]; + in + helpers.defaultNullOpts.mkNullable (with types; listOf (enum sessionOpts)) + ''["buffers" "curdir" "tabpages" "winsize" "skiprtp"]'' "sessionoptions used for saving"; + + preSave = helpers.mkNullOrOption types.str "a function to call before saving the session"; + + saveEmpty = helpers.defaultNullOpts.mkBool false '' + don't save if there are no open file buffers + ''; + }; + + config = let + cfg = config.plugins.persistence; + in + mkIf cfg.enable { + extraPlugins = [cfg.package]; + + extraConfigLua = let + opts = { + inherit (cfg) dir options; + pre_save = helpers.mkRawIfNonNull cfg.preSave; + save_empty = cfg.saveEmpty; + }; + in '' + require('persistence').setup(${helpers.toLuaObject opts}) + ''; + }; +} diff --git a/tests/test-sources/plugins/utils/persistence.nix b/tests/test-sources/plugins/utils/persistence.nix new file mode 100644 index 0000000000..81beb1e2e1 --- /dev/null +++ b/tests/test-sources/plugins/utils/persistence.nix @@ -0,0 +1,16 @@ +{ + empty = { + plugins.persistence.enable = true; + }; + + defaults = { + plugins.persistence = { + enable = true; + + dir.__raw = ''vim.fn.expand(vim.fn.stdpath("state") .. "/sessions/")''; + options = ["buffers" "curdir" "tabpages" "winsize"]; + preSave = null; + saveEmpty = false; + }; + }; +}