Wolfram Function Repository
Instant-use add-on functions for the Wolfram Language
Function Repository Resource:
Highlight the changes between two expressions
ResourceFunction["ExpressionLineDiff"][expr1,expr2] highlights changes between expr1 and expr2. |
"TrimmingThreshold" | 2 | the maximum number of consecutive unchanged lines to display |
"IndentSize" | 1 | the number of spaces to use per indentation level |
"PageWidth" | 100 | the desired formatting width of the output |
PerformanceGoal | "Quality" | control the level of detail in the highlighting |
"MergeThreshold" | 2 | minimum length of consecutive unchanged characters required to merge into surrounding changes |
Highlight the differences between two expressions on a per-line basis:
In[1]:= | ![]() |
In[2]:= | ![]() |
Out[2]= | ![]() |
Check larger expressions:
In[3]:= | ![]() |
In[4]:= | ![]() |
Out[4]= | ![]() |
Control the maximum number of consecutive unchanged lines to display:
In[5]:= | ![]() |
In[6]:= | ![]() |
Out[6]= | ![]() |
Show all lines:
In[7]:= | ![]() |
Out[7]= | ![]() |
Adjust the amount of indentation:
In[8]:= | ![]() |
In[9]:= | ![]() |
Out[9]= | ![]() |
Reducing the page width will increase the number of lines:
In[10]:= | ![]() |
In[11]:= | ![]() |
Out[11]= | ![]() |
Conversely, a larger page width will decrease the number of lines:
In[12]:= | ![]() |
Out[12]= | ![]() |
By default, ExpressionLineDiff will use darker colors to highlight per-character changes:
In[13]:= | ![]() |
In[14]:= | ![]() |
Out[14]= | ![]() |
With the option setting PerformanceGoal→"Speed", only per-line highlighting will be used:
In[15]:= | ![]() |
Out[15]= | ![]() |
Control how many unchanged characters get merged into surrounding changes:
In[16]:= | ![]() |
In[17]:= | ![]() |
Out[17]= | ![]() |
In[18]:= | ![]() |
Out[18]= | ![]() |
A merge threshold of zero highlights exact changes:
In[19]:= | ![]() |
Out[19]= | ![]() |
Quickly find why two expressions that otherwise appear identical are different:
In[20]:= | ![]() |
Out[20]= | ![]() |
In[21]:= | ![]() |
Out[21]= | ![]() |
These are not in fact SameQ:
In[22]:= | ![]() |
Out[22]= | ![]() |
The difference is due to underlying Dynamic elements that use different local variables:
In[23]:= | ![]() |
Out[23]= | ![]() |
ExpressionLineDiff compares InputForm strings, so highlighting does not necessarily align with subexpressions:
In[24]:= | ![]() |
In[25]:= | ![]() |
Out[25]= | ![]() |
This is comparable to using SequenceAlignment on the string representations:
In[26]:= | ![]() |
Out[26]= | ![]() |
In[27]:= | ![]() |
Out[27]= | ![]() |
For flat lists, SequenceAlignment can identify changes per element:
In[28]:= | ![]() |
Out[28]= | ![]() |
In[29]:= | ![]() |
Out[29]= | ![]() |
With the option setting PerformanceGoal→"Speed", ExpressionLineDiff only looks for changes on a per-line basis and does not highlight detailed changes:
In[30]:= | ![]() |
In[31]:= | ![]() |
Out[31]= | ![]() |
Using a smaller page width can increase the accuracy of highlighting at the cost of readability:
In[32]:= | ![]() |
Out[32]= | ![]() |
For small expressions, it's better to use the default option setting PerformanceGoal→"Quality" to add additional per-character highlighting:
In[33]:= | ![]() |
Out[33]= | ![]() |
This work is licensed under a Creative Commons Attribution 4.0 International License