Skip to content

Commit 030ee3a

Browse files
committed
fypp: fix nested fypp / Fortran scopes
This ensures globally consistent indentation
1 parent 7d10bd5 commit 030ee3a

File tree

4 files changed

+51
-12
lines changed

4 files changed

+51
-12
lines changed

fortran_tests/after/test_fypp.f90

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -316,14 +316,30 @@ end function maxRelError_${RANK}$_${PREC}$
316316
#:endfor
317317
#:endfor
318318
319-
end module errorcalc
319+
end module errorcalc
320320
321321
! tests for fypp directives inside Fortran continuation lines
322-
call test(arg1, &
323-
${a if a > b else b}$, arg3, &
322+
call test(arg1, &
323+
${a if a > b else b}$, arg3, &
324324
#:if c>d
325-
c, &
325+
c, &
326326
#:else
327-
d, &
327+
d, &
328328
#:endif
329-
arg4)
329+
arg4)
330+
331+
! test for nested fypp / fortran constructs
332+
! this is globally consistent even though the logical scopes can not be matched correctly
333+
334+
#:if do
335+
do x = 1, 3
336+
ax = x*0.1
337+
#:else
338+
ax = 0.1
339+
#:endif
340+
r = ax
341+
#:if do
342+
end do
343+
#:endif
344+
345+
r2 = r**2

fortran_tests/before/test_fypp.f90

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,3 +330,19 @@ end module errorcalc
330330
d,&
331331
#:endif
332332
arg4)
333+
334+
! test for nested fypp / fortran constructs
335+
! this is globally consistent even though the logical scopes can not be matched correctly
336+
337+
#:if do
338+
do x=1,3
339+
ax=x*0.1
340+
#:else
341+
ax=0.1
342+
#:endif
343+
r=ax
344+
#:if do
345+
enddo
346+
#:endif
347+
348+
r2 = r**2

fortran_tests/test_results/expected_results

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2025,7 +2025,7 @@ cp2k/src/spglib_f08.F : db2758c990ccb023e2c7c3567d003a0b74f167096af95c75b628c270
20252025
cp2k/src/subsys/molecule_kind_list_types.F : b32df6ecf4df8a48925c87b3fbdf6758814de5ed10051e9ea19199799961af46
20262026
cp2k/src/subsys/molecule_list_types.F : 294f87e09017693239a3359105786600bde4f1d0af5f145fb3c9c471dfa654b4
20272027
cp2k/src/subsys/molecule_types.F : 0a893fc573a3bf81762821498fc31186cd844df80727f07424b7fc190af5ec1f
2028-
test_fypp.f90 : 3dc115bbf6282679af795e41516c431e99dd85316097008d5df1614ade330096
2028+
test_fypp.f90 : 31f61e4343e01cc9a981165b2905372ed51236653e304a5948990672df2510e9
20292029
wannier90/pwscf/post_v6.2.1/pw2wannier90.f90 : e41bc8f625bd729bd16ef8ae54b3e9bb909a6cc606d75c72cf9f04f47272f7fe
20302030
wannier90/pwscf/v3.2.3/pw2wannier90.f90 : 9db89aacccc86da7297c2cd8b38eaa118ca9a917e2c5c10f4ac782cb7f723eb7
20312031
wannier90/pwscf/v3.2.3/wannier.f90 : c4d7d9c40d3459f837283474545ddcb922a776ddfb6f30866c9402351d2047bc

fprettify/__init__.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -586,7 +586,7 @@ def process_lines_of_fline(self, f_line, lines, rel_ind, rel_ind_con,
586586
what_con, f_line), "debug", filename, line_nr)
587587
if len(scopes) > 0:
588588
what = scopes[-1]
589-
if what == what_con:
589+
if what == what_con or indent_fypp:
590590
valid_con = True
591591

592592
# check statements that end scope
@@ -600,7 +600,8 @@ def process_lines_of_fline(self, f_line, lines, rel_ind, rel_ind_con,
600600
what_end, f_line), "debug", filename, line_nr)
601601
if len(scopes) > 0:
602602
what = scopes.pop()
603-
if what == what_end or not self._parser['end'][what_end].spec:
603+
if (what == what_end or not self._parser['end'][what_end].spec
604+
or indent_fypp):
604605
valid_end = True
605606
log_message("{}: {}".format(
606607
what_end, f_line), "debug", filename, line_nr)
@@ -858,12 +859,15 @@ def inspect_ffile_format(infile, indent_size, strict_indent, indent_fypp=False,
858859
stream = InputStream(infile, filter_fypp=not indent_fypp, orig_filename=orig_filename)
859860
prev_offset = 0
860861
first_indent = -1
862+
has_fypp = False
861863

862864
while 1:
863865
f_line, _, lines = stream.next_fortran_line()
864866
if not lines:
865867
break
866868

869+
if FYPP_LINE_RE.search(f_line): has_fypp = True
870+
867871
f_line, lines, label = preprocess_labels(f_line, lines)
868872

869873
offset = len(lines[0]) - len(lines[0].lstrip(' '))
@@ -880,7 +884,7 @@ def inspect_ffile_format(infile, indent_size, strict_indent, indent_fypp=False,
880884

881885
prev_offset = offset
882886

883-
return indents, first_indent
887+
return indents, first_indent, has_fypp
884888

885889

886890
def replace_relational_single_fline(f_line, cstyle):
@@ -1468,13 +1472,16 @@ def reformat_ffile_combined(infile, outfile, impose_indent=True, indent_size=3,
14681472
if not impose_indent:
14691473
indent_fypp = False
14701474

1471-
scope_parser = build_scope_parser(fypp=indent_fypp, mod=indent_mod)
14721475

14731476
infile.seek(0)
1474-
req_indents, first_indent = inspect_ffile_format(
1477+
req_indents, first_indent, has_fypp = inspect_ffile_format(
14751478
infile, indent_size, strict_indent, indent_fypp, orig_filename)
14761479
infile.seek(0)
14771480

1481+
if not has_fypp: indent_fypp = False
1482+
1483+
scope_parser = build_scope_parser(fypp=indent_fypp, mod=indent_mod)
1484+
14781485
# initialization
14791486

14801487
# special cases for indentation:

0 commit comments

Comments
 (0)