Skip to content
This repository was archived by the owner on Feb 12, 2025. It is now read-only.

Commit d1063d4

Browse files
authored
Merge branch 'master' into feat/select/override_telescope_config
2 parents 43f325b + b36b69c commit d1063d4

File tree

7 files changed

+236
-78
lines changed

7 files changed

+236
-78
lines changed

README.md

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -128,21 +128,29 @@ require('dressing').setup({
128128

129129
-- These are passed to nvim_open_win
130130
anchor = "SW",
131-
relative = "cursor",
132-
row = 0,
133-
col = 0,
134131
border = "rounded",
132+
-- 'editor' and 'win' will default to being centered
133+
relative = "cursor",
135134

136135
-- These can be integers or a float between 0 and 1 (e.g. 0.4 for 40%)
137136
prefer_width = 40,
138-
max_width = nil,
139-
min_width = 20,
137+
width = nil,
138+
-- min_width and max_width can be a list of mixed types.
139+
-- min_width = {20, 0.2} means "the greater of 20 columns or 20% of total"
140+
max_width = { 140, 0.9 },
141+
min_width = { 20, 0.2 },
140142

141143
-- Window transparency (0-100)
142144
winblend = 10,
143145
-- Change default highlight groups (see :help winhl)
144146
winhighlight = "",
145147

148+
override = function(conf)
149+
-- This is the config that will be passed to nvim_open_win.
150+
-- Change values here to customize the layout
151+
return conf
152+
end,
153+
146154
-- see :help dressing_get_config
147155
get_config = nil,
148156
},
@@ -156,6 +164,8 @@ require('dressing').setup({
156164
-- Options for telescope selector
157165
telescope = {
158166
-- can be 'dropdown', 'cursor', or 'ivy'
167+
-- or you can use a configuration directly:
168+
-- theme = require('telescope.themes').get_ivy({...})
159169
theme = "dropdown",
160170
},
161171

@@ -191,23 +201,30 @@ require('dressing').setup({
191201
builtin = {
192202
-- These are passed to nvim_open_win
193203
anchor = "NW",
194-
relative = "cursor",
195-
row = 0,
196-
col = 0,
197204
border = "rounded",
205+
-- 'editor' and 'win' will default to being centered
206+
relative = "editor",
198207

199208
-- Window transparency (0-100)
200209
winblend = 10,
201210
-- Change default highlight groups (see :help winhl)
202211
winhighlight = "",
203212

204213
-- These can be integers or a float between 0 and 1 (e.g. 0.4 for 40%)
214+
-- the min_ and max_ options can be a list of mixed types.
215+
-- max_width = {140, 0.8} means "the lesser of 140 columns or 80% of total"
205216
width = nil,
206-
max_width = 0.8,
207-
min_width = 40,
217+
max_width = { 140, 0.8 },
218+
min_width = { 40, 0.2 },
208219
height = nil,
209220
max_height = 0.9,
210-
min_height = 10,
221+
min_height = { 10, 0.2 },
222+
223+
override = function(conf)
224+
-- This is the config that will be passed to nvim_open_win.
225+
-- Change values here to customize the layout
226+
return conf
227+
end,
211228
},
212229

213230
-- Used to override format_item. See :help dressing-format

doc/dressing.txt

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,29 @@ Configure dressing.nvim by calling the setup() function.
1818
1919
-- These are passed to nvim_open_win
2020
anchor = "SW",
21-
relative = "cursor",
22-
row = 0,
23-
col = 0,
2421
border = "rounded",
22+
-- 'editor' and 'win' will default to being centered
23+
relative = "cursor",
2524
2625
-- These can be integers or a float between 0 and 1 (e.g. 0.4 for 40%)
2726
prefer_width = 40,
28-
max_width = nil,
29-
min_width = 20,
27+
width = nil,
28+
-- min_width and max_width can be a list of mixed types.
29+
-- min_width = {20, 0.2} means "the greater of 20 columns or 20% of total"
30+
max_width = { 140, 0.9 },
31+
min_width = { 20, 0.2 },
3032
3133
-- Window transparency (0-100)
3234
winblend = 10,
3335
-- Change default highlight groups (see :help winhl)
3436
winhighlight = "",
3537
38+
override = function(conf)
39+
-- This is the config that will be passed to nvim_open_win.
40+
-- Change values here to customize the layout
41+
return conf
42+
end,
43+
3644
-- see :help dressing_get_config
3745
get_config = nil,
3846
},
@@ -46,6 +54,8 @@ Configure dressing.nvim by calling the setup() function.
4654
-- Options for telescope selector
4755
telescope = {
4856
-- can be 'dropdown', 'cursor', or 'ivy'
57+
-- or you can use a configuration directly:
58+
-- theme = require('telescope.themes').get_ivy({...})
4959
theme = "dropdown",
5060
},
5161
@@ -81,23 +91,30 @@ Configure dressing.nvim by calling the setup() function.
8191
builtin = {
8292
-- These are passed to nvim_open_win
8393
anchor = "NW",
84-
relative = "cursor",
85-
row = 0,
86-
col = 0,
8794
border = "rounded",
95+
-- 'editor' and 'win' will default to being centered
96+
relative = "editor",
8897
8998
-- Window transparency (0-100)
9099
winblend = 10,
91100
-- Change default highlight groups (see :help winhl)
92101
winhighlight = "",
93102
94103
-- These can be integers or a float between 0 and 1 (e.g. 0.4 for 40%)
104+
-- the min_ and max_ options can be a list of mixed types.
105+
-- max_width = {140, 0.8} means "the lesser of 140 columns or 80% of total"
95106
width = nil,
96-
max_width = 0.8,
97-
min_width = 40,
107+
max_width = { 140, 0.8 },
108+
min_width = { 40, 0.2 },
98109
height = nil,
99110
max_height = 0.9,
100-
min_height = 10,
111+
min_height = { 10, 0.2 },
112+
113+
override = function(conf)
114+
-- This is the config that will be passed to nvim_open_win.
115+
-- Change values here to customize the layout
116+
return conf
117+
end,
101118
},
102119
103120
-- Used to override format_item. See :help dressing-format

lua/dressing/config.lua

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,29 @@ local default_config = {
1111

1212
-- These are passed to nvim_open_win
1313
anchor = "SW",
14-
relative = "cursor",
15-
row = 0,
16-
col = 0,
1714
border = "rounded",
15+
-- 'editor' and 'win' will default to being centered
16+
relative = "cursor",
1817

1918
-- These can be integers or a float between 0 and 1 (e.g. 0.4 for 40%)
2019
prefer_width = 40,
21-
max_width = nil,
22-
min_width = 20,
20+
width = nil,
21+
-- min_width and max_width can be a list of mixed types.
22+
-- min_width = {20, 0.2} means "the greater of 20 columns or 20% of total"
23+
max_width = { 140, 0.9 },
24+
min_width = { 20, 0.2 },
2325

2426
-- Window transparency (0-100)
2527
winblend = 10,
2628
-- Change default highlight groups (see :help winhl)
2729
winhighlight = "",
2830

31+
override = function(conf)
32+
-- This is the config that will be passed to nvim_open_win.
33+
-- Change values here to customize the layout
34+
return conf
35+
end,
36+
2937
-- see :help dressing_get_config
3038
get_config = nil,
3139
},
@@ -39,6 +47,8 @@ local default_config = {
3947
-- Options for telescope selector
4048
telescope = {
4149
-- can be 'dropdown', 'cursor', or 'ivy'
50+
-- or you can use a configuration directly:
51+
-- theme = require('telescope.themes').get_ivy({...})
4252
theme = "dropdown",
4353
},
4454

@@ -74,23 +84,30 @@ local default_config = {
7484
builtin = {
7585
-- These are passed to nvim_open_win
7686
anchor = "NW",
77-
relative = "cursor",
78-
row = 0,
79-
col = 0,
8087
border = "rounded",
88+
-- 'editor' and 'win' will default to being centered
89+
relative = "editor",
8190

8291
-- Window transparency (0-100)
8392
winblend = 10,
8493
-- Change default highlight groups (see :help winhl)
8594
winhighlight = "",
8695

8796
-- These can be integers or a float between 0 and 1 (e.g. 0.4 for 40%)
97+
-- the min_ and max_ options can be a list of mixed types.
98+
-- max_width = {140, 0.8} means "the lesser of 140 columns or 80% of total"
8899
width = nil,
89-
max_width = 0.8,
90-
min_width = 40,
100+
max_width = { 140, 0.8 },
101+
min_width = { 40, 0.2 },
91102
height = nil,
92103
max_height = 0.9,
93-
min_height = 10,
104+
min_height = { 10, 0.2 },
105+
106+
override = function(conf)
107+
-- This is the config that will be passed to nvim_open_win.
108+
-- Change values here to customize the layout
109+
return conf
110+
end,
94111
},
95112

96113
-- Used to override format_item. See :help dressing-format
@@ -105,6 +122,19 @@ local M = vim.deepcopy(default_config)
105122

106123
M.update = function(opts)
107124
local newconf = vim.tbl_deep_extend("force", default_config, opts or {})
125+
126+
if
127+
newconf.input.row
128+
or newconf.input.col
129+
or newconf.select.builtin.row
130+
or newconf.select.builtin.col
131+
then
132+
vim.notify(
133+
"Deprecated: Dressing row and col are no longer used. Use the override to customize layout (:help dressing)",
134+
vim.log.levels.WARN
135+
)
136+
end
137+
108138
for k, v in pairs(newconf) do
109139
M[k] = v
110140
end

lua/dressing/input.lua

Lines changed: 70 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,74 @@ M.trigger_completion = function()
162162
end
163163
end
164164

165+
local function create_or_update_win(config, prompt, opts)
166+
local parent_win = 0
167+
local winopt
168+
local win_conf
169+
-- If the previous window is still open and valid, we're going to update it
170+
if context.winid and vim.api.nvim_win_is_valid(context.winid) then
171+
win_conf = vim.api.nvim_win_get_config(context.winid)
172+
parent_win = win_conf.win
173+
winopt = {
174+
relative = win_conf.relative,
175+
win = win_conf.win,
176+
}
177+
else
178+
winopt = {
179+
relative = config.relative,
180+
anchor = config.anchor,
181+
border = config.border,
182+
height = 1,
183+
style = "minimal",
184+
noautocmd = true,
185+
}
186+
end
187+
-- First calculate the desired base width of the modal
188+
local prefer_width = util.calculate_width(
189+
config.relative,
190+
config.prefer_width,
191+
config,
192+
parent_win
193+
)
194+
-- Then expand the width to fit the prompt and default value
195+
prefer_width = math.max(prefer_width, 4 + vim.api.nvim_strwidth(prompt))
196+
if opts.default then
197+
prefer_width = math.max(prefer_width, 2 + vim.api.nvim_strwidth(opts.default))
198+
end
199+
-- Then recalculate to clamp final value to min/max
200+
local width = util.calculate_width(config.relative, prefer_width, config, parent_win)
201+
winopt.row = util.calculate_row(config.relative, 1, parent_win)
202+
winopt.col = util.calculate_col(config.relative, width, parent_win)
203+
winopt.width = width
204+
205+
if win_conf and config.relative == "cursor" then
206+
-- If we're cursor-relative we should actually not adjust the row/col to
207+
-- prevent jumping. Also remove related args.
208+
if config.relative == "cursor" then
209+
winopt.row = nil
210+
winopt.col = nil
211+
winopt.relative = nil
212+
winopt.win = nil
213+
end
214+
end
215+
216+
winopt = config.override(winopt) or winopt
217+
218+
-- If the floating win was already open
219+
if win_conf then
220+
-- Make sure the previous on_confirm callback is called with nil
221+
vim.schedule(context.on_confirm)
222+
vim.api.nvim_win_set_config(context.winid, winopt)
223+
local start_in_insert = context.start_in_insert
224+
return context.winid, start_in_insert
225+
else
226+
local start_in_insert = string.sub(vim.api.nvim_get_mode().mode, 1, 1) == "i"
227+
local bufnr = vim.api.nvim_create_buf(false, true)
228+
local winid = vim.api.nvim_open_win(bufnr, true, winopt)
229+
return winid, start_in_insert
230+
end
231+
end
232+
165233
setmetatable(M, {
166234
-- use schedule_wrap to avoid a bug when vim opens
167235
-- (see https://github.com/stevearc/dressing.nvim/issues/15)
@@ -178,40 +246,7 @@ setmetatable(M, {
178246
-- Create or update the window
179247
local prompt = opts.prompt or config.default_prompt
180248

181-
-- First calculate the desired base width of the modal
182-
local prefer_width = util.calculate_width(config.prefer_width, config)
183-
-- Then expand the width to fit the prompt and default value
184-
prefer_width = math.max(prefer_width, 4 + vim.api.nvim_strwidth(prompt))
185-
if opts.default then
186-
prefer_width = math.max(prefer_width, 2 + vim.api.nvim_strwidth(opts.default))
187-
end
188-
-- Then recalculate to clamp final value to min/max
189-
local width = util.calculate_width(prefer_width, config)
190-
local winopt = {
191-
relative = config.relative,
192-
anchor = config.anchor,
193-
row = config.row,
194-
col = config.col,
195-
border = config.border,
196-
width = width,
197-
height = 1,
198-
style = "minimal",
199-
noautocmd = true,
200-
}
201-
local winid, bufnr, start_in_insert
202-
-- If the input window is already open, hijack it
203-
if context.winid and vim.api.nvim_win_is_valid(context.winid) then
204-
winid = context.winid
205-
-- Make sure the previous on_confirm callback is called with nil
206-
vim.schedule(context.on_confirm)
207-
vim.api.nvim_win_set_width(winid, width)
208-
bufnr = vim.api.nvim_win_get_buf(winid)
209-
start_in_insert = context.start_in_insert
210-
else
211-
start_in_insert = string.sub(vim.api.nvim_get_mode().mode, 1, 1) == "i"
212-
bufnr = vim.api.nvim_create_buf(false, true)
213-
winid = vim.api.nvim_open_win(bufnr, true, winopt)
214-
end
249+
local winid, start_in_insert = create_or_update_win(config, prompt, opts)
215250
context = {
216251
winid = winid,
217252
on_confirm = on_confirm,
@@ -221,6 +256,7 @@ setmetatable(M, {
221256
}
222257
vim.api.nvim_win_set_option(winid, "winblend", config.winblend)
223258
vim.api.nvim_win_set_option(winid, "winhighlight", config.winhighlight)
259+
local bufnr = vim.api.nvim_win_get_buf(winid)
224260

225261
-- Finish setting up the buffer
226262
vim.api.nvim_buf_set_option(bufnr, "swapfile", false)

0 commit comments

Comments
 (0)