From 31f12fff6e71a14ddce30bfc7ec9b29a2137ccde Mon Sep 17 00:00:00 2001 From: Steven Arcangeli Date: Wed, 23 Mar 2022 18:46:21 -0700 Subject: [PATCH] feat: allow get_config to modify enabled (#29) --- lua/dressing/init.lua | 28 +++++---------------------- lua/dressing/input.lua | 4 ++++ lua/dressing/patch.lua | 37 ++++++++++++++++++++++++++++++++++++ lua/dressing/select/init.lua | 6 ++++++ 4 files changed, 52 insertions(+), 23 deletions(-) create mode 100644 lua/dressing/patch.lua diff --git a/lua/dressing/init.lua b/lua/dressing/init.lua index db467e9..fd8d5f7 100644 --- a/lua/dressing/init.lua +++ b/lua/dressing/init.lua @@ -1,43 +1,25 @@ local config = require("dressing.config") +local patch = require("dressing.patch") local M = {} -local all_modules = { "input", "select" } -local original_mods = {} M.setup = function(opts) config.update(opts) - for _, name in ipairs(all_modules) do - if not config[name].enabled then - M.unpatch(name) - end - end + patch.all() end M.patch = function() - -- For Neovim before 0.6 - if not vim.ui then - vim.ui = {} - end - - for _, name in ipairs(all_modules) do - if config[name].enabled and original_mods[name] == nil then - original_mods[name] = vim.ui[name] - vim.ui[name] = require(string.format("dressing.%s", name)) - end - end + patch.all() end M.unpatch = function(names) if not names then - names = all_modules + return patch.all(false) elseif type(names) ~= "table" then names = { names } end for _, name in ipairs(names) do - local mod = require(string.format("dressing.%s", name)) - if vim.ui[name] == mod then - vim.ui[name] = original_mods[name] - end + patch.mod(name, false) end end diff --git a/lua/dressing/input.lua b/lua/dressing/input.lua index 4d25d98..b96cbfb 100644 --- a/lua/dressing/input.lua +++ b/lua/dressing/input.lua @@ -1,4 +1,5 @@ local global_config = require("dressing.config") +local patch = require("dressing.patch") local util = require("dressing.util") local M = {} @@ -242,6 +243,9 @@ setmetatable(M, { opts = { prompt = tostring(opts) } end local config = global_config.get_mod_config("input", opts) + if not config.enabled then + return patch.original_mods.input(opts, on_confirm) + end -- Create or update the window local prompt = opts.prompt or config.default_prompt diff --git a/lua/dressing/patch.lua b/lua/dressing/patch.lua new file mode 100644 index 0000000..c8e37e9 --- /dev/null +++ b/lua/dressing/patch.lua @@ -0,0 +1,37 @@ +local config = require("dressing.config") + +local all_modules = { "input", "select" } + +local M = {} + +-- For Neovim before 0.6 +if not vim.ui then + vim.ui = {} +end + +M.original_mods = {} + +M.all = function(enabled) + for _, name in ipairs(all_modules) do + M.mod(name, enabled) + end +end + +M.mod = function(name, enabled) + if enabled == nil then + enabled = config[name].enabled + end + if enabled then + if M.original_mods[name] == nil then + M.original_mods[name] = vim.ui[name] + end + vim.ui[name] = require(string.format("dressing.%s", name)) + else + local mod = require(string.format("dressing.%s", name)) + if vim.ui[name] == mod then + vim.ui[name] = M.original_mods[name] + end + end +end + +return M diff --git a/lua/dressing/select/init.lua b/lua/dressing/select/init.lua index 894cf3e..9cce94f 100644 --- a/lua/dressing/select/init.lua +++ b/lua/dressing/select/init.lua @@ -1,4 +1,5 @@ local global_config = require("dressing.config") +local patch = require("dressing.patch") local function get_backend(config) local backends = config.backend @@ -29,6 +30,11 @@ return vim.schedule_wrap(function(items, opts, on_choice) }) opts = opts or {} local config = global_config.get_mod_config("select", opts) + + if not config.enabled then + return patch.original_mods.input(items, opts, on_choice) + end + opts.prompt = opts.prompt or "Select one of:" local format_override = config.format_item_override[opts.kind] if format_override then