Skip to content

Commit

Permalink
Merge pull request #425 from hhatto/fix-w503-and-w504-into-comments
Browse files Browse the repository at this point in the history
fix w503 with into line comments
  • Loading branch information
hhatto committed Sep 4, 2018
2 parents 3034398 + 4d53a3d commit 72821e7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
17 changes: 11 additions & 6 deletions autopep8.py
Original file line number Diff line number Diff line change
Expand Up @@ -1190,12 +1190,16 @@ def fix_w503(self, result):
return
# find comment
comment_index = 0
comment_only_linenum = 0
for i in range(5):
# NOTE: try to parse code in 5 times
if (line_index - i) < 0:
break
from_index = line_index - i - 1
to_index = line_index + 1
if self.source[from_index].lstrip()[0] == '#':
comment_only_linenum += 1
continue
try:
ts = generate_tokens("".join(self.source[from_index:to_index]))
except (SyntaxError, tokenize.TokenError):
Expand All @@ -1210,25 +1214,26 @@ def fix_w503(self, result):
tts = ts[newline_index[-3]:]
else:
tts = ts
old = []
old = None
for t in tts:
if tokenize.COMMENT == t[0] and old:
if tokenize.COMMENT == t[0] and old and old[0] != tokenize.NL:
comment_index = old[3][1]
break
old = t
break
i = target.index(one_string_token)
fix_target_line = line_index - 1 - comment_only_linenum
self.source[line_index] = '{}{}'.format(
target[:i], target[i + len(one_string_token):].lstrip())
nl = find_newline(self.source[line_index - 1:line_index])
before_line = self.source[line_index - 1]
nl = find_newline(self.source[fix_target_line:line_index])
before_line = self.source[fix_target_line]
bl = before_line.index(nl)
if comment_index:
self.source[line_index - 1] = '{} {} {}'.format(
self.source[fix_target_line] = '{} {} {}'.format(
before_line[:comment_index], one_string_token,
before_line[comment_index + 1:])
else:
self.source[line_index - 1] = '{} {}{}'.format(
self.source[fix_target_line] = '{} {}{}'.format(
before_line[:bl], one_string_token, before_line[bl:])

def fix_w504(self, result):
Expand Down
7 changes: 6 additions & 1 deletion test/test_autopep8.py
Original file line number Diff line number Diff line change
Expand Up @@ -4334,13 +4334,18 @@ def test_w503_over_5lines(self):
with autopep8_context(line, options=['--select=W503']) as result:
self.assertEqual(fixed, result)

@unittest.skip('TODO')
def test_w503_with_line_comment(self):
line = '(width == 0\n # this is comment\n + height == 0)\n'
fixed = '(width == 0 +\n # this is comment\n height == 0)\n'
with autopep8_context(line, options=['--select=W503', '--ignore=E']) as result:
self.assertEqual(fixed, result)

def test_w503_with_line_comments(self):
line = '(width == 0\n # this is comment\n # comment2\n + height == 0)\n'
fixed = '(width == 0 +\n # this is comment\n # comment2\n height == 0)\n'
with autopep8_context(line, options=['--select=W503', '--ignore=E']) as result:
self.assertEqual(fixed, result)

def test_w504(self):
line = '(width == 0 +\n height == 0)\n'
fixed = '(width == 0\n + height == 0)\n'
Expand Down

0 comments on commit 72821e7

Please sign in to comment.