Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ugly fix for #153 #154

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions fprettify/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -899,6 +899,8 @@ def replace_relational_single_fline(f_line, cstyle):
.ne. <--> /=
"""

# keep track of whether the line may be affected
line_affected = False
new_line = f_line

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

new_line = ''.join(line_parts)
line_affected = True

return new_line
return new_line, line_affected


def replace_relational_lines(lines, cstyle):
"""
format a list of lines - replaces scalar relational
operators in logical expressions to either Fortran or C-style
by calling original function for single line.
"""

new_lines = []

for f_line in lines:
new_line, changed = replace_relational_single_fline(f_line, cstyle)
new_lines.append(new_line)

return new_lines


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

if impose_replacements:
f_line = replace_relational_single_fline(f_line, cstyle)
f_line, relation_found = replace_relational_single_fline(f_line, cstyle)
if relation_found:
updated_lines = replace_relational_lines(lines, cstyle)
linebreak_pos = get_linebreak_pos(updated_lines, filter_fypp=not indent_fypp)

if impose_case:
f_line = replace_keywords_single_fline(f_line, case_dict)
Expand Down
6 changes: 3 additions & 3 deletions fprettify/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ def test_relation_replacement(self):
"'==== heading",
"if (vtk%my_rank .eq. 0) write (vtk%filehandle_par, '(\"<DataArray",
"'(\"</Collection>\",",
"if (abc(1) .lt. -bca .or. &\n qwe .gt. ewq) then"]
"if (abc(1)<=-bca .or. &\n qwe .gt. ewq) then"]
f_outstring = ["if (min .lt. max .and. min .lt. thres)",
"if (min .gt. max .and. min .gt. thres)",
"if (min .eq. max .and. min .eq. thres)",
Expand All @@ -391,7 +391,7 @@ def test_relation_replacement(self):
"'==== heading",
"if (vtk%my_rank .eq. 0) write (vtk%filehandle_par, '(\"<DataArray",
"'(\"</Collection>\",",
"if (abc(1) .lt. -bca .or. &\n qwe .gt. ewq) then"]
"if (abc(1) .le. -bca .or. &\n qwe .gt. ewq) then"]
c_outstring = ["if (min < max .and. min < thres)",
"if (min > max .and. min > thres)",
"if (min == max .and. min == thres)",
Expand All @@ -401,7 +401,7 @@ def test_relation_replacement(self):
"'==== heading",
"if (vtk%my_rank == 0) write (vtk%filehandle_par, '(\"<DataArray",
"'(\"</Collection>\",",
"if (abc(1) < -bca .or. &\n qwe > ewq) then"]
"if (abc(1) <= -bca .or. &\n qwe > ewq) then"]
for i in range(0, len(instring)):
self.assert_fprettify_result(['--enable-replacements', '--c-relations'], instring[i], c_outstring[i])
self.assert_fprettify_result(['--enable-replacements'], instring[i], f_outstring[i])
Expand Down