Skip to content

Commit

Permalink
Ignore end of line # copybara: directives.
Browse files Browse the repository at this point in the history
pylint and pytype end of line comments adjusting their behavior are
already handled specially.  This does the same for copybara.

I don't like that this is turning into a chain of property calls,
refactoring this into a single question of "is this end of line comment
special" would be better if we gain any more of these.

A usual internal comment this will ignore is `# copybara:strip` placed
on a long import line for something being excluded by copybara when
pushing internally maintained code to upstream to keep internal
google-isms from escaping.
  • Loading branch information
gpshead committed Dec 18, 2020
1 parent bbfebc2 commit 560c942
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 4 deletions.
4 changes: 2 additions & 2 deletions yapf/yapflib/format_decision_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -741,8 +741,8 @@ def MoveStateToNextToken(self):

# Calculate the penalty for overflowing the column limit.
penalty = 0
if (not current.is_pylint_comment and not current.is_pytype_comment and
self.column > self.column_limit):
if (not current.is_pylint_comment and not current.is_pytype_comment
and not current.is_copybara_comment and self.column > self.column_limit):
excess_characters = self.column - self.column_limit
penalty += style.Get('SPLIT_PENALTY_EXCESS_CHARACTER') * excess_characters

Expand Down
5 changes: 5 additions & 0 deletions yapf/yapflib/format_token.py
Original file line number Diff line number Diff line change
Expand Up @@ -378,3 +378,8 @@ def is_pylint_comment(self):
def is_pytype_comment(self):
return self.is_comment and re.match(r'#.*\bpytype:\s*(disable|enable)=',
self.value)

@property
def is_copybara_comment(self):
return self.is_comment and re.match(r'#.*\bcopybara:(strip|insert|replace)',
self.value)
3 changes: 2 additions & 1 deletion yapf/yapflib/reformatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,8 @@ def _CanPlaceOnSingleLine(uwline):
indent_amt = style.Get('INDENT_WIDTH') * uwline.depth
last = uwline.last
last_index = -1
if last.is_pylint_comment or last.is_pytype_comment:
if (last.is_pylint_comment or last.is_pytype_comment
or last.is_copybara_comment):
last = last.previous_token
last_index = -2
if last is None:
Expand Down
5 changes: 4 additions & 1 deletion yapftests/reformatter_buganizer_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,10 @@ class _():
def _():
X = (
_ares_label_prefix +
'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa') # pylint: disable=line-too-long
'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' # pylint: disable=line-too-long
'PyTypePyTypePyTypePyTypePyTypePyTypePyTypePyTypePyTypePyTypePyTypePyTypePyType' # pytype: disable=attribute-error
'CopybaraCopybaraCopybaraCopybaraCopybaraCopybaraCopybaraCopybaraCopybara' # copybara:strip
)
"""
uwlines = yapf_test_helper.ParseAndUnwrap(code)
self.assertCodeEqual(code, reformatter.Reformat(uwlines))
Expand Down

0 comments on commit 560c942

Please sign in to comment.