Fix small issues in prefix positioning.

This commit is contained in:
Dave Halter
2017-05-30 09:35:13 +02:00
parent 3e4b2f41cd
commit 8356bcab10
3 changed files with 18 additions and 5 deletions

View File

@@ -11,9 +11,9 @@ class PrefixToken(object):
@property
def end_pos(self):
if '\n' in self.value:
if self.value.endswith('\n'):
return self.start_pos[0] + 1, 0
return self.end_pos[1]
return self.start_pos[0], self.start_pos[1] + len(self.value)
_comment = r'#[^\n\r\f]*'
@@ -46,6 +46,6 @@ def split_prefix(prefix, start_pos):
yield PrefixToken(typ, value, (line, column + start))
start = match.end(0)
if '\n' in value:
if value.endswith('\n'):
line += 1
column = -start

View File

@@ -101,7 +101,7 @@ class PythonLeaf(PythonMixin, Leaf):
__slots__ = ()
def _split_prefix(self):
return split_prefix(self.prefix, self.start_pos)
return split_prefix(self.prefix, self.get_start_pos_of_prefix())
class _LeafWithoutNewlines(PythonLeaf):

View File

@@ -20,8 +20,21 @@ def test_simple_prefix_splitting(string, tokens):
tree = parso.parse(string)
leaf = tree.children[0]
assert leaf.type == 'endmarker'
parsed_tokens = list(leaf._split_prefix())
assert [t.value for t in parsed_tokens] == tokens
start_pos = (1, 0)
for pt, expected in zip(parsed_tokens, tokens):
assert pt.value == expected
# Calculate the estimated end_pos
if expected.endswith('\n'):
end_pos = start_pos[0] + 1, 0
else:
end_pos = start_pos[0], start_pos[1] + len(expected)
#assert start_pos == pt.start_pos
assert end_pos == pt.end_pos
start_pos = end_pos
@pytest.mark.parametrize(('string', 'types'), [