From 7762c8c45b6f0349d769682b26349fb25a7a8657 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Mon, 31 Dec 2018 01:36:48 -0800 Subject: [PATCH] Added the ": for $ in $: ..." shorthand for indented comprehensions. --- nomsu_decompiler.lua | 8 +++++++- nomsu_decompiler.moon | 8 +++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/nomsu_decompiler.lua b/nomsu_decompiler.lua index e17f25e..1918ac2 100644 --- a/nomsu_decompiler.lua +++ b/nomsu_decompiler.lua @@ -562,6 +562,12 @@ tree_to_nomsu = function(tree) if i > 1 then sep = '\n' end + elseif item.type == 'Block' and #item == 1 then + item_nomsu = tree_to_nomsu(item[1]) + item_nomsu:prepend(": ") + if i > 1 then + sep = '\n' + end else item_nomsu = tree_to_inline_nomsu(item) if #item_nomsu:text() > MAX_LINE then @@ -573,7 +579,7 @@ tree_to_nomsu = function(tree) end nomsu:add(sep) nomsu:add(item_nomsu) - if item_nomsu:is_multiline() or item.type == 'Comment' or nomsu:trailing_line_len() + #tostring(item_nomsu) >= MAX_LINE then + if item_nomsu:is_multiline() or item.type == 'Comment' or item.type == "Block" or nomsu:trailing_line_len() + #tostring(item_nomsu) >= MAX_LINE then sep = '\n' else sep = ', ' diff --git a/nomsu_decompiler.moon b/nomsu_decompiler.moon index 3c20f76..3deaabc 100644 --- a/nomsu_decompiler.moon +++ b/nomsu_decompiler.moon @@ -427,6 +427,11 @@ tree_to_nomsu = (tree)-> elseif item.type == 'Comment' item_nomsu = tree_to_nomsu(item) sep = '\n' if i > 1 + elseif item.type == 'Block' and #item == 1 + -- Comprehensions use the more concise ": for $ in $: ..." form + item_nomsu = tree_to_nomsu(item[1]) + item_nomsu\prepend ": " + sep = '\n' if i > 1 else item_nomsu = tree_to_inline_nomsu(item) if #item_nomsu\text! > MAX_LINE @@ -434,7 +439,8 @@ tree_to_nomsu = (tree)-> item_nomsu = tree_to_nomsu(item) nomsu\add sep nomsu\add item_nomsu - if item_nomsu\is_multiline! or item.type == 'Comment' or nomsu\trailing_line_len! + #tostring(item_nomsu) >= MAX_LINE + if item_nomsu\is_multiline! or item.type == 'Comment' or item.type == "Block" or + nomsu\trailing_line_len! + #tostring(item_nomsu) >= MAX_LINE sep = '\n' else sep = ', '