Skip to content

Commit

Permalink
fixed #29 - no-minify-spaces needs 'need_linebreak' logic too
Browse files Browse the repository at this point in the history
  • Loading branch information
thisismypassport committed Oct 7, 2023
1 parent 3b6ea85 commit 35bec28
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 4 deletions.
17 changes: 13 additions & 4 deletions pico_minify.py
Original file line number Diff line number Diff line change
Expand Up @@ -548,8 +548,9 @@ def output_original_wspace(root, exclude_comments=False):
prev_token = Token.none
prev_welded_token = None
prev_vline = 0
need_linebreak = False

def get_wspace(pre, post, allow_linebreaks):
def get_wspace(pre, post, allow_linebreaks, need_linebreak=False):
source = default(pre.source, post.source)
text = source.text[pre.endidx:post.idx]

Expand All @@ -561,15 +562,17 @@ def get_wspace(pre, post, allow_linebreaks):

if not allow_linebreaks and "\n" in text:
text = text[:text.index("\n")] + " "
if need_linebreak and "\n" not in text:
text += "\n"

return text

def output_tokens(token):
nonlocal prev_token, prev_welded_token, prev_vline
nonlocal prev_token, prev_welded_token, prev_vline, need_linebreak

if prev_token.endidx != token.idx:
allow_linebreaks = e(token.vline) and token.vline != prev_vline
wspace = get_wspace(prev_token, token, allow_linebreaks)
wspace = get_wspace(prev_token, token, allow_linebreaks, need_linebreak)

if exclude_comments and token.children:
# only output spacing before and after the comments between the tokens
Expand All @@ -589,6 +592,7 @@ def output_tokens(token):
output.append(wspace)

prev_welded_token = None
need_linebreak = False

# extra whitespace may be needed due to modified or deleted tokens
if prev_welded_token and token.value and (prev_welded_token.modified or token.modified or prev_welded_token != prev_token):
Expand All @@ -603,7 +607,12 @@ def output_tokens(token):
if e(token.vline):
prev_vline = token.vline

root.traverse_nodes(tokens=output_tokens)
def post_node_output(node):
nonlocal need_linebreak
if need_newline_after(node):
need_linebreak = True

root.traverse_nodes(tokens=output_tokens, post=post_node_output)
return "".join(output)

from pico_process import PicoSource
1 change: 1 addition & 0 deletions run_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ def run():
run_test("short", "short.p8", "short.p8", "-m", "--focus-chars", pico8_output_val="K\nK")
run_test("short-lines", "short.p8", "short-lines.p8", "-m", "--no-minify-lines", "--focus-chars", pico8_output_val="K\nK")
run_test("short-spaces", "short.p8", "short-spaces.p8", "-m", "--no-minify-spaces", "--focus-chars", pico8_output_val="K\nK")
run_test("short2", "short2.p8", "short2.p8", "-m", "--focus-compressed", "--no-minify-spaces")

def main(raw_args):
global g_opts
Expand Down
9 changes: 9 additions & 0 deletions test_compare/short2.p8
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
pico-8 cartridge // http://www.pico-8.com
version 41
__lua__

if i==1then?1
end if i==0 then
?2
?3
end
7 changes: 7 additions & 0 deletions test_input/short2.p8
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
__lua__

if(x==1)?1
if x==0 then
?2
?3
end

0 comments on commit 35bec28

Please sign in to comment.