Skip to content

Commit 5504100

Browse files
committed
More fixes for Fypp / FORD compatibility
don't align Fypp directives and FORD comments in Fortran continuation lines
1 parent 189c348 commit 5504100

File tree

5 files changed

+52
-17
lines changed

5 files changed

+52
-17
lines changed

fortran_tests/after/test_fypp.f90

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,3 +318,12 @@ end function maxRelError_${RANK}$_${PREC}$
318318
319319
end module errorcalc
320320
321+
! tests for fypp directives inside Fortran continuation lines
322+
call test(arg1, &
323+
${a if a > b else b}$, arg3, &
324+
#:if c>d
325+
c, &
326+
#:else
327+
d, &
328+
#:endif
329+
arg4)

fortran_tests/before/test_fypp.f90

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,3 +321,12 @@ end function maxRelError_${RANK}$_${PREC}$
321321
322322
end module errorcalc
323323
324+
! tests for fypp directives inside Fortran continuation lines
325+
call test(arg1,&
326+
${a if a > b else b}$, arg3, &
327+
#:if c>d
328+
c,&
329+
#:else
330+
d,&
331+
#:endif
332+
arg4)

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 : 73ca32b9888bfbb3d6aa83400ef1f06a6e75c757229b8f08c6006ada
20252025
cp2k/src/subsys/molecule_kind_list_types.F : c7bea1daf80996474c7cb28b37bcca100438e7786c5407ccab2de600790d78d6
20262026
cp2k/src/subsys/molecule_list_types.F : 8710f953acb71f2a06a6330db825f4ccdfd7967d5ba04a5227b41a44f02cb4cd
20272027
cp2k/src/subsys/molecule_types.F : 3cd84f819410634c322cb7c3625597fd1460f4df7d08dcd8c1f1c860effb696e
2028-
test_fypp.f90 : 640604e591b2711e08b52b48dc91bc6653e66b541f1551d5de60770418c3ad23
2028+
test_fypp.f90 : 9202d61e58959ad8c80db6afefa9007fbf9b72b306547337d6ba628c13eb2ca5
20292029
wannier90/pwscf/post_v6.2.1/pw2wannier90.f90 : 319c6b563bbd5e69c666ed9cd972540abbbde70d17708064d9aae19f25d5a217
20302030
wannier90/pwscf/v3.2.3/pw2wannier90.f90 : 42dba4a02d1257912baf939d57bc75aa9e412da619ea0c5413199167ede7efe6
20312031
wannier90/pwscf/v3.2.3/wannier.f90 : c4d7d9c40d3459f837283474545ddcb922a776ddfb6f30866c9402351d2047bc

fprettify/__init__.py

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1061,10 +1061,10 @@ def reformat_ffile(infile, outfile, impose_indent=True, indent_size=3, strict_in
10611061
f_line, lines)
10621062
f_line, lines, label = preprocess_labels(f_line, lines)
10631063

1064-
lines, do_format, prev_indent, is_blank, is_fypp, is_ford = preprocess_line(
1064+
lines, do_format, prev_indent, is_blank, is_special = preprocess_line(
10651065
f_line, lines, comments, orig_filename, stream.line_nr)
10661066

1067-
if is_fypp or is_ford:
1067+
if is_special[0]:
10681068
indent_special = 3
10691069

10701070
if prev_indent and indent_special == 0:
@@ -1100,7 +1100,7 @@ def reformat_ffile(infile, outfile, impose_indent=True, indent_size=3, strict_in
11001100
f_line, whitespace, whitespace_dict, linebreak_pos, ampersand_sep,
11011101
orig_filename, stream.line_nr, auto_format)
11021102

1103-
lines = append_comments(lines, comment_lines)
1103+
lines = append_comments(lines, comment_lines, is_special)
11041104

11051105
# target indent for next line
11061106
rel_indent = req_indents[nfl] if nfl < len(req_indents) else 0
@@ -1111,6 +1111,13 @@ def reformat_ffile(infile, outfile, impose_indent=True, indent_size=3, strict_in
11111111
stream.line_nr, manual_lines_indent)
11121112
indent = indenter.get_lines_indent()
11131113

1114+
# use actual indents if line is special
1115+
if any(is_special):
1116+
for pos, line in enumerate(lines):
1117+
if is_special[pos]:
1118+
indent[pos] = len(line) - len(line.lstrip(' '))
1119+
lines[pos] = line.lstrip(' ')
1120+
11141121
lines, indent = prepend_ampersands(lines, indent, pre_ampersand)
11151122

11161123
lines = remove_trailing_whitespace(lines)
@@ -1208,26 +1215,30 @@ def preprocess_line(f_line, lines, comments, filename, line_nr):
12081215
is_blank = False
12091216
prev_indent = False
12101217
do_format = False
1211-
is_fypp = False
1212-
is_ford = False
1218+
1219+
# is_special: special directives that should not be treated as Fortran
1220+
# currently supported: fypp preprocessor directives or comments for FORD documentation
1221+
is_special = [False]*len(lines)
1222+
1223+
for pos, line in enumerate(lines):
1224+
line_strip = line.lstrip()
1225+
is_special[pos] = FYPP_LINE_RE.search(line_strip) or line_strip.startswith('!!')
1226+
1227+
# if first line is special, all lines should be special
1228+
if is_special[0]: is_special = [True]*len(lines)
12131229

12141230
if EMPTY_RE.search(f_line): # empty lines including comment lines
12151231
if any(comments):
12161232
if lines[0].startswith(' '):
12171233
# indent comment lines only if they were not indented before.
12181234
prev_indent = True
1219-
line_strip = lines[0].lstrip()
1220-
is_fypp = FYPP_LINE_RE.search(line_strip)
1221-
is_ford = line_strip.startswith('!!')
12221235
else:
12231236
is_blank = True
1224-
if not is_fypp and not is_ford:
1225-
lines = [l.strip(' ') for l in lines]
1226-
1237+
lines = [l.strip(' ') if not is_special[n] else l for n, l in enumerate(lines)]
12271238
else:
12281239
do_format = True
12291240

1230-
return [lines, do_format, prev_indent, is_blank, is_fypp, is_ford]
1241+
return [lines, do_format, prev_indent, is_blank, is_special]
12311242

12321243

12331244
def pass_defaults_to_next_line(f_line):
@@ -1261,12 +1272,12 @@ def prepend_ampersands(lines, indent, pre_ampersand):
12611272
return [lines, indent]
12621273

12631274

1264-
def append_comments(lines, comment_lines):
1275+
def append_comments(lines, comment_lines, is_special):
12651276
"""append comments to lines"""
12661277
for pos, (line, comment) in enumerate(zip(lines, comment_lines)):
12671278
if pos < len(lines) - 1:
12681279
has_nl = True # has next line
1269-
if not line.strip(): comment = comment.lstrip()
1280+
if not line.strip() and not is_special[pos]: comment = comment.lstrip()
12701281
else:
12711282
has_nl = not re.search(EOL_SC, line)
12721283
lines[pos] = lines[pos].rstrip(' ') + comment + '\n' * has_nl

fprettify/tests/__init__.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -428,12 +428,18 @@ def test_ford(self):
428428
instring = (" a = b\n"
429429
" !! ford docu\n"
430430
"b=c\n"
431-
" !! ford docu"
431+
" !! ford docu\n"
432+
"subroutine test(a,b,&\n"
433+
" !! ford docu\n"
434+
" c, d, e)"
432435
)
433436
outstring = (" a = b\n"
434437
" !! ford docu\n"
435438
" b = c\n"
436-
" !! ford docu"
439+
" !! ford docu\n"
440+
" subroutine test(a, b, &\n"
441+
" !! ford docu\n"
442+
" c, d, e)"
437443
)
438444

439445
self.assert_fprettify_result([], instring, outstring)

0 commit comments

Comments
 (0)