From bea3b4373358d40b5099071fd8573675e6851288 Mon Sep 17 00:00:00 2001 From: Ching Pei Yang Date: Fri, 19 Apr 2024 21:11:22 +0200 Subject: [PATCH 1/4] fix: renamed option as setupOpts --- modules/plugins/filetree/nvimtree/nvimtree.nix | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/modules/plugins/filetree/nvimtree/nvimtree.nix b/modules/plugins/filetree/nvimtree/nvimtree.nix index fb9a9d0..8ccd735 100644 --- a/modules/plugins/filetree/nvimtree/nvimtree.nix +++ b/modules/plugins/filetree/nvimtree/nvimtree.nix @@ -34,12 +34,6 @@ in { }; }; - disableNetrw = mkOption { - default = false; - description = "Disables netrw and replaces it with tree"; - type = bool; - }; - setupOpts = mkPluginSetupOption "Nvim Tree" { hijack_netrw = mkOption { default = true; @@ -47,6 +41,12 @@ in { type = bool; }; + disable_netrw = mkOption { + default = false; + description = "Disables netrw and replaces it with tree"; + type = bool; + }; + auto_reload_on_write = mkOption { default = true; description = "Auto reload tree on write"; From e710afd1ac19c69e1eacd7be7c561c32ce22a80a Mon Sep 17 00:00:00 2001 From: Ching Pei Yang Date: Fri, 19 Apr 2024 21:14:41 +0200 Subject: [PATCH 2/4] fix: add rename warnings for nvimtree.setupOpts --- .../plugins/filetree/nvimtree/nvimtree.nix | 76 +++++++++++++++++++ 1 file changed, 76 insertions(+) diff --git a/modules/plugins/filetree/nvimtree/nvimtree.nix b/modules/plugins/filetree/nvimtree/nvimtree.nix index 8ccd735..b962dfe 100644 --- a/modules/plugins/filetree/nvimtree/nvimtree.nix +++ b/modules/plugins/filetree/nvimtree/nvimtree.nix @@ -3,11 +3,87 @@ lib, ... }: let + inherit (lib.modules) mkRenamedOptionModule; inherit (lib.options) mkEnableOption mkOption literalExpression; inherit (lib.generators) mkLuaInline; inherit (lib.types) nullOr str bool int submodule listOf enum oneOf attrs addCheck; inherit (lib.nvim.types) mkPluginSetupOption; + inherit (lib.lists) flatten; + inherit (lib.attrsets) mapAttrsToList; + + migrationTable = { + disableNetrw = "disable_netrw"; + hijackNetrw = "hijack_netrw"; + autoreloadOnWrite = "autoreload_on_write"; + updateFocusedFile = "update_focused_file"; + sort = { + sorter = "sorter"; + foldersFirst = "folders_first"; + }; + hijackCursor = "hijack_cursor"; + hijackUnnamedBufferWhenOpening = "hijack_unnamed_buffer_when_opening"; + rootDirs = "root_dirs"; + preferStartupRoot = "prefer_startup_root"; + syncRootWithCwd = "sync_root_with_cwd"; + reloadOnBufEnter = "reload_on_buf_enter"; + respectBufCwd = "respect_buf_cwd"; + hijackDirectories = "hijack_directories"; + systemOpen = { + args = "args"; + cmd = "cmd"; + }; + diagnostics = "diagnostics"; + git = { + enable = "enable"; + showOnDirs = "show_on_dirs"; + showOnOpenDirs = "show_on_open_dirs"; + disableForDirs = "disable_for_dirs"; + timeout = "timeout"; + }; + modified = "modified"; + filesystemWatchers = "filesystem_watchers"; + selectPrompts = "select_prompts"; + view = "view"; + renderer = { + addTrailing = "add_trailing"; + groupEmpty = "group_empty"; + fullName = "full_name"; + highlightGit = "highlight_git"; + highlightOpenedFiles = "highlight_opened_files"; + highlightModified = "highlight_modified"; + rootFolderLabel = "root_folder_label"; + indentWidth = "indent_width"; + indentMarkers = "indent_markers"; + specialFiles = "special_files"; + symlinkDestination = "symlink_destination"; + icons = "icons"; + }; + filters = "filters"; + trash = "trash"; + actions = "actions"; + liveFilter = "live_filter"; + tab = "tab"; + notify = "notify"; + ui = "ui"; + }; + + renamedSetupOpts = flatten (genSetupOptRenames [] migrationTable); + + # Note: I cut a few corners so it only works in this specific case + # if the parent of a nested option needs to be renamed, this would not work + genSetupOptRenames = path: table: + mapAttrsToList ( + oldName: newNameOrAttr: + if builtins.isAttrs newNameOrAttr + then genSetupOptRenames (path ++ [oldName]) newNameOrAttr + else + mkRenamedOptionModule + (["vim" "filetree" "nvimTree"] ++ path ++ [oldName]) + (["vim" "filetree" "nvimTree" "setupOpts"] ++ path ++ [newNameOrAttr]) + ) + table; in { + imports = renamedSetupOpts; options.vim.filetree.nvimTree = { enable = mkEnableOption "filetree via nvim-tree.lua"; From b38886d25d3bdc105d8f91dcce2bc8f920bb15c0 Mon Sep 17 00:00:00 2001 From: Ching Pei Yang Date: Sat, 20 Apr 2024 14:59:46 +0200 Subject: [PATCH 3/4] refactor: extract function to lib --- lib/config.nix | 55 +++++++++++++++++++ .../plugins/filetree/nvimtree/nvimtree.nix | 24 ++------ 2 files changed, 61 insertions(+), 18 deletions(-) diff --git a/lib/config.nix b/lib/config.nix index a2b8970..c7430b8 100644 --- a/lib/config.nix +++ b/lib/config.nix @@ -1,6 +1,9 @@ {lib}: let inherit (lib.options) mkOption; inherit (lib.types) bool; + inherit (lib.modules) mkRenamedOptionModule; + inherit (lib.attrsets) mapAttrsToList; + inherit (lib.lists) flatten; in { mkBool = value: description: mkOption { @@ -8,4 +11,56 @@ in { default = value; inherit description; }; + + /* + Generates a list of mkRenamedOptionModule, from a mapping of the old name to + the new name. Nested options can optionally supply a "_name" to indicate its + new name. + + # Example + + ```nix + batchRenameOptions ["nvimTree"] ["nvimTree" "setupOpts"] { + disableNetrw = "disable_netrw"; + nestedOption = { + _name = "nested_option"; + somethingElse = "something_else"; + }; + } + ``` + + The above code is equivalent to this: + + ```nix + [ + ( + mkRenamedOptionModule + ["nvimTree" "disableNetrw"] + ["nvimTree" "setupOpts" "disable_netrw"] + ) + ( + mkRenamedOptionModule + ["nvimTree" "nestedOption" "somethingElse"] + ["nvimTree" "setupOpts" "nested_option" "something_else"] + ) + ] + ``` + */ + batchRenameOptions = oldBasePath: newBasePath: mappings: let + genSetupOptRenames = oldSubpath: newSubpath: table: + mapAttrsToList ( + oldName: newNameOrAttr: + if builtins.isAttrs newNameOrAttr + then + genSetupOptRenames (oldSubpath ++ [oldName]) (newSubpath + ++ [newNameOrAttr._name or oldName]) + newNameOrAttr + else + mkRenamedOptionModule + (oldBasePath ++ oldSubpath ++ [oldName]) + (newBasePath ++ newSubpath ++ [newNameOrAttr]) + ) + table; + in + flatten (genSetupOptRenames [] [] mappings); } diff --git a/modules/plugins/filetree/nvimtree/nvimtree.nix b/modules/plugins/filetree/nvimtree/nvimtree.nix index b962dfe..9c443a6 100644 --- a/modules/plugins/filetree/nvimtree/nvimtree.nix +++ b/modules/plugins/filetree/nvimtree/nvimtree.nix @@ -3,13 +3,11 @@ lib, ... }: let - inherit (lib.modules) mkRenamedOptionModule; inherit (lib.options) mkEnableOption mkOption literalExpression; inherit (lib.generators) mkLuaInline; inherit (lib.types) nullOr str bool int submodule listOf enum oneOf attrs addCheck; inherit (lib.nvim.types) mkPluginSetupOption; - inherit (lib.lists) flatten; - inherit (lib.attrsets) mapAttrsToList; + inherit (lib.nvim.config) batchRenameOptions; migrationTable = { disableNetrw = "disable_netrw"; @@ -67,21 +65,11 @@ ui = "ui"; }; - renamedSetupOpts = flatten (genSetupOptRenames [] migrationTable); - - # Note: I cut a few corners so it only works in this specific case - # if the parent of a nested option needs to be renamed, this would not work - genSetupOptRenames = path: table: - mapAttrsToList ( - oldName: newNameOrAttr: - if builtins.isAttrs newNameOrAttr - then genSetupOptRenames (path ++ [oldName]) newNameOrAttr - else - mkRenamedOptionModule - (["vim" "filetree" "nvimTree"] ++ path ++ [oldName]) - (["vim" "filetree" "nvimTree" "setupOpts"] ++ path ++ [newNameOrAttr]) - ) - table; + renamedSetupOpts = + batchRenameOptions + ["vim" "filetree" "nvimTree"] + ["vim" "filetree" "nvimTree" "setupOpts"] + migrationTable; in { imports = renamedSetupOpts; options.vim.filetree.nvimTree = { From f5270d30a9830e40b48ca1e8b40c5532a30b0656 Mon Sep 17 00:00:00 2001 From: Ching Pei Yang Date: Sat, 20 Apr 2024 15:15:31 +0200 Subject: [PATCH 4/4] fixup! refactor: extract function to lib --- lib/config.nix | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/lib/config.nix b/lib/config.nix index c7430b8..40d8157 100644 --- a/lib/config.nix +++ b/lib/config.nix @@ -49,16 +49,15 @@ in { batchRenameOptions = oldBasePath: newBasePath: mappings: let genSetupOptRenames = oldSubpath: newSubpath: table: mapAttrsToList ( - oldName: newNameOrAttr: - if builtins.isAttrs newNameOrAttr + oldName: newNameOrNestedOpts: + if builtins.isAttrs newNameOrNestedOpts then - genSetupOptRenames (oldSubpath ++ [oldName]) (newSubpath - ++ [newNameOrAttr._name or oldName]) - newNameOrAttr + genSetupOptRenames (oldSubpath ++ [oldName]) (newSubpath ++ [newNameOrNestedOpts._name or oldName]) + (builtins.removeAttrs newNameOrNestedOpts ["_name"]) else mkRenamedOptionModule (oldBasePath ++ oldSubpath ++ [oldName]) - (newBasePath ++ newSubpath ++ [newNameOrAttr]) + (newBasePath ++ newSubpath ++ [newNameOrNestedOpts]) ) table; in