cedricrupb / code_diff Goto Github PK
View Code? Open in Web Editor NEWFast AST based code differencing in Python
License: MIT License
Fast AST based code differencing in Python
License: MIT License
Hi, thank you very much for implementing this library.
I would like to have more information about its use for the following scenario: given two code snippets, I would like to know the location of the lines and tokens that have been changed or added.
Let's take this case as an example:
output = cd.difference(
'''
String var = "Hello";
int x = x + 1 + 8;
List list = new ArrayList<>();
''',
'''
String var1 = "Hello";
int x = x + 1;
List list = new ArrayList<>();
List list2 = new ArrayList<>();
''',
lang = "java")
As you can see, the difference between the two code snippets lies in line 1 (renaming the variable 'var' to 'var1'), line 2 (adding a new addition), and finally, line 4 (adding a new statement).
The output of this execution is as follows:
[
Insert((local_variable_declaration, N0), (program, line 1:8 - 4:4), 3),
Insert((generic_type, N1), N0, 0),
Insert((variable_declarator, N2), N0, 1),
Insert(;:;, N0, 2),
Update((identifier:var, line 1:15 - 1:18), var1),
Move((binary_expression, line 2:16 - 2:21), (variable_declarator, line 2:12 - 2:25), 2),
Insert(type_identifier:List, N1, 0),
Insert((type_arguments, N3), N1, 1),
Insert(identifier:list2, N2, 0),
Insert(=:=, N2, 1),
Insert((object_creation_expression, N4), N2, 2),
Insert(<:<, N3, 0),
Insert(type_identifier:String, N3, 1),
Insert(>:>, N3, 2),
Insert(new:new, N4, 0),
Insert((generic_type, N5), N4, 1),
Insert((argument_list, N6), N4, 2),
Insert(type_identifier:ArrayList, N5, 0),
Insert((type_arguments, N7), N5, 1),
Insert((:(, N6, 0),
Insert():), N6, 1),
Insert(<:<, N7, 0),
Insert(>:>, N7, 1),
Delete((+:+, line 2:22 - 2:23)),
Delete((decimal_integer_literal:8, line 2:24 - 2:25)),
Delete((binary_expression, line 2:16 - 2:25))
]
From this output, however, I cannot get the information I mentioned above. Does the library support this functionality or is there a way to get this information?
Thanks again for the support.
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)])
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.