diff --git a/lib/helpers.nix b/lib/helpers.nix index 6c307761..95f3d677 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 0a9555de..c632e132 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 00000000..be42c3a9 --- /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 00000000..81beb1e2 --- /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; + }; + }; +}