Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

extend CONTINUATION_ALIGN_STYLE=FIXED for space indentation #774

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
parameter list is formatted, keeping state along the way. This helps when
supporting Python 3 type annotations.
- Catch and report `UnicodeDecodeError` exceptions.
- `CONTINUATION_ALIGN_STYLE = FIXED` now work with space indentation.
### Fixed
- Format subscript lists so that splits are essentially free after a comma.
- Don't add a space between a string and its subscript.
Expand Down
7 changes: 3 additions & 4 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -424,14 +424,13 @@ Knobs
- ``SPACE``: Use spaces for continuation alignment. This is default
behavior.
- ``FIXED``: Use fixed number (CONTINUATION_INDENT_WIDTH) of columns
(ie: CONTINUATION_INDENT_WIDTH/INDENT_WIDTH tabs) for continuation
alignment.
(ie: CONTINUATION_INDENT_WIDTH/INDENT_WIDTH tabs or CONTINUATION_INDENT_WIDTH
spaces) for continuation alignment.
- ``VALIGN-RIGHT``: Vertically align continuation lines with indent
characters. Slightly right (one more indent character) if cannot
vertically align continuation lines with indent characters.

For options ``FIXED``, and ``VALIGN-RIGHT`` are only available when
``USE_TABS`` is enabled.
The ``VALIGN-RIGHT`` option only available when ``USE_TABS`` is enabled.

``CONTINUATION_INDENT_WIDTH``
Indent width used for line continuations.
Expand Down
29 changes: 27 additions & 2 deletions yapf/yapflib/format_token.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,25 @@ def _TabbedContinuationAlignPadding(spaces, align_style, tab_width,
return ' ' * spaces


def _SpacedContinuationAlignPadding(spaces, align_style,
continuation_indent_width):
"""Build padding string for continuation alignment in space indentation.

Arguments:
spaces: (int) The number of spaces to place before the token for alignment.
align_style: (str) The alignment style for continuation lines.
continuation_indent_width: (int) Indent columns for line continuations.

Returns:
A padding string for alignment with style specified by align_style option.
"""
if align_style == 'FIXED':
if spaces > 0:
return ' ' * continuation_indent_width
return ''
return ' ' * spaces


class FormatToken(object):
"""A wrapper around pytree Leaf nodes.

Expand Down Expand Up @@ -175,8 +194,14 @@ def AddWhitespacePrefix(self, newlines_before, spaces=0, indent_level=0):
else:
indent_before = '\t' * indent_level + ' ' * spaces
else:
indent_before = (' ' * indent_level * style.Get('INDENT_WIDTH') +
' ' * spaces)
if newlines_before > 0:
indent_before = (' ' * indent_level * style.Get('INDENT_WIDTH') +
_SpacedContinuationAlignPadding(
spaces, style.Get('CONTINUATION_ALIGN_STYLE'),
style.Get('CONTINUATION_INDENT_WIDTH')))
else:
indent_before = (' ' * indent_level * style.Get('INDENT_WIDTH') +
' ' * spaces)

if self.is_comment:
comment_lines = [s.lstrip() for s in self.value.splitlines()]
Expand Down
7 changes: 3 additions & 4 deletions yapf/yapflib/style.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,14 +123,13 @@ def method():

- SPACE: Use spaces for continuation alignment. This is default behavior.
- FIXED: Use fixed number (CONTINUATION_INDENT_WIDTH) of columns
(ie: CONTINUATION_INDENT_WIDTH/INDENT_WIDTH tabs) for continuation
alignment.
(ie: CONTINUATION_INDENT_WIDTH/INDENT_WIDTH tabs or
CONTINUATION_INDENT_WIDTH spaces) for continuation alignment.
- VALIGN-RIGHT: Vertically align continuation lines with indent
characters. Slightly right (one more indent character) if cannot
vertically align continuation lines with indent characters.

For options FIXED, and VALIGN-RIGHT are only available when USE_TABS is
enabled."""),
Option VALIGN-RIGHT only available when USE_TABS is enabled."""),
CONTINUATION_INDENT_WIDTH=textwrap.dedent("""\
Indent width used for line continuations."""),
DEDENT_CLOSING_BRACKETS=textwrap.dedent("""\
Expand Down
27 changes: 27 additions & 0 deletions yapftests/format_token_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,33 @@ def testVAlignRight(self):
self.assertEqual(pad, '\t' * 2)


class SpacedContinuationAlignPaddingTest(unittest.TestCase):

def testSpace(self):
align_style = 'SPACE'

pad = format_token._SpacedContinuationAlignPadding(0, align_style, 4)
self.assertEqual(pad, '')

pad = format_token._SpacedContinuationAlignPadding(2, align_style, 4)
self.assertEqual(pad, ' ' * 2)

pad = format_token._SpacedContinuationAlignPadding(5, align_style, 4)
self.assertEqual(pad, ' ' * 5)

def testFixed(self):
align_style = 'FIXED'

pad = format_token._SpacedContinuationAlignPadding(0, align_style, 8)
self.assertEqual(pad, '')

pad = format_token._SpacedContinuationAlignPadding(2, align_style, 8)
self.assertEqual(pad, ' ' * 8)

pad = format_token._SpacedContinuationAlignPadding(5, align_style, 8)
self.assertEqual(pad, ' ' * 8)


class FormatTokenTest(unittest.TestCase):

def testSimple(self):
Expand Down
27 changes: 27 additions & 0 deletions yapftests/yapf_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1302,6 +1302,33 @@ def foo_function(arg1, arg2,
INDENT_WIDTH=4
CONTINUATION_INDENT_WIDTH=8
CONTINUATION_ALIGN_STYLE = valign-right
"""
with utils.TempFileContents(self.test_tmpdir, style_contents) as stylepath:
self.assertYapfReformats(
unformatted_code,
expected_formatted_code,
extra_options=['--style={0}'.format(stylepath)])

def testUseSpacesContinuationAlignStyleFixed(self):
unformatted_code = """\
def foo_function(arg1, arg2, arg3):
return ['hello', 'world',]
"""
expected_formatted_code = """\
def foo_function(arg1, arg2,
arg3):
return [
'hello',
'world',
]
"""
style_contents = u"""\
[style]
based_on_style = chromium
COLUMN_LIMIT=32
INDENT_WIDTH=4
CONTINUATION_INDENT_WIDTH=8
CONTINUATION_ALIGN_STYLE = fixed
"""
with utils.TempFileContents(self.test_tmpdir, style_contents) as stylepath:
self.assertYapfReformats(
Expand Down