Add better line length checks (E501).

This commit is contained in:
Dave Halter
2017-07-06 17:39:23 +02:00
parent 507f190123
commit b2e35bc573
2 changed files with 150 additions and 9 deletions

View File

@@ -455,15 +455,7 @@ class PEP8Normalizer(Normalizer):
self._check_spacing(leaf, spacing)
self._analyse_non_prefix(leaf)
last_column = leaf.end_pos[1]
if last_column > self._config.max_characters:
self.add_issue(
501,
'Line too long (%s > %s characters)' %
(last_column, self._config.max_characters),
leaf
)
self._check_line_length(leaf, spacing)
# -------------------------------
# Finalizing. Updating the state.
# -------------------------------
@@ -504,6 +496,29 @@ class PEP8Normalizer(Normalizer):
self._previous_spacing = spacing
return value
def _check_line_length(self, leaf, spacing):
if leaf.type == 'backslash':
last_column = leaf.start_pos[1] + 1
else:
last_column = leaf.end_pos[1]
if last_column > self._config.max_characters \
and spacing.start_pos[1] <= self._config.max_characters :
# Special case for long URLs in multi-line docstrings or comments,
# but still report the error when the 72 first chars are whitespaces.
report = True
if leaf.type == 'comment':
splitted = leaf.value[1:].split()
if len(splitted) == 1 \
and (leaf.end_pos[1] - len(splitted[0])) < 72:
report = False
if report:
self.add_issue(
501,
'Line too long (%s > %s characters)' %
(last_column, self._config.max_characters),
leaf
)
def _check_spacing(self, leaf, spacing):
def add_if_spaces(*args):
if spaces: