code_diff icon indicating copy to clipboard operation
code_diff copied to clipboard

The target node information is not available in update operation

Open mohayemin opened this issue 1 year ago • 1 comments

I have a use case where I need the information of the target nodes in the EditScript. However, only the target node text is passed to the Update objects. For example the code at code_diff/__init__.py:L200 is

if source_ast.type == target_ast.type and len(source_ast.children) == 0 and len(target_ast.children) == 0:
  # Both nodes are tokens of the same type 
  # Only an update is required
  return EditScript([Update(source_ast, target_ast.text)])

I suggest that the last line to be replaced with:

return EditScript([Update(source_ast, target_ast)])

mohayemin avatar Nov 27 '23 23:11 mohayemin

I am not a fan of referencing the target tree in the edit script. I would prefer if the edit script is only dependent on the tree for which it was computed for.

In addition, finding the subtree which is related to specific operation is not always possible (e.g. what is the related subtree for Move or Delete operations?)

If you are only interested in the update operation, you could compute the edit map (see code_diff/gumtree/init.py#L23 ). Whenever there are two nodes with different labels in the edit map, we would generate an update operation (+ some adjustments of the child nodes).

cedricrupb avatar Nov 28 '23 08:11 cedricrupb