|
18 | 18 | end |
19 | 19 | local concat, insert = table.concat, table.insert |
20 | 20 | local pos_to_line, get_closest_line, trim = util.pos_to_line, util.get_closest_line, util.trim |
| 21 | +local 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 |
21 | 34 | local Line |
22 | 35 | Line = (function() |
23 | 36 | local _parent_0 = nil |
@@ -53,19 +66,41 @@ Line = (function() |
53 | 66 | return nil |
54 | 67 | end, |
55 | 68 | render = function(self) |
56 | | - local buff = { } |
57 | | - for i = 1, #self do |
58 | | - local c = self[i] |
59 | | - insert(buff, (function() |
60 | | - if util.moon.type(c) == Block then |
61 | | - c:bubble() |
62 | | - return c:render() |
63 | | - else |
64 | | - return c |
| 69 | + local parts = { } |
| 70 | + local current = { } |
| 71 | + local add_current |
| 72 | + add_current = function() |
| 73 | + return insert(parts, table.concat(current)) |
| 74 | + end |
| 75 | + local _list_0 = self |
| 76 | + for _index_0 = 1, #_list_0 do |
| 77 | + local chunk = _list_0[_index_0] |
| 78 | + local _exp_0 = mtype(chunk) |
| 79 | + if Block == _exp_0 then |
| 80 | + local _list_1 = { |
| 81 | + chunk:render() |
| 82 | + } |
| 83 | + for _index_1 = 1, #_list_1 do |
| 84 | + local block_chunk = _list_1[_index_1] |
| 85 | + if "string" == type(block_chunk) then |
| 86 | + insert(current, block_chunk) |
| 87 | + else |
| 88 | + add_current() |
| 89 | + insert(parts, block_chunk) |
| 90 | + current = { } |
| 91 | + end |
65 | 92 | end |
66 | | - end)()) |
| 93 | + else |
| 94 | + insert(current, chunk) |
| 95 | + end |
67 | 96 | end |
68 | | - return concat(buff) |
| 97 | + if #current > 0 then |
| 98 | + add_current() |
| 99 | + end |
| 100 | + return unpack(parts) |
| 101 | + end, |
| 102 | + __tostring = function(self) |
| 103 | + return "Line<" .. tostring(self:render()) .. ">" |
69 | 104 | end |
70 | 105 | } |
71 | 106 | _base_0.__index = _base_0 |
@@ -103,6 +138,7 @@ Line = (function() |
103 | 138 | return _class_0 |
104 | 139 | end)() |
105 | 140 | Block = (function() |
| 141 | + local flatten |
106 | 142 | local _parent_0 = nil |
107 | 143 | local _base_0 = { |
108 | 144 | header = "do", |
@@ -251,8 +287,8 @@ Block = (function() |
251 | 287 | end |
252 | 288 | end |
253 | 289 | end, |
254 | | - add_line_text = function(self, text) |
255 | | - return insert(self._lines, text) |
| 290 | + add_raw = function(self, item) |
| 291 | + return insert(self._lines, item) |
256 | 292 | end, |
257 | 293 | append_line_table = function(self, sub_table, offset) |
258 | 294 | offset = offset + self.current_line |
@@ -281,62 +317,48 @@ Block = (function() |
281 | 317 | end |
282 | 318 | end, |
283 | 319 | add = function(self, line) |
284 | | - local t = util.moon.type(line) |
285 | | - if t == "string" then |
286 | | - self:add_line_text(line) |
287 | | - elseif t == Block then |
288 | | - self:add(self:line(line)) |
289 | | - elseif t == Line then |
290 | | - self:add_line_tables(line) |
291 | | - self:add_line_text(line:render()) |
292 | | - self.current_line = self.current_line + 1 |
| 320 | + local _exp_0 = util.moon.type(line) |
| 321 | + if "string" == _exp_0 then |
| 322 | + return insert(self._lines, line) |
| 323 | + elseif Block == _exp_0 then |
| 324 | + return insert_many(self._lines, line:render()) |
| 325 | + elseif Line == _exp_0 then |
| 326 | + return insert_many(self._lines, line:render()) |
293 | 327 | else |
294 | | - error("Adding unknown item") |
295 | | - end |
296 | | - return nil |
297 | | - end, |
298 | | - _insert_breaks = function(self) |
299 | | - for i = 1, #self._lines - 1 do |
300 | | - local left, right = self._lines[i], self._lines[i + 1] |
301 | | - local lc = left:sub(-1) |
302 | | - if (lc == ")" or lc == "]") and right:sub(1, 1) == "(" then |
303 | | - self._lines[i] = self._lines[i] .. ";" |
304 | | - end |
| 328 | + return error("Adding unknown item") |
305 | 329 | end |
306 | 330 | end, |
307 | 331 | render = function(self) |
308 | | - local flatten |
309 | | - flatten = function(line) |
310 | | - if type(line) == "string" then |
311 | | - return line |
| 332 | + local out = { |
| 333 | + flatten(self.header) |
| 334 | + } |
| 335 | + local lines = (function() |
| 336 | + local _accum_0 = { } |
| 337 | + local _len_0 = 0 |
| 338 | + local _list_0 = self._lines |
| 339 | + for _index_0 = 1, #_list_0 do |
| 340 | + local line = _list_0[_index_0] |
| 341 | + local _value_0 = flatten(line) |
| 342 | + if _value_0 ~= nil then |
| 343 | + _len_0 = _len_0 + 1 |
| 344 | + _accum_0[_len_0] = _value_0 |
| 345 | + end |
| 346 | + end |
| 347 | + return _accum_0 |
| 348 | + end)() |
| 349 | + if self.next then |
| 350 | + insert(out, lines) |
| 351 | + insert_many(out, self.next:render()) |
| 352 | + else |
| 353 | + local footer = flatten(self.footer) |
| 354 | + if #lines == 0 and #out == 1 then |
| 355 | + out[1] = out[1] .. (" " .. footer) |
312 | 356 | else |
313 | | - return line:render() |
| 357 | + insert(out, lines) |
| 358 | + insert(out, footer) |
314 | 359 | end |
315 | 360 | end |
316 | | - local header = flatten(self.header) |
317 | | - if #self._lines == 0 then |
318 | | - local footer = flatten(self.footer) |
319 | | - return concat({ |
320 | | - header, |
321 | | - footer |
322 | | - }, " ") |
323 | | - end |
324 | | - local indent = indent_char:rep(self.indent) |
325 | | - if not self.delim then |
326 | | - self:_insert_breaks() |
327 | | - end |
328 | | - local body = indent .. concat(self._lines, (self.delim or "") .. "\n" .. indent) |
329 | | - return concat({ |
330 | | - header, |
331 | | - body, |
332 | | - indent_char:rep(self.indent - 1) .. (function() |
333 | | - if self.next then |
334 | | - return self.next:render() |
335 | | - else |
336 | | - return flatten(self.footer) |
337 | | - end |
338 | | - end)() |
339 | | - }, "\n") |
| 361 | + return unpack(out) |
340 | 362 | end, |
341 | 363 | block = function(self, header, footer) |
342 | 364 | return Block(self, header, footer) |
@@ -492,20 +514,62 @@ Block = (function() |
492 | 514 | end |
493 | 515 | }) |
494 | 516 | _base_0.__class = _class_0 |
| 517 | + local self = _class_0 |
| 518 | + flatten = function(line) |
| 519 | + local _exp_0 = mtype(line) |
| 520 | + if Line == _exp_0 then |
| 521 | + return line:render() |
| 522 | + else |
| 523 | + return line |
| 524 | + end |
| 525 | + end |
495 | 526 | if _parent_0 and _parent_0.__inherited then |
496 | 527 | _parent_0.__inherited(_parent_0, _class_0) |
497 | 528 | end |
498 | 529 | return _class_0 |
499 | 530 | end)() |
| 531 | +local flatten_lines |
| 532 | +flatten_lines = function(lines, indent, buffer) |
| 533 | + if indent == nil then |
| 534 | + indent = nil |
| 535 | + end |
| 536 | + if buffer == nil then |
| 537 | + buffer = { } |
| 538 | + end |
| 539 | + for i = 1, #lines do |
| 540 | + local l = lines[i] |
| 541 | + local _exp_0 = type(l) |
| 542 | + if "string" == _exp_0 then |
| 543 | + if indent then |
| 544 | + insert(buffer, indent) |
| 545 | + end |
| 546 | + insert(buffer, l) |
| 547 | + if "string" == type(lines[i + 1]) then |
| 548 | + local lc = l:sub(-1) |
| 549 | + if (lc == ")" or lc == "]") and lines[i + 1]:sub(1, 1) == "(" then |
| 550 | + insert(buffer, ";") |
| 551 | + end |
| 552 | + end |
| 553 | + insert(buffer, "\n") |
| 554 | + local last = l |
| 555 | + elseif "table" == _exp_0 then |
| 556 | + flatten_lines(l, indent and indent .. indent_char or indent_char, buffer) |
| 557 | + end |
| 558 | + end |
| 559 | + return buffer |
| 560 | +end |
500 | 561 | RootBlock = (function() |
501 | 562 | local _parent_0 = Block |
502 | 563 | local _base_0 = { |
503 | 564 | __tostring = function(self) |
504 | 565 | return "RootBlock<>" |
505 | 566 | end, |
506 | 567 | render = function(self) |
507 | | - self:_insert_breaks() |
508 | | - return concat(self._lines, "\n") |
| 568 | + local buffer = flatten_lines(self._lines) |
| 569 | + if buffer[#buffer] == "\n" then |
| 570 | + buffer[#buffer] = nil |
| 571 | + end |
| 572 | + return table.concat(buffer) |
509 | 573 | end |
510 | 574 | } |
511 | 575 | _base_0.__index = _base_0 |
|
0 commit comments