Skip to content

Commit

Permalink
Fix most diff tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
davidhalter committed Jan 22, 2017
1 parent 005b24e commit 8f4b862
Showing 1 changed file with 17 additions and 12 deletions.
29 changes: 17 additions & 12 deletions jedi/parser/diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ def _is_flow_node(node):
return value in ('if', 'for', 'while', 'try')


'''
def _last_leaf_is_newline(last_leaf):
if last_leaf.prefix.endswith('\n'):
return True
Expand All @@ -103,6 +104,7 @@ def _last_leaf_is_newline(last_leaf):
previous_leaf.original_type == 'newline')
'''
def _update_positions(nodes, line_offset):
for node in nodes:
try:
Expand Down Expand Up @@ -187,7 +189,10 @@ def update(self, lines_new):

self._parser.source = ''.join(lines_new)

#assert self._module.end_pos[0] == line_length
if self._module.end_pos[0] == line_length:
# In case of failure.
assert self._module.get_code() == self._parser.source
assert self._module.end_pos[0] == line_length

return self._module

Expand Down Expand Up @@ -222,7 +227,7 @@ def _copy_from_old_parser(self, line_offset, until_line_old, until_line_new):
self._copy_count += 1

from_ = copied_nodes[0].get_start_pos_of_prefix()[0]
to = _get_last_line(copied_nodes[-1])
to = self._nodes_stack.parsed_until_line
self._copied_ranges.append((from_, to))

debug.dbg('diff actually copy %s to %s', from_, to)
Expand Down Expand Up @@ -308,7 +313,7 @@ def _parse(self, until_line):

debug.dbg(
'parse part %s to %s',
self._nodes_stack.parsed_until_line,
self._nodes_stack.parsed_until_line + 1,
node.end_pos[0] - 1
)
self._nodes_stack.add_parsed_nodes(nodes)
Expand Down Expand Up @@ -441,7 +446,7 @@ def update_last_children_group(self, new_children):
group = self.ChildrenGroup(new_children, self.children_groups[-1].line_offset)
self.children_groups[-1] = group

def get_last_line(self):
def get_last_line(self, suffix):
if not self.children_groups:
assert not self.parent
return 0
Expand All @@ -454,16 +459,17 @@ def get_last_line(self):
while element is not None:
line += element.children_groups[-1].line_offset
element = element.parent
print(last_leaf.end_pos, line, self.children_groups)

# Newlines end on the next line, which means that they would cover
# the next line. That line is not fully parsed at this point.
if last_leaf.type == 'error_leaf':
typ = last_leaf.original_type
else:
typ = last_leaf.type
if typ == 'newline':

if typ == 'newline' or suffix.endswith('\n'):
line -= 1
line += suffix.count('\n')
return line


Expand All @@ -481,9 +487,8 @@ def is_empty(self):
return not self._base_node.children

@property
def parsed_until_line(self):
print('until_line', self._tos.get_last_line() , self.prefix.count('\n'))
return self._tos.get_last_line() + self.prefix.count('\n')
def parsed_until_line(self, ):
return self._tos.get_last_line(self.prefix)

def _get_insertion_node(self, indentation_node):
indentation = indentation_node.start_pos[1]
Expand Down Expand Up @@ -574,6 +579,8 @@ def _copy_nodes(self, tos, nodes, until_line, line_offset=0):
# Endmarkers just distort all the checks below. Remove them.
break

if node.start_pos[0] > until_line:
break
# TODO this check might take a bit of time for large files. We
# might want to change this to do more intelligent guessing or
# binary search.
Expand All @@ -596,7 +603,6 @@ def _copy_nodes(self, tos, nodes, until_line, line_offset=0):
# parent.
suite_tos = _NodesStackNode(suite)
suite_nodes, recursive_tos = self._copy_nodes(suite_tos, suite.children, until_line)
print(suite_nodes)
if len(suite_nodes) < 2:
# A suite only with newline is not valid.
new_nodes.pop()
Expand All @@ -616,10 +622,9 @@ def _copy_nodes(self, tos, nodes, until_line, line_offset=0):
new_nodes.pop()
while new_nodes:
last_node = new_nodes[-1]
new_nodes.pop()
if last_node.last_leaf().type == 'newline':
break
print('x', new_nodes)
new_nodes.pop()

if new_nodes:
tos.add(new_nodes, line_offset)
Expand Down

0 comments on commit 8f4b862

Please sign in to comment.