Skip to content

Commit c0d79ed

Browse files
committed
Reuse note_from_range inside nearest_node_from_offset
Yet another commit for #19 By using the range version, we get to redo use time complexity from O(nodes) to something closer to O(depth) We also alter the signature of node_from_range to remain backwards compatible With previous versions for obtaining note selection Finally, A small detail to notice Is that the inside function used for single car ets Is slightly changed
1 parent af0b66b commit c0d79ed

File tree

1 file changed

+7
-8
lines changed

1 file changed

+7
-8
lines changed

library/selection_node.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
def nearest_node_from_offset(root,atok,offset):
1111
converter = atok._line_numbers
12-
inside = lambda x,y: (y[0]<=x<y[1])
1312
original_token = atok.get_token_from_offset(offset)
1413
token = original_token
1514
while token.string.isspace() or not token.string:
@@ -22,9 +21,7 @@ def nearest_node_from_offset(root,atok,offset):
2221
if following:
2322
token = following
2423
s = token.startpos
25-
candidates =([(node,atok.get_text_range(node)) for node in ast.walk( root ) if not isinstance(node,ast.Module)
26-
and inside(s,atok.get_text_range(node))])
27-
return min( candidates , key= lambda y :(y[1][1]-y[1][0]) )[0]
24+
return node_from_range(root,atok,(s,s),special= False,lenient = True)
2825

2926
def node_from_range_old(root,atok, r ):
3027
inside = lambda x,y: (y[0]<=x[0]<y[1] and y[0]<x[1]<=y[1])
@@ -38,17 +35,19 @@ def node_from_range_old(root,atok, r ):
3835
return min( candidates , key= lambda y :(y[1][1]-y[1][0]) )[0]
3936

4037

41-
def node_from_range_new(root,atok,r,special = False):
38+
def node_from_range_new(root,atok,r,special = False,lenient = False):
4239
# print(" inside the new note from range\n",root)
4340
inside = lambda x,y: (y[0]<=x[0]<y[1] and y[0]<x[1]<=y[1])
41+
if lenient:
42+
inside = lambda x,y: (y[0]<=x[0]<=y[1] and y[0]<=x[1]<=y[1])
4443
generic_fix(root,atok)
4544
# print(" the fields are now",root._fields)
4645
for child in ast.iter_child_nodes(root):
4746
# print(" just to check something out",child,atok.get_text_range(child))
4847
# print(" and the child fields are ",child._fields)
4948
if inside(r,atok.get_text_range(child)):
5049
# print(" success with",child,"special = ",special)
51-
return node_from_range_new(child,atok,r,special)
50+
return node_from_range_new(child,atok,r,special,lenient)
5251
if special:
5352
# print("Special:\n",ast.dump(root))
5453
if match_node(root,(ast.Tuple,ast.List,ast.Set,ast.Dict,ast.DictComp)):
@@ -66,9 +65,9 @@ def node_from_range_new(root,atok,r,special = False):
6665
return root
6766

6867

69-
def node_from_range(root,atok, r,special = False):
68+
def node_from_range(root,atok, r,special = False,lenient = False):
7069
# return node_from_range_old(root,atok,r)
71-
return node_from_range_new(root,atok,r,special)
70+
return node_from_range_new(root,atok,r,special,lenient)
7271

7372

7473

0 commit comments

Comments
 (0)