Improving codegen line wrapping.
This commit is contained in:
parent
26a6174f28
commit
194146d365
26
code_obj.lua
26
code_obj.lua
@ -85,21 +85,27 @@ do
|
||||
local _continue_0 = false
|
||||
repeat
|
||||
local b = select(i, ...)
|
||||
assert(b, "bit is nil")
|
||||
assert(b, "code bit is nil")
|
||||
if b == '' then
|
||||
_continue_0 = true
|
||||
break
|
||||
end
|
||||
bits[#bits + 1] = b
|
||||
if type(b) == 'string' then
|
||||
do
|
||||
local spaces = match(b, "\n([ ]*)[^\n]*$")
|
||||
if spaces then
|
||||
self.current_indent = #spaces
|
||||
end
|
||||
local trailing_text, spaces = match(b, "\n(([ ]*)[^\n]*)$")
|
||||
if trailing_text then
|
||||
self.current_indent = #spaces
|
||||
self.trailing_line_len = #trailing_text
|
||||
end
|
||||
else
|
||||
if #b.indents > 1 then
|
||||
self.trailing_line_len = b.trailing_line_len
|
||||
else
|
||||
self.trailing_line_len = self.trailing_line_len + #tostring(b)
|
||||
end
|
||||
if self.current_indent ~= 0 then
|
||||
indents[#bits] = self.current_indent
|
||||
end
|
||||
elseif self.current_indent ~= 0 then
|
||||
indents[#bits] = self.current_indent
|
||||
end
|
||||
_continue_0 = true
|
||||
until true
|
||||
@ -172,7 +178,9 @@ do
|
||||
_class_0 = setmetatable({
|
||||
__init = function(self, source, ...)
|
||||
self.source = source
|
||||
self.bits, self.indents, self.current_indent = { }, { }, 0
|
||||
self.bits = { }
|
||||
self.indents, self.current_indent = { }, 0
|
||||
self.trailing_line_len = 0
|
||||
if type(self.source) == 'string' then
|
||||
self.source = Source:from_string(self.source)
|
||||
end
|
||||
|
@ -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)=>
|
||||
|
@ -150,7 +150,7 @@ local NomsuCompiler = setmetatable({ }, {
|
||||
end
|
||||
})
|
||||
do
|
||||
NomsuCompiler.NOMSU_COMPILER_VERSION = 3
|
||||
NomsuCompiler.NOMSU_COMPILER_VERSION = 4
|
||||
NomsuCompiler.NOMSU_SYNTAX_VERSION = Parser.version
|
||||
NomsuCompiler._ENV = NomsuCompiler
|
||||
NomsuCompiler.nomsu = NomsuCompiler
|
||||
@ -259,20 +259,14 @@ do
|
||||
end
|
||||
local add_lua_string_bits
|
||||
add_lua_string_bits = function(self, val_or_stmt, code)
|
||||
local line_len = 0
|
||||
local cls_str = val_or_stmt == "value" and "LuaCode.Value(" or "LuaCode("
|
||||
if code.type ~= "Text" then
|
||||
return LuaCode(code.source, cls_str, repr(tostring(code.source)), ", ", self:compile(code), ")")
|
||||
end
|
||||
local add_bit_lua
|
||||
add_bit_lua = function(lua, bit_lua)
|
||||
line_len = line_len + #tostring(bit_lua)
|
||||
if line_len > MAX_LINE then
|
||||
lua:append(",\n ")
|
||||
line_len = 4
|
||||
else
|
||||
lua:append(", ")
|
||||
end
|
||||
local bit_leading_len = #(tostring(bit_lua):match("^[^\n]*"))
|
||||
lua:append(lua.trailing_line_len + bit_leading_len > MAX_LINE and ",\n " or ", ")
|
||||
return lua:append(bit_lua)
|
||||
end
|
||||
local operate_on_text
|
||||
|
@ -92,7 +92,7 @@ dict = (t)-> setmetatable(t, _dict_mt)
|
||||
MAX_LINE = 80 -- For beautification purposes, try not to make lines much longer than this value
|
||||
NomsuCompiler = setmetatable({}, {__index: (k)=> if _self = rawget(@, "self") then _self[k] else nil})
|
||||
with NomsuCompiler
|
||||
.NOMSU_COMPILER_VERSION = 3
|
||||
.NOMSU_COMPILER_VERSION = 4
|
||||
.NOMSU_SYNTAX_VERSION = Parser.version
|
||||
._ENV = NomsuCompiler
|
||||
.nomsu = NomsuCompiler
|
||||
@ -157,18 +157,13 @@ with NomsuCompiler
|
||||
return operate_on_text code
|
||||
|
||||
add_lua_string_bits = (val_or_stmt, code)=>
|
||||
line_len = 0
|
||||
cls_str = val_or_stmt == "value" and "LuaCode.Value(" or "LuaCode("
|
||||
if code.type != "Text"
|
||||
return LuaCode(code.source, cls_str, repr(tostring(code.source)), ", ", @compile(code), ")")
|
||||
add_bit_lua = (lua, bit_lua)->
|
||||
line_len += #tostring(bit_lua)
|
||||
if line_len > MAX_LINE
|
||||
lua\append ",\n "
|
||||
line_len = 4
|
||||
else
|
||||
lua\append ", "
|
||||
lua\append bit_lua
|
||||
bit_leading_len = #(tostring(bit_lua)\match("^[^\n]*"))
|
||||
lua\append(lua.trailing_line_len + bit_leading_len > MAX_LINE and ",\n " or ", ")
|
||||
lua\append(bit_lua)
|
||||
operate_on_text = (text)->
|
||||
lua = LuaCode.Value(text.source, cls_str, repr(tostring(text.source)))
|
||||
for bit in *text
|
||||
|
Loading…
Reference in New Issue
Block a user