Skip to content

Commit 9107ab6

Browse files
committed
Ugly fix for fortran-lang#153
This is a sort of brute-force workaround to avoid fortran-lang#153. Likely not up to the standard of `fprettify` but 'works-for-me'. Test amended to cover the corner case, I think.
1 parent b0ec764 commit 9107ab6

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed

Diff for: fprettify/__init__.py

+24-2
Original file line numberDiff line numberDiff line change
@@ -899,6 +899,8 @@ def replace_relational_single_fline(f_line, cstyle):
899899
.ne. <--> /=
900900
"""
901901

902+
# keep track of whether the line may be affected
903+
line_affected = False
902904
new_line = f_line
903905

904906
# only act on lines that do contain a relation
@@ -942,8 +944,25 @@ def replace_relational_single_fline(f_line, cstyle):
942944
line_parts[pos] = part
943945

944946
new_line = ''.join(line_parts)
947+
line_affected = True
945948

946-
return new_line
949+
return new_line, line_affected
950+
951+
952+
def replace_relational_lines(lines, cstyle):
953+
"""
954+
format a list of lines - replaces scalar relational
955+
operators in logical expressions to either Fortran or C-style
956+
by calling original function for single line.
957+
"""
958+
959+
new_lines = []
960+
961+
for f_line in lines:
962+
new_line, changed = replace_relational_single_fline(f_line, cstyle)
963+
new_lines.append(new_line)
964+
965+
return new_lines
947966

948967

949968
def replace_keywords_single_fline(f_line, case_dict):
@@ -1559,7 +1578,10 @@ def reformat_ffile_combined(infile, outfile, impose_indent=True, indent_size=3,
15591578
f_line = f_line.strip(' ')
15601579

15611580
if impose_replacements:
1562-
f_line = replace_relational_single_fline(f_line, cstyle)
1581+
f_line, relation_found = replace_relational_single_fline(f_line, cstyle)
1582+
if relation_found:
1583+
updated_lines = replace_relational_lines(lines, cstyle)
1584+
linebreak_pos = get_linebreak_pos(updated_lines, filter_fypp=not indent_fypp)
15631585

15641586
if impose_case:
15651587
f_line = replace_keywords_single_fline(f_line, case_dict)

Diff for: fprettify/tests/__init__.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ def test_relation_replacement(self):
381381
"'==== heading",
382382
"if (vtk%my_rank .eq. 0) write (vtk%filehandle_par, '(\"<DataArray",
383383
"'(\"</Collection>\",",
384-
"if (abc(1) .lt. -bca .or. &\n qwe .gt. ewq) then"]
384+
"if (abc(1)<=-bca .or. &\n qwe .gt. ewq) then"]
385385
f_outstring = ["if (min .lt. max .and. min .lt. thres)",
386386
"if (min .gt. max .and. min .gt. thres)",
387387
"if (min .eq. max .and. min .eq. thres)",
@@ -391,7 +391,7 @@ def test_relation_replacement(self):
391391
"'==== heading",
392392
"if (vtk%my_rank .eq. 0) write (vtk%filehandle_par, '(\"<DataArray",
393393
"'(\"</Collection>\",",
394-
"if (abc(1) .lt. -bca .or. &\n qwe .gt. ewq) then"]
394+
"if (abc(1) .le. -bca .or. &\n qwe .gt. ewq) then"]
395395
c_outstring = ["if (min < max .and. min < thres)",
396396
"if (min > max .and. min > thres)",
397397
"if (min == max .and. min == thres)",
@@ -401,7 +401,7 @@ def test_relation_replacement(self):
401401
"'==== heading",
402402
"if (vtk%my_rank == 0) write (vtk%filehandle_par, '(\"<DataArray",
403403
"'(\"</Collection>\",",
404-
"if (abc(1) < -bca .or. &\n qwe > ewq) then"]
404+
"if (abc(1) <= -bca .or. &\n qwe > ewq) then"]
405405
for i in range(0, len(instring)):
406406
self.assert_fprettify_result(['--enable-replacements', '--c-relations'], instring[i], c_outstring[i])
407407
self.assert_fprettify_result(['--enable-replacements'], instring[i], f_outstring[i])

0 commit comments

Comments
 (0)