Skip to content

Commit 68fe482

Browse files
committed
Introduce --strict-indent argument to strictly impose indentation in
nested loops, this fixes #26
1 parent e4e1cf2 commit 68fe482

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

fprettify/__init__.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,7 @@ def __align_line_continuations(self, line, is_decl, indent_size, line_nr):
539539
self._level = level
540540

541541

542-
def inspect_ffile_format(infile, indent_size, orig_filename=None):
542+
def inspect_ffile_format(infile, indent_size, strict_indent, orig_filename=None):
543543
"""
544544
Determine indentation by inspecting original Fortran file.
545545
@@ -573,9 +573,12 @@ def inspect_ffile_format(infile, indent_size, orig_filename=None):
573573
indents.append(offset - prev_offset)
574574

575575
# don't impose indentation for blocked do/if constructs:
576-
if (prev_offset != offset or (not IF_RE.search(f_line) and
577-
not DO_RE.search(f_line))):
576+
if (IF_RE.search(f_line) or DO_RE.search(f_line)):
577+
if (prev_offset != offset or strict_indent):
578+
indents[-1] = indent_size
579+
else:
578580
indents[-1] = indent_size
581+
579582
prev_offset = offset
580583

581584
return indents, first_indent
@@ -911,7 +914,7 @@ def reformat_inplace(filename, stdout=False, **kwargs): # pragma: no cover
911914
outfile.write(newfile.getvalue())
912915

913916

914-
def reformat_ffile(infile, outfile, impose_indent=True, indent_size=3, impose_whitespace=True,
917+
def reformat_ffile(infile, outfile, impose_indent=True, indent_size=3, strict_indent=False, impose_whitespace=True,
915918
whitespace=2, strip_comments=False, orig_filename=None):
916919
"""main method to be invoked for formatting a Fortran file."""
917920

@@ -920,7 +923,7 @@ def reformat_ffile(infile, outfile, impose_indent=True, indent_size=3, impose_wh
920923

921924
infile.seek(0)
922925
req_indents, first_indent = inspect_ffile_format(
923-
infile, indent_size, orig_filename)
926+
infile, indent_size, strict_indent, orig_filename)
924927
infile.seek(0)
925928

926929
# initialization
@@ -1334,6 +1337,7 @@ def run(argv=sys.argv): # pragma: no cover
13341337
" | 2: operators, print/read, plus/minus"
13351338
" | 3: operators, print/read, plus/minus, muliply/divide"
13361339
" | 4: operators, print/read, plus/minus, muliply/divide, type component selector")
1340+
parser.add_argument("--strict-indent", action='store_true', default=False, help="strictly impose indentation even for nested loops")
13371341
parser.add_argument("--disable-indent", action='store_true', default=False, help="don't impose indentation")
13381342
parser.add_argument("--disable-whitespace", action='store_true', default=False, help="don't impose whitespace formatting")
13391343
parser.add_argument("--strip-comments", action='store_true', default=False, help="strip whitespaces before comments")
@@ -1403,6 +1407,7 @@ def run(argv=sys.argv): # pragma: no cover
14031407
stdout=stdout,
14041408
impose_indent=not args.disable_indent,
14051409
indent_size=args.indent,
1410+
strict_indent=args.strict_indent,
14061411
impose_whitespace=not args.disable_whitespace,
14071412
whitespace=args.whitespace,
14081413
strip_comments=args.strip_comments)

fprettify/tests/__init__.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,18 @@ def test_indent(self):
174174
args = ['-i', str(ind)]
175175
self.assert_fprettify_result(args, instring, out)
176176

177+
def test_nested(self):
178+
"""test correct indentation of nested loops"""
179+
instring = ("integer :: i,j\ndo i=1,2\ndo j=1,3\n"
180+
"print*,i,j,i*j\nend do\nend do")
181+
outstring_exp_default = ("integer :: i, j\ndo i = 1, 2\ndo j = 1, 3\n"
182+
" print *, i, j, i*j\nend do\nend do")
183+
outstring_exp_strict = ("integer :: i, j\ndo i = 1, 2\n do j = 1, 3\n"
184+
" print *, i, j, i*j\n end do\nend do")
185+
186+
self.assert_fprettify_result([], instring, outstring_exp_default)
187+
self.assert_fprettify_result(['--strict-indent'], instring, outstring_exp_strict)
188+
177189
def test_directive(self):
178190
"""
179191
test deactivate directives '!&' (inline) and '!&<', '!&>' (block)

0 commit comments

Comments
 (0)