Skip to content

Commit

Permalink
Merge pull request #63 from younesaassila/fix/too_few_tab
Browse files Browse the repository at this point in the history
Fix #62
  • Loading branch information
cacharle authored Aug 31, 2023
2 parents 5784dae + b20ce13 commit 32d3437
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
21 changes: 18 additions & 3 deletions c_formatter_42/formatters/line_breaker.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,17 @@ def insert_break(line: str, column_limit: int) -> str:
segments = line.split("\n")

line_indent_level = indent_level(line)
nest_indent_level = additional_nest_indent_level(line)

# 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:
tabulation = "\t" * (line_indent_level + additional_indent_level(line))
tabulation = "\t" * (
line_indent_level + additional_indent_level(line, nest_indent_level)
)
line = ("\n" + tabulation).join([line, segment])
current_line_length = line_length(tabulation + segment)
else:
Expand Down Expand Up @@ -55,7 +58,7 @@ def insert_break(line: str, column_limit: int) -> str:
# (foo(bar()
# > > * baz())) Next line should be indented with 2 tabs (paren depth is 2)
# -----------------------------------------------------------------------------------
def additional_indent_level(s: str) -> int:
def additional_indent_level(s: str, nest_indent_level: int = 0) -> int:
paren_depth = 0
is_surrounded_sq = False
is_surrounded_dq = False
Expand All @@ -69,7 +72,18 @@ def additional_indent_level(s: str) -> int:
elif c == ")" and not is_surrounded_sq and not is_surrounded_dq:
paren_depth -= 1

return max(1, paren_depth) # 1 is the default additional indent level
if paren_depth > 0:
return nest_indent_level + paren_depth
else:
return 1 # 1 is the default additional indent level


def additional_nest_indent_level(line: str) -> int:
# An exceptional rule for variable assignment
# https://github.com/42School/norminette/blob/921b5e22d991591f385e1920f7e7ee5dcf71f3d5/norminette/rules/check_assignation_indent.py#L59
align_pattern = r"^\s*({decl})((\.|->){decl})*\s+=\s+[^;]*?;$"
align_pattern = align_pattern.format(decl=helper.REGEX_DECL_NAME)
return 1 if re.match(align_pattern, line) is not None else 0


def line_length(line: str) -> int:
Expand All @@ -79,6 +93,7 @@ def line_length(line: str) -> int:

def indent_level(line: str) -> int:
# An exceptional rule for function declaration
# https://github.com/42School/norminette/blob/921b5e22d991591f385e1920f7e7ee5dcf71f3d5/norminette/rules/check_assignation_indent.py#L61
align_pattern = r"^(static\s+)?{type}\s+{name}\((.|\s)*?\);"
align_pattern = align_pattern.format(type=helper.REGEX_TYPE, name=helper.REGEX_NAME)
if re.match(align_pattern, line):
Expand Down
10 changes: 10 additions & 0 deletions tests/formatters/test_line_breaker.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,16 @@ def test_insert_line_break_basic_23():
assert output == line_breaker("foooooo(bar * baz)", 7)


def test_insert_line_break_basic_24():
output = "*foo = foooooo(bar\n\t\t* baz);"
assert output == line_breaker("*foo = foooooo(bar * baz);", 18)


def test_insert_line_break_basic_25():
output = "foo[0] = foooooo(bar\n\t\t* baz);"
assert output == line_breaker("foo[0] = foooooo(bar * baz);", 20)


def test_insert_line_break_long_function_declaration():
input = """
static void\tst_merge_fields_in_curr(char *strs[3], t_tok_lst **curr, t_tok_lst *fields);
Expand Down

0 comments on commit 32d3437

Please sign in to comment.