Skip to content

Commit

Permalink
Fix AlignVariablesSection using fixed separator
Browse files Browse the repository at this point in the history
  • Loading branch information
bhirsz committed Dec 24, 2023
1 parent 0372450 commit d7032f7
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 10 deletions.
5 changes: 5 additions & 0 deletions docs/releasenotes/unreleased/fix.1.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
AlignVariablesSection not using configured spacecount (#605)
------------------------------------------------------------

``AlignVariablesSection`` transformed should now use configured ``--spacecount`` instead of fixed value of ``4``
spaces.
23 changes: 13 additions & 10 deletions robotidy/transformers/AlignVariablesSection.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ class AlignVariablesSection(Transformer):
... b=c
```
You can configure how many columns should be aligned to longest token in given column. The remaining columns
will use fixed length separator length ``--spacecount``. By default only first two columns are aligned.
You can configure how many columns should be aligned to the longest token in given column. The remaining columns
will use fixed length separator length ``--spacecount``. By default, only first two columns are aligned.
To align first three columns:
```console
Expand Down Expand Up @@ -106,7 +106,7 @@ def align_rows(self, statements, look_up):
up_to = self.up_to_column if self.up_to_column != -1 else len(line) - 2
for index, token in enumerate(line[:-2]):
aligned_statement.append(token)
separator = self.calc_separator(index, up_to, token, look_up)
separator = self.get_separator(index, up_to, token, look_up)
aligned_statement.append(Token(Token.SEPARATOR, separator))
last_token = line[-2]
# remove leading whitespace before token
Expand All @@ -116,21 +116,24 @@ def align_rows(self, statements, look_up):
aligned_statements.append(Statement.from_tokens(aligned_statement))
return aligned_statements

def calc_separator(self, index, up_to, token, look_up):
def get_separator(self, index: int, up_to: int, token, look_up: dict[int, int]) -> str:
if index < up_to:
if self.fixed_width:
return max(self.fixed_width - len(token.value), self.formatting_config.space_count) * " "
return (look_up[index] - len(token.value) + 4) * " "
return (look_up[index] - len(token.value)) * " "
else:
return self.formatting_config.space_count * " "
return self.formatting_config.separator

def create_look_up(self, statements):
def create_look_up(self, statements) -> dict[int, int]:
look_up = defaultdict(int)
for st in statements:
for line in st:
up_to = self.up_to_column if self.up_to_column != -1 else len(line)
for index, token in enumerate(line[:up_to]):
look_up[index] = max(look_up[index], len(token.value))
if self.min_width:
look_up = {index: max(length, self.min_width - 4) for index, length in look_up.items()}
return {index: misc.round_to_four(length) for index, length in look_up.items()}
for index, length in look_up.items():
min_for_token = length + self.formatting_config.space_count
if self.min_width:
min_for_token = max(min_for_token, self.min_width)
look_up[index] = misc.round_to_four(min_for_token)
return look_up
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
*** Settings ***
Documentation Test
*** Variables ***
${VAR_NAME_25_CHARACTERS} Test

*** Test Cases ***
Test1
${test_col_21_chars}= Set Variable Test
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
*** Settings ***
Documentation Test
*** Variables ***
${VAR_NAME_25_CHARACTERS} Test

*** Test Cases ***
Test1
${test_col_21_chars}= Set Variable Test
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
*** Variables ***
# some comment

${VARIABLE 1} 10 # comment
@{LIST} a b c d
${LONGER_NAME_THAT_GOES_AND_GOES} longer value that goes and goes

&{MULTILINE} a=b
... b=c
... d=1
${invalid}
${invalid_more}

# should be left aligned
# should be left aligned
${variable} 1
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
*** Settings ***
Documentation Test
*** Variables ***
${VAR_NAME_25_CHARACTERS} Test

*** Test Cases ***
Test1
${test_col_21_chars}= Set Variable Test
12 changes: 12 additions & 0 deletions tests/atest/transformers/AlignVariablesSection/test_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ class TestAlignVariablesSection(TransformerAcceptanceTest):
def test_align_variables(self):
self.compare(source="tests.robot")

def test_align_variables_2space_sep(self):
self.compare(source="tests.robot", expected="tests_2space_sep.robot", config=" --spacecount 2")

def test_align_three_columns(self):
self.compare(source="tests.robot", expected="three_columns.robot", config=":up_to_column=3")

Expand Down Expand Up @@ -74,3 +77,12 @@ def test_min_width_shorter(self, min_width):
@pytest.mark.parametrize("min_width", [49, 50, 51, 52])
def test_min_width_longer(self, min_width):
self.compare(source="tests.robot", expected="tests_min_width_50_width.robot", config=f":min_width={min_width}")

@pytest.mark.parametrize("space_count", [2, 4])
def test_single_var(self, space_count):
if space_count == 2:
self.compare(
source="single_var.robot", expected=f"single_var_2spaces.robot", config=f" --spacecount {space_count}"
)
else:
self.compare(source="single_var.robot", not_modified=True, config=f" --spacecount {space_count}")

0 comments on commit d7032f7

Please sign in to comment.