Codiff is a library for computing line by line diff using the same diff algorithms in VSCode.
npm install codiffimport{linesDiffComputers}from"codiff";constdoc=`onetwothreefourfive`;constdocModified=doc.replace(/t/g,"T")+"\nSix";constcodiff=newCodiff();constresult=codiff.computeDiff(doc,docmodified);diff result:
{
"changes": [
{
"original": {
"startLineNumber": 2,
"endLineNumberExclusive": 4
},
"modified": {
"startLineNumber": 2,
"endLineNumberExclusive": 4
},
"innerChanges": [
{
"originalRange": {
"startLineNumber": 2,
"startColumn": 1,
"endLineNumber": 2,
"endColumn": 2
},
"modifiedRange": {
"startLineNumber": 2,
"startColumn": 1,
"endLineNumber": 2,
"endColumn": 2
}
},
{
"originalRange": {
"startLineNumber": 3,
"startColumn": 1,
"endLineNumber": 3,
"endColumn": 2
},
"modifiedRange": {
"startLineNumber": 3,
"startColumn": 1,
"endLineNumber": 3,
"endColumn": 2
}
}
]
},
{
"original": {
"startLineNumber": 6,
"endLineNumberExclusive": 6
},
"modified": {
"startLineNumber": 6,
"endLineNumberExclusive": 7
},
"innerChanges": [
{
"originalRange": {
"startLineNumber": 5,
"startColumn": 5,
"endLineNumber": 5,
"endColumn": 5
},
"modifiedRange": {
"startLineNumber": 5,
"startColumn": 5,
"endLineNumber": 6,
"endColumn": 4
}
}
]
}
],
"quitEarly": false,
"identical": false,
"moves": []
}Codiff provide a playground to test the diff algorithm and preview the diff result in code diff editor and JSON editor.
- Diff performance is related to the number of lines of code and the number of changes.
- Suggestion:
- Cache the diff result for same content.
- Use wroker to do the diff in the background.