forked from dawnbeen/c_formatter_42
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from keyhr/master
Release 0.1.0
- Loading branch information
Showing
6 changed files
with
209 additions
and
9 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 |
---|---|---|
|
@@ -7,3 +7,5 @@ build/ | |
docs/build/ | ||
|
||
/.venv | ||
/debug | ||
/.vimspector.json |
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,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) |
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
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,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) | ||
|