feat: allow get_config to modify enabled (#29)

This commit is contained in:
Steven Arcangeli 2022-03-23 18:46:21 -07:00
parent 8c42b8f854
commit 31f12fff6e
4 changed files with 52 additions and 23 deletions

View File

@ -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

View File

@ -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

37
lua/dressing/patch.lua Normal file
View File

@ -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

View File

@ -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