Skip to content

Commit 0dda345

Browse files
authored
Merge pull request #25 from PaulXiCao/master
added an option for whitespaces: en-/disable whitespace around type selector %
2 parents 1f40ece + 9f465ca commit 0dda345

File tree

2 files changed

+31
-5
lines changed

2 files changed

+31
-5
lines changed

fprettify/__init__.py

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -606,15 +606,18 @@ def format_single_fline(f_line, whitespace, linebreak_pos, ampersand_sep,
606606
# 4: arithm. operators plus and minus
607607
# 5: arithm. operators multiply and divide
608608
# 6: print / read statements
609+
# 7: select type components
609610

610611
if whitespace == 0:
611-
spacey = [0, 0, 0, 0, 0, 0, 0]
612+
spacey = [0, 0, 0, 0, 0, 0, 0, 0]
612613
elif whitespace == 1:
613-
spacey = [1, 1, 1, 1, 0, 0, 1]
614+
spacey = [1, 1, 1, 1, 0, 0, 1, 0]
614615
elif whitespace == 2:
615-
spacey = [1, 1, 1, 1, 1, 0, 1]
616+
spacey = [1, 1, 1, 1, 1, 0, 1, 0]
616617
elif whitespace == 3:
617-
spacey = [1, 1, 1, 1, 1, 1, 1]
618+
spacey = [1, 1, 1, 1, 1, 1, 1, 0]
619+
elif whitespace == 4:
620+
spacey = [1, 1, 1, 1, 1, 1, 1, 1]
618621
else:
619622
raise NotImplementedError("unknown value for whitespace")
620623

@@ -744,6 +747,17 @@ def add_whitespace_charwise(line, spacey, filename, line_nr):
744747
spacey[0] + rhs.lstrip(' ')
745748
line_ftd = line_ftd.rstrip(' ')
746749

750+
# format type selector %
751+
if char == "%":
752+
lhs = line_ftd[:pos + offset]
753+
rhs = line_ftd[pos + 1 + offset:]
754+
line_ftd = lhs.rstrip(' ') \
755+
+ ' ' * spacey[7] \
756+
+ char \
757+
+ ' ' * spacey[7] \
758+
+ rhs.lstrip(' ')
759+
line_ftd = line_ftd.rstrip(' ')
760+
747761
# format .NOT.
748762
if re.search(r"^\.NOT\.", line[pos:pos + 5], RE_FLAGS):
749763
lhs = line_ftd[:pos + offset]
@@ -1318,7 +1332,12 @@ def run(argv=sys.argv): # pragma: no cover
13181332
parser.add_argument("-i", "--indent", type=int, default=3,
13191333
help="relative indentation width")
13201334
parser.add_argument("-w", "--whitespace", type=int,
1321-
choices=range(0, 4), default=2, help="Amount of whitespace")
1335+
choices=range(0, 5), default=2, help="Amount of whitespace - "
1336+
" 0: minimal whitespace"
1337+
" | 1: operators (except arithmetic), print/read"
1338+
" | 2: operators, print/read, plus/minus"
1339+
" | 3: operators, print/read, plus/minus, muliply/divide"
1340+
" | 4: operators, print/read, plus/minus, muliply/divide, type component selector")
13221341
parser.add_argument("--disable-indent", action='store_true', default=False, help="don't impose indentation")
13231342
parser.add_argument("--disable-whitespace", action='store_true', default=False, help="don't impose whitespace formatting")
13241343
parser.add_argument("--strip-comments", action='store_true', default=False, help="strip whitespaces before comments")

fprettify/tests/__init__.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,13 @@ def test_whitespace(self):
149149
args = ['-w', str(w)]
150150
self.assert_fprettify_result(args, instring, out)
151151

152+
def test_type_selector(self):
153+
"""test for whitespace formatting option -w 4"""
154+
instring = "A%component=func(mytype%a,mytype%abc+mytype%abcd)"
155+
outstring_exp = "A % component = func(mytype % a, mytype % abc + mytype % abcd)"
156+
157+
self.assert_fprettify_result(['-w 4'], instring, outstring_exp)
158+
152159
def test_indent(self):
153160
"""simple test for indent options -i in [0, 3, 4]"""
154161

0 commit comments

Comments
 (0)