feat(session-manager): custom setup opts

This commit is contained in:
Ching Pei Yang 2024-03-03 16:48:12 +00:00
parent 47acf325d6
commit 59d5ac0d63
2 changed files with 56 additions and 65 deletions

View file

@ -3,12 +3,8 @@
lib, lib,
... ...
}: let }: let
inherit (lib.modules) mkIf mkMerge; inherit (lib) mkIf optionals mkMerge mkBinding nvim;
inherit (lib.lists) optionals; inherit (lib.nvim.lua) toLuaObject;
inherit (lib.strings) concatStringsSep;
inherit (lib.trivial) boolToString;
inherit (lib.nvim.binds) mkBinding;
inherit (lib.nvim.dag) entryAnywhere;
cfg = config.vim.session.nvim-session-manager; cfg = config.vim.session.nvim-session-manager;
in { in {
@ -19,7 +15,7 @@ in {
"nvim-session-manager" "nvim-session-manager"
"plenary-nvim" "plenary-nvim"
] ]
++ optionals (cfg.usePicker) ["dressing-nvim"]; ++ optionals cfg.usePicker ["dressing-nvim"];
maps.normal = mkMerge [ maps.normal = mkMerge [
(mkBinding cfg.mappings.loadSession ":SessionManager load_session<CR>" "Load session") (mkBinding cfg.mappings.loadSession ":SessionManager load_session<CR>" "Load session")
@ -29,31 +25,10 @@ in {
# TODO: load_current_dir_session # TODO: load_current_dir_session
]; ];
luaConfigRC.nvim-session-manager = entryAnywhere '' luaConfigRC.nvim-session-manager = nvim.dag.entryAnywhere ''
local Path = require('plenary.path') local Path = require('plenary.path')
local sm = require('session_manager.config') local sm = require('session_manager.config')
require('session_manager').setup({ require('session_manager').setup(${toLuaObject cfg.setupOpts})
sessions_dir = Path:new(vim.fn.stdpath('data'), 'sessions'),
path_replacer = '${toString cfg.pathReplacer}',
colon_replacer = '${toString cfg.colonReplacer}',
autoload_mode = sm.AutoloadMode.${toString cfg.autoloadMode},
autosave_last_session = ${boolToString cfg.autoSave.lastSession},
autosave_ignore_not_normal = ${boolToString cfg.autoSave.ignoreNotNormal},
autosave_ignore_dirs = {${concatStringsSep ", " (map (x: "\'" + x + "\'") cfg.autoSave.ignoreDirs)}},
autosave_ignore_filetypes = {${concatStringsSep ", " (map (x: "\'" + x + "\'") cfg.autoSave.ignoreFiletypes)}},
autosave_ignore_buftypes = {${concatStringsSep ", " (map (x: "\'" + x + "\'") cfg.autoSave.ignoreBufTypes)}},
autosave_only_in_session = ${boolToString cfg.autoSave.onlyInSession},
max_path_length = ${toString cfg.maxPathLength},
})
''; '';
}; };
}; };

View file

@ -1,7 +1,23 @@
{lib, ...}: let {lib, ...}: let
inherit (lib.options) mkOption mkEnableOption; inherit (lib.types) nullOr str bool;
inherit (lib.types) nullOr str bool int listOf enum; inherit (lib) mkEnableOption mkOption types mkRenamedOptionModule;
in { in {
imports = let
renameSetupOpt = oldPath: newPath:
mkRenamedOptionModule (["vim" "session" "nvim-session-manager"] ++ oldPath) (["vim" "session" "nvim-session-manager" "setupOpts"] ++ newPath);
in [
(renameSetupOpt ["pathReplacer"] ["path_replacer"])
(renameSetupOpt ["colonReplacer"] ["colon_replacer"])
(renameSetupOpt ["autoloadMode"] ["autoload_mode"])
(renameSetupOpt ["maxPathLength"] ["max_path_length"])
(renameSetupOpt ["autoSave" "lastSession"] ["autosave_last_session"])
(renameSetupOpt ["autoSave" "ignoreNotNormal"] ["autosave_ignore_not_normal"])
(renameSetupOpt ["autoSave" "ignoreDirs"] ["autosave_ignore_dirs"])
(renameSetupOpt ["autoSave" "ignoreFiletypes"] ["autosave_ignore_filetypes"])
(renameSetupOpt ["autoSave" "ignoreBufTypes"] ["autosave_ignore_buftypes"])
(renameSetupOpt ["autoSave" "onlyInSession"] ["autosave_only_in_session"])
];
options.vim.session.nvim-session-manager = { options.vim.session.nvim-session-manager = {
enable = mkEnableOption "nvim-session-manager: manage sessions like folders in VSCode"; enable = mkEnableOption "nvim-session-manager: manage sessions like folders in VSCode";
@ -37,63 +53,63 @@ in {
description = "Whether or not we should use dressing.nvim to build a session picker UI"; description = "Whether or not we should use dressing.nvim to build a session picker UI";
}; };
pathReplacer = mkOption { setupOpts = {
type = str; path_replacer = mkOption {
default = "__"; type = types.str;
description = "The character to which the path separator will be replaced for session files"; default = "__";
}; description = "The character to which the path separator will be replaced for session files";
};
colonReplacer = mkOption { colon_replacer = mkOption {
type = str; type = types.str;
default = "++"; default = "++";
description = "The character to which the colon symbol will be replaced for session files"; description = "The character to which the colon symbol will be replaced for session files";
}; };
autoloadMode = mkOption { autoload_mode = mkOption {
type = enum ["Disabled" "CurrentDir" "LastSession"]; type = types.enum ["Disabled" "CurrentDir" "LastSession"];
default = "LastSession"; default = "LastSession";
description = "Define what to do when Neovim is started without arguments. Possible values: Disabled, CurrentDir, LastSession"; description = "Define what to do when Neovim is started without arguments. Possible values: Disabled, CurrentDir, LastSession";
}; };
maxPathLength = mkOption { max_path_length = mkOption {
type = nullOr int; type = types.nullOr types.int;
default = 80; default = 80;
description = "Shorten the display path if length exceeds this threshold. Use 0 if don't want to shorten the path at all"; description = "Shorten the display path if length exceeds this threshold. Use 0 if don't want to shorten the path at all";
}; };
autoSave = { autosave_last_session = mkOption {
lastSession = mkOption { type = types.bool;
type = bool;
default = true; default = true;
description = "Automatically save last session on exit and on session switch"; description = "Automatically save last session on exit and on session switch";
}; };
ignoreNotNormal = mkOption { autosave_ignore_not_normal = mkOption {
type = bool; type = types.bool;
default = true; default = true;
description = "Plugin will not save a session when no buffers are opened, or all of them aren't writable or listed"; description = "Plugin will not save a session when no buffers are opened, or all of them aren't writable or listed";
}; };
ignoreDirs = mkOption { autosave_ignore_dirs = mkOption {
type = listOf str; type = types.listOf types.str;
default = []; default = [];
description = "A list of directories where the session will not be autosaved"; description = "A list of directories where the session will not be autosaved";
}; };
ignoreFiletypes = mkOption { autosave_ignore_filetypes = mkOption {
type = listOf str; type = types.listOf types.str;
default = ["gitcommit"]; default = ["gitcommit"];
description = "All buffers of these file types will be closed before the session is saved"; description = "All buffers of these file types will be closed before the session is saved";
}; };
ignoreBufTypes = mkOption { autosave_ignore_buftypes = mkOption {
type = listOf str; type = types.listOf types.str;
default = []; default = [];
description = "All buffers of these bufer types will be closed before the session is saved"; description = "All buffers of these bufer types will be closed before the session is saved";
}; };
onlyInSession = mkOption { autosave_only_in_session = mkOption {
type = bool; type = types.bool;
default = false; default = false;
description = "Always autosaves session. If true, only autosaves after a session is active"; description = "Always autosaves session. If true, only autosaves after a session is active";
}; };