Diff utility used in the Visual Studio Code source.
npm install vscode-diffThe package includes typescript definitions.
bash
npm install vscode-diff --save
`
Usage
Starting from version 1.71.0 VS Code introduced new diffing algorithm that can also detect code moves. It is currently used by default in VS Code and can be accessed in this library as
DefaultLinesDiffComputer.
The legacy algorithm can be accessed as DiffComputer. It is also possible to use the wrapper class LegacyLinesDiffComputer which adapts the output of DiffComputer to a newer format.$3
`typescript
const defaultOptions: ILinesDiffComputerOptions = {
ignoreTrimWhitespace: true,
computeMoves: true,
maxComputationTimeMs: 0
}
const defaultDiffComputer = new DefaultLinesDiffComputer()
const defaultLineChanges = defaultDiffComputer.computeDiff(originalLines, modifiedLines, defaultOptions).changes;console.log(JSON.stringify(defaultLineChanges, null, 2));
`Output:
`json
[
{
"original": {
"startLineNumber": 2,
"endLineNumberExclusive": 3
},
"modified": {
"startLineNumber": 2,
"endLineNumberExclusive": 3
},
"innerChanges": [
{
"originalRange": {
"startLineNumber": 2,
"startColumn": 1,
"endLineNumber": 2,
"endColumn": 9
},
"modifiedRange": {
"startLineNumber": 2,
"startColumn": 1,
"endLineNumber": 2,
"endColumn": 9
}
}
]
},
{
"original": {
"startLineNumber": 4,
"endLineNumberExclusive": 4
},
"modified": {
"startLineNumber": 4,
"endLineNumberExclusive": 5
},
"innerChanges": [
{
"originalRange": {
"startLineNumber": 3,
"startColumn": 6,
"endLineNumber": 3,
"endColumn": 6
},
"modifiedRange": {
"startLineNumber": 3,
"startColumn": 6,
"endLineNumber": 4,
"endColumn": 7
}
}
]
}
]
`The format of the result mapping is similar to that returned by
DiffComputer but beware that then ending line number in line range is exclusive, e.g.
`json
"originalRange": {
"startLineNumber": 2,
"endLineNumberExclusive": 3
}
`as opposed to
`json
"originalStartLineNumber": 2,
"originalEndLineNumber": 2,
`$3
`typescript
import { DiffComputer, IDiffComputerOpts, ILineChange } from 'vscode-diff';const originalLines: string[] = ["hello", "original", "world"];
const modifiedLines: string[] = ["hello", "modified", "world", "foobar"];
const options: IDiffComputerOpts = {
shouldPostProcessCharChanges: true,
shouldIgnoreTrimWhitespace: true,
shouldMakePrettyDiff: true,
shouldComputeCharChanges: true,
maxComputationTime: 0 // time in milliseconds, 0 => no computation limit.
}
const diffComputer = new DiffComputer(originalLines, modifiedLines, options);
const lineChanges: ILineChange[] = diffComputer.computeDiff().changes;
console.log(JSON.stringify(lineChanges, null, 2));
`
Output:
`json
[
{
"originalStartLineNumber": 2,
"originalEndLineNumber": 2,
"modifiedStartLineNumber": 2,
"modifiedEndLineNumber": 2,
"charChanges": [
{
"originalStartLineNumber": 2,
"originalStartColumn": 1,
"originalEndLineNumber": 2,
"originalEndColumn": 9,
"modifiedStartLineNumber": 2,
"modifiedStartColumn": 1,
"modifiedEndLineNumber": 2,
"modifiedEndColumn": 9
}
]
},
{
"originalStartLineNumber": 3,
"originalEndLineNumber": 0,
"modifiedStartLineNumber": 4,
"modifiedEndLineNumber": 4
}
]
`
Each element in the produced lineChanges array corresponds to a change from the original lines to the modified lines.The column and row indices are 1-based. If a 0 index is present, it means that a row has been added/removed, eg:
`json
{
"originalStartLineNumber": 3,
"originalEndLineNumber": 0,
"modifiedStartLineNumber": 4,
"modifiedEndLineNumber": 4
}
`
means that the 4th line in the modified text was added after line 3 in the original text.The opposite:
`json
{
"originalStartLineNumber": 4,
"originalEndLineNumber": 4,
"modifiedStartLineNumber": 3,
"modifiedEndLineNumber": 0
}
`
means that the 4th line in the original text was removed from after line 3 in the modified text.
Changelog
$3
* Remove vscode settings from npm package$3
$3
* The class AdvancedLinesDiffComputer has change name to DefaultLinesDiffComputer
* THe output of DefaultLinesDiffComputer has renamed keys:
`
// OLD
[
{
"originalRange": {
"startLineNumber": 2,
"endLineNumberExclusive": 3
},
"modifiedRange": {
"startLineNumber": 2,
"endLineNumberExclusive": 3
}
}
]
// NEW
[
{
"original": {
"startLineNumber": 2,
"endLineNumberExclusive": 3
},
"modified": {
"startLineNumber": 2,
"endLineNumberExclusive": 3
}
}
]
`
$3
* Various refactorings and bug fixes such as:
- https://github.com/microsoft/vscode/pull/213035$3
* Fix build errors in 2.1.0 #3$3
* Update to VS Code 1.82.1 that introduces new diffing algorithm AdvancedLinesDiffComputer.$3
* Fix issue 121436$3
* Fix missing typescript types
* Fix issue 119051$3
* New DiffComputer option: maxComputationTime. Specify maximum time that the diff computer should run. Specify 0 for no limit. For character changes (charChanges) there is a new hard coded maximum limit of 5 seconds.
* New return type from diffComputer:
`
interface IDiffComputerResult {
quitEarly: boolean;
changes: ILineChange[];
}
`$3
Initial releaseContribute
Since we do not want this package to differ from the original implementation in VS Code, no changes that differs from the source repository will be merged. Any changes that only affect this npm package (like changes to this README) are welcome via pull requests.Steps for updating diff algorithm:
* Copy all necessary files from VS Code repo.
* Verify with
npm run build that all code is self-contained.
* Verify with npm run knip that there are no unused files or exports.
* Run npm test to run all the tests.
* Update [example/example.ts] on any API changes.
* Run npm run example` and update this README with example usage code and output.Any help documenting the diff API is very welcome.
Only minor modifications have been made to the source files:
* Removed code unused by the diff implementation.
* Updated import paths to reflect their new location.
* Added comment in each source file with a commit hash from their last modification.
I am in no way affiliated to Microsoft or the VS Code team.