MarketSquare / robotframework-tidy

Robot Framework code formatter
https://robotidy.readthedocs.io/
Apache License 2.0
103 stars 16 forks source link

Comments under "Run Keywords" inappropriately moved around #702

Closed gohierf closed 1 month ago

gohierf commented 2 months ago

I often use the Run Keywords methods for Setup or Teardown, sometimes I need to disable part of the setup/teardown, and I like to do this by just commenting the line(s) to disable.

Unfortunately, this doesn't work well with tidy... As the commented line is moved above the keyword/setting call.

Not sure which rule(s) is/are involved. But it seems related to keyword with sub-indentation.

For instance, in the example below, tidy does not change the location of message B or message 2, which is what I am expecting. However, the commented No Operation is moved around because it is within Run Keywords.

This looks like a bug/undesired behavior, but it might be a question of configuring correctly one or more rules... Let me know!

Example code

*** Settings ***
Documentation       Example of comments being moved around.

Suite Teardown      Run Keywords
...                     No Operation
# ...                     No Operation
...                     No Operation
Test Setup          Log Many
...                     message A
# ...                     message B
...                     message C

*** Test Cases ***
My test with 2 ident comment
    Run Keywords
    ...    No Operation
    # ...    No Operation
    ...    No Operation

My test with comment
    Log Many
    ...    message 1
    # ...    message 2
    ...    message 3

Desired

image

After formatting

image

bhirsz commented 2 months ago

'Popping out' comments is by design. Thanks for that we can ignore comments as it may be difficult to handle them in some cases (especially if for example we split into new lines etc). I need to think how to workaround this behaviour. It is doable to handle comments in transformer that indents run keywords (if its not different transformer like order settings) but I would need to see if its always applicable.

bhirsz commented 1 month ago

Returning to the issue. There is a problem that sometimes comments 'are in a way'. For example in your case, IndentNestedKeywords may completely reorder the statement:

    Run Keyword If    True    My Keyword     ${arg}    ELSE    My Other Keyword  # comment about this line
    ...  random argument
   Run Keywords   # comment
    ...    No Operation  # comment
    #    No Operation
    ,,,    No Operation  No Operation  No Operation  # comment

Comments such as # No Operation are clear but comments after data tokens are more difficult to handle (they usually need to stay close to the commented line). They way this transformation works is transforming everything to 'base' state and then reconstructing it with desired indent etc (and that's when 'popping' comments out happens).

I have some idea though - for example I can try to check if code before and after transformation was modified outside comments position. If not, we can ignore the transformation. I will work on this solution