-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(vscode): show inline diff of charactors
- Loading branch information
Showing
5 changed files
with
187 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { Range } from "vscode-languageserver"; | ||
import { calcCharDiffRange } from "./diff"; | ||
import { expect } from "chai"; | ||
|
||
describe("diff", () => { | ||
describe("calcCharDiffRange", () => { | ||
it("diff chars test case 1", () => { | ||
/** | ||
* <<<<<<< tabby-000000 | ||
* Deleted Line | ||
* Unchanged Line | ||
* Added Line | ||
* Modified Line before | ||
* Modified Line after | ||
* >>>>>>> tabby-000000 [-=+-+] | ||
*/ | ||
const originText = `Deleted Line | ||
Unchanged Line | ||
Modified Line (Before Changes)`; | ||
const editText = `Unchanged Line | ||
Added Line | ||
Modified Line (After Changes)`; | ||
const editTextRanges: Range[] = [ | ||
{ start: { line: 2, character: 0 }, end: { line: 2, character: 14 } }, | ||
{ start: { line: 3, character: 0 }, end: { line: 3, character: 10 } }, | ||
{ start: { line: 5, character: 0 }, end: { line: 5, character: 29 } }, | ||
]; | ||
const ranges = calcCharDiffRange(originText, editText, editTextRanges); | ||
const diffRanges = [ | ||
{ start: { line: 2, character: 0 }, end: { line: 2, character: 7 } }, | ||
{ start: { line: 3, character: 0 }, end: { line: 3, character: 3 } }, | ||
{ start: { line: 5, character: 15 }, end: { line: 5, character: 18 } }, | ||
]; | ||
expect(ranges).to.deep.equal(diffRanges); | ||
}); | ||
|
||
it("diff chars test case 2", () => { | ||
/** | ||
* <<<<<<< tabby-8td8Ip | ||
* examples/assets/downloads/* | ||
* !examples/assets/downloads/.tracked | ||
* examples/headless/outputs/* | ||
* !examples/headless/outputs/.tracked | ||
* | ||
* examples/Assets/downloads/* | ||
* !examples/Assets/downloads/.tracked | ||
* examples/Headless/outputs/* | ||
* !examples/Headless/outputs/.tracked | ||
* >>>>>>> tabby-8td8Ip [----+++++] | ||
*/ | ||
const originText = `examples/assets/downloads/* | ||
!examples/assets/downloads/.tracked | ||
examples/headless/outputs/* | ||
!examples/headless/outputs/.tracked`; | ||
const editText = ` | ||
examples/Assets/downloads/* | ||
!examples/Assets/downloads/.tracked | ||
examples/Headless/outputs/* | ||
!examples/Headless/outputs/.tracked`; | ||
const editTextRanges: Range[] = [ | ||
{ start: { line: 9, character: 0 }, end: { line: 9, character: 0 } }, | ||
{ start: { line: 10, character: 0 }, end: { line: 10, character: 27 } }, | ||
{ start: { line: 11, character: 0 }, end: { line: 11, character: 35 } }, | ||
{ start: { line: 12, character: 0 }, end: { line: 12, character: 27 } }, | ||
{ start: { line: 13, character: 0 }, end: { line: 13, character: 35 } }, | ||
]; | ||
const ranges = calcCharDiffRange(originText, editText, editTextRanges); | ||
const diffRanges = [ | ||
{ start: { line: 9, character: 0 }, end: { line: 9, character: 1 } }, | ||
{ start: { line: 10, character: 9 }, end: { line: 10, character: 10 } }, | ||
{ start: { line: 11, character: 10 }, end: { line: 11, character: 11 } }, | ||
{ start: { line: 12, character: 9 }, end: { line: 12, character: 10 } }, | ||
{ start: { line: 13, character: 10 }, end: { line: 13, character: 11 } }, | ||
]; | ||
expect(ranges).to.deep.equal(diffRanges); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { Position, Range } from "vscode-languageserver"; | ||
import { diffChars } from "diff"; | ||
|
||
export function calcCharDiffRange(originText: string, editText: string, editTextRanges: Range[]): Range[] { | ||
const diffRanges: Range[] = []; | ||
const changes = diffChars(originText, editText); | ||
let index = 0; | ||
changes.forEach((item) => { | ||
if (item.added) { | ||
const position = getPositionFromIndex(index, editTextRanges); | ||
const addedRange: Range = { | ||
start: position, | ||
end: { line: position.line, character: position.character + (item.count ?? 0) }, | ||
}; | ||
diffRanges.push(addedRange); | ||
index += item.count ?? 0; | ||
} else if (item.removed) { | ||
// nothing | ||
} else { | ||
index += item.count ?? 0; | ||
} | ||
}); | ||
return diffRanges; | ||
} | ||
|
||
export function getPositionFromIndex(index: number, ranges: Range[]): Position { | ||
let line = 0; | ||
let character = 0; | ||
let length = 0; | ||
for (let i = 0; i < ranges.length; i++) { | ||
const range = ranges[i]; | ||
if (!range) { | ||
continue; | ||
} | ||
const rangeLength = range.end.character - range.start.character + length + 1; | ||
if (index >= length && index < rangeLength) { | ||
line = range.start.line; | ||
character = index - length; | ||
return { | ||
line, | ||
character, | ||
}; | ||
} else { | ||
length = rangeLength; | ||
} | ||
} | ||
|
||
return { | ||
line, | ||
character, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters