Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions lua/code-preview/diff.lua
Original file line number Diff line number Diff line change
Expand Up @@ -127,15 +127,35 @@ local function active_count()
return n
end

local VALID_LAYOUTS = { tab = true, vsplit = true, inline = true }

-- Values we've already warned about, so a misconfigured layout warns once
-- rather than on every diff (log.warn surfaces via vim.notify).
local warned_layouts = {}

-- Validate a resolved layout. An unknown value (a typo like "vspllit", or a
-- layout removed in a future version) otherwise falls through to the "tabnew"
-- branch silently, making the config look ignored. Warn once and fall back to
-- the supported default.
local function valid_layout(layout)
if VALID_LAYOUTS[layout] then return layout end
if layout and not warned_layouts[layout] then
warned_layouts[layout] = true
log.warn(log.fmt(
'unknown diff layout %q; falling back to "tab" (valid: tab, vsplit, inline)', layout))
end
return "tab"
end

local function layout_for_backend(cfg, backend)
local diff_cfg = (cfg and cfg.diff) or {}
local layouts = diff_cfg.layouts or {}

if backend and layouts[backend] then
return layouts[backend]
return valid_layout(layouts[backend])
end

return diff_cfg.layout or "tab"
return valid_layout(diff_cfg.layout or "tab")
end

function M.is_open(file_path)
Expand Down
40 changes: 40 additions & 0 deletions tests/plugin/diff_lifecycle_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -333,4 +333,44 @@ describe("diff layouts", function()
os.remove(orig)
os.remove(prop)
end)

it("an unknown layout value falls back to the default tab layout", function()
local orig = tmp_file("bad_layout_orig.txt", "alpha\nbeta")
local prop = tmp_file("bad_layout_prop.txt", "alpha\ngamma")

local tabs_before = #vim.api.nvim_list_tabpages()

-- A typo'd layout must not silently behave like vsplit/inline; it warns
-- (once) and falls back to a new tab.
with_layout("vspllit", function()
diff.show_diff(orig, prop, "layout_bad_value.txt")
end)

assert.is_true(diff.is_open())
assert.equals(tabs_before + 1, #vim.api.nvim_list_tabpages())
local diff_tabpage = vim.api.nvim_get_current_tabpage()
assert.equals(2, #vim.api.nvim_tabpage_list_wins(diff_tabpage))

diff.close_diff()
os.remove(orig)
os.remove(prop)
end)

it("an unknown per-backend layout override falls back to tab", function()
local orig = tmp_file("bad_backend_orig.txt", "alpha\nbeta")
local prop = tmp_file("bad_backend_prop.txt", "alpha\ngamma")

local tabs_before = #vim.api.nvim_list_tabpages()

with_layout("tab", function()
diff.show_diff(orig, prop, "layout_bad_backend.txt", nil, nil, "codex")
end, { codex = "vspllit" })

assert.is_true(diff.is_open())
assert.equals(tabs_before + 1, #vim.api.nvim_list_tabpages())

diff.close_diff()
os.remove(orig)
os.remove(prop)
end)
end)
Loading