Michael Müller
31 Aug, 2023

SemanticDiff 0.8.6: Support For Java And More

The sixth beta release of our VS Code extension / GitHub App SemanticDiff adds support for Java, introduces invariances for integer literals and more.

We have just released version 0.8.6 of SemanticDiff - our Visual Studio Code extension and GitHub App. This update adds support for generating programming language aware diffs for Java. Other notable improvements include more options to expand context lines and support for more invariances.

If you are using the GitHub App, all updates have already been rolled out and no action is required on your part. VS Code users will usually receive the update automatically when the editor checks for extension updates. If not, you can get the latest packages from the Visual Studio Marketplace:

Install VS Code Extension

Java Support

The most significant change in SemanticDiff 0.8.6 is that it can now generate programming language aware diffs for Java. What this means in practice is that you can reformat your Java code and SemanticDiff should display an empty diff as it ignores style-only changes. This is especially useful if your files also contain other modifications that would be easily missed in a standard diff amidst all the format changes.

If you haven’t used SemanticDiff yet, here is a screenshot of a toy example to demonstrate what this looks like:

Java code changes in SemanticDiff

On the right side a new parameter has been added, while the existing ones have been split into multiple lines. SemanticDiff highlights only the newly added parameter and indicates that the old line is mapped to multiple lines in the new code.

This toy example shows only a subset of the supported features. The full list of invariances (i.e. changes that are not displayed in the diff) include:

  • Adding/Removing whitespaces or line breaks outside of strings and comments
  • Adding/Removing optional commas
  • Adding/Removing unnecessary parenthesis
  • Exchanging numeric literals with equivalent ones in different bases
  • Exchanging escaped characters in a string/char with an equivalent representation (e.g. the literal)

As with any other supported language, SemanticDiff can also detect and highlight moved code blocks in Java:

Moved Java code in SemanticDiff

If you like to see more examples, checkout these OpenJDK commits with our GitHub App: [1] [2] [3]

Expand Context Lines

By default SemanticDiff mimics git’s behavior and only shows 3 lines of context around changes. So far you had two options if you wanted to view more of the surrounding code: You could either move your cursor near a hunk header and click the “Show More” button to expand 20 lines, or you could click on a scope (function/class/struct name, etc.) in the hunk header to fully expand it.

This release gives you another option that is useful if you want to expand larger areas in a diff. There is a new button in the middle of the hunk header that expands all the code substituted by that header. So if you click the button in the last hunk header, it will add all the remaining code till the end of the document. All previous buttons expand the code between the current hunk and the previous hunk (or the beginning of the document).

SemanticDiff - Expand Hunk

If you are using the Visual Studio Code extension, you can now also expand all context by selecting the “Expand Context Lines” button from the action menu.

SemanticDiff - Expand All Hunks

If you want to collapse the context lines again, you can select the “Collapse Context Lines” menu item.

Comparing Integers

SemanticDiff hides changes that don’t affect program execution to help you focus on the relevant changes. This release continues that idea and adds another invariance: SemanticDiff can now compare integers independently of their base. This means that the diff algorithm knows that 0x80 and 128 are the same numbers.

For example, SemanticDiff 0.8.6 treats the following two Python snippets as identical and does not display any changes:

result = 0x157 + 1_2_6 * 0b1011_0001 + 0o644
result = 343 + 0x7E * 0o261 + 0b1101_00100

This feature is enabled for C#, Go, Java, JavaScript, JSON5, Python and TypeScript. There is currently a limitation that will be removed in a future release: If the number is larger than 64 bit or contains a size postfix (L/UL/…), the comparison falls back to a text based approach.

Minor Changes

This release also introduces two minor changes:

  • SemanticDiff can now be used in a restricted VS Code workspace. We do not believe that our extension poses any danger when used with untrusted code. The code is not evaluated nor can it have any other adverse effects on your system.

  • We fixed a bug in the comment handling code that could cause changes in comments to be displayed incorrectly.

We hope you enjoyed this update. If you encounter any issues, please let us know in our issue tracker.

Recent Articles

SemanticDiff vs. Difftastic: How do they differ?
SemanticDiff vs. Difftastic: How do they differ?

Both tools aim to provide a better diff, but which one fits your needs? We compared their inner workings, features, and workflow integration to help you decide.

Read More
Unicode tricks in pull requests: Do review tools warn us?
Unicode tricks in pull requests: Do review tools warn us?

How well do GitHub, GitLab and Bitbucket support reviewers in finding malicious code changes in pull requests? Let’s give it a test.

Read More
SemanticDiff 0.8.8: Support For Rust And More
SemanticDiff 0.8.8: Support For Rust And More

The eight beta release of our VS Code extension / GitHub App SemanticDiff adds support for Rust, new invariances and other enhancements.

Read More