Skip to content

Commit

Permalink
wmlindent: refactored indentation level handling code
Browse files Browse the repository at this point in the history
  • Loading branch information
Elvish-Hunter committed Apr 25, 2022
1 parent 745a0e1 commit 71f142b
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions data/tools/wmlindent
Expand Up @@ -111,7 +111,7 @@ def reindent(name, infp, outfp):
inmacro = False
ignoring = False
instring = False
indent = ""
indent_level = 0
lasttag = ""
countlines = 0
countblanks = 0
Expand Down Expand Up @@ -157,23 +157,23 @@ def reindent(name, infp, outfp):
print('wmlindent: "%s", line %d: Expected string, received tag.' % (name, countlines), file=sys.stderr)
# Track whether we've seen real WML rather than just macro definitions
elif transformed.startswith("#define"):
saved_indent = indent
indent = wmltools.baseindent
saved_indent = indent_level
indent_level = 1
inmacro = True
# Be sure to ignore the newlines and comments
elif transformed.rstrip().endswith("#enddef") and transformed.find("#") == transformed.find("#enddef"):
indent = saved_indent
indent_level = saved_indent
inmacro = False
elif not inmacro and transformed[0] in ('[', ']'):
seen_wml = True
# In the close case, we must compute new indent *before* emitting
# the new line so the close tag will be at the same level as the
# one that started the block.
if closer(transformed):
if indent == "":
if indent_level == 0:
print('wmlindent: "%s", line %d: close tag %s with indent already zero.' % (name, countlines, transformed.strip()), file=sys.stderr)
else:
indent = indent[:-len(wmltools.baseindent)]
indent_level -= 1
# Cope with blank lines outside of multiline literals
if dostrip:
if transformed == "\n":
Expand All @@ -188,7 +188,7 @@ def reindent(name, infp, outfp):
outfp.write("\n")
# Here's where we apply the current indent
if dostrip and transformed and not is_directive(transformed):
output = indent + transformed
output = (wmltools.baseindent * indent_level) + transformed
else:
output = transformed
# Nuke trailing space and canonicalize to Unix-style end-of-line
Expand All @@ -198,13 +198,13 @@ def reindent(name, infp, outfp):
outfp.write(output)
# May need to indent based on the line we just saw.
if opener(transformed):
indent += wmltools.baseindent
indent_level += 1
if continued_string.search(transformed):
if not instring:
indent += wmltools.baseindent
indent_level += 1
instring = True
elif instring and not (transformed.startswith("#")):
indent = indent[:-len(wmltools.baseindent)]
indent_level -= 1
instring = False
# Compute the dostrip state likewise.
# We look for unbalanced string quotes.
Expand All @@ -227,7 +227,7 @@ def reindent(name, infp, outfp):
else:
lasttag = ""
# Pure macro files look like they have unbalanced indents. That's OK
if indent != "" and seen_wml:
if indent_level != 0 and seen_wml:
print('wmlindent: "%s". line %d: end of file with indent nonzero.' % (name, countlines), file=sys.stderr)

def allwmlfiles(directory):
Expand Down

0 comments on commit 71f142b

Please sign in to comment.