Skip to content

Commit b43c3b3

Browse files
committed
fix on_change when text changes back to original
1 parent 0553f8e commit b43c3b3

File tree

2 files changed

+54
-2
lines changed

2 files changed

+54
-2
lines changed

lua/morph.lua

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ local H = {}
7474
--- @field attributes morph.TagAttributes
7575
--- @field children morph.Tree
7676
--- @field private ctx? morph.Ctx
77+
--- @field private curr_text? string
7778

7879
--- An element is an instantiated Tag
7980
--- @class morph.Element : morph.Tag
@@ -350,6 +351,9 @@ Morph.__index = Morph
350351
--- on_tag?: fun(tag: morph.Tag, start0: morph.Pos00, stop0: morph.Pos00): any
351352
--- }
352353
function Morph.markup_to_lines(opts)
354+
--- @type (fun(s: string))[]
355+
local listeners = {}
356+
353357
--- @type string[]
354358
local lines = {}
355359

@@ -359,11 +363,17 @@ function Morph.markup_to_lines(opts)
359363
local function put(s)
360364
lines[curr_line1] = (lines[curr_line1] or '') .. s
361365
curr_col1 = #lines[curr_line1] + 1
366+
for _, listener in ipairs(listeners) do
367+
listener(s)
368+
end
362369
end
363370
local function put_line()
364371
table.insert(lines, '')
365372
curr_line1 = curr_line1 + 1
366373
curr_col1 = 1
374+
for _, listener in ipairs(listeners) do
375+
listener '\n'
376+
end
367377
end
368378

369379
--- @param node morph.Tree
@@ -382,9 +392,13 @@ function Morph.markup_to_lines(opts)
382392
end
383393
end,
384394
tag = function(t)
395+
local curr_text = ''
396+
listeners[#listeners + 1] = function(s) curr_text = curr_text .. s end
385397
local start0 = Pos00.new(curr_line1 - 1, curr_col1 - 1)
386398
visit(t.children)
387399
local stop0 = Pos00.new(curr_line1 - 1, curr_col1 - 1)
400+
table.remove(listeners, #listeners)
401+
t.curr_text = curr_text
388402

389403
if opts.on_tag then opts.on_tag(t, start0, stop0) end
390404
end,
@@ -917,10 +931,10 @@ function Morph:_on_text_changed()
917931
-- Just because extmarks have shifted, doesn't mean their text has changed:
918932
-- Lookup the old value vs the new on and check:
919933
local cached_tag = assert(self.text_content.curr.extmark_ids_to_tag[cached_extmark.id])
920-
local cached_tag_text = Morph.markup_to_string { tree = cached_tag }
921934
local curr_text = live_extmark:_text()
922935

923-
if cached_tag_text ~= curr_text then
936+
if cached_tag.curr_text ~= curr_text then
937+
cached_tag.curr_text = curr_text
924938
table.insert(changed, { extmark = live_extmark, text = curr_text })
925939
end
926940
end

spec/morph_spec.lua

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1235,4 +1235,42 @@ describe('Morph', function()
12351235
assert.are.same(captured_changed_text, '')
12361236
end)
12371237
end)
1238+
1239+
it('should recognize a text-change when text changes back to original content', function()
1240+
with_buf({}, function()
1241+
local captured_changed_text = ''
1242+
1243+
-- Text:
1244+
-- 01234
1245+
-- hello
1246+
--- @param ctx morph.Ctx
1247+
local function App(ctx)
1248+
return {
1249+
h('text', {
1250+
id = 'the-id',
1251+
on_change = function(e)
1252+
e.bubble_up = false
1253+
captured_changed_text = e.text
1254+
end,
1255+
}, { 'hello' }),
1256+
}
1257+
end
1258+
local r = Morph.new()
1259+
r:mount(h(App))
1260+
1261+
assert.are.same(get_text(), 'hello')
1262+
1263+
-- Delete the trailing 'o':
1264+
vim.api.nvim_buf_set_text(0, 0, 4, 0, 5, {})
1265+
assert.are.same(get_text(), 'hell')
1266+
r:_on_text_changed()
1267+
assert.are.same(captured_changed_text, 'hell')
1268+
1269+
-- Reinsert the trailing 'o':
1270+
vim.api.nvim_buf_set_text(0, 0, 4, 0, 4, { 'o' })
1271+
assert.are.same(get_text(), 'hello')
1272+
r:_on_text_changed()
1273+
assert.are.same(captured_changed_text, 'hello')
1274+
end)
1275+
end)
12381276
end)

0 commit comments

Comments
 (0)