Skip to content

Commit 4df615d

Browse files
authored
Merge pull request #87 from olehermanse/improve
Small refactorings
2 parents 48fbd8d + 291498f commit 4df615d

2 files changed

Lines changed: 13 additions & 372 deletions

File tree

src/cfengine_cli/format.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ def maybe_split_rval(
280280
# ---------------------------------------------------------------------------
281281

282282

283-
def attempt_split_attribute(node: Node, indent: int, line_length: int) -> list[str]:
283+
def _attempt_split_attribute(node: Node, indent: int, line_length: int) -> list[str]:
284284
"""Split an attribute node, wrapping the rval if it's a list or call."""
285285
assert len(node.children) >= 3 # lval + arrow + rval + optionally comments
286286

@@ -311,15 +311,15 @@ def attempt_split_attribute(node: Node, indent: int, line_length: int) -> list[s
311311
return comment_lines + [" " * indent + stringify_single_line_node(node)]
312312

313313

314-
def stringify(node: Node, indent: int, line_length: int) -> list[str]:
314+
def _stringify(node: Node, indent: int, line_length: int) -> list[str]:
315315
"""Return a node as pre-indented line(s), splitting if it exceeds line_length."""
316316
single_line = " " * indent + stringify_single_line_node(node)
317317
# Reserve 1 char for trailing ; or , after attributes
318318
effective_length = line_length - 1 if node.type == "attribute" else line_length
319319
if len(single_line) < effective_length:
320320
return [single_line]
321321
if node.type == "attribute":
322-
return attempt_split_attribute(node, indent, line_length - 1)
322+
return _attempt_split_attribute(node, indent, line_length - 1)
323323
return [single_line]
324324

325325

@@ -447,7 +447,7 @@ def _has_stakeholder(children: list[Node]) -> bool:
447447
return any(c.type == "stakeholder" for c in children)
448448

449449

450-
def can_single_line_promise(node: Node, indent: int, line_length: int) -> bool:
450+
def _can_single_line_promise(node: Node, indent: int, line_length: int) -> bool:
451451
"""Check if a promise can be formatted entirely on one line.
452452
453453
Returns False for multi-attribute promises, promises with a
@@ -514,7 +514,7 @@ def _format_promise(
514514
) -> bool:
515515
"""Format a promise node. Returns True if handled, False to fall through."""
516516
# Single-line promise
517-
if can_single_line_promise(node, indent, line_length):
517+
if _can_single_line_promise(node, indent, line_length):
518518
prefix = _promiser_line_with_stakeholder(children)
519519
assert prefix is not None
520520
attr = next((c for c in children if c.type == "attribute"), None)
@@ -569,7 +569,7 @@ def _format_remaining_children(
569569
for child in children:
570570
if child.type in PROMISER_PARTS:
571571
continue
572-
autoformat(child, fmt, line_length, indent)
572+
_autoformat(child, fmt, line_length, indent)
573573

574574

575575
# ---------------------------------------------------------------------------
@@ -645,8 +645,8 @@ def _needs_blank_line_before(child: Node, indent: int, line_length: int) -> bool
645645
promise_indent = indent + 2
646646
both_single = (
647647
prev_content.type == "promise"
648-
and can_single_line_promise(prev_content, promise_indent, line_length)
649-
and can_single_line_promise(child, promise_indent, line_length)
648+
and _can_single_line_promise(prev_content, promise_indent, line_length)
649+
and _can_single_line_promise(child, promise_indent, line_length)
650650
)
651651
return not both_single
652652

@@ -708,7 +708,7 @@ def _comment_indent(node: Node, indent: int) -> int:
708708
# ---------------------------------------------------------------------------
709709

710710

711-
def autoformat(
711+
def _autoformat(
712712
node: Node,
713713
fmt: Formatter,
714714
line_length: int,
@@ -744,7 +744,7 @@ def autoformat(
744744

745745
# Attribute — stringify and return
746746
if node.type == "attribute":
747-
fmt.print_lines(stringify(node, indent, line_length), indent=0)
747+
fmt.print_lines(_stringify(node, indent, line_length), indent=0)
748748
return
749749

750750
# Promise — delegate to promise formatter
@@ -757,7 +757,7 @@ def autoformat(
757757
for child in children:
758758
if _needs_blank_line_before(child, indent, line_length):
759759
fmt.blank_line()
760-
autoformat(child, fmt, line_length, indent)
760+
_autoformat(child, fmt, line_length, indent)
761761
return
762762

763763
# Leaf nodes
@@ -796,7 +796,7 @@ def format_policy_file(filename: str, line_length: int, check: bool) -> int:
796796
check_policy_syntax(tree, filename)
797797

798798
fmt = Formatter()
799-
autoformat(root_node, fmt, line_length)
799+
_autoformat(root_node, fmt, line_length)
800800

801801
new_data = fmt.buffer + "\n"
802802
if new_data != original_data.decode("utf-8"):
@@ -828,7 +828,7 @@ def format_policy_fin_fout(
828828
check_policy_syntax(tree, "<stdin>")
829829

830830
fmt = Formatter()
831-
autoformat(root_node, fmt, line_length)
831+
_autoformat(root_node, fmt, line_length)
832832

833833
new_data = fmt.buffer + "\n"
834834
fout.write(new_data)

0 commit comments

Comments
 (0)