Skip to content

Commit

Permalink
Fix lookup creation with min_width
Browse files Browse the repository at this point in the history
  • Loading branch information
bhirsz committed Aug 17, 2023
1 parent a3da7c9 commit 161f9c6
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 9 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
4 changes: 2 additions & 2 deletions robotidy/transformers/AlignSettingsSection.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ def create_look_up(self, statements):
else:
up_to = len(line)
for index, token in enumerate(line[:up_to]):
if self.min_width:
look_up[index] = self.min_width - 4
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()}
4 changes: 2 additions & 2 deletions robotidy/transformers/AlignVariablesSection.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ def create_look_up(self, 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]):
if self.min_width:
look_up[index] = self.min_width - 4
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()}

0 comments on commit 161f9c6

Please sign in to comment.