Skip to content

Commit 88da8b6

Browse files
authored
Merge pull request #8 from keyhr/master
Release 0.1.0
2 parents 625362f + 7c90de4 commit 88da8b6

File tree

6 files changed

+209
-9
lines changed

6 files changed

+209
-9
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,5 @@ build/
77
docs/build/
88

99
/.venv
10+
/debug
11+
/.vimspector.json

README.md

+4-8
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,19 @@
1-
21
<p align="center">
32
<a style="text-decoration:none" href="https://badge.fury.io/py/c-formatter-42"><img src="https://badge.fury.io/py/c-formatter-42.svg" alt="PyPI version" height="20"></a>
43
<a style="text-decoration:none" href="https://github.com/cacharle/c_formatter_42/actions"><img src="https://github.com/cacharle/c_formatter_42/actions/workflows/python-package.yml/badge.svg" height="20"></a>
54
<a style="text-decoration:none" href="https://github.com/cacharle/c_formatter_42/actions"><img src="https://github.com/cacharle/c_formatter_42/actions/workflows/python-publish.yml/badge.svg" height="20"></a>
65
<a style="text-decoration:none" href="https://pypi.org/project/c-formatter-42/"><img src="https://img.shields.io/pypi/pyversions/c-formatter-42" height="20"></a>
76
</p>
87

9-
<h1 align="center">
10-
c_formatter_42
11-
</h1>
12-
8+
<br />
139

1410
<p align="center">
1511
<img width="65%" align="center" src="./Img/final_back.png">
1612
</p>
1713

18-
## What is c_formatter_42?
14+
# c_formatter_42
1915

20-
It is Prettier for C in 42.
16+
C language prettier that almost meets 42 norm.
2117
I know you are already a good Human norm.
2218
It's just for convenience.
2319

@@ -49,7 +45,7 @@ Checkout [c_formatter_42.vim](https://github.com/cacharle/c_formatter_42.vim) pl
4945
### VSCode
5046

5147
1. Install [emeraldwalk.runonsave](https://marketplace.visualstudio.com/items?itemName=emeraldwalk.RunOnSave) extension.
52-
2. Add Configuration to vscode. (We recommend you to put it in `Workspace Preference`)
48+
2. Add Configuration to format with c_formatter_42 on save to vscode. (We recommend you to put it in `Workspace Preference`)
5349

5450
```
5551
"emeraldwalk.runonsave": {
+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import re
2+
3+
4+
def line_breaker(content: str, column_limit: int = 80) -> str:
5+
lines = content.split("\n")
6+
lines = list(map(lambda s: insert_break(s, column_limit), lines))
7+
return "\n".join(lines)
8+
9+
10+
def insert_break(line: str, column_limit: int) -> str:
11+
if line_length(line) <= column_limit:
12+
return line
13+
14+
line_indent_level = indent_level(line)
15+
tabulation = "\t" * (line_indent_level + 1)
16+
17+
# break at all breakable spaces (space after comma or space before binary operators)
18+
breakable_space_pattern = r"((?<=,) | (?=[+\-*/%])(?!\*+\S|\+\+|\-\-))"
19+
line = re.sub(breakable_space_pattern, "\n", line)
20+
segments = line.split("\n")
21+
22+
# join as many segments as it doesn't exceed line length limit
23+
line = segments[0]
24+
current_line_length = line_length(segments[0])
25+
for segment in segments[1:]:
26+
current_line_length += line_length(segment) + 1
27+
if current_line_length > column_limit:
28+
line = ("\n" + tabulation).join([line, segment])
29+
current_line_length = line_length(tabulation + segment)
30+
else:
31+
line = " ".join([line, segment])
32+
33+
return line
34+
35+
36+
def line_length(line: str) -> int:
37+
line = line.expandtabs(4)
38+
return len(line)
39+
40+
41+
def indent_level(line: str) -> int:
42+
last_tab_occurance = line.rfind("\t")
43+
if last_tab_occurance < 0:
44+
return 0
45+
46+
line = line[: (last_tab_occurance + 1)]
47+
return int(len(line.expandtabs(4)) / 4)

c_formatter_42/run.py

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
remove_multiline_condition_space,
2222
insert_void,
2323
)
24+
from c_formatter_42.formatters.line_breaker import line_breaker
2425

2526

2627
def run_all(content: str) -> str:
@@ -34,4 +35,5 @@ def run_all(content: str) -> str:
3435
content = align(content)
3536
content = return_type_single_tab(content)
3637
content = insert_void(content)
38+
content = line_breaker(content)
3739
return content

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.0.4
3+
version = 0.1.0
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

+153
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
# from c_formatter_42.formatters.line_breaker import line_breaker, indent_level
2+
from c_formatter_42.formatters.line_breaker import *
3+
4+
def test_line_indent_depth_basic_1():
5+
input = """\
6+
\t\t\tlooooooooooooooooooooooong = 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2
7+
"""
8+
assert 3 == indent_level(input)
9+
10+
def test_line_indent_depth_basic_2():
11+
input = """\
12+
looooooooooooooooooooooong = 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2
13+
"""
14+
assert 0 == indent_level(input)
15+
16+
def test_line_indent_depth_basic_3():
17+
input = """\
18+
\t\t\t + 2 + 2 + 2\t
19+
"""
20+
assert 7 == indent_level(input)
21+
22+
23+
def test_insert_line_break_basic_1():
24+
output = """\
25+
\t\t\tlooooooooooooooooooooooong = 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2
26+
\t\t\t\t+ 2 + 2 + 2;
27+
"""
28+
assert output == line_breaker("""\
29+
\t\t\tlooooooooooooooooooooooong = 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2;
30+
""")
31+
32+
def test_insert_line_break_basic_2():
33+
output = """\
34+
looooooooooooooooooooooong = 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2;
35+
"""
36+
assert output == line_breaker("""\
37+
looooooooooooooooooooooong = 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2;
38+
""")
39+
40+
def test_insert_line_break_basic_3():
41+
output = """\
42+
\t\t\t\treturn (fooooooooooooooooooooooooo(a, b, cccccccccccc,
43+
\t\t\t\t\tddddddddddddd, eeeeeeeeeeeeeeee, fffffffffffffff,
44+
\t\t\t\t\tgggggggggggg, hhhhhhhhhhhhhhhhhh));
45+
"""
46+
assert output == line_breaker("""\
47+
\t\t\t\treturn (fooooooooooooooooooooooooo(a, b, cccccccccccc, ddddddddddddd, eeeeeeeeeeeeeeee, fffffffffffffff, gggggggggggg, hhhhhhhhhhhhhhhhhh));
48+
""")
49+
50+
def test_insert_line_break_basic_4():
51+
output = """\
52+
void\t\tlooooooooooooooooooooooong = 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2
53+
\t\t\t\t+ 2 + 2 + 2;
54+
"""
55+
assert output == line_breaker("""\
56+
void\t\tlooooooooooooooooooooooong = 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2;
57+
""")
58+
59+
def test_insert_line_break_basic_5():
60+
output = """\
61+
int\t\tlooooooooooooooooooooooong = 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2
62+
\t\t\t+ 2 + 2;
63+
"""
64+
assert output == line_breaker("""\
65+
int\t\tlooooooooooooooooooooooong = 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2;
66+
""")
67+
68+
def test_insert_line_break_basic_6():
69+
output = """\
70+
int\tlooooooooooooooooooooooong = 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2
71+
\t\t+ 2 + 2;
72+
"""
73+
assert output == line_breaker("""\
74+
int\tlooooooooooooooooooooooong = 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2;
75+
""")
76+
77+
def test_insert_line_break_basic_7():
78+
output = """\
79+
hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh hhhhhhhhhhhhhhhh
80+
"""
81+
assert output == line_breaker("""\
82+
hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh hhhhhhhhhhhhhhhh
83+
""")
84+
85+
def test_insert_line_break_basic_8():
86+
output = """\
87+
hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh hhhhhhhhhhhhhhh
88+
"""
89+
assert output == line_breaker("""\
90+
hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh hhhhhhhhhhhhhhh
91+
""")
92+
93+
def test_insert_line_break_basic_9():
94+
output = """\
95+
hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
96+
\t+ hhhhhhhhhhhhhh
97+
"""
98+
assert output == line_breaker("""\
99+
hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh + hhhhhhhhhhhhhh
100+
""")
101+
102+
def test_insert_line_break_basic_10():
103+
output = "aaaa\n\t+ b"
104+
assert output == line_breaker("aaaa + b", 7)
105+
106+
def test_insert_line_break_basic_11():
107+
output = "aaaa\n\t- b"
108+
assert output == line_breaker("aaaa - b", 7)
109+
110+
def test_insert_line_break_basic_12():
111+
output = "aaaa\n\t* b"
112+
assert output == line_breaker("aaaa * b", 7)
113+
114+
def test_insert_line_break_basic_13():
115+
output = "aaaa\n\t/ b"
116+
assert output == line_breaker("aaaa / b", 7)
117+
118+
def test_insert_line_break_basic_14():
119+
output = "aaaa\n\t% b"
120+
assert output == line_breaker("aaaa % b", 7)
121+
122+
def test_insert_line_break_basic_15():
123+
output = "aaaa\n\t+ *b"
124+
assert output == line_breaker("aaaa + *b", 7)
125+
126+
def test_insert_line_break_basic_16():
127+
output = "aaaa\n\t+ b*"
128+
assert output == line_breaker("aaaa + b*", 7)
129+
130+
def test_insert_line_break_basic_17():
131+
output = "aaaa\n\t* *b"
132+
assert output == line_breaker("aaaa * *b", 7)
133+
134+
def test_insert_line_break_basic_18():
135+
output = "aaaa\n\t* b*"
136+
assert output == line_breaker("aaaa * b*", 7)
137+
138+
def test_insert_line_break_basic_19():
139+
output = "aaaa*\n\t* b"
140+
assert output == line_breaker("aaaa* * b", 7)
141+
142+
def test_insert_line_break_basic_20():
143+
output = "*aaaa\n\t* b"
144+
assert output == line_breaker("*aaaa * b", 7)
145+
146+
def test_insert_line_break_basic_21():
147+
output = ",\n\taaaa *b"
148+
assert output == line_breaker(", aaaa *b", 7)
149+
150+
def test_insert_line_break_basic_22():
151+
output = ",\n\taaaa* b"
152+
assert output == line_breaker(", aaaa* b", 7)
153+

0 commit comments

Comments
 (0)