The formatter now calculates and passes explicit leading and trailing whitespace around expression content to the `_brace` method. This ensures correct rendering of single-line expressions that include braces.
34 lines
1.5 KiB
Python
34 lines
1.5 KiB
Python
from tclint.format import Formatter as BaseFormatter
|
|
from typing import List
|
|
|
|
|
|
class NxFormatter(BaseFormatter):
|
|
"""
|
|
Custom formatter that inherits from tclint's Formatter but preserves explicit
|
|
line continuations (\\) inside braced expressions when they span multiple lines.
|
|
|
|
This avoids generating syntax errors in environments that require a trailing
|
|
backslash for multi-line expressions (e.g., certain NX Post interpreters),
|
|
while leaving all other formatting behavior unchanged.
|
|
"""
|
|
|
|
def format_braced_expression(self, expr) -> List[str]: # type: ignore[override]
|
|
# This method mirrors BaseFormatter.format_braced_expression but inserts
|
|
# a line continuation (" \") between continuation lines similar to
|
|
# BaseFormatter.format_expression.
|
|
formatted = [""]
|
|
for child in expr.children:
|
|
lines = self.format(child)
|
|
formatted[-1] += lines[0]
|
|
for line in lines[1:]:
|
|
# add continuation on the previous line; keep next line at the same level
|
|
formatted[-1] += " \\" # keep explicit continuation
|
|
formatted += [line]
|
|
|
|
if expr.pos[0] == expr.end_pos[0]:
|
|
space_before = expr.children[0].pos[1] - expr.pos[1] - 1
|
|
space_after = expr.end_pos[1] - expr.children[-1].end_pos[1] - 1
|
|
return self._brace(formatted, (space_before, space_after))
|
|
|
|
return ["{"] + self._indent(formatted, self.opts.indent) + ["}"]
|