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

fix w503 with into line comments #425

Merged
merged 1 commit into from
Sep 4, 2018
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
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