Skip to content

Commit d94dcb3

Browse files
bhirszmnojek
andauthored
OrderSettings now preserves comments order and groups them with settings (#488)
* OrderSettings now preserver comments order and group them with settings * Update docs/releasenotes/4.0.0.rst Co-authored-by: Mateusz Nojek <matnojek@gmail.com>
1 parent 30a2aa7 commit d94dcb3

File tree

6 files changed

+114
-7
lines changed

6 files changed

+114
-7
lines changed

docs/releasenotes/4.0.0.rst

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,36 @@ rather than the sections you want to format. For example following command::
116116
Is now equivalent of::
117117

118118
> robotidy -c NormalizeSeparators:skip_sections=keywords
119+
120+
Group comments with settings in OrderSettings #468
121+
---------------------------------------------------
122+
123+
``OrderSettings`` transformer adjusts the order of the settings such as ``[Arguments]`` or ``[Teardown]`` inside test,
124+
task or keyword. Previously, it only ordered the settings and comments were not moved. Following code::
125+
126+
*** Keywords ***
127+
Keyword
128+
# comment about step
129+
Step
130+
# comment about arguments
131+
[Arguments] ${arg}
132+
133+
would result in::
134+
135+
136+
*** Keywords ***
137+
Keyword
138+
[Arguments] ${arg}
139+
# comment about step
140+
Step
141+
# comment about arguments
142+
143+
However in most cases such comments are meant only the very next line. That's why now the comments will be moved
144+
together with settings::
145+
146+
*** Keywords ***
147+
Keyword
148+
# comment about arguments
149+
[Arguments] ${arg}
150+
# comment about step
151+
Step

docs/source/transformers/OrderSettings.rst

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ settings like ``[Teardown]``, ``[Return]`` are moved to the end of keyword.
4444
Test case settings ``[Documentation]``, ``[Tags]``, ``[Template]``, ``[Timeout]``, ``[Setup]`` are put before test case body and
4545
``[Teardown]`` is moved to the end of test case.
4646

47+
Configure order of the settings
48+
----------------------------------
49+
4750
Default order can be changed using following parameters:
4851

4952
- ``keyword_before = documentation,tags,timeout,arguments``
@@ -103,3 +106,32 @@ This configuration is invalid because teardown is by default part of the ``test_
103106
We need to overwrite both orders::
104107

105108
robotidy --configure OrderSettings:test_before=teardown:test_after=
109+
110+
Settings comments
111+
---------------------
112+
113+
Comments next to settings will be moved together.
114+
115+
.. tab-set::
116+
117+
.. tab-item:: Before
118+
119+
.. code:: robotframework
120+
121+
*** Keywords ***
122+
Keyword
123+
# comment about step
124+
Step
125+
# comment about arguments
126+
[Arguments] ${arg}
127+
128+
.. tab-item:: After
129+
130+
.. code:: robotframework
131+
132+
*** Keywords ***
133+
Keyword
134+
# comment about arguments
135+
[Arguments] ${arg}
136+
# comment about step
137+
Step

robotidy/transformers/OrderSettings.py

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ def __init__(self, transformer, param_name, param_value):
2222
class SettingInBothOrdersError(RobotidyConfigError):
2323
def __init__(self, transformer, first_order, second_order, duplicates):
2424
names = ",".join(setting.lower() for setting in duplicates)
25-
msg = f"{transformer}: Invalid '{first_order}' and '{second_order}' order values. Following setting names exists in both orders: {names}"
25+
msg = (
26+
f"{transformer}: Invalid '{first_order}' and '{second_order}' order values. "
27+
f"Following setting names exists in both orders: {names}"
28+
)
2629
super().__init__(msg)
2730

2831

@@ -156,24 +159,33 @@ def order_settings(self, node, setting_types, before, after):
156159
after_seen = False
157160
# when after_seen is set to True then all statements go to trailing_after and last non data
158161
# will be appended after tokens defined in `after` set (like [Return])
159-
comment = []
162+
comments, header_line = [], []
160163
for child in node.body:
161-
if isinstance(child, Comment) and child.lineno == node.lineno:
162-
comment.append(child)
164+
if isinstance(child, Comment):
165+
if child.lineno == node.lineno: # comment in the same line as test/kw name
166+
header_line.append(child)
167+
else:
168+
comments.append(child)
163169
elif getattr(child, "type", "invalid") in setting_types:
164170
after_seen = after_seen or child.type in after
165-
settings[child.type] = child
171+
settings[child.type] = (comments, child)
172+
comments = []
166173
elif after_seen:
174+
trailing_after.extend(comments)
175+
comments = []
167176
trailing_after.append(child)
168177
else:
178+
not_settings.extend(comments)
179+
comments = []
169180
not_settings.append(child)
181+
trailing_after.extend(comments)
170182
# comments after last data statement are considered as comment outside body
171183
trailing_non_data = []
172184
while trailing_after and isinstance(trailing_after[-1], (EmptyLine, Comment)):
173185
trailing_non_data.insert(0, trailing_after.pop())
174186
not_settings += trailing_after
175187
node.body = (
176-
comment
188+
header_line
177189
+ self.add_in_order(before, settings)
178190
+ not_settings
179191
+ self.add_in_order(after, settings)
@@ -183,4 +195,11 @@ def order_settings(self, node, setting_types, before, after):
183195

184196
@staticmethod
185197
def add_in_order(order, settings_in_node):
186-
return [settings_in_node[token_type] for token_type in order if token_type in settings_in_node]
198+
nodes = []
199+
for token_type in order:
200+
if token_type not in settings_in_node:
201+
continue
202+
comments, node = settings_in_node[token_type]
203+
nodes.extend(comments)
204+
nodes.append(node)
205+
return nodes
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
*** Keywords ***
2+
Comment between settings
3+
# comment 3
4+
[Documentation] doc
5+
# comment 1
6+
[Arguments] ${arg}
7+
# comment 2
8+
Step
9+
# comment 4
10+
[Return] ${arg}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
*** Keywords ***
2+
Comment between settings
3+
# comment 1
4+
[Arguments] ${arg}
5+
# comment 2
6+
Step
7+
# comment 4
8+
[Return] ${arg}
9+
# comment 3
10+
[Documentation] doc

tests/atest/transformers/OrderSettings/test_order_settings.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,6 @@ def test_custom_order_setting_twice_in_after_before(self):
8585

8686
def test_translated(self):
8787
self.compare(source="translated.robot", target_version=">=6")
88+
89+
def test_stick_comments_with_settings(self):
90+
self.compare(source="stick_comments.robot")

0 commit comments

Comments
 (0)