Skip to content

fix: keep comments with statement in IndentNestedKeywords - #1919

Merged
bhirsz merged 1 commit into
mainfrom
fix/keep-comments-with-statement-in-indent-nested-keywords
Aug 26, 2026
Merged

fix: keep comments with statement in IndentNestedKeywords#1919
bhirsz merged 1 commit into
mainfrom
fix/keep-comments-with-statement-in-indent-nested-keywords

Conversation

@bhirsz

@bhirsz bhirsz commented Aug 26, 2026

Copy link
Copy Markdown
Member

Description

The IndentNestedKeywords formatter used to collect all comments from a run-keyword statement and move them in front of the formatted keyword. This caused two problems:

  • Line-scoped disablers (e.g. # robocop: fmt: off) were relocated away from the line they were meant to affect.
  • Comments were detached from the code they reference.

This PR makes the formatter keep comments together with their code:

  • Trailing comments (sharing a line with data) are anchored to the closest keyword/argument and rendered on the same output line.
  • Standalone comments (comment-only lines) are still kept as separate comment lines before the statement.

The same anchoring logic is now applied consistently across keyword calls and settings (Suite Setup/Suite Teardown/Test Setup/Test Teardown/[Setup]/[Teardown]), replacing the old "merge every comment onto the first line" behavior for settings.

Example

Before:

Run Keywords    Log    1    AND    Log    2    # keep me next to log 2

After:

Run Keywords
...    Log    1
...    AND
...    Log    2    # keep me next to log 2

Previously the comment was hoisted above Run Keywords.

Implementation

  • misc.py: added split_comments_by_anchor() returning standalone comments plus a id(data_token) -> trailing comments map.
  • IndentNestedKeywords.py: parse_keyword_lines appends trailing comments per output line; the visit methods use the anchor logic; node_was_formatted now strips comments from both sides before comparing.

Tests & docs

  • Updated expected files and added a demonstrative case in the source data.
  • All IndentNestedKeywords tests and the full formatter suite pass; ruff and mypy (strict) are clean.
  • Documented the behavior in the class docstring and docs/formatter/formatters/IndentNestedKeywords.md.

Closes #1507

Previously all comments were moved in front of the formatted keyword,
which could relocate line-scoped disablers (e.g. robocop: fmt: off) and
detach comments from the code they reference.

Comments trailing data on a line are now anchored to the closest keyword
or argument and rendered on the same output line, while comment-only
lines are kept as standalone comment lines before the statement.

Closes #1507

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@bhirsz
bhirsz merged commit 7be0805 into main Aug 26, 2026
11 checks passed
@bhirsz
bhirsz deleted the fix/keep-comments-with-statement-in-indent-nested-keywords branch August 26, 2026 12:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Various nitpicks

1 participant