Skip to content

Commit

Permalink
Include trailing comments in the literally quoted pyxl block (gvanros…
Browse files Browse the repository at this point in the history
…sum#17)

This keeps the black+pyxl combo from shifting type: ignores off the
line.
  • Loading branch information
msullivan committed Oct 21, 2019
1 parent 3cfddef commit c0c6c96
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 0 deletions.
2 changes: 2 additions & 0 deletions pyxl/codec/pytokenize.py
Expand Up @@ -259,6 +259,8 @@ def feed(self, t):
tok_type, token, start, end, line = t
if (self.prev_row is None):
self.prev_row, self.prev_col = start
if tok_type in (NEWLINE, NL):
self.prev_col = end[1]
self.add_whitespace(start)
self.tokens.append(token)
self.prev_row, self.prev_col = end
Expand Down
13 changes: 13 additions & 0 deletions pyxl/codec/tokenizer.py
Expand Up @@ -308,6 +308,19 @@ def get_pyxl_token(start_token, tokens, invertible):

pyxl_parser_start = Pos(*pyxl_parser.start)
if invertible:
# We want to include a trailing comment after a pyxl close in
# the pyxl block itself to prevent black from migrating it.
# (which matters a lot if it is a type: ignore.)
try:
peek = fix_token(next(tokens))
except StopIteration:
pass
else:
if peek.ttype == tokenize.COMMENT:
pyxl_tokens.append(peek)
else:
tokens.unshift(peek)

output = "html.PYXL('''{}''', {}, {}, {}{}{})".format(
untokenize(pyxl_tokens).replace('\\', '\\\\').replace("'", "\\'"),
# Include the real compiled pyxl so that tools can see all the gritty details
Expand Down
7 changes: 7 additions & 0 deletions tests/test_black_input.py
Expand Up @@ -79,3 +79,10 @@ def foo():

))}
</div>)

def bar(self):
return (
<something
class="foo"
/> # type: ignore[arg-type]
)
7 changes: 7 additions & 0 deletions tests/test_black_input.py.exp
Expand Up @@ -125,3 +125,10 @@ class B:
}
</div>
)

def bar(self):
return (
<something
class="foo"
/> # type: ignore[arg-type]
)

0 comments on commit c0c6c96

Please sign in to comment.