Vipitis commited on
Commit
abed9bd
1 Parent(s): 50c1955

rework indecies, will continue

Browse files
Files changed (1) hide show
  1. utils/tree_utils.py +14 -6
utils/tree_utils.py CHANGED
@@ -30,8 +30,10 @@ def node_str_idx(node):
30
  returns the character index of start and end of a node
31
  """
32
  whole_text = get_root(node).text.decode()
33
- start_idx = line_chr2char(whole_text, node.start_point[0], node.start_point[1])
34
- end_idx = line_chr2char(whole_text, node.end_point[0], node.end_point[1])
 
 
35
  return start_idx, end_idx
36
 
37
  def give_tree(func_node):
@@ -68,16 +70,18 @@ def get_docstrings(func_node):
68
  return docstring
69
  return docstring
70
 
71
- def full_func_head(func_node):
72
  """
73
  returns function head including docstrings before any real body code
74
  """
75
  cursor = func_node.child_by_field_name("body").walk()
76
  cursor.goto_first_child()
77
  while cursor.node.type == "comment" or cursor.node.type == "{":
 
78
  cursor.goto_next_sibling()
79
  end = cursor.node.start_point
80
- return "\n".join(func_node.text.decode().split("\n")[:(end[0]-func_node.start_point[0])])
 
81
 
82
  def grab_before_comments(func_node):
83
  """
@@ -93,7 +97,7 @@ def grab_before_comments(func_node):
93
  last_comment_line = node.start_point[0]
94
  elif node == func_node:
95
  return precomment
96
- return precomment
97
 
98
  def has_docstrings(func_node):
99
  """
@@ -104,11 +108,15 @@ def has_docstrings(func_node):
104
 
105
  def line_chr2char(text, line_idx, chr_idx):
106
  """
 
107
  returns the character index at the given line and character index.
108
  """
109
  lines = text.split("\n")
110
  char_idx = 0
111
  for i in range(line_idx):
112
- char_idx += len(lines[i]) + 1
 
 
 
113
  char_idx += chr_idx
114
  return char_idx
 
30
  returns the character index of start and end of a node
31
  """
32
  whole_text = get_root(node).text.decode()
33
+ # start_idx = line_chr2char(whole_text, node.start_point[0], node.start_point[1])
34
+ # end_idx = line_chr2char(whole_text, node.end_point[0], node.end_point[1])
35
+ start_idx = node.start_byte #actual numbers?
36
+ end_idx = node.end_byte
37
  return start_idx, end_idx
38
 
39
  def give_tree(func_node):
 
70
  return docstring
71
  return docstring
72
 
73
+ def full_func_head(func_node) -> str:
74
  """
75
  returns function head including docstrings before any real body code
76
  """
77
  cursor = func_node.child_by_field_name("body").walk()
78
  cursor.goto_first_child()
79
  while cursor.node.type == "comment" or cursor.node.type == "{":
80
+ last_char = cursor.node.end_byte
81
  cursor.goto_next_sibling()
82
  end = cursor.node.start_point
83
+ # return "\n".join(func_node.text.decode().split("\n")[:(end[0]-func_node.start_point[0])])[:-(last_char)-1]
84
+ return func_node.text.decode()[:(last_char - func_node.start_byte)]
85
 
86
  def grab_before_comments(func_node):
87
  """
 
97
  last_comment_line = node.start_point[0]
98
  elif node == func_node:
99
  return precomment
100
+ return precomment
101
 
102
  def has_docstrings(func_node):
103
  """
 
108
 
109
  def line_chr2char(text, line_idx, chr_idx):
110
  """
111
+ ## just use strat_byte and end_byte instead!
112
  returns the character index at the given line and character index.
113
  """
114
  lines = text.split("\n")
115
  char_idx = 0
116
  for i in range(line_idx):
117
+ try:
118
+ char_idx += len(lines[i]) + 1
119
+ except IndexError as e:
120
+ raise IndexError(f"{i=} of {line_idx=} does not exist in {text=}") from e
121
  char_idx += chr_idx
122
  return char_idx