Skip to content

Commit

Permalink
google#844 new line added after logical operator
Browse files Browse the repository at this point in the history
  • Loading branch information
swapnalshahil committed Aug 17, 2020
1 parent 51ffe2d commit a954db0
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 42 deletions.
3 changes: 2 additions & 1 deletion yapf/yapflib/blank_line_calculator.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,8 @@ def _SetNumNewlines(node, num_newlines):

def _StartsInZerothColumn(node):
return (pytree_utils.FirstLeafNode(node).column == 0 or
(_AsyncFunction(node) and node.prev_sibling.column == 0))
(_AsyncFunction(node) and
node.prev_sibling.column == 0))


def _AsyncFunction(node):
Expand Down
9 changes: 6 additions & 3 deletions yapf/yapflib/comment_splicer.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@ def _VisitNodeRec(node):
# Skip empty lines at the top of the first comment group
comment_lineno += 1
continue
elif comment_column is None or col < comment_column:
elif comment_column is None or
col < comment_column:
comment_column = col
comment_indent = cmt[:comment_column]
comment_groups.append((comment_column, comment_indent, []))
Expand Down Expand Up @@ -229,7 +230,8 @@ def _CreateCommentsFromPrefix(comment_prefix,
index = 0
while index < len(lines):
comment_block = []
while index < len(lines) and lines[index].lstrip().startswith('#'):
while index < len(lines) and
lines[index].lstrip().startswith('#'):
comment_block.append(lines[index].strip())
index += 1

Expand Down Expand Up @@ -358,7 +360,8 @@ def _AnnotateIndents(tree):
if child.type == token.INDENT:
child_indent = pytree_utils.GetNodeAnnotation(
tree, pytree_utils.Annotation.CHILD_INDENT)
if child_indent is not None and child_indent != child.value:
if child_indent is not None and
child_indent != child.value:
raise RuntimeError('inconsistent indentation for child', (tree, child))
pytree_utils.SetNodeAnnotation(tree, pytree_utils.Annotation.CHILD_INDENT,
child.value)
Expand Down
14 changes: 10 additions & 4 deletions yapf/yapflib/file_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ def _GetExcludePatternsFromFile(filename):
"""Get a list of file patterns to ignore."""
ignore_patterns = []
# See if we have a .yapfignore file.
if os.path.isfile(filename) and os.access(filename, os.R_OK):
if os.path.isfile(filename) and
os.access(filename, os.R_OK):
with open(filename, 'r') as fd:
for line in fd:
if line.strip() and not line.startswith('#'):
Expand Down Expand Up @@ -157,7 +158,9 @@ def _FindPythonFiles(filenames, recursive, exclude):

python_files = []
for filename in filenames:
if filename != '.' and exclude and IsIgnored(filename, exclude):
if filename != '.' and
exclude and
IsIgnored(filename, exclude):
continue
if os.path.isdir(filename):
if not recursive:
Expand All @@ -167,14 +170,17 @@ def _FindPythonFiles(filenames, recursive, exclude):
# TODO(morbo): Look into a version of os.walk that can handle recursion.
excluded_dirs = []
for dirpath, _, filelist in os.walk(filename):
if dirpath != '.' and exclude and IsIgnored(dirpath, exclude):
if dirpath != '.' and
exclude and
IsIgnored(dirpath, exclude):
excluded_dirs.append(dirpath)
continue
elif any(dirpath.startswith(e) for e in excluded_dirs):
continue
for f in filelist:
filepath = os.path.join(dirpath, f)
if exclude and IsIgnored(filepath, exclude):
if exclude and
IsIgnored(filepath, exclude):
continue
if IsPythonFile(filepath):
python_files.append(filepath)
Expand Down
72 changes: 48 additions & 24 deletions yapf/yapflib/format_decision_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,8 @@ def MustSplit(self):
if not previous:
return False

if style.Get('SPLIT_ALL_COMMA_SEPARATED_VALUES') and previous.value == ',':
if style.Get('SPLIT_ALL_COMMA_SEPARATED_VALUES') and
previous.value == ',':
return True

if (style.Get('SPLIT_ALL_TOP_LEVEL_COMMA_SEPARATED_VALUES') and
Expand All @@ -204,8 +205,10 @@ def MustSplit(self):
return not self._ContainerFitsOnStartLine(opening)

if (self.stack[-1].split_before_closing_bracket and
(current.value in '}]' and style.Get('SPLIT_BEFORE_CLOSING_BRACKET') or
current.value in '}])' and style.Get('INDENT_CLOSING_BRACKETS'))):
(current.value in '}]' and
style.Get('SPLIT_BEFORE_CLOSING_BRACKET') or
current.value in '}])' and
style.Get('INDENT_CLOSING_BRACKETS'))):
# Split before the closing bracket if we can.
if format_token.Subtype.SUBSCRIPT_BRACKET not in current.subtypes:
return current.node_split_penalty != split_penalty.UNBREAKABLE
Expand Down Expand Up @@ -247,7 +250,8 @@ def MustSplit(self):
return True

elif (style.Get('DEDENT_CLOSING_BRACKETS') or
style.Get('INDENT_CLOSING_BRACKETS')) and current.ClosesScope():
style.Get('INDENT_CLOSING_BRACKETS')) and
current.ClosesScope():
# Split before and dedent the closing bracket.
return self.stack[-1].split_before_closing_bracket

Expand Down Expand Up @@ -293,13 +297,17 @@ def SurroundedByParens(token):
func_call_or_string_format = False
tok = current.next_token
if current.is_name:
while tok and (tok.is_name or tok.value == '.'):
while tok and (tok.is_name or
tok.value == '.'):
tok = tok.next_token
func_call_or_string_format = tok and tok.value == '('
func_call_or_string_format = tok and
tok.value == '('
elif current.is_string:
while tok and tok.is_string:
while tok and
tok.is_string:
tok = tok.next_token
func_call_or_string_format = tok and tok.value == '%'
func_call_or_string_format = tok and
tok.value == '%'
if func_call_or_string_format:
open_bracket = unwrapped_line.IsSurroundedByBrackets(current)
if open_bracket:
Expand All @@ -316,7 +324,8 @@ def SurroundedByParens(token):
# If we have a list of tuples, then we can get a similar look as above. If
# the full list cannot fit on the line, then we want a split.
open_bracket = unwrapped_line.IsSurroundedByBrackets(current)
if (open_bracket and open_bracket.value in '[{' and
if (open_bracket and
open_bracket.value in '[{' and
format_token.Subtype.SUBSCRIPT_BRACKET not in open_bracket.subtypes):
if not self._FitsOnLine(current, current.matching_bracket):
return True
Expand All @@ -329,7 +338,8 @@ def SurroundedByParens(token):
# Place each dictionary entry onto its own line.
if previous.value == '{' and previous.previous_token:
opening = _GetOpeningBracket(previous.previous_token)
if (opening and opening.value == '(' and opening.previous_token and
if (opening and opening.value == '(' and
opening.previous_token and
opening.previous_token.is_name):
# This is a dictionary that's an argument to a function.
if (self._FitsOnLine(previous, previous.matching_bracket) and
Expand Down Expand Up @@ -454,7 +464,8 @@ def SurroundedByParens(token):
# KEY_2: 'value two',
# },
# default=False)
if (current.value == '{' and previous.value == '(' and pprevious and
if (current.value == '{' and previous.value == '(' and
pprevious and
pprevious.is_name):
dict_end = current.matching_bracket
next_token = dict_end.next_token
Expand Down Expand Up @@ -483,7 +494,8 @@ def SurroundedByParens(token):
return True

opening = _GetOpeningBracket(current)
if (opening and opening.value == '(' and opening.previous_token and
if (opening and opening.value == '(' and
opening.previous_token and
(opening.previous_token.is_name or
opening.previous_token.value in {'*', '**'})):
is_func_call = False
Expand Down Expand Up @@ -547,7 +559,8 @@ def SurroundedByParens(token):
# These checks rely upon the original formatting. This is in order to
# attempt to keep hand-written code in the same condition as it was before.
# However, this may cause the formatter to fail to be idempotent.
if (style.Get('SPLIT_BEFORE_BITWISE_OPERATOR') and current.value in '&|' and
if (style.Get('SPLIT_BEFORE_BITWISE_OPERATOR') and
current.value in '&|' and
previous.lineno < current.lineno):
# Retain the split before a bitwise operator.
return True
Expand Down Expand Up @@ -722,15 +735,17 @@ def MoveStateToNextToken(self):

# If we encounter a closing bracket, we can remove a level from our
# parenthesis stack.
if len(self.stack) > 1 and current.ClosesScope():
if len(self.stack) > 1 and
current.ClosesScope():
if format_token.Subtype.DICTIONARY_KEY_PART in current.subtypes:
self.stack[-2].last_space = self.stack[-2].indent
else:
self.stack[-2].last_space = self.stack[-1].last_space
self.stack.pop()
self.paren_level -= 1

is_multiline_string = current.is_string and '\n' in current.value
is_multiline_string = current.is_string and
'\n' in current.value
if is_multiline_string:
# This is a multiline string. Only look at the first line.
self.column += len(current.value.split('\n')[0])
Expand Down Expand Up @@ -855,7 +870,8 @@ def _CalculateParameterListState(self, newline):
first_param_column = previous.total_length + self.stack[-2].indent
if not newline:
param_list = self.param_list_stack[-1]
if param_list.parameters and param_list.has_typed_return:
if param_list.parameters and
param_list.has_typed_return:
last_param = param_list.parameters[-1].first_token
last_token = _LastTokenInLine(previous.matching_bracket)
total_length = last_token.total_length
Expand Down Expand Up @@ -888,13 +904,15 @@ def _CalculateParameterListState(self, newline):
param_list = self.param_list_stack[-1]
if current == self.param_list_stack[-1].closing_bracket:
self.param_list_stack.pop() # We're done with this state.
if newline and param_list.has_typed_return:
if newline and
param_list.has_typed_return:
if param_list.split_before_closing_bracket:
penalty -= split_penalty.STRONGLY_CONNECTED
elif param_list.LastParamFitsOnLine(self.column):
penalty += split_penalty.STRONGLY_CONNECTED

if (not newline and param_list.has_typed_return and
if (not newline and
param_list.has_typed_return and
param_list.has_split_before_first_param):
# Prefer splitting before the closing bracket if there's a return type
# and we've already split before the first parameter.
Expand Down Expand Up @@ -981,7 +999,8 @@ def _GetNewlineColumn(self):

if (self.param_list_stack and
not self.param_list_stack[-1].SplitBeforeClosingBracket(
top_of_stack.indent) and top_of_stack.indent
top_of_stack.indent) and
top_of_stack.indent
== ((self.line.depth + 1) * style.Get('INDENT_WIDTH'))):
if (format_token.Subtype.PARAMETER_START in current.subtypes or
(previous.is_comment and
Expand Down Expand Up @@ -1024,7 +1043,8 @@ def DictValueIsContainer(opening, closing):
if not colon.is_pseudo_paren:
break
colon = colon.previous_token
if not colon or colon.value != ':':
if not colon or
colon.value != ':':
return False
key = colon.previous_token
if not key:
Expand Down Expand Up @@ -1136,7 +1156,8 @@ def _IsArgumentToFunction(token):
if not bracket or bracket.value != '(':
return False
previous = bracket.previous_token
return previous and previous.is_name
return previous and
previous.is_name


def _GetOpeningBracket(current):
Expand All @@ -1156,22 +1177,25 @@ def _GetOpeningBracket(current):


def _LastTokenInLine(current):
while not current.is_comment and current.next_token:
while not current.is_comment and
current.next_token:
current = current.next_token
return current


def _IsFunctionDefinition(current):
prev = current.previous_token
return (current.value == '(' and prev and
return (current.value == '(' and
prev and
format_token.Subtype.FUNC_DEF in prev.subtypes)


def _IsLastScopeInLine(current):
current = current.matching_bracket
while current:
current = current.next_token
if current and current.OpensScope():
if current and
current.OpensScope():
return False
return True

Expand Down
15 changes: 10 additions & 5 deletions yapf/yapflib/format_token.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,8 @@ def RetainHorizontalSpacing(self, first_column, depth):
prev_column = previous.node.column
prev_len = len(previous.value)

if previous.is_pseudo_paren and previous.value == ')':
if previous.is_pseudo_paren and
previous.value == ')':
prev_column -= 1
prev_len = 0

Expand Down Expand Up @@ -357,7 +358,8 @@ def is_multiline_string(self):
token, inspect the last 3 characters and return True if it is a triple
double or triple single quote mark.
"""
return self.is_string and self.value.endswith(('"""', "'''"))
return self.is_string and
self.value.endswith(('"""', "'''"))

@property
@py3compat.lru_cache()
Expand All @@ -367,14 +369,17 @@ def is_docstring(self):
@property
@py3compat.lru_cache()
def is_pseudo_paren(self):
return hasattr(self.node, 'is_pseudo') and self.node.is_pseudo
return hasattr(self.node, 'is_pseudo') and
self.node.is_pseudo

@property
def is_pylint_comment(self):
return self.is_comment and re.match(r'#.*\bpylint:\s*(disable|enable)=',
return self.is_comment and
re.match(r'#.*\bpylint:\s*(disable|enable)=',
self.value)

@property
def is_pytype_comment(self):
return self.is_comment and re.match(r'#.*\bpytype:\s*(disable|enable)=',
return self.is_comment and
re.match(r'#.*\bpytype:\s*(disable|enable)=',
self.value)
6 changes: 4 additions & 2 deletions yapf/yapflib/line_joiner.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ def CanMergeMultipleLines(lines, last_was_merged=False):

if lines[0].first.value == 'if':
return _CanMergeLineIntoIfStatement(lines, limit)
if last_was_merged and lines[0].first.value in {'elif', 'else'}:
if last_was_merged and
lines[0].first.value in {'elif', 'else'}:
return _CanMergeLineIntoIfStatement(lines, limit)

# TODO(morbo): Other control statements?
Expand All @@ -97,7 +98,8 @@ def _CanMergeLineIntoIfStatement(lines, limit):
Returns:
True if the lines can be merged, False otherwise.
"""
if len(lines[1].tokens) == 1 and lines[1].last.is_multiline_string:
if len(lines[1].tokens) == 1 and
lines[1].last.is_multiline_string:
# This might be part of a multiline shebang.
return True
if lines[0].lineno != lines[1].lineno:
Expand Down
3 changes: 2 additions & 1 deletion yapf/yapflib/object_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,8 @@ def ends_in_comma(self):
@py3compat.lru_cache()
def last_token(self):
token = self.opening_bracket.matching_bracket
while not token.is_comment and token.next_token:
while not token.is_comment and
token.next_token:
token = token.next_token
return token

Expand Down
6 changes: 4 additions & 2 deletions yapf/yapflib/pytree_unwrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,8 @@ def Visit_async_stmt(self, node): # pylint: disable=invalid-name
if pytree_utils.NodeName(child) == 'ASYNC':
break
for child in node.children[index].children:
if pytree_utils.NodeName(child) == 'NAME' and child.value == 'else':
if pytree_utils.NodeName(child) == 'NAME' and
child.value == 'else':
self._StartNewLine()
self.Visit(child)

Expand Down Expand Up @@ -292,7 +293,8 @@ def DefaultLeafVisit(self, leaf):
"""
if leaf.type in _WHITESPACE_TOKENS:
self._StartNewLine()
elif leaf.type != grammar_token.COMMENT or leaf.value.strip():
elif leaf.type != grammar_token.COMMENT or
leaf.value.strip():
# Add non-whitespace tokens and comments that aren't empty.
self._cur_unwrapped_line.AppendNode(leaf)

Expand Down

0 comments on commit a954db0

Please sign in to comment.