@@ -95,6 +95,10 @@ def is_block_valid(self, block):
95
95
def convert_sections (self , block ):
96
96
return [self .format_module (block )]
97
97
98
+ def needs_new_header (self , file_contents ):
99
+ return (re .search (r'^\/\/\/ \\file$' , file_contents , flags = re .MULTILINE )
100
+ is None )
101
+
98
102
99
103
class FunctionFormatter (GenericFormatter ):
100
104
def __init__ (self , doc_width ):
@@ -188,6 +192,7 @@ def convert_sections(self, block):
188
192
189
193
def replace_block (
190
194
block_contents ,
195
+ file_contents ,
191
196
file ,
192
197
header_formatter ,
193
198
class_formatter ,
@@ -199,9 +204,12 @@ def replace_block(
199
204
{f .name : f .contents for f in parse_fields (block_contents .group (1 ))})
200
205
201
206
if header_formatter .is_block_valid (block ):
202
- return '%s%s\n ' % (
203
- block_contents .group (0 ),
204
- header_formatter .convert (header_from_block (block )))
207
+ converted = header_formatter .convert (header_from_block (block ))
208
+ if header_formatter .needs_new_header (file_contents ) and converted :
209
+ return '%s%s' % (
210
+ block_contents .group (0 ),
211
+ header_formatter .convert (header_from_block (block )) + '\n ' )
212
+ return block_contents .group (0 )
205
213
206
214
if class_formatter .is_block_valid (block ):
207
215
return class_formatter .convert (class_from_block (block ))
@@ -231,6 +239,7 @@ def convert_file(file, inplace):
231
239
new_contents = block_re .sub (
232
240
lambda match : replace_block (
233
241
match ,
242
+ contents ,
234
243
file ,
235
244
header_formatter ,
236
245
class_formatter ,
0 commit comments