Skip to content

Commit

Permalink
Merge pull request #8 from keyhr/master
Browse files Browse the repository at this point in the history
Release 0.1.0
  • Loading branch information
keyhr authored Jan 27, 2022
2 parents 625362f + 7c90de4 commit 88da8b6
Show file tree
Hide file tree
Showing 6 changed files with 209 additions and 9 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ build/
docs/build/

/.venv
/debug
/.vimspector.json
12 changes: 4 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@

<p align="center">
<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>
<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>
<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>
<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>
</p>

<h1 align="center">
c_formatter_42
</h1>

<br />

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

## What is c_formatter_42?
# c_formatter_42

It is Prettier for C in 42.
C language prettier that almost meets 42 norm.
I know you are already a good Human norm.
It's just for convenience.

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

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

```
"emeraldwalk.runonsave": {
Expand Down
47 changes: 47 additions & 0 deletions c_formatter_42/formatters/line_breaker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import re


def line_breaker(content: str, column_limit: int = 80) -> str:
lines = content.split("\n")
lines = list(map(lambda s: insert_break(s, column_limit), lines))
return "\n".join(lines)


def insert_break(line: str, column_limit: int) -> str:
if line_length(line) <= column_limit:
return line

line_indent_level = indent_level(line)
tabulation = "\t" * (line_indent_level + 1)

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

# join as many segments as it doesn't exceed line length limit
line = segments[0]
current_line_length = line_length(segments[0])
for segment in segments[1:]:
current_line_length += line_length(segment) + 1
if current_line_length > column_limit:
line = ("\n" + tabulation).join([line, segment])
current_line_length = line_length(tabulation + segment)
else:
line = " ".join([line, segment])

return line


def line_length(line: str) -> int:
line = line.expandtabs(4)
return len(line)


def indent_level(line: str) -> int:
last_tab_occurance = line.rfind("\t")
if last_tab_occurance < 0:
return 0

line = line[: (last_tab_occurance + 1)]
return int(len(line.expandtabs(4)) / 4)
2 changes: 2 additions & 0 deletions c_formatter_42/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
remove_multiline_condition_space,
insert_void,
)
from c_formatter_42.formatters.line_breaker import line_breaker


def run_all(content: str) -> str:
Expand All @@ -34,4 +35,5 @@ def run_all(content: str) -> str:
content = align(content)
content = return_type_single_tab(content)
content = insert_void(content)
content = line_breaker(content)
return content
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = c_formatter_42
version = 0.0.4
version = 0.1.0
description = formatting tool complient with 42 school's norm
long_description = file: README.md
long_description_content_type = text/markdown
Expand Down
153 changes: 153 additions & 0 deletions tests/formatters/test_line_breaker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
# from c_formatter_42.formatters.line_breaker import line_breaker, indent_level
from c_formatter_42.formatters.line_breaker import *

def test_line_indent_depth_basic_1():
input = """\
\t\t\tlooooooooooooooooooooooong = 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2
"""
assert 3 == indent_level(input)

def test_line_indent_depth_basic_2():
input = """\
looooooooooooooooooooooong = 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2
"""
assert 0 == indent_level(input)

def test_line_indent_depth_basic_3():
input = """\
\t\t\t + 2 + 2 + 2\t
"""
assert 7 == indent_level(input)


def test_insert_line_break_basic_1():
output = """\
\t\t\tlooooooooooooooooooooooong = 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2
\t\t\t\t+ 2 + 2 + 2;
"""
assert output == line_breaker("""\
\t\t\tlooooooooooooooooooooooong = 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2;
""")

def test_insert_line_break_basic_2():
output = """\
looooooooooooooooooooooong = 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2;
"""
assert output == line_breaker("""\
looooooooooooooooooooooong = 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2;
""")

def test_insert_line_break_basic_3():
output = """\
\t\t\t\treturn (fooooooooooooooooooooooooo(a, b, cccccccccccc,
\t\t\t\t\tddddddddddddd, eeeeeeeeeeeeeeee, fffffffffffffff,
\t\t\t\t\tgggggggggggg, hhhhhhhhhhhhhhhhhh));
"""
assert output == line_breaker("""\
\t\t\t\treturn (fooooooooooooooooooooooooo(a, b, cccccccccccc, ddddddddddddd, eeeeeeeeeeeeeeee, fffffffffffffff, gggggggggggg, hhhhhhhhhhhhhhhhhh));
""")

def test_insert_line_break_basic_4():
output = """\
void\t\tlooooooooooooooooooooooong = 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2
\t\t\t\t+ 2 + 2 + 2;
"""
assert output == line_breaker("""\
void\t\tlooooooooooooooooooooooong = 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2;
""")

def test_insert_line_break_basic_5():
output = """\
int\t\tlooooooooooooooooooooooong = 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2
\t\t\t+ 2 + 2;
"""
assert output == line_breaker("""\
int\t\tlooooooooooooooooooooooong = 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2;
""")

def test_insert_line_break_basic_6():
output = """\
int\tlooooooooooooooooooooooong = 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2
\t\t+ 2 + 2;
"""
assert output == line_breaker("""\
int\tlooooooooooooooooooooooong = 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2;
""")

def test_insert_line_break_basic_7():
output = """\
hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh hhhhhhhhhhhhhhhh
"""
assert output == line_breaker("""\
hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh hhhhhhhhhhhhhhhh
""")

def test_insert_line_break_basic_8():
output = """\
hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh hhhhhhhhhhhhhhh
"""
assert output == line_breaker("""\
hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh hhhhhhhhhhhhhhh
""")

def test_insert_line_break_basic_9():
output = """\
hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
\t+ hhhhhhhhhhhhhh
"""
assert output == line_breaker("""\
hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh + hhhhhhhhhhhhhh
""")

def test_insert_line_break_basic_10():
output = "aaaa\n\t+ b"
assert output == line_breaker("aaaa + b", 7)

def test_insert_line_break_basic_11():
output = "aaaa\n\t- b"
assert output == line_breaker("aaaa - b", 7)

def test_insert_line_break_basic_12():
output = "aaaa\n\t* b"
assert output == line_breaker("aaaa * b", 7)

def test_insert_line_break_basic_13():
output = "aaaa\n\t/ b"
assert output == line_breaker("aaaa / b", 7)

def test_insert_line_break_basic_14():
output = "aaaa\n\t% b"
assert output == line_breaker("aaaa % b", 7)

def test_insert_line_break_basic_15():
output = "aaaa\n\t+ *b"
assert output == line_breaker("aaaa + *b", 7)

def test_insert_line_break_basic_16():
output = "aaaa\n\t+ b*"
assert output == line_breaker("aaaa + b*", 7)

def test_insert_line_break_basic_17():
output = "aaaa\n\t* *b"
assert output == line_breaker("aaaa * *b", 7)

def test_insert_line_break_basic_18():
output = "aaaa\n\t* b*"
assert output == line_breaker("aaaa * b*", 7)

def test_insert_line_break_basic_19():
output = "aaaa*\n\t* b"
assert output == line_breaker("aaaa* * b", 7)

def test_insert_line_break_basic_20():
output = "*aaaa\n\t* b"
assert output == line_breaker("*aaaa * b", 7)

def test_insert_line_break_basic_21():
output = ",\n\taaaa *b"
assert output == line_breaker(", aaaa *b", 7)

def test_insert_line_break_basic_22():
output = ",\n\taaaa* b"
assert output == line_breaker(", aaaa* b", 7)

0 comments on commit 88da8b6

Please sign in to comment.