Skip to content

Commit

Permalink
refactor!: expose generic way to set window/buffer options in config (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
stevearc committed Dec 5, 2022
1 parent 872cc4e commit c7eda5a
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 17 deletions.
39 changes: 31 additions & 8 deletions lua/dressing/config.lua
Expand Up @@ -29,10 +29,13 @@ local default_config = {
max_width = { 140, 0.9 },
min_width = { 20, 0.2 },

-- Window transparency (0-100)
winblend = 10,
-- Change default highlight groups (see :help winhl)
winhighlight = "",
buf_options = {},
win_options = {
-- Window transparency (0-100)
winblend = 10,
-- Disable line wrapping
wrap = false,
},

-- Set to `false` to disable
mappings = {
Expand Down Expand Up @@ -117,10 +120,11 @@ local default_config = {
-- 'editor' and 'win' will default to being centered
relative = "editor",

-- Window transparency (0-100)
winblend = 10,
-- Change default highlight groups (see :help winhl)
winhighlight = "",
buf_options = {},
win_options = {
-- Window transparency (0-100)
winblend = 10,
},

-- These can be integers or a float between 0 and 1 (e.g. 0.4 for 40%)
-- the min_ and max_ options can be a list of mixed types.
Expand Down Expand Up @@ -171,6 +175,25 @@ M.update = function(opts)
)
end

for _, ns in ipairs({ "input", "select.builtin" }) do
local subconf = ns == "input" and newconf.input or newconf.select.builtin
for _, key in ipairs({ "winblend", "winhighlight" }) do
if subconf[key] then
vim.notify_once(
string.format(
"Deprecated(dressing.%s.%s) has moved to dressing.%s.win_options.%s\nSupport will be removed on 2023-03-01",
ns,
key,
ns,
key
),
vim.log.levels.WARN
)
subconf.win_options[key] = subconf[key]
end
end
end

if
newconf.select.telescope
and newconf.select.telescope.theme
Expand Down
9 changes: 6 additions & 3 deletions lua/dressing/input.lua
Expand Up @@ -300,14 +300,17 @@ setmetatable(M, {
history_idx = nil,
start_in_insert = start_in_insert,
}
vim.api.nvim_win_set_option(winid, "winblend", config.winblend)
vim.api.nvim_win_set_option(winid, "winhighlight", config.winhighlight)
vim.api.nvim_win_set_option(winid, "wrap", false)
for option, value in pairs(config.win_options) do
vim.api.nvim_win_set_option(winid, option, value)
end
local bufnr = vim.api.nvim_win_get_buf(winid)

-- Finish setting up the buffer
vim.api.nvim_buf_set_option(bufnr, "swapfile", false)
vim.api.nvim_buf_set_option(bufnr, "bufhidden", "wipe")
for k, v in pairs(config.buf_options) do
vim.api.nvim_buf_set_option(bufnr, k, v)
end

map_util.create_plug_maps(bufnr, keymaps)
for mode, user_maps in pairs(config.mappings) do
Expand Down
16 changes: 10 additions & 6 deletions lua/dressing/select/builtin.lua
Expand Up @@ -42,6 +42,9 @@ M.select = function(config, items, opts, on_choice)
local bufnr = vim.api.nvim_create_buf(false, true)
vim.api.nvim_buf_set_option(bufnr, "swapfile", false)
vim.api.nvim_buf_set_option(bufnr, "bufhidden", "wipe")
for k, v in pairs(config.buf_options) do
vim.api.nvim_buf_set_option(bufnr, k, v)
end
local lines = {}
local max_width = 1
for _, item in ipairs(items) do
Expand All @@ -67,13 +70,14 @@ M.select = function(config, items, opts, on_choice)
style = "minimal",
}
winopt = config.override(winopt) or winopt
local winnr = vim.api.nvim_open_win(bufnr, true, winopt)
vim.api.nvim_win_set_option(winnr, "winblend", config.winblend)
vim.api.nvim_win_set_option(winnr, "winhighlight", config.winhighlight)
vim.api.nvim_win_set_option(winnr, "cursorline", true)
pcall(vim.api.nvim_win_set_option, winnr, "cursorlineopt", "both")
local winid = vim.api.nvim_open_win(bufnr, true, winopt)
vim.api.nvim_win_set_option(winid, "cursorline", true)
pcall(vim.api.nvim_win_set_option, winid, "cursorlineopt", "both")
for option, value in pairs(config.win_options) do
vim.api.nvim_win_set_option(winid, option, value)
end
vim.api.nvim_buf_set_option(bufnr, "filetype", "DressingSelect")
util.add_title_to_win(winnr, opts.prompt)
util.add_title_to_win(winid, opts.prompt)

map_util.create_plug_maps(bufnr, keymaps)
map_util.create_maps_to_plug(bufnr, "n", config.mappings, "DressingSelect:")
Expand Down

0 comments on commit c7eda5a

Please sign in to comment.