diff --git a/server/src/tools/formatter.py b/server/src/tools/formatter.py index e69de29..58edfac 100644 --- a/server/src/tools/formatter.py +++ b/server/src/tools/formatter.py @@ -0,0 +1,31 @@ +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]: + return self._brace(formatted) + + return ["{"] + self._indent(formatted, self.opts.indent) + ["}"]