Keep leading comments in proc bodies unindented #66

Closed
Christoph wants to merge 0 commits from 58-nocomplain-is-missing into main
Owner

This change adjusts the formatter to preserve leading header comments inside procedure bodies at the procedure's own indentation level, and fixes the Tcl completion metadata for the unset option to include its leading -.

What changed

  • Formatter behavior

    • The formatter now tracks procedure bodies (self._proc_bodies) when formatting commands and, when formatting a script that is a proc body, leaves comment lines that appear before the first real command unindented. Everything from the first real command onward (including subsequent comments) is indented normally.
    • Implementation: NxFormatter.__init__ initializes _proc_bodies; format_command records the proc body id(...) for procs; format_script post-processes the script lines to remove the extra indentation from leading comment lines.
  • Tcl completion metadata

    • OPTIONS_BY_PATH entry for ("unset",) now lists the option as "-nocomplain" (with the leading dash) rather than "nocomplain", so completion will suggest the hyphen-prefixed flag.
    • A few generator-expression formatting changes were made in tcl_command_completion.py to compress multi-line comprehensions into single-line forms; these are formatting-only and do not change behavior.
  • Tests

    • server/tests/python_tests/test_formatter.py was updated: an existing test's expected output includes a set y 2 line and a new test test_leading_proc_comments_are_not_indented was added to assert the new indentation semantics for proc header comments.

Why this matters (evidence-based)

  • The formatter change is aimed at preserving the visual position of header comments placed immediately inside a proc { ... } body. The diff shows code that records proc bodies and then strips one level of indent from leading comment lines in those bodies; the new test exercises that exact case.
  • The completion change is a direct fix to the OPTIONS_BY_PATH mapping for unset so the completion system will include the correct literal -nocomplain flag.

Tests

No test execution results were provided.

Recommended checks for reviewers:

  • Run the new/updated formatter tests:
    • python -m pytest server/tests/python_tests/test_formatter.py::test_leading_proc_comments_are_not_indented
    • or run the full formatter test file: python -m pytest server/tests/python_tests/test_formatter.py
  • Manually verify formatting on a small snippet with a proc that has leading header comments to confirm the before/after behavior matches expectations.

Compatibility / reviewer notes

  • The completion metadata change affects completion suggestions for unset by including the leading -. If any consumer relied on the previous no-dash entry in OPTIONS_BY_PATH, update accordingly.
  • The formatter relies on object identity (id(...)) to correlate a recorded proc body with the subsequent format_script call; this is consistent with the existing formatter call pattern in the diff but is worth noting if the formatter call ordering or object reuse changes in future.

No other migration or configuration changes are required.

Closes #58

This change adjusts the formatter to preserve leading header comments inside procedure bodies at the procedure's own indentation level, and fixes the Tcl completion metadata for the `unset` option to include its leading `-`. ## What changed - Formatter behavior - The formatter now tracks procedure bodies (`self._proc_bodies`) when formatting commands and, when formatting a script that is a proc body, leaves comment lines that appear before the first real command unindented. Everything from the first real command onward (including subsequent comments) is indented normally. - Implementation: `NxFormatter.__init__` initializes `_proc_bodies`; `format_command` records the proc body `id(...)` for procs; `format_script` post-processes the script lines to remove the extra indentation from leading comment lines. - Tcl completion metadata - `OPTIONS_BY_PATH` entry for `("unset",)` now lists the option as `"-nocomplain"` (with the leading dash) rather than `"nocomplain"`, so completion will suggest the hyphen-prefixed flag. - A few generator-expression formatting changes were made in `tcl_command_completion.py` to compress multi-line comprehensions into single-line forms; these are formatting-only and do not change behavior. - Tests - `server/tests/python_tests/test_formatter.py` was updated: an existing test's expected output includes a `set y 2` line and a new test `test_leading_proc_comments_are_not_indented` was added to assert the new indentation semantics for proc header comments. ## Why this matters (evidence-based) - The formatter change is aimed at preserving the visual position of header comments placed immediately inside a `proc { ... }` body. The diff shows code that records proc bodies and then strips one level of indent from leading comment lines in those bodies; the new test exercises that exact case. - The completion change is a direct fix to the `OPTIONS_BY_PATH` mapping for `unset` so the completion system will include the correct literal `-nocomplain` flag. ## Tests No test execution results were provided. Recommended checks for reviewers: - Run the new/updated formatter tests: - `python -m pytest server/tests/python_tests/test_formatter.py::test_leading_proc_comments_are_not_indented` - or run the full formatter test file: `python -m pytest server/tests/python_tests/test_formatter.py` - Manually verify formatting on a small snippet with a proc that has leading header comments to confirm the before/after behavior matches expectations. ## Compatibility / reviewer notes - The completion metadata change affects completion suggestions for `unset` by including the leading `-`. If any consumer relied on the previous no-dash entry in `OPTIONS_BY_PATH`, update accordingly. - The formatter relies on object identity (`id(...)`) to correlate a recorded proc body with the subsequent `format_script` call; this is consistent with the existing formatter call pattern in the diff but is worth noting if the formatter call ordering or object reuse changes in future. No other migration or configuration changes are required. Closes #58
Previously the "unset" option was registered as "nocomplain" (no leading hyphen), which was inconsistent with other OptionSpec entries and could cause option-matching to fail. This commit changes that entry to "-nocomplain" so the option is recognized with its expected dash.

Also reformatted two multiline generator expressions that build DynamicCompletionRule entries into single-line comprehensions; this is a whitespace-only tidy and does not change behavior.

Fixes #58
feat(formatter): keep leading comments in proc bodies unindented
Some checks failed
Tests / node (pull_request) Successful in 24s
Tests / python (pull_request) Failing after 35s
14cdfc9bfe
Preserve comment lines that occur before the first real command inside a proc
body at the proc's own indentation level instead of indenting them with the
body. NxFormatter now tracks proc body objects (by id) when formatting
commands and adjusts format_script (only when should_indent is True) to
leave those leading header comments unindented. Adds unit test
test_leading_proc_comments_are_not_indented to cover nested and namespaced cases.

Fixes #58
test(completion): expect '-nocomplain' label for unset completions
All checks were successful
Tests / python (pull_request) Successful in 45s
Tests / node (pull_request) Successful in 26s
934bfe0848
Adjust test expectation to match completion item labels that include the leading
hyphen for the unset option. Previously the test asserted {"nocomplain"}; it
now asserts {"-nocomplain"} so the test reflects the actual completion output.

Also tidy up import ordering in the test file for clarity.

Fixes #58
Christoph closed this pull request 2026-10-10 16:17:42 +00:00
All checks were successful
Tests / python (pull_request) Successful in 45s
Required
Details
Tests / node (pull_request) Successful in 26s
Required
Details

Pull request closed

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!66
No description provided.