1919local concat , insert = table.concat , table.insert
2020local pos_to_line , get_closest_line , trim = util .pos_to_line , util .get_closest_line , util .trim
2121local mtype = util .moon .type
22- local insert_many
23- insert_many = function (tbl , ...)
24- local i = # tbl + 1
25- local _list_0 = {
26- ...
27- }
28- for _index_0 = 1 , # _list_0 do
29- local val = _list_0 [_index_0 ]
30- tbl [i ] = val
31- i = i + 1
32- end
33- end
3422local Line
3523Line = (function ()
3624 local _parent_0 = nil
@@ -65,28 +53,25 @@ Line = (function()
6553 end
6654 return nil
6755 end ,
68- render = function (self )
69- local parts = { }
56+ render = function (self , buffer )
7057 local current = { }
7158 local add_current
7259 add_current = function ()
73- return insert (parts , table. concat (current ))
60+ return insert (buffer , concat (current ))
7461 end
7562 local _list_0 = self
7663 for _index_0 = 1 , # _list_0 do
7764 local chunk = _list_0 [_index_0 ]
7865 local _exp_0 = mtype (chunk )
7966 if Block == _exp_0 then
80- local _list_1 = {
81- chunk :render ()
82- }
67+ local _list_1 = chunk :render ({ })
8368 for _index_1 = 1 , # _list_1 do
8469 local block_chunk = _list_1 [_index_1 ]
8570 if " string" == type (block_chunk ) then
8671 insert (current , block_chunk )
8772 else
8873 add_current ()
89- insert (parts , block_chunk )
74+ insert (buffer , block_chunk )
9075 current = { }
9176 end
9277 end
@@ -97,7 +82,7 @@ Line = (function()
9782 if # current > 0 then
9883 add_current ()
9984 end
100- return unpack ( parts )
85+ return buffer
10186 end ,
10287 __tostring = function (self )
10388 return " Line<" .. tostring (self :render ()) .. " >"
@@ -138,7 +123,7 @@ Line = (function()
138123 return _class_0
139124end )()
140125Block = (function ()
141- local flatten
126+ local add_to_buffer
142127 local _parent_0 = nil
143128 local _base_0 = {
144129 header = " do" ,
@@ -150,7 +135,7 @@ Block = (function()
150135 if " string" == type (self .header ) then
151136 h = self .header
152137 else
153- h = self .header :render ()
138+ h = unpack ( self .header :render ({ }) )
154139 end
155140 return " Block<" .. tostring (h ) .. " > <- " .. tostring (self .parent )
156141 end ,
@@ -317,44 +302,38 @@ Block = (function()
317302 if " string" == _exp_0 then
318303 return insert (self ._lines , line )
319304 elseif Block == _exp_0 then
320- return insert_many ( self . _lines , line :render () )
305+ return line :render (self . _lines )
321306 elseif Line == _exp_0 then
322- return insert_many ( self . _lines , line :render () )
307+ return line :render (self . _lines )
323308 else
324309 return error (" Adding unknown item" )
325310 end
326311 end ,
327- render = function (self )
328- local out = {
329- flatten (self .header )
330- }
312+ render = function (self , buffer )
313+ add_to_buffer (buffer , self .header )
331314 local lines = (function ()
332315 local _accum_0 = { }
333316 local _len_0 = 0
334317 local _list_0 = self ._lines
335318 for _index_0 = 1 , # _list_0 do
336- local line = _list_0 [_index_0 ]
337- local _value_0 = flatten (line )
338- if _value_0 ~= nil then
339- _len_0 = _len_0 + 1
340- _accum_0 [_len_0 ] = _value_0
341- end
319+ local l = _list_0 [_index_0 ]
320+ _len_0 = _len_0 + 1
321+ _accum_0 [_len_0 ] = l
342322 end
343323 return _accum_0
344324 end )()
345325 if self .next then
346- insert (out , lines )
347- insert_many ( out , self .next :render () )
326+ insert (buffer , lines )
327+ self .next :render (buffer )
348328 else
349- local footer = flatten (self .footer )
350- if # lines == 0 and # out == 1 then
351- out [1 ] = out [1 ] .. (" " .. footer )
329+ if # lines == 0 and " string" == type (buffer [# buffer ]) then
330+ buffer [# buffer ] = buffer [# buffer ] .. (" " .. (unpack (add_to_buffer ({ }, self .footer ))))
352331 else
353- insert (out , lines )
354- insert ( out , footer )
332+ insert (buffer , lines )
333+ add_to_buffer ( buffer , self . footer )
355334 end
356335 end
357- return unpack ( out )
336+ return buffer
358337 end ,
359338 block = function (self , header , footer )
360339 return Block (self , header , footer )
@@ -511,13 +490,14 @@ Block = (function()
511490 })
512491 _base_0 .__class = _class_0
513492 local self = _class_0
514- flatten = function (line )
493+ add_to_buffer = function (buffer , line )
515494 local _exp_0 = mtype (line )
516495 if Line == _exp_0 then
517- return line :render ()
496+ line :render (buffer )
518497 else
519- return line
498+ insert ( buffer , line )
520499 end
500+ return buffer
521501 end
522502 if _parent_0 and _parent_0 .__inherited then
523503 _parent_0 .__inherited (_parent_0 , _class_0 )
0 commit comments