From 194146d36591e6c3a964667ead9109bbc6e00524 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Tue, 10 Jul 2018 17:34:39 -0700 Subject: Improving codegen line wrapping. --- code_obj.moon | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) (limited to 'code_obj.moon') diff --git a/code_obj.moon b/code_obj.moon index 8ac7146..1586d15 100644 --- a/code_obj.moon +++ b/code_obj.moon @@ -45,7 +45,9 @@ class Source class Code new: (@source, ...)=> - @bits, @indents, @current_indent = {}, {}, 0 + @bits = {} + @indents, @current_indent = {}, 0 + @trailing_line_len = 0 if type(@source) == 'string' @source = Source\from_string(@source) assert(@source and Source\is_instance(@source), "Source has the wrong type") @@ -57,14 +59,21 @@ class Code match = string.match for i=1,n b = select(i, ...) - assert(b, "bit is nil") + assert(b, "code bit is nil") if b == '' then continue bits[#bits+1] = b if type(b) == 'string' - if spaces = match(b, "\n([ ]*)[^\n]*$") + trailing_text, spaces = match(b, "\n(([ ]*)[^\n]*)$") + if trailing_text @current_indent = #spaces - elseif @current_indent != 0 - indents[#bits] = @current_indent + @trailing_line_len = #trailing_text + else + if #b.indents > 1 + @trailing_line_len = b.trailing_line_len + else + @trailing_line_len += #tostring(b) + if @current_indent != 0 + indents[#bits] = @current_indent @__str = nil concat_append: (values, joiner, wrapping_joiner)=> -- cgit v1.2.3