Skip to content

Commit

Permalink
Resolve inconsinstency with min_width working as (not existing before…
Browse files Browse the repository at this point in the history
…) fixed_width (#560)

* Add new parameter fixed_width to fix min_width not calculating min width

* Add new parameter fixed_width in AlignSettingsSection to fix min_width not calculating min width

* Fix lookup creation with min_width
  • Loading branch information
bhirsz authored Aug 21, 2023
1 parent 5e90b16 commit d0ff174
Show file tree
Hide file tree
Showing 11 changed files with 247 additions and 16 deletions.
10 changes: 10 additions & 0 deletions docs/releasenotes/unreleased/fixes.4.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Minimal width of the column in AlignVariablesSection and AlignSettingsSection (#558)
------------------------------------------------------------------------------------

``AlignVariablesSection`` and ``AlignSettingsSection`` transformers use ``min_width`` parameter. Originally it didn't
work as ``min_width`` but more as ``fixed_width`` parameter. That's why we are now introducing new parameter
(``fixed_width``) that will work same as previous ``min_width``. ``min_width`` is now fixed and is used to configure
minimal width of the aligned column.

If you are relying on ``min_width`` to set fixed width of the column, rename it to ``fixed_width`` in your
configuration.
38 changes: 36 additions & 2 deletions docs/source/transformers/AlignSettingsSection.rst
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,9 @@ You can configure the indent or disable it by setting ``argument_indent`` to 0.
Fixed width of column
-------------------------
It's possible to set fixed minimal width of column. To configure it use ``min_width`` parameter::
It's possible to set fixed width of the column. To configure it use ``fixed_width`` parameter::

robotidy --configure AlignSettingsSection:min_width=30 src
robotidy --configure AlignSettingsSection:fixed_width=30 src

This configuration respects ``up_to_column`` parameter but ignores ``argument_indent``.

Expand Down Expand Up @@ -197,6 +197,40 @@ This configuration respects ``up_to_column`` parameter but ignores ``argument_in
... and this documentation is multiline
... where this line should go I wonder?
Minimal width of column
-------------------------
It's possible to set minimal width of the column. To configure it use ``min_width`` parameter::

robotidy --configure AlignSettingsSection:min_width=20 src

This configuration respects ``up_to_column`` parameter.

.. tab-set::

.. tab-item:: Before

.. code:: robotframework
*** Settings ***
Library CustomLibrary WITH NAME name
Library ArgsedLibrary ${1} ${2} ${3}
Documentation Example using the space separated format.
... and this documentation is multiline
... where this line should go I wonder?
.. tab-item:: After

.. code:: robotframework
*** Settings ***
Library CustomLibrary WITH NAME name
Library ArgsedLibrary ${1} ${2} ${3}
Documentation Example using the space separated format.
... and this documentation is multiline
... where this line should go I wonder?
Select lines to transform
-------------------------
AlignSettingsSection does also support global formatting params ``startline`` and ``endline``::
Expand Down
77 changes: 74 additions & 3 deletions docs/source/transformers/AlignVariablesSection.rst
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,11 @@ Using above configuration code will be aligned in following way:
${LONGER_NAME_THAT_GOES_AND_GOES} longer value that goes and goes
&{SOME_DICT} key=value key2=value
Fixed width of column
-------------------------
It's possible to set fixed minimal width of column. To configure it use ``min_width`` parameter::
It's possible to set fixed width of the column. To configure it use ``fixed_width`` parameter::

robotidy --configure AlignVariablesSection:min_width=20 src
robotidy --configure AlignVariablesSection:fixed_width=20 src

This configuration respects ``up_to_column`` parameter:

Expand Down Expand Up @@ -164,6 +163,78 @@ This configuration respects ``up_to_column`` parameter:
... b=c
... d=1
Minimal width of column
-------------------------
It's possible to set minimal width of the the column. To configure it use ``min_width`` parameter::

robotidy --configure AlignVariablesSection:min_width=20 src

This configuration respects ``up_to_column`` parameter. Example where there is variable longer than ``min_width``:

.. tab-set::

.. tab-item:: Before

.. code:: robotframework
*** 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
.. tab-item:: After

.. code:: robotframework
*** 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
Example where all variables are shorter than ``min_width``:

.. tab-set::

.. tab-item:: Before

.. code:: robotframework
*** Variables ***
# some comment
${VARIABLE 1} 10 # comment
@{LIST} a b c d
&{MULTILINE} a=b
... b=c
... d=1
.. tab-item:: After

.. code:: robotframework
*** Variables ***
# some comment
${VARIABLE 1} 10 # comment
@{LIST} a b c d
&{MULTILINE} a=b
... b=c
... d=1
Select lines to align
-------------------------
AlignVariablesSection does also support global formatting params ``startline`` and ``endline``::
Expand Down
9 changes: 6 additions & 3 deletions robotidy/transformers/AlignSettingsSection.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,12 @@ class AlignSettingsSection(Transformer):
Token.VARIABLES,
}

def __init__(self, up_to_column: int = 2, argument_indent: int = 4, min_width: int = None):
def __init__(self, up_to_column: int = 2, argument_indent: int = 4, min_width: int = None, fixed_width: int = None):
super().__init__()
self.up_to_column = up_to_column - 1
self.argument_indent = argument_indent
self.min_width = min_width
self.fixed_width = fixed_width

@skip_section_if_disabled
def visit_SettingSection(self, node): # noqa
Expand Down Expand Up @@ -131,8 +132,8 @@ def align_rows(self, statements, look_up):

def calc_separator(self, index, up_to, indent_arg, token, look_up):
if index < up_to:
if self.min_width:
return max(self.min_width - len(token.value), self.formatting_config.space_count) * " "
if self.fixed_width:
return max(self.fixed_width - len(token.value), self.formatting_config.space_count) * " "
arg_indent = self.argument_indent if indent_arg else 0
if indent_arg and index != 0:
return (
Expand Down Expand Up @@ -160,4 +161,6 @@ def create_look_up(self, statements):
up_to = 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: round_to_four(length) for index, length in look_up.items()}
9 changes: 6 additions & 3 deletions robotidy/transformers/AlignVariablesSection.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,11 @@ class AlignVariablesSection(Transformer):
To align all columns set ``up_to_column`` to 0.
"""

def __init__(self, up_to_column: int = 2, skip_types: str = "", min_width: int = None):
def __init__(self, up_to_column: int = 2, skip_types: str = "", min_width: int = None, fixed_width: int = None):
super().__init__()
self.up_to_column = up_to_column - 1
self.min_width = min_width
self.fixed_width = fixed_width
self.skip_types = self.parse_skip_types(skip_types)

def parse_skip_types(self, skip_types):
Expand Down Expand Up @@ -117,8 +118,8 @@ def align_rows(self, statements, look_up):

def calc_separator(self, index, up_to, token, look_up):
if index < up_to:
if self.min_width:
return max(self.min_width - len(token.value), self.formatting_config.space_count) * " "
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) * " "
else:
return self.formatting_config.space_count * " "
Expand All @@ -130,4 +131,6 @@ def create_look_up(self, statements):
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: round_to_four(length) for index, length in look_up.items()}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
*** Settings ***
# whole line comment that should be ignored
Resource ..${/}resources${/}resource.robot
Library SeleniumLibrary
Library Mylibrary.py
Variables variables.py
Test Timeout 1 min

# this should be left aligned
Library CustomLibrary WITH NAME name
Library ArgsedLibrary ${1} ${2} ${3}

Documentation Example using the space separated format.
... and this documentation is multiline
... where this line should go I wonder?
Default Tags default tag 1 default tag 2 default tag 3 default tag 4 default tag 5
Test Setup Open Application App A
Test Teardown Close Application

Metadata Version 2.0
Metadata More Info For more information about *Robot Framework* see http://robotframework.org
Metadata Executed At {HOST}
# this should be left aligned
Test Template

*** Keywords ***
Keyword
Keyword A
Keyword B
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
*** Settings ***
# whole line comment that should be ignored
Resource ..${/}resources${/}resource.robot
Library SeleniumLibrary
Library Mylibrary.py
Variables variables.py
Test Timeout 1 min

# this should be left aligned
Library CustomLibrary WITH NAME name
Library ArgsedLibrary ${1} ${2} ${3}

Documentation Example using the space separated format.
... and this documentation is multiline
... where this line should go I wonder?
Default Tags default tag 1 default tag 2 default tag 3 default tag 4 default tag 5
Test Setup Open Application App A
Test Teardown Close Application

Metadata Version 2.0
Metadata More Info For more information about *Robot Framework* see http://robotframework.org
Metadata Executed At {HOST}
# this should be left aligned
Test Template

*** Keywords ***
Keyword
Keyword A
Keyword B
12 changes: 10 additions & 2 deletions tests/atest/transformers/AlignSettingsSection/test_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,25 @@ def test_doc_multiline_and_whitespace(self):
self.compare(source="blank_line_and_whitespace.robot")

def test_fixed_test(self):
self.compare(source="test.robot", expected="test_fixed.robot", config=":min_width=35")
self.compare(source="test.robot", expected="test_fixed.robot", config=":fixed_width=35")

def test_fixed_all_columns(self):
self.compare(
source="test.robot",
expected="all_columns_fixed.robot",
config=":min_width=20:up_to_column=0",
config=":fixed_width=20:up_to_column=0",
)

def test_disablers(self):
self.compare(source="test_disablers.robot")

def test_argument_indents(self):
self.compare(source="argument_indents.robot")

@pytest.mark.parametrize("min_width", [0, 1, 20])
def test_min_width_shorter(self, min_width):
self.compare(source="test.robot", expected="test_min_width.robot", config=f":min_width={min_width}")

@pytest.mark.parametrize("min_width", [49, 50, 51, 52])
def test_min_width_longer(self, min_width):
self.compare(source="test.robot", expected="test_min_width_50_width.robot", config=f":min_width={min_width}")
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,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
16 changes: 13 additions & 3 deletions tests/atest/transformers/AlignVariablesSection/test_transformer.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import pytest

from tests.atest import TransformerAcceptanceTest


Expand Down Expand Up @@ -50,17 +52,25 @@ def test_multiline_skip(self):
self.compare(source="multiline_skip.robot", config=":skip_types=list,dict")

def test_fixed_tests(self):
self.compare(source="tests.robot", expected="tests_fixed.robot", config=":min_width=30")
self.compare(source="tests.robot", expected="tests_fixed.robot", config=":fixed_width=30")

def test_fixed_tests_zero(self):
self.compare(source="tests.robot", expected="tests_fixed_one.robot", config=":min_width=1")
self.compare(source="tests.robot", expected="tests_fixed_one.robot", config=":fixed_width=1")

def test_fixed_all_columns(self):
self.compare(
source="tests.robot",
expected="all_columns_fixed.robot",
config=":min_width=20:up_to_column=0",
config=":fixed_width=20:up_to_column=0",
)

def test_disablers(self):
self.compare(source="tests_disablers.robot")

@pytest.mark.parametrize("min_width", [0, 1, 20])
def test_min_width_shorter(self, min_width):
self.compare(source="tests.robot", expected="tests_min_width.robot", config=f":min_width={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}")

0 comments on commit d0ff174

Please sign in to comment.