diff --git a/Tools/scripts/reindent.py b/Tools/scripts/reindent.py --- a/Tools/scripts/reindent.py +++ b/Tools/scripts/reindent.py @@ -45,6 +45,7 @@ import tokenize import os +import re import shutil import sys @@ -209,6 +210,8 @@ have2want = {} # Program after transformation. after = self.after = [] + # Regular expression to detect a triple quote + triple_re = re.compile(tokenize.Triple) # Copy over initial empty lines -- there's nothing to do until # we see a line with *something* on it. i = stats[0][0] @@ -254,10 +257,27 @@ if diff == 0 or have == 0: after.extend(lines[thisstmt:nextstmt]) else: + inside_triple = False for line in lines[thisstmt:nextstmt]: if diff > 0: + changed_triple = False + if not inside_triple and triple_re.search(line): + triple_style = '"""' if '"""' in line else "'''" + # Conditions for NOT setting inside_triple: + # 1. The triple quote gets closed in the same line + # 2. It's a docstring + if (not line.strip().startswith(triple_style) and + triple_style not in line.replace( + triple_style, '', 1)): + inside_triple = True + changed_triple = True + if line == "\n": after.append(line) + # Avoid not indenting the line where the triple quote + # first appears. + elif inside_triple and not changed_triple: + after.append(line) else: after.append(" " * diff + line) else: