mirror of
https://github.com/davidhalter/parso.git
synced 2025-12-07 21:34:32 +08:00
Fix small issues in prefix positioning.
This commit is contained in:
@@ -11,9 +11,9 @@ class PrefixToken(object):
|
|||||||
|
|
||||||
@property
|
@property
|
||||||
def end_pos(self):
|
def end_pos(self):
|
||||||
if '\n' in self.value:
|
if self.value.endswith('\n'):
|
||||||
return self.start_pos[0] + 1, 0
|
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]*'
|
_comment = r'#[^\n\r\f]*'
|
||||||
@@ -46,6 +46,6 @@ def split_prefix(prefix, start_pos):
|
|||||||
yield PrefixToken(typ, value, (line, column + start))
|
yield PrefixToken(typ, value, (line, column + start))
|
||||||
|
|
||||||
start = match.end(0)
|
start = match.end(0)
|
||||||
if '\n' in value:
|
if value.endswith('\n'):
|
||||||
line += 1
|
line += 1
|
||||||
column = -start
|
column = -start
|
||||||
|
|||||||
@@ -101,7 +101,7 @@ class PythonLeaf(PythonMixin, Leaf):
|
|||||||
__slots__ = ()
|
__slots__ = ()
|
||||||
|
|
||||||
def _split_prefix(self):
|
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):
|
class _LeafWithoutNewlines(PythonLeaf):
|
||||||
|
|||||||
@@ -20,8 +20,21 @@ def test_simple_prefix_splitting(string, tokens):
|
|||||||
tree = parso.parse(string)
|
tree = parso.parse(string)
|
||||||
leaf = tree.children[0]
|
leaf = tree.children[0]
|
||||||
assert leaf.type == 'endmarker'
|
assert leaf.type == 'endmarker'
|
||||||
|
|
||||||
parsed_tokens = list(leaf._split_prefix())
|
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'), [
|
@pytest.mark.parametrize(('string', 'types'), [
|
||||||
|
|||||||
Reference in New Issue
Block a user