Skip to content

Commit

Permalink
Fix AlignVariablesSection using fixed separator (#609)
Browse files Browse the repository at this point in the history
  • Loading branch information
bhirsz authored Dec 24, 2023
1 parent cffe8eb commit a9aead9
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 11 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.
26 changes: 15 additions & 11 deletions robotidy/transformers/AlignVariablesSection.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from collections import defaultdict
from typing import Dict

from robot.api.parsing import Token
from robot.parsing.model import Statement
Expand Down Expand Up @@ -33,8 +34,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 @@ -87,7 +88,7 @@ def visit_VariableSection(self, node): # noqa
nodes_to_be_aligned = [st for st in statements if isinstance(st, list)]
if not nodes_to_be_aligned:
return node
look_up = self.create_look_up(nodes_to_be_aligned) # for every col find longest value
look_up = self.create_look_up(nodes_to_be_aligned) # for every col find the longest value
node.body = self.align_rows(statements, look_up)
return node

Expand All @@ -106,7 +107,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 +117,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,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
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,8 @@ 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):
not_modified = space_count == 4
self.compare(source="single_var.robot", not_modified=not_modified, config=f" --spacecount {space_count}")

0 comments on commit a9aead9

Please sign in to comment.