Skip to content

Commit

Permalink
refactor(fzf): move fzf win configs to config functions
Browse files Browse the repository at this point in the history
  • Loading branch information
willothy committed Jan 25, 2024
1 parent a397fd7 commit 38f222f
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 38 deletions.
51 changes: 43 additions & 8 deletions lua/dropbar/configs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,49 @@ M.opts = {
},
},
fzf = {
win_configs = {
relative = 'win',
anchor = 'NW',
height = 1,
win = function(menu)
return menu.win
end,
row = function(menu)
local menu_border = menu._win_configs.border
if
type(menu_border) == 'string'
and menu_border ~= 'shadow'
and menu_border ~= 'none'
then
return menu._win_configs.height + 1
end
local len_menu_border = #menu_border
if
len_menu_border == 1 and menu_border[1] ~= ''
or (len_menu_border == 2 or len_menu_border == 4) and menu_border[2] ~= ''
or len_menu_border == 8 and menu_border[8] ~= ''
then
return menu._win_configs.height + 1
else
return menu._win_configs.height
end
end,
col = function(menu)
local menu_border = menu._win_configs.border
if
type(menu_border) == 'string'
and menu_border ~= 'shadow'
and menu_border ~= 'none'
then
return -1
end
if menu_border[#menu_border] ~= '' then
return -1
else
return 0
end
end,
},
---@type table<string, string | fun()>
keymaps = {
['<LeftMouse>'] = function()
Expand Down Expand Up @@ -409,14 +452,6 @@ M.opts = {
api.fuzzy_find_click(-1)
end,
},
win_configs = {
win = function(self)
return self.win
end,
relative = 'win',
anchor = 'NW',
height = 1,
},
prompt = '%#htmlTag# ',
char_pattern = '[%w%p]',
retain_inner_spaces = true,
Expand Down
34 changes: 4 additions & 30 deletions lua/dropbar/menu.lua
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ end
---@field win integer?
---@field is_opened boolean?
---@field entries dropbar_menu_entry_t[]
---@field win_configs table | fun(self: dropbar_menu_t): table window configuration, value can be a function
---@field win_configs table window configuration, value can be a function
---@field _win_configs table evaluated window configuration
---@field cursor integer[]? initial cursor position
---@field prev_win integer? previous window, assigned when calling new() or automatically determined in open()
Expand All @@ -189,7 +189,7 @@ end
---@field prev_cursor integer[]? previous cursor position
---@field symbol_previewed dropbar_symbol_t? symbol being previewed
---@field fzf_state fzf_state_t? fuzzy-finding state, or nil if not currently fuzzy-finding
---@field fzf_win_configs table | fun(self: dropbar_menu_t): table window configuration, value can be a function
---@field fzf_win_configs table window configuration, value can be a function
---@field scrollbar { thumb: integer, background: integer }? scrollbar window handlers
local dropbar_menu_t = {}
dropbar_menu_t.__index = dropbar_menu_t
Expand Down Expand Up @@ -904,12 +904,9 @@ function dropbar_menu_t:merge_win_configs(...)
for i = 1, select('#', ...) do
local chunk = select(i, ...)
if chunk then
if type(chunk) == 'function' then
chunk = chunk(self)
end
for k, v in pairs(chunk) do
if type(v) == 'function' then
merged[k] = v(self)
merged[k] = v(self) or merged[k]
else
merged[k] = v
end
Expand Down Expand Up @@ -955,33 +952,10 @@ function dropbar_menu_t:fuzzy_find_open(opts)
vim.bo[buf].filetype = 'dropbar_menu_fzf'
vim.bo[buf].bufhidden = 'wipe'

-- check if menu has left or bottom border to adjust fzf window's
-- col/row option to align with menu window
local menu_border = self._win_configs.border
local menu_has_left_border = false
local menu_has_bottom_border = false
if type(menu_border) == 'string' then
if menu_border ~= 'shadow' and menu_border ~= 'none' then
menu_has_left_border = true
menu_has_bottom_border = true
end
else -- border is non-empty (guaranteed by nvim api) array
local len_menu_border = #menu_border
menu_has_left_border = menu_border[len_menu_border] ~= ''
menu_has_bottom_border = len_menu_border == 1 and menu_border[1] ~= ''
or (len_menu_border == 2 or len_menu_border == 4) and menu_border[2] ~= ''
or len_menu_border == 8 and menu_border[8] ~= ''
end

local win_config = self:merge_win_configs(
self.win_configs,
self.fzf_win_configs,
opts.win_configs,
{
-- make sure that fzf window aligns well with menu window
col = menu_has_left_border and -1 or 0,
row = self._win_configs.height + (menu_has_bottom_border and 1 or 0),
}
opts.win_configs
)

-- don't show title in the fzf window
Expand Down

0 comments on commit 38f222f

Please sign in to comment.