wmlindent: refactored indentation level handling code

This commit is contained in:
Elvish_Hunter 2022-04-01 22:53:44 +02:00
parent b3b6d7b781
commit c2ece934bd

View file

@ -111,7 +111,7 @@ def reindent(name, infp, outfp):
inmacro = False
ignoring = False
instring = False
indent = ""
indent_level = 0
lasttag = ""
countlines = 0
countblanks = 0
@ -157,12 +157,12 @@ 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
@ -170,10 +170,10 @@ def reindent(name, infp, outfp):
# 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":
@ -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
@ -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.
@ -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):