-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
07f3dd8
commit 1940188
Showing
3 changed files
with
142 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
name: Pretty | ||
|
||
on: | ||
push: | ||
|
||
pull_request: | ||
|
||
workflow_dispatch: | ||
|
||
jobs: | ||
docs: | ||
name: Code formatting | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- uses: actions/checkout@v3 | ||
|
||
- name: Count | ||
run: ./mfc.sh format diff |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
#!/usr/bin/env python3 | ||
import sys | ||
import os | ||
import filecmp | ||
|
||
def main(): | ||
num_args = len(sys.argv) | ||
if num_args != 2 or num_args !=3: | ||
AssertionError('Invalid number of arguments, found ', num_args) | ||
if num_args == 3 and str(sys.argv[2]) != "diff": | ||
AssertionError('Invalid third argument, ', sys.argv[2]) | ||
|
||
infile = str(sys.argv[1]) | ||
outfile = infile+".new" | ||
|
||
adjust_indentation(infile, outfile) | ||
if num_args == 2: | ||
print('replacing',infile,'with',outfile) | ||
os.replace(outfile,infile) | ||
if num_args == 3: | ||
same = filecmp.cmp(outfile,infile) | ||
if not same: | ||
print(infile) | ||
print('different!') | ||
# os.remove(outfile) | ||
|
||
def adjust_indentation(input_file, output_file): | ||
startingchar='!$acc' | ||
startingloop1='!$acc parallel loop' | ||
startingloop2='!$acc loop' | ||
max_empty_lines=4 | ||
|
||
with open(input_file, 'r') as file_in, open(output_file, 'w') as file_out: | ||
lines = file_in.readlines() | ||
|
||
# this makes sure !$acc lines that have line continuations get indented at proper level | ||
for kk in range(10): | ||
# loop through file | ||
for i in range(len(lines)): | ||
if lines[i].lstrip().startswith(startingchar) and i + 1 < len(lines): | ||
j = i + 1 | ||
empty_lines = 0 | ||
# look down to see how to indent a line | ||
while j < len(lines) and empty_lines < max_empty_lines: | ||
# if the following line starts with [end, else, contains], skip to looking up | ||
if lines[j].lstrip().startswith('end') or \ | ||
lines[j].lstrip().startswith('contains') or \ | ||
lines[j].lstrip().startswith('else'): | ||
empty_lines = max_empty_lines | ||
# skip empty lines | ||
elif lines[j].strip() == '': | ||
empty_lines += 1 | ||
# indent acc lines | ||
elif not lines[j].lstrip().startswith(startingchar): | ||
indent = len(lines[j]) - len(lines[j].lstrip()) | ||
lines[i] = ' ' * indent + lines[i].lstrip() | ||
break | ||
j += 1 | ||
# if looking down just finds empty lines, start looking up for indendation level | ||
if empty_lines == max_empty_lines: | ||
k = i - 1 | ||
while k >= 0: | ||
# if line above is not empty | ||
if lines[k].strip() != '': | ||
# if line 2 above ends with line continuation, indent at that level | ||
if lines[k-1].strip().endswith('&'): | ||
indent = len(lines[k-1]) - len(lines[k-1].lstrip()) | ||
# else indent at level of line above | ||
else: | ||
indent = len(lines[k]) - len(lines[k].lstrip()) | ||
lines[i] = ' ' * indent + lines[i].lstrip() | ||
break | ||
k -= 1 | ||
|
||
# remove empty lines following an acc loop directive | ||
i = 0 | ||
while i < len(lines): | ||
if (lines[i].lstrip().startswith(startingloop1) or \ | ||
lines[i].lstrip().startswith(startingloop2)) and \ | ||
i+1 < len(lines) and lines[i+1].strip() == '': | ||
file_out.write(lines[i]) | ||
i +=2 | ||
else: | ||
file_out.write(lines[i]) | ||
i += 1 | ||
|
||
if __name__ == "__main__": | ||
main() |