From f64a1e9c3211abf86845e17db06afb1726834cfc Mon Sep 17 00:00:00 2001 From: eleith Date: Mon, 30 Nov 2026 12:28:21 -0800 Subject: [PATCH] Share card relayout between resize and reparent --- CHANGELOG.md | 1 + lua/scratch_comments/ui/frame.lua | 17 +++++++++-------- tests/features/window_spec.lua | 6 +++++- 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7619dea..057518a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,7 @@ ### Changed - Simplify quickfix setup and remove unused test helpers and redundant tests. +- Share card layout code between resizing and moving to another split. ## [0.1.0] - 2026-09-30 diff --git a/lua/scratch_comments/ui/frame.lua b/lua/scratch_comments/ui/frame.lua index 12e69dc..820a8d8 100644 --- a/lua/scratch_comments/ui/frame.lua +++ b/lua/scratch_comments/ui/frame.lua @@ -189,6 +189,13 @@ function M.open(frame, parent) return true end + local function relayout() + backdrop.resize(dim, parent) + local context_config, comment_config = placement(layout()) + vim.api.nvim_win_set_config(context_win, context_config) + vim.api.nvim_win_set_config(comment_win, comment_config) + end + local function watch_parent() vim.api.nvim_create_autocmd("WinClosed", { group = group, @@ -235,10 +242,7 @@ function M.open(frame, parent) end if replacement then parent = replacement - backdrop.resize(dim, parent) - local context_next, comment_next = placement(layout()) - vim.api.nvim_win_set_config(context_win, context_next) - vim.api.nvim_win_set_config(comment_win, comment_next) + relayout() watch_parent() end end) @@ -260,10 +264,7 @@ function M.open(frame, parent) if not vim.api.nvim_win_is_valid(parent) then return end - backdrop.resize(dim, parent) - local context_resized, comment_resized = placement(layout()) - vim.api.nvim_win_set_config(context_win, context_resized) - vim.api.nvim_win_set_config(comment_win, comment_resized) + relayout() end, }) diff --git a/tests/features/window_spec.lua b/tests/features/window_spec.lua index 6ca3987..def634b 100644 --- a/tests/features/window_spec.lua +++ b/tests/features/window_spec.lua @@ -139,7 +139,8 @@ describe("the comment window", function() expect.equality(config.width <= vim.api.nvim_win_get_width(record.source_win), true) end) - it("keeps unsaved edits when the source window closes", function() + it("moves the whole card and keeps unsaved edits when the source window closes", function() + local floats = helpers.every_float() local record = assert(require("scratch_comments.ui.window").current()) ---@cast record { comment_win: integer, source_win: integer } local comment_buf = vim.api.nvim_win_get_buf(record.comment_win) @@ -153,6 +154,9 @@ describe("the comment window", function() local parent = assert(vim.api.nvim_win_get_config(record.comment_win).win) expect.equality(vim.api.nvim_win_is_valid(parent), true) expect.no_equality(vim.bo[vim.api.nvim_win_get_buf(parent)].buftype, "quickfix") + for _, win in ipairs(floats) do + expect.equality(vim.api.nvim_win_get_config(win).win, parent) + end end) it("closes a clean card if its source window was the last file pane", function() -- 2.54.1