Skip to content

Commit 285ce7a

Browse files
committed
fix(file-history): Handle 'width' not being set in a column panel config
Problem: No way to infer dimensions for a panel if they're not configured. Solution: Add methods for inferring panel dimensions. (fixes #394)
1 parent b0cc22f commit 285ce7a

File tree

3 files changed

+59
-16
lines changed

3 files changed

+59
-16
lines changed

lua/diffview/scene/views/diff/render.lua

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -138,12 +138,7 @@ return function(panel)
138138

139139
panel.render_data:clear()
140140
local conf = config.get_config()
141-
local width = panel:get_width()
142-
143-
if width == -1 then
144-
local panel_config = panel:get_config()
145-
width = panel_config.width
146-
end
141+
local width = panel:infer_width()
147142

148143
---@type RenderComponent
149144
local comp = panel.components.path.comp

lua/diffview/scene/views/file_history/render.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ local function prepare_panel_cache(panel)
175175
c.root_path = panel.state.form == "column"
176176
and pl:truncate(
177177
pl:vim_fnamemodify(panel.adapter.ctx.toplevel, ":~"),
178-
panel:get_config().width - 6
178+
panel:infer_width() - 6
179179
)
180180
or pl:vim_fnamemodify(panel.adapter.ctx.toplevel, ":~")
181181
c.args = table.concat(panel.log_options.single_file.path_args, " ")

lua/diffview/ui/panel.lua

Lines changed: 57 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ local M = {}
1414
local uid_counter = 0
1515

1616
---@alias PanelConfig PanelFloatSpec|PanelSplitSpec
17-
---@alias PanelType '"split"'|'"float"'
17+
---@alias PanelType "split"|"float"
1818

1919
---@type PerfTimer
2020
local perf = PerfTimer("[Panel] redraw")
@@ -68,8 +68,8 @@ Panel.default_type = "split"
6868
---@field position "left"|"top"|"right"|"bottom"
6969
---@field relative "editor"|"win"
7070
---@field win integer
71-
---@field width integer
72-
---@field height integer
71+
---@field width? integer
72+
---@field height? integer
7373
---@field win_opts WindowOptions
7474

7575
---@type PanelSplitSpec
@@ -249,7 +249,7 @@ function Panel:resize()
249249
if config.type == "split" then
250250
if self.state.form == "column" and config.width then
251251
api.nvim_win_set_width(self.winid, config.width)
252-
elseif config.height then
252+
elseif self.state.form == "row" and config.height then
253253
api.nvim_win_set_height(self.winid, config.height)
254254
end
255255
elseif config.type == "float" then
@@ -487,22 +487,70 @@ function Panel:get_default_config(panel_type)
487487
return config
488488
end
489489

490-
---@return integer
490+
---@return integer?
491491
function Panel:get_width()
492492
if self:is_open() then
493493
return api.nvim_win_get_width(self.winid)
494494
end
495-
496-
return -1
497495
end
498496

499-
---@return integer
497+
---@return integer?
500498
function Panel:get_height()
501499
if self:is_open() then
502500
return api.nvim_win_get_height(self.winid)
503501
end
502+
end
503+
504+
function Panel:infer_width()
505+
local cur_width = self:get_width()
506+
if cur_width then return cur_width end
507+
508+
local config = self:get_config()
509+
if config.width then return config.width end
510+
511+
-- PanelFloatSpec requires both width and height to be defined. If we get
512+
-- here then the panel is a split.
513+
---@cast config PanelSplitSpec
514+
515+
if config.win and api.nvim_win_is_valid(config.win) then
516+
if self.state.form == "row" then
517+
return api.nvim_win_get_width(config.win)
518+
elseif self.state.form == "column" then
519+
return math.floor(api.nvim_win_get_width(config.win) / 2)
520+
end
521+
end
522+
523+
if self.state.form == "row" then
524+
return vim.o.columns
525+
end
526+
527+
return math.floor(vim.o.columns / 2)
528+
end
529+
530+
function Panel:infer_height()
531+
local cur_height = self:get_height()
532+
if cur_height then return cur_height end
533+
534+
local config = self:get_config()
535+
if config.height then return config.height end
536+
537+
-- PanelFloatSpec requires both width and height to be defined. If we get
538+
-- here then the panel is a split.
539+
---@cast config PanelSplitSpec
540+
541+
if config.win and api.nvim_win_is_valid(config.win) then
542+
if self.state.form == "row" then
543+
return math.floor(api.nvim_win_get_height(config.win) / 2)
544+
elseif self.state.form == "column" then
545+
return api.nvim_win_get_height(config.win)
546+
end
547+
end
548+
549+
if self.state.form == "row" then
550+
return math.floor(vim.o.lines / 2)
551+
end
504552

505-
return -1
553+
return vim.o.lines
506554
end
507555

508556
function Panel.next_uid()

0 commit comments

Comments
 (0)