Skip to content

Commit 6c299b2

Browse files
committed
Improved FORD compatibility
Don't indent comments starting with `!!` to allow for FORD documentation formatted as described in https://github.com/Fortran-FOSS-Programmers/ford/wiki/Writing-Documentation
1 parent 2b2801b commit 6c299b2

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

fprettify/__init__.py

Lines changed: 8 additions & 5 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 = preprocess_line(
1064+
lines, do_format, prev_indent, is_blank, is_fypp, is_ford = preprocess_line(
10651065
f_line, lines, comments, orig_filename, stream.line_nr)
10661066

1067-
if is_fypp:
1067+
if is_fypp or is_ford:
10681068
indent_special = 3
10691069

10701070
if prev_indent and indent_special == 0:
@@ -1209,22 +1209,25 @@ def preprocess_line(f_line, lines, comments, filename, line_nr):
12091209
prev_indent = False
12101210
do_format = False
12111211
is_fypp = False
1212+
is_ford = False
12121213

12131214
if EMPTY_RE.search(f_line): # empty lines including comment lines
12141215
if any(comments):
12151216
if lines[0].startswith(' '):
12161217
# indent comment lines only if they were not indented before.
12171218
prev_indent = True
1218-
is_fypp = FYPP_LINE_RE.search(lines[0].lstrip())
1219+
line_strip = lines[0].lstrip()
1220+
is_fypp = FYPP_LINE_RE.search(line_strip)
1221+
is_ford = line_strip.startswith('!!')
12191222
else:
12201223
is_blank = True
1221-
if not is_fypp:
1224+
if not is_fypp and not is_ford:
12221225
lines = [l.strip(' ') for l in lines]
12231226

12241227
else:
12251228
do_format = True
12261229

1227-
return [lines, do_format, prev_indent, is_blank, is_fypp]
1230+
return [lines, do_format, prev_indent, is_blank, is_fypp, is_ford]
12281231

12291232

12301233
def pass_defaults_to_next_line(f_line):

fprettify/tests/__init__.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ def assert_fprettify_result(self, args, instring, outstring_exp):
246246
p1 = subprocess.Popen(
247247
args, stdout=subprocess.PIPE, stdin=subprocess.PIPE)
248248
outstring = p1.communicate(instring.encode(
249-
'UTF-8'))[0].decode('UTF-8').strip()
249+
'UTF-8'))[0].decode('UTF-8').rstrip()
250250
self.assertEqual(outstring_exp, outstring)
251251

252252
def test_io(self):
@@ -423,6 +423,20 @@ def test_omp(self):
423423

424424
self.assert_fprettify_result([], instring, outstring)
425425

426+
def test_ford(self):
427+
"""test formatting of ford comments"""
428+
instring = (" a = b\n"
429+
" !! ford docu\n"
430+
"b=c\n"
431+
" !! ford docu"
432+
)
433+
outstring = (" a = b\n"
434+
" !! ford docu\n"
435+
" b = c\n"
436+
" !! ford docu"
437+
)
438+
439+
self.assert_fprettify_result([], instring, outstring)
426440

427441
def addtestmethod(testcase, fpath, ffile):
428442
"""add a test method for each example."""

0 commit comments

Comments
 (0)