Skip to content

Commit ff80c2d

Browse files
authored
Fix extra trailing whitespace when aligning multiline statements with blank lines (#220)
* align blank lines in multiline statement without leading whitespace * update changelog
1 parent b0ea7f6 commit ff80c2d

File tree

12 files changed

+69
-12
lines changed

12 files changed

+69
-12
lines changed

CHANGES.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
## Unreleased
44

5+
### Fixes
6+
- Trailing whitespace is no longer added to blank lines in multiline statements when using align transformers ([#219](https://github.com/MarketSquare/robotframework-tidy/issues/219))
7+
8+
## 1.6.0
9+
510
### Transformers
611
- New non default transformer `RenameTestCases`. It capitalizes first letter of the test case name, removes trailing dot and can replace provided regex pattern with substitute string ([#183](https://github.com/MarketSquare/robotframework-tidy/issues/183))
712
- New non default transformer `RenameKeywords`. It applies Title Case to keyword name and replace underscores by spaces and can replace provided regex pattern with substitute string ([#183](https://github.com/MarketSquare/robotframework-tidy/issues/183))

robotidy/transformers/AlignSettingsSection.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,7 @@
33
from robot.api.parsing import ModelTransformer, Token
44
from robot.parsing.model import Statement
55

6-
from robotidy.utils import (
7-
node_outside_selection,
8-
round_to_four,
9-
tokens_by_lines,
10-
left_align,
11-
)
6+
from robotidy.utils import node_outside_selection, round_to_four, tokens_by_lines, left_align, is_blank_multiline
127

138

149
class AlignSettingsSection(ModelTransformer):
@@ -97,6 +92,10 @@ def align_rows(self, statements, look_up):
9792
keyword_statement = st[0][0].type in self.TOKENS_WITH_KEYWORDS
9893
aligned_statement = []
9994
for line in st:
95+
if is_blank_multiline(line):
96+
line[-1].value = line[-1].value.lstrip(" \t") # normalize eol from ' \n' to '\n'
97+
aligned_statement.extend(line)
98+
continue
10099
keyword_arg = keyword_statement and line[0].type == Token.CONTINUATION
101100
up_to = self.up_to_column if self.up_to_column != -1 else len(line) - 2
102101
for index, token in enumerate(line[:-2]):

robotidy/transformers/AlignVariablesSection.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,7 @@
33
from robot.api.parsing import ModelTransformer, Token
44
from robot.parsing.model import Statement
55

6-
from robotidy.utils import (
7-
node_outside_selection,
8-
round_to_four,
9-
tokens_by_lines,
10-
left_align,
11-
)
6+
from robotidy.utils import node_outside_selection, round_to_four, tokens_by_lines, left_align, is_blank_multiline
127

138

149
class AlignVariablesSection(ModelTransformer):
@@ -73,6 +68,10 @@ def align_rows(self, statements, look_up):
7368
continue
7469
aligned_statement = []
7570
for line in st:
71+
if is_blank_multiline(line):
72+
line[-1].value = line[-1].value.lstrip(" \t") # normalize eol from ' \n' to '\n'
73+
aligned_statement.extend(line)
74+
continue
7675
up_to = self.up_to_column if self.up_to_column != -1 else len(line) - 2
7776
for index, token in enumerate(line[:-2]):
7877
aligned_statement.append(token)

robotidy/utils.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,3 +276,12 @@ def is_suite_templated(node):
276276
template_finder = TestTemplateFinder()
277277
template_finder.visit(node)
278278
return template_finder.templated
279+
280+
281+
def is_blank_multiline(statements):
282+
return (
283+
statements[0].type == Token.CONTINUATION
284+
and len(statements) == 3
285+
and statements[1].type == "ARGUMENT"
286+
and not statements[1].value
287+
)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
*** Settings ***
2+
Documentation Documentation with extra
3+
...
4+
... spaces
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
*** Settings ***
2+
Documentation Description of what the file does
3+
...
4+
... Copyright 2021 by company I am paid by
5+
Test Template test
6+
Force Tags tag
7+
...
8+
... tag2
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
*** Settings ***
2+
Documentation Documentation with extra
3+
...
4+
... spaces
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
*** Settings ***
2+
Documentation Description of what the file does
3+
...
4+
... Copyright 2021 by company I am paid by
5+
Test Template test
6+
Force Tags tag
7+
...
8+
... tag2

tests/atest/transformers/AlignSettingsSection/test_transformer.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,9 @@ def test_continued_statement_style_all_columns_configure_indent(self, indent):
6464
expected=f'multiline_keywords_{indent}indent.robot',
6565
config=f':up_to_column=3:argument_indent={indent}'
6666
)
67+
68+
def test_multiline_with_blank_line(self):
69+
run_tidy_and_compare(self.TRANSFORMER_NAME, source='blank_line_doc.robot')
70+
71+
def test_doc_multiline_and_whitespace(self):
72+
run_tidy_and_compare(self.TRANSFORMER_NAME, source='blank_line_and_whitespace.robot')
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
*** Variables ***
2+
${VARIABLE} 1
3+
... 2
4+
...
5+
... 3
6+
${OTHER_AND_LONGER} a

0 commit comments

Comments
 (0)