@@ -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
12331244def 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
0 commit comments