@sczzt
It looks like the idea of traversing the tree from the top-down may ultimately be the correct way to go for the indentation - you can modify the tree at the same time as the text by doing something like:
nodes_to_investigate = [
"arithmetic_series",
"code_block",
"function_block",
"method_name",
"variable_definition",
]
while reached_root == False:
# print(cursor.node.start_point)
# Check Cursor
if cursor.node.type in nodes_to_investigate:
node = cursor.node
print(node)
text_start = Helpers.get_start_of_node(node, newline_offsets)
text_end = Helpers.get_end_of_node(node, newline_offsets)
node_start = node.start_point
old_node_end = (node.end_point[0], node.end_point[1] + 1)
new_node_end = (node.end_point[0], node.end_point[1] + 2)
# node_end = (node.end_point[0] + 1, 0)
data = data[:text_start] + "\t" + data[text_start:]
# newline_offsets = Helpers.get_all_newline_offsets(data)
tree.edit(
start_byte=text_start,
old_end_byte=text_start + 1,
new_end_byte=text_start + 2,
start_point=node_start,
old_end_point=old_node_end,
new_end_point=new_node_end,
)
# Rebuild Tree If Anything Changed
if cursor.goto_first_child():
continue
if cursor.goto_next_sibling():
continue
retracing = True
while retracing:
if not cursor.goto_parent():
retracing = False
reached_root = True
if cursor.goto_next_sibling():
retracing = False
which updates both at the same time. The āget_all_newline_offsetsā is inefficient if itās checking outside of the length of string that can be modified, so I need to write a method that just checks for newlines within the potentially modified string.
But yeah, it looks like Iām almost there with getting indentation on one pass with a DFS of the tree, but thereās still some things to work out. This is tricky!