Skip to content

Commit

Permalink
plugin/persistence: init + tests (#645)
Browse files Browse the repository at this point in the history
  • Loading branch information
traxys authored Oct 16, 2023
1 parent a9698ea commit 0b87e5b
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/helpers.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
1 change: 1 addition & 0 deletions plugins/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
68 changes: 68 additions & 0 deletions plugins/utils/persistence.nix
Original file line number Diff line number Diff line change
@@ -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})
'';
};
}
16 changes: 16 additions & 0 deletions tests/test-sources/plugins/utils/persistence.nix
Original file line number Diff line number Diff line change
@@ -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;
};
};
}

0 comments on commit 0b87e5b

Please sign in to comment.