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 for space indentation #791

Merged
merged 2 commits into from
Apr 23, 2020
Merged
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
2 changes: 2 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
slice operator.
### Changed
- Renamed "chromium" style to "yapf". Chromium will now use PEP-8 directly.
- `CONTINUATION_ALIGN_STYLE` with `FIXED` or `VALIGN-RIGHT` now works with
space indentation.
### Fixed
- Honor a disable directive at the end of a multiline comment.
- Don't require splitting before comments in a list when
Expand Down
11 changes: 4 additions & 7 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -434,15 +434,12 @@ 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.
- ``VALIGN-RIGHT``: Vertically align continuation lines with indent
characters. Slightly right (one more indent character) if cannot
(ie: CONTINUATION_INDENT_WIDTH/INDENT_WIDTH tabs or CONTINUATION_INDENT_WIDTH
spaces) for continuation alignment.
- ``VALIGN-RIGHT``: Vertically align continuation lines to multiple of
INDENT_WIDTH columns. Slightly right (one tab or a few spaces) if cannot
vertically align continuation lines with indent characters.

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

``CONTINUATION_INDENT_WIDTH``
Indent width used for line continuations.

Expand Down
19 changes: 17 additions & 2 deletions yapf/yapflib/format_decision_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -921,6 +921,18 @@ def _CalculateParameterListState(self, newline):

return penalty

def _IndentWithContinuationAlignStyle(self, column):
if column == 0:
return column
align_style = style.Get('CONTINUATION_ALIGN_STYLE')
if align_style == 'FIXED':
return ((self.line.depth * style.Get('INDENT_WIDTH')) +
style.Get('CONTINUATION_INDENT_WIDTH'))
if align_style == 'VALIGN-RIGHT':
indent_width = style.Get('INDENT_WIDTH')
return indent_width * int((column + indent_width - 1) / indent_width)
return column

def _GetNewlineColumn(self):
"""Return the new column on the newline."""
current = self.next_token
Expand All @@ -934,8 +946,11 @@ def _GetNewlineColumn(self):
elif current.spaces_required_before > 2 or self.line.disable:
return current.spaces_required_before

cont_aligned_indent = self._IndentWithContinuationAlignStyle(
top_of_stack.indent)

if current.OpensScope():
return top_of_stack.indent if self.paren_level else self.first_indent
return cont_aligned_indent if self.paren_level else self.first_indent

if current.ClosesScope():
if (previous.OpensScope() or
Expand Down Expand Up @@ -973,7 +988,7 @@ def _GetNewlineColumn(self):
format_token.Subtype.PARAMETER_START in previous.subtypes)):
return top_of_stack.indent + style.Get('CONTINUATION_INDENT_WIDTH')

return top_of_stack.indent
return cont_aligned_indent

def _FitsOnLine(self, start, end):
"""Determines if line between start and end can fit on the current line."""
Expand Down
12 changes: 4 additions & 8 deletions yapf/yapflib/format_token.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,25 +62,21 @@ class Subtype(object):
PARAMETER_STOP = 26


def _TabbedContinuationAlignPadding(spaces, align_style, tab_width,
continuation_indent_width):
def _TabbedContinuationAlignPadding(spaces, align_style, tab_width):
"""Build padding string for continuation alignment in tabbed indentation.

Arguments:
spaces: (int) The number of spaces to place before the token for alignment.
align_style: (str) The alignment style for continuation lines.
tab_width: (int) Number of columns of each tab character.
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 align_style in ('FIXED', 'VALIGN-RIGHT'):
if spaces > 0:
return '\t' * int(continuation_indent_width / tab_width)
return '\t' * int((spaces + tab_width - 1) / tab_width)
return ''
elif align_style == 'VALIGN-RIGHT':
return '\t' * int((spaces + tab_width - 1) / tab_width)
return ' ' * spaces


Expand Down Expand Up @@ -171,7 +167,7 @@ def AddWhitespacePrefix(self, newlines_before, spaces=0, indent_level=0):
if newlines_before > 0:
indent_before = '\t' * indent_level + _TabbedContinuationAlignPadding(
spaces, style.Get('CONTINUATION_ALIGN_STYLE'),
style.Get('INDENT_WIDTH'), style.Get('CONTINUATION_INDENT_WIDTH'))
style.Get('INDENT_WIDTH'))
else:
indent_before = '\t' * indent_level + ' ' * spaces
else:
Expand Down
13 changes: 5 additions & 8 deletions yapf/yapflib/style.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,11 @@ 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.
- VALIGN-RIGHT: Vertically align continuation lines with indent
characters. Slightly right (one more indent character) if cannot
vertically align continuation lines with indent characters.

Options FIXED and VALIGN-RIGHT are only available when USE_TABS is
enabled."""),
(ie: CONTINUATION_INDENT_WIDTH/INDENT_WIDTH tabs or
CONTINUATION_INDENT_WIDTH spaces) for continuation alignment.
- VALIGN-RIGHT: Vertically align continuation lines to multiple of
INDENT_WIDTH columns. Slightly right (one tab or a few spaces) if
cannot vertically align continuation lines with indent characters."""),
CONTINUATION_INDENT_WIDTH=textwrap.dedent("""\
Indent width used for line continuations."""),
DEDENT_CLOSING_BRACKETS=textwrap.dedent("""\
Expand Down
22 changes: 11 additions & 11 deletions yapftests/format_token_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,40 +26,40 @@ class TabbedContinuationAlignPaddingTest(unittest.TestCase):
def testSpace(self):
align_style = 'SPACE'

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

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

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

def testFixed(self):
align_style = 'FIXED'

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

pad = format_token._TabbedContinuationAlignPadding(2, align_style, 4, 8)
self.assertEqual(pad, '\t' * 2)
pad = format_token._TabbedContinuationAlignPadding(2, align_style, 4)
self.assertEqual(pad, '\t')

pad = format_token._TabbedContinuationAlignPadding(5, align_style, 4, 8)
pad = format_token._TabbedContinuationAlignPadding(5, align_style, 4)
self.assertEqual(pad, '\t' * 2)

def testVAlignRight(self):
align_style = 'VALIGN-RIGHT'

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

pad = format_token._TabbedContinuationAlignPadding(2, align_style, 4, 8)
pad = format_token._TabbedContinuationAlignPadding(2, align_style, 4)
self.assertEqual(pad, '\t')

pad = format_token._TabbedContinuationAlignPadding(4, align_style, 4, 8)
pad = format_token._TabbedContinuationAlignPadding(4, align_style, 4)
self.assertEqual(pad, '\t')

pad = format_token._TabbedContinuationAlignPadding(5, align_style, 4, 8)
pad = format_token._TabbedContinuationAlignPadding(5, align_style, 4)
self.assertEqual(pad, '\t' * 2)


Expand Down
58 changes: 56 additions & 2 deletions yapftests/yapf_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1269,8 +1269,8 @@ def foo_function(arg1, arg2, arg3):
return ['hello', 'world',]
"""
expected_formatted_code = """\
def foo_function(arg1, arg2,
arg3):
def foo_function(
arg1, arg2, arg3):
return [
'hello',
'world',
Expand Down Expand Up @@ -1312,6 +1312,60 @@ 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(
unformatted_code,
expected_formatted_code,
extra_options=['--style={0}'.format(stylepath)])

def testUseSpacesContinuationAlignStyleVAlignRight(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 = valign-right
"""
with utils.TempFileContents(self.test_tmpdir, style_contents) as stylepath:
self.assertYapfReformats(
Expand Down