Skip to content

Commit f137370

Browse files
committed
Merge pull request #14 from keyhr/master
Fixed line break inserter
2 parents aa2f0c6 + 47e95e2 commit f137370

File tree

3 files changed

+40
-8
lines changed

3 files changed

+40
-8
lines changed

c_formatter_42/formatters/line_breaker.py

+17-3
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ def insert_break(line: str, column_limit: int) -> str:
1313
if line_length(line) <= column_limit:
1414
return line
1515

16-
# break at all breakable spaces (space after comma or space before binary operators)
17-
breakable_space_pattern = r"((?<=,) | (?=[+\-*/%])(?!\*+\S|\+\+|\-\-))"
16+
# break at all breakable spaces (space after comma or space before binary
17+
# operators or logical AND or OR)
18+
breakable_space_pattern = r"((?<=,) | (?=[+\-*/%]|\|\||&&)(?!\*+\S|\+\+|\-\-))"
1819
line = re.sub(breakable_space_pattern, "\n", line)
1920
segments = line.split("\n")
2021

@@ -54,7 +55,8 @@ def insert_break(line: str, column_limit: int) -> str:
5455
def additional_indent_level(s: str) -> int:
5556
additional_indent_level = 1
5657

57-
discount_pattern = r"(^\t*{type}\t+.*?[a-zA-Z0-9_]\()|(^\t*typedef)|(^\t*(if|while))"
58+
discount_pattern = r"(^\t*{type}\t+.*?[a-zA-Z0-9_]\((?!.*?;))|(^\t*typedef)" \
59+
"|(^\t*(if|while|return))"
5860
discount_pattern = discount_pattern.format(
5961
type=helper.REGEX_TYPE,
6062
)
@@ -83,4 +85,16 @@ def line_length(line: str) -> int:
8385

8486

8587
def indent_level(line: str) -> int:
88+
# an exeptional rule for function declaration
89+
align_pattern = r"^(static\s+)?{type}\s+{name}\((.|\s)*?\);"
90+
align_pattern = align_pattern.format(
91+
type=helper.REGEX_TYPE,
92+
name=helper.REGEX_NAME
93+
)
94+
if re.match(align_pattern, line):
95+
last_tab_index = line.rfind("\t")
96+
if last_tab_index == -1:
97+
return 0
98+
return int(len(line[:last_tab_index + 1].expandtabs(4)) / 4)
99+
86100
return line.count("\t")

setup.cfg

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[metadata]
22
name = c_formatter_42
3-
version = 0.1.3.4
3+
version = 0.1.4
44
description = formatting tool complient with 42 school's norm
55
long_description = file: README.md
66
long_description_content_type = text/markdown

tests/formatters/test_line_breaker.py

+22-4
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,24 @@ def test_line_indent_depth_basic_3():
1919
"""
2020
assert 4 == indent_level(input)
2121

22+
def test_line_indent_depth_basic_4():
23+
input = """\
24+
static\tint\t\ttest();
25+
"""
26+
assert 4 == indent_level(input)
27+
28+
def test_line_indent_depth_basic_5():
29+
input = """\
30+
static void\tst_merge_fields_in_curr(char *strs[3], t_tok_lst **curr, t_tok_lst *fields);
31+
"""
32+
assert 3 == indent_level(input)
33+
34+
def test_additional_indent_level_1():
35+
input = """\
36+
static void\tst_merge_fields_in_curr(char *strs[3], t_tok_lst **curr, t_tok_lst *fields);
37+
"""
38+
assert 1 == additional_indent_level(input)
39+
2240

2341
def test_insert_line_break_basic_1():
2442
output = """\
@@ -40,8 +58,8 @@ def test_insert_line_break_basic_2():
4058
def test_insert_line_break_basic_3():
4159
output = """\
4260
\t\t\t\treturn (fooooooooooooooooooooooooo(a, b, cccccccccccc,
43-
\t\t\t\t\t\t\tddddddddddddd, eeeeeeeeeeeeeeee, fffffffffffffff,
44-
\t\t\t\t\t\t\tgggggggggggg, hhhhhhhhhhhhhhhhhh));
61+
\t\t\t\t\t\tddddddddddddd, eeeeeeeeeeeeeeee, fffffffffffffff,
62+
\t\t\t\t\t\tgggggggggggg, hhhhhhhhhhhhhhhhhh));
4563
"""
4664
assert output == line_breaker("""\
4765
\t\t\t\treturn (fooooooooooooooooooooooooo(a, b, cccccccccccc, ddddddddddddd, eeeeeeeeeeeeeeee, fffffffffffffff, gggggggggggg, hhhhhhhhhhhhhhhhhh));
@@ -158,11 +176,11 @@ def test_insert_line_break_basic_23():
158176

159177
def test_insert_line_break_long_function_declaration():
160178
input = """
161-
static void\tst_merge_fields_in_curr(char *strs[3], t_tok_lst **curr, t_tok_lst *fields)
179+
static void\tst_merge_fields_in_curr(char *strs[3], t_tok_lst **curr, t_tok_lst *fields);
162180
"""
163181
output = """
164182
static void\tst_merge_fields_in_curr(char *strs[3], t_tok_lst **curr,
165-
\t\tt_tok_lst *fields)
183+
\t\t\t\tt_tok_lst *fields);
166184
"""
167185
assert line_breaker(input) == output
168186

0 commit comments

Comments
 (0)