From eda14029813906b1ef355823de237d86fea59908 Mon Sep 17 00:00:00 2001 From: Austin Horstman Date: Sun, 6 Oct 2024 00:03:01 -0500 Subject: [PATCH] plugins/auto-session: migrate to mkNeovimPlugin --- plugins/by-name/auto-session/default.nix | 304 ++++++++++-------- .../plugins/by-name/auto-session/default.nix | 74 +++-- 2 files changed, 205 insertions(+), 173 deletions(-) diff --git a/plugins/by-name/auto-session/default.nix b/plugins/by-name/auto-session/default.nix index 59e2fba22..577677efe 100644 --- a/plugins/by-name/auto-session/default.nix +++ b/plugins/by-name/auto-session/default.nix @@ -1,120 +1,191 @@ { lib, - config, - pkgs, ... }: let + inherit (lib.nixvim) defaultNullOpts; inherit (lib) types; - cfg = config.plugins.auto-session; in -{ - options.plugins.auto-session = lib.nixvim.neovim-plugin.extraOptionsOptions // { - enable = lib.mkEnableOption "auto-session"; - - package = lib.mkPackageOption pkgs "auto-session" { - default = [ - "vimPlugins" +lib.nixvim.neovim-plugin.mkNeovimPlugin { + name = "auto-session"; + package = "auto-session"; + + maintainers = [ lib.maintainers.khaneliman ]; + + # TODO: added 204-10-05 remove after 24.11 + deprecateExtraOptions = true; + optionsRenamedToSettings = [ + "logLevel" + [ + "sessionLens" + "loadOnSetup" + ] + [ + "sessionLens" + "themeConf" + ] + [ + "sessionLens" + "previewer" + ] + [ + "sessionControl" + "controlDir" + ] + [ + "sessionControl" + "controlFilename" + ] + ]; + # NOTE: rename the old settings to use the correct variables + imports = + let + basePluginPath = [ + "plugins" "auto-session" ]; - }; - - logLevel = lib.nixvim.defaultNullOpts.mkEnum [ - "debug" - "info" - "warn" - "error" - ] "error" "Sets the log level of the plugin."; - - autoSession = { - enabled = lib.nixvim.defaultNullOpts.mkBool true '' - Enables/disables auto creating, saving and restoring. - ''; - - enableLastSession = lib.nixvim.defaultNullOpts.mkBool false '' - Whether to enable the "last session" feature. - ''; + settingsPath = basePluginPath ++ [ "settings" ]; + nestedAutoSessionPluginPath = basePluginPath ++ [ "autoSession" ]; + in + [ + (lib.mkRenamedOptionModule ( + basePluginPath + ++ [ + "autoSave" + "enabled" + ] + ) (settingsPath ++ [ "auto_save" ])) + (lib.mkRenamedOptionModule ( + basePluginPath + ++ [ + "autoRestore" + "enabled" + ] + ) (settingsPath ++ [ "auto_restore" ])) + + (lib.mkRenamedOptionModule ( + basePluginPath + ++ [ + "bypassSessionSaveFileTypes" + ] + ) (settingsPath ++ [ "bypass_save_filetypes" ])) + (lib.mkRenamedOptionModule (nestedAutoSessionPluginPath ++ [ "enableLastSession" ]) ( + settingsPath + ++ [ + "auto_restore_last_session" + ] + )) + (lib.mkRenamedOptionModule (nestedAutoSessionPluginPath ++ [ "rootDir" ]) ( + settingsPath + ++ [ + "root_dir" + ] + )) + (lib.mkRenamedOptionModule (nestedAutoSessionPluginPath ++ [ "enabled" ]) ( + settingsPath + ++ [ + "enabled" + ] + )) + (lib.mkRenamedOptionModule (nestedAutoSessionPluginPath ++ [ "createEnabled" ]) ( + settingsPath + ++ [ + "auto_create" + ] + )) + (lib.mkRenamedOptionModule (nestedAutoSessionPluginPath ++ [ "suppressDirs" ]) ( + settingsPath + ++ [ + "suppressed_dirs" + ] + )) + (lib.mkRenamedOptionModule (nestedAutoSessionPluginPath ++ [ "allowedDirs" ]) ( + settingsPath + ++ [ + "allowed_dirs" + ] + )) + (lib.mkRenamedOptionModule (nestedAutoSessionPluginPath ++ [ "useGitBranch" ]) ( + settingsPath + ++ [ + "use_git_branch" + ] + )) + (lib.mkRemovedOptionModule ( + basePluginPath + ++ [ + "cwdChangeHandling" + ] + ) ''Please switch to `cwd_change_handling` with just a boolean value.'') + ]; + + settingsOptions = { + enabled = defaultNullOpts.mkBool true '' + Enables/disables auto creating, saving and restoring. + ''; - rootDir = lib.nixvim.defaultNullOpts.mkStr { __raw = "vim.fn.stdpath 'data' .. '/sessions/'"; } '' - Root directory for session files. - Can be either a string or lua code (using `{__raw = 'foo';}`). - ''; + root_dir = defaultNullOpts.mkStr { __raw = "vim.fn.stdpath 'data' .. '/sessions/'"; } '' + Root directory for session files. + Can be either a string or lua code (using `{__raw = 'foo';}`). + ''; - createEnabled = lib.nixvim.mkNullOrOption types.bool '' - Whether to enable auto creating new sessions - ''; + auto_save = defaultNullOpts.mkBool true '' + Whether to enable auto saving session. + ''; - suppressDirs = lib.nixvim.mkNullOrOption (with types; listOf str) '' - Suppress session create/restore if in one of the list of dirs. - ''; + auto_restore = defaultNullOpts.mkBool true '' + Whether to enable auto restoring session. + ''; - allowedDirs = lib.nixvim.mkNullOrOption (with types; listOf str) '' - Allow session create/restore if in one of the list of dirs. - ''; + auto_create = defaultNullOpts.mkBool true '' + Whether to enable auto creating new sessions + ''; - useGitBranch = lib.nixvim.mkNullOrOption types.bool '' - Include git branch name in session name to differentiate between sessions for different - git branches. - ''; - }; + suppressed_dirs = defaultNullOpts.mkListOf types.str null '' + Suppress session create/restore if in one of the list of dirs. + ''; - autoSave = { - enabled = lib.nixvim.defaultNullOpts.mkNullable types.bool null '' - Whether to enable auto saving session. - ''; - }; + allowed_dirs = defaultNullOpts.mkListOf types.str null '' + Allow session create/restore if in one of the list of dirs. + ''; - autoRestore = { - enabled = lib.nixvim.defaultNullOpts.mkNullable types.bool null '' - Whether to enable auto restoring session. - ''; - }; + auto_restore_last_session = defaultNullOpts.mkBool false '' + On startup, loads the last saved session if session for cwd does not exist. + ''; - cwdChangeHandling = - lib.nixvim.defaultNullOpts.mkNullable - ( - with types; - either (enum [ false ]) (submodule { - options = { - restoreUpcomingSession = lib.nixvim.defaultNullOpts.mkBool true '' - Restore session for upcoming cwd on cwd change. - ''; - - preCwdChangedHook = lib.nixvim.defaultNullOpts.mkLuaFn "nil" '' - lua function hook. - This is called after auto_session code runs for the `DirChangedPre` autocmd. - ''; - - postCwdChangedHook = lib.nixvim.defaultNullOpts.mkLuaFn "nil" '' - lua function hook. - This is called after auto_session code runs for the `DirChanged` autocmd. - ''; - }; - }) - ) - false - '' - Config for handling the DirChangePre and DirChanged autocmds. - Set to `false` to disable the feature. - ''; + use_git_branch = defaultNullOpts.mkBool false '' + Include git branch name in session name to differentiate between sessions for different + git branches. + ''; - bypassSessionSaveFileTypes = lib.nixvim.mkNullOrOption (with types; listOf str) '' + bypass_save_filetypes = defaultNullOpts.mkListOf types.str null '' List of file types to bypass auto save when the only buffer open is one of the file types listed. ''; - sessionLens = { - loadOnSetup = lib.nixvim.defaultNullOpts.mkBool true '' - If `loadOnSetup` is set to false, one needs to eventually call + cwd_change_handling = defaultNullOpts.mkBool false '' + Follow cwd changes, saving a session before change and restoring after. + ''; + + log_level = defaultNullOpts.mkEnum [ + "debug" + "info" + "warn" + "error" + ] "error" "Sets the log level of the plugin."; + + session_lens = { + load_on_setup = defaultNullOpts.mkBool true '' + If `load_on_setup` is set to false, one needs to eventually call `require("auto-session").setup_session_lens()` if they want to use session-lens. ''; - themeConf = lib.nixvim.defaultNullOpts.mkAttrsOf types.anything { + theme_conf = defaultNullOpts.mkAttrsOf types.anything { winblend = 10; border = true; } "Theme configuration."; - previewer = lib.nixvim.defaultNullOpts.mkBool false '' + previewer = defaultNullOpts.mkBool false '' Use default previewer config by setting the value to `null` if some sets previewer to true in the custom config. Passing in the boolean value errors out in the telescope code with the picker trying to @@ -123,61 +194,16 @@ in configs if they want to. ''; - sessionControl = { - controlDir = - lib.nixvim.defaultNullOpts.mkStr { __raw = "vim.fn.stdpath 'data' .. '/auto_session/'"; } - '' - Auto session control dir, for control files, like alternating between two sessions - with session-lens. - ''; + session_control = { + control_dir = defaultNullOpts.mkStr { __raw = "vim.fn.stdpath 'data' .. '/auto_session/'"; } '' + Auto session control dir, for control files, like alternating between two sessions + with session-lens. + ''; - controlFilename = lib.nixvim.defaultNullOpts.mkStr "session_control.json" '' + control_filename = defaultNullOpts.mkStr "session_control.json" '' File name of the session control file. ''; }; }; }; - - config = - let - setupOptions = { - log_level = cfg.logLevel; - auto_session_enable_last_session = cfg.autoSession.enableLastSession; - auto_session_root_dir = cfg.autoSession.rootDir; - auto_session_enabled = cfg.autoSession.enabled; - auto_session_create_enabled = cfg.autoSession.createEnabled; - auto_save_enabled = cfg.autoSave.enabled; - auto_restore_enabled = cfg.autoRestore.enabled; - auto_session_suppress_dirs = cfg.autoSession.suppressDirs; - auto_session_allowed_dirs = cfg.autoSession.allowedDirs; - auto_session_use_git_branch = cfg.autoSession.useGitBranch; - cwd_change_handling = - if lib.isAttrs cfg.cwdChangeHandling then - with cfg.cwdChangeHandling; - { - restore_upcoming_session = restoreUpcomingSession; - pre_cwd_changed_hook = preCwdChangedHook; - post_cwd_changed_hook = postCwdChangedHook; - } - else - cfg.cwdChangeHandling; - bypass_session_save_file_types = cfg.bypassSessionSaveFileTypes; - session_lens = with cfg.sessionLens; { - load_on_setup = loadOnSetup; - theme_conf = themeConf; - inherit previewer; - session_control = with sessionControl; { - control_dir = controlDir; - control_filename = controlFilename; - }; - }; - } // cfg.extraOptions; - in - lib.mkIf cfg.enable { - extraPlugins = [ cfg.package ]; - - extraConfigLua = '' - require('auto-session').setup(${lib.nixvim.toLuaObject setupOptions}) - ''; - }; } diff --git a/tests/test-sources/plugins/by-name/auto-session/default.nix b/tests/test-sources/plugins/by-name/auto-session/default.nix index 421302cba..a1c235231 100644 --- a/tests/test-sources/plugins/by-name/auto-session/default.nix +++ b/tests/test-sources/plugins/by-name/auto-session/default.nix @@ -3,44 +3,50 @@ plugins.auto-session.enable = true; }; - example = { + defaults = { plugins.auto-session = { enable = true; - logLevel = "error"; - autoSession = { + settings = { enabled = true; - enableLastSession = false; - rootDir = { - __raw = "vim.fn.stdpath 'data' .. '/sessions/'"; - }; - createEnabled = true; - suppressDirs = null; - allowedDirs = [ ]; - useGitBranch = true; - }; - autoSave = { - enabled = true; - }; - autoRestore = { - enabled = true; - }; - cwdChangeHandling = { - restoreUpcomingSession = true; - preCwdChangedHook = null; - postCwdChangedHook = null; - }; - bypassSessionSaveFileTypes = [ ]; - sessionLens = { - loadOnSetup = true; - themeConf = { - winblend = 10; - border = true; - }; - previewer = false; - sessionControl = { - controlDir = "vim.fn.stdpath 'data' .. '/auto_session/'"; - controlFilename = "session_control.json"; + root_dir.__raw = ''vim.fn.stdpath "data" .. "/sessions/"''; + auto_save = true; + auto_restore = true; + auto_create = true; + suppressed_dirs = null; + allowed_dirs = null; + auto_restore_last_session = false; + use_git_branch = false; + lazy_support = true; + bypass_save_filetypes = null; + close_unsupported_windows = true; + args_allow_single_directory = true; + args_allow_files_auto_save = false; + continue_restore_on_error = true; + cwd_change_handling = false; + log_level = "error"; + session_lens = { + load_on_setup = true; + theme_conf = { }; + previewer = false; + mappings = { + delete_session = { + __unkeyed-1 = "i"; + __unkeyed-2 = ""; + }; + alternate_session = [ + "i" + "" + ]; + copy_session = [ + "i" + "" + ]; + }; + session_control = { + control_dir.__raw = ''vim.fn.stdpath "data" .. "/auto_session/"''; + control_filename = "session_control.json"; + }; }; }; };