What is SemanticDiff?

TLDR: SemanticDiff is a programming language aware diff that distinguishes between relevant and irrelevant changes. Irrelevant changes such as added/removed whitespace, optional commas or unnecessary parentheses are hidden to help you focus on the actual changes. Relevant changes are further analyzed to highlight moved code, renames or other refactorings. This helps you understand code changes faster, especially when working with reformatted or refactored code.

SemanticDiff is an alternative diff viewer for Visual Studio Code and GitHub. While SemanticDiff can be used for all text based files through its fallback diff, it is optimized for (semi-)structured text. This includes source code as well as data exchange formats such as JSON. By taking the structure of the text into account, SemanticDiff is able to hide changes that don’t alter the meaning or highlight certain types of changes.

In the context of programming languages, this means that SemanticDiff takes the syntax of the language into account when comparing files. Changes that are visible in the input texts but have no relevant meaning in the language syntax (such as whitespace between parameters or optional commas) are ignored. This is combined with other language specific rules (invariances) that define which code constructs are identical on a semantic level. An example of such an invariance is shown in the following two Python snippets:

foo(a=1, b=2)
foo(b=2, a=1)

While the order of the arguments has changed on a syntactic level, the program flow remains the same because Python uses the argument name of keyword arguments to map the parameters. The semantic meaning of the two snippets is therefore identical.

Further rules are used to detect and highlight typical refactorings, such as renames, to help you better understand what actions the author of the change has performed. You can read more about this on the Refactoring/Grouping page.

Unlike general purpose diff viewers that simply compare text line by line, SemanticDiff requires specific rules and logic for each supported programming language. If you open a file with an unsupported language or format, SemanticDiff will use its fallback diff implementation which doesn’t support most of the features mentioned above. Nevertheless, it can still detect moved code.