From 71f142bb864f69efbdbedd735fab31f0df95c649 Mon Sep 17 00:00:00 2001 From: Elvish_Hunter Date: Fri, 1 Apr 2022 22:53:44 +0200 Subject: [PATCH] wmlindent: refactored indentation level handling code --- data/tools/wmlindent | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/data/tools/wmlindent b/data/tools/wmlindent index 31f5aab0fd4a..c6e56fd8509d 100755 --- a/data/tools/wmlindent +++ b/data/tools/wmlindent @@ -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):