Skip to content

Commit dc63760

Browse files
committed
fix bug: lines in buffer can only ever be { "" } at minimum
1 parent 3a434fa commit dc63760

2 files changed

Lines changed: 32 additions & 0 deletions

File tree

lua/morph.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -963,6 +963,10 @@ function Morph:render(tree)
963963
end,
964964
}
965965

966+
-- Edge case: empty trees produce empty lines array, but buffers always have
967+
-- at least one line. Set curr.lines to reflect reality, not the empty tree.
968+
if #lines == 0 then lines = { '' } end
969+
966970
-- Update buffer text with minimal edits
967971
--- @diagnostic disable-next-line: assign-type-mismatch
968972
self.text_content.old = self.text_content.curr

spec/morph_spec.lua

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3437,4 +3437,32 @@ describe('Morph', function()
34373437
end)
34383438
end)
34393439
end)
3440+
3441+
----------------------------------------------------------------------------
3442+
-- REGRESSION TEST: Rendering empty tree should not cause blank lines
3443+
--
3444+
-- Bug: When rendering an empty tree ({}) between non-empty renders,
3445+
-- the next render would have old.lines = {} (empty array) instead of
3446+
-- {""}, causing patch_lines to add an extra blank line.
3447+
----------------------------------------------------------------------------
3448+
3449+
it('should not add blank lines when rendering empty tree between content', function()
3450+
with_buf({}, function()
3451+
local r = Morph.new(0)
3452+
3453+
-- First render: non-empty content
3454+
r:render { 'First' }
3455+
assert.are.same({ 'First' }, get_lines())
3456+
3457+
-- Second render: empty tree (may or may not clear buffer)
3458+
r:render {}
3459+
3460+
-- Third render: new non-empty content
3461+
-- The key assertion: no blank line should appear after the content
3462+
r:render { 'Second' }
3463+
local lines = get_lines()
3464+
assert.are.same(1, #lines, 'Buffer should have exactly 1 line, no blank lines')
3465+
assert.are.same('Second', lines[1])
3466+
end)
3467+
end)
34403468
end)

0 commit comments

Comments
 (0)