Avoid continuations for script lines in braced expressions #75

Merged
Christoph merged 1 commit from 72-indent-error into main 2026-10-14 20:07:58 +00:00
Owner

This change prevents the formatter from inserting line-continuation backslashes between lines that come from script bodies (for example, a catch body) when those scripts appear inside a braced expression. It also standardizes the LSP server's indentation to always use 4 spaces.

Summary of behavior changes

  • Formatter: introduces a hidden marker (_SCRIPT_LINE) and an expression-depth counter (_expr_depth) to detect lines produced inside a command substitution while formatting a braced expression. Lines marked as script lines are not given a trailing \ continuation when the braced expression formatter would otherwise add one. The marker is removed from the final output and empty-looking lines are preserved as empty strings.

    • NxFormatter.format_command_sub now tags subsequent lines with the marker when formatting command substitutions while inside an expression.
    • NxFormatter.format_braced_expression increments/decrements _expr_depth around its work, skips inserting continuation backslashes for marked script lines, and strips the marker afterward.
  • LSP server indentation: TclLanguageServer now sets the formatter indent to four spaces unconditionally (indent = " " * 4) instead of deriving indentation from options.insert_spaces and options.tab_size.

Why this change matters

  • Before: the braced-expression formatting logic always inserted \ between continuation lines. When a command substitution produced lines that were actually a script body (multiple separate commands), adding \ could unintentionally join separate commands into a single continued command.
  • After: script lines produced inside command substitutions are preserved as separate commands without inserted continuations, preventing accidental command joining.

Files changed (high level)

  • server/src/tools/formatter.py: add _SCRIPT_LINE marker, track expression depth, tag command-substitution lines, and avoid adding \ for script lines in braced expressions.
  • server/src/lsp_tclserver.py: force 4-space indentation in the formatter options.
  • server/tests/python_tests/test_formatter.py: add test_catch_body_in_braced_expression_gets_no_continuation to assert the intended formatting for a catch body inside a braced expression.

Testing

No test execution results were provided.

Recommended checks for reviewers

  1. Run the formatter unit tests: pytest server/tests/python_tests/test_formatter.py (or the project's test suite) and inspect the new test test_catch_body_in_braced_expression_gets_no_continuation.
  2. Try formatting a sample that contains a braced expression with a command substitution whose substitution is a multi-command script (e.g. catch { ... }) and confirm no \ continuations are inserted inside that script body.
  3. Confirm the LSP server's formatting output: invoking formatting via the language server will now use four-space indentation regardless of client insertSpaces/tabSize settings; verify this is intended for your environment.

Compatibility / reviewer notes

  • The LSP change concretely changes indentation behavior: it ignores the editor/client indentation settings and always uses 4 spaces. If preserving user-configured tabs/spaces was desired, this change must be adjusted.
  • The _SCRIPT_LINE marker is an internal implementation detail; the code strips it before producing final output, so no marker should appear in formatted files.

If you want, I can run through a few representative examples of input/output formatting to demonstrate the change further.

Closes #72

This change prevents the formatter from inserting line-continuation backslashes between lines that come from script bodies (for example, a `catch` body) when those scripts appear inside a braced expression. It also standardizes the LSP server's indentation to always use 4 spaces. Summary of behavior changes - Formatter: introduces a hidden marker (`_SCRIPT_LINE`) and an expression-depth counter (`_expr_depth`) to detect lines produced inside a command substitution while formatting a braced expression. Lines marked as script lines are not given a trailing ` \` continuation when the braced expression formatter would otherwise add one. The marker is removed from the final output and empty-looking lines are preserved as empty strings. - `NxFormatter.format_command_sub` now tags subsequent lines with the marker when formatting command substitutions while inside an expression. - `NxFormatter.format_braced_expression` increments/decrements `_expr_depth` around its work, skips inserting continuation backslashes for marked script lines, and strips the marker afterward. - LSP server indentation: `TclLanguageServer` now sets the formatter `indent` to four spaces unconditionally (`indent = " " * 4`) instead of deriving indentation from `options.insert_spaces` and `options.tab_size`. Why this change matters - Before: the braced-expression formatting logic always inserted ` \` between continuation lines. When a command substitution produced lines that were actually a script body (multiple separate commands), adding ` \` could unintentionally join separate commands into a single continued command. - After: script lines produced inside command substitutions are preserved as separate commands without inserted continuations, preventing accidental command joining. Files changed (high level) - `server/src/tools/formatter.py`: add `_SCRIPT_LINE` marker, track expression depth, tag command-substitution lines, and avoid adding ` \` for script lines in braced expressions. - `server/src/lsp_tclserver.py`: force 4-space indentation in the formatter options. - `server/tests/python_tests/test_formatter.py`: add `test_catch_body_in_braced_expression_gets_no_continuation` to assert the intended formatting for a `catch` body inside a braced expression. Testing No test execution results were provided. Recommended checks for reviewers 1. Run the formatter unit tests: `pytest server/tests/python_tests/test_formatter.py` (or the project's test suite) and inspect the new test `test_catch_body_in_braced_expression_gets_no_continuation`. 2. Try formatting a sample that contains a braced expression with a command substitution whose substitution is a multi-command script (e.g. `catch { ... }`) and confirm no ` \` continuations are inserted inside that script body. 3. Confirm the LSP server's formatting output: invoking formatting via the language server will now use four-space indentation regardless of client `insertSpaces`/`tabSize` settings; verify this is intended for your environment. Compatibility / reviewer notes - The LSP change concretely changes indentation behavior: it ignores the editor/client indentation settings and always uses 4 spaces. If preserving user-configured tabs/spaces was desired, this change must be adjusted. - The `_SCRIPT_LINE` marker is an internal implementation detail; the code strips it before producing final output, so no marker should appear in formatted files. If you want, I can run through a few representative examples of input/output formatting to demonstrate the change further. Closes #72
fix(formatter): avoid inserting continuations for script lines in braced expressions
All checks were successful
Tests / node (pull_request) Successful in 25s
Tests / python (pull_request) Successful in 37s
69417155c2
Introduce a sentinel marker and an expression depth counter so lines
produced inside command substitutions (e.g. a catch body) can be
recognized while formatting a braced expression. Those lines belong to a
script and must not receive the " \\" continuation that joins expression
continuation lines. The marker is removed before final output.

- Add NxFormatter._expr_depth and format_command_sub to mark script lines.
- Update format_braced_expression to skip adding continuations for marked
  lines and to normalize empty lines after unmarking.
- Add a unit test asserting that a catch body inside a braced expression
  does not get a line continuation.

Also standardize LSP formatter indentation to 4 spaces (tabs in leading
indentation are replaced) by always passing an indent of "    " from the
language server.

Fixes #72
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
Christoph/nx_post_support!75
No description provided.