@@ -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--- }
352353function 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
0 commit comments