Skip to content

Commit

Permalink
fix diff line not match
Browse files Browse the repository at this point in the history
  • Loading branch information
MrWangJustToDo committed Feb 22, 2024
1 parent db9efa5 commit cdde872
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 14 deletions.
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "@git-diff-view/core",
"author": "MrWangJustToDo",
"license": "MIT",
"version": "0.0.5",
"version": "0.0.6",
"main": "index.js",
"types": "index.d.ts",
"files": [
Expand Down
20 changes: 17 additions & 3 deletions packages/core/src/change-range.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ export interface IRange {

/** The length of the range. */
readonly length: number;

readonly isNewLineSymbolChanged?: boolean;
}

const maxLength = 1000;
Expand Down Expand Up @@ -45,10 +47,22 @@ export function relativeChanges(stringA: string, stringB: string): { stringARang

const _stringB = stringB.trimEnd();

if (_stringA === _stringB) {
const aEndStr = stringA.slice(-2);

const bEndStr = stringB.slice(-2);

if (_stringA === _stringB && aEndStr !== bEndStr && (aEndStr === "\r\n" || bEndStr === "\r\n")) {
return {
stringARange: { location: _stringA.length, length: stringA.length - _stringA.length },
stringBRange: { location: _stringB.length, length: stringB.length - _stringB.length },
stringARange: {
location: _stringA.length,
length: stringA.length - _stringA.length,
isNewLineSymbolChanged: true,
},
stringBRange: {
location: _stringB.length,
length: stringB.length - _stringB.length,
isNewLineSymbolChanged: true,
},
};
}

Expand Down
11 changes: 9 additions & 2 deletions packages/core/src/diff-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -435,14 +435,21 @@ export class DiffFile {
const maxNewFileLineNumber = this.#newFileResult?.maxLineNumber || 0;

while (oldFileLineNumber <= maxOldFileLineNumber || newFileLineNumber <= maxNewFileLineNumber) {
const oldRawLine = this.#getOldRawLine(oldFileLineNumber);
const oldDiffLine = this.#getOldDiffLine(oldFileLineNumber);
const newRawLine = this.#getNewRawLine(newFileLineNumber);
const newDiffLine = this.#getNewDiffLine(newFileLineNumber);
const oldRawLine = this.#getOldRawLine(oldFileLineNumber);
const newRawLine = this.#getNewRawLine(newFileLineNumber);
const oldLineHasChange = oldDiffLine?.isIncludeableLine();
const newLineHasChange = newDiffLine?.isIncludeableLine();
const len = this.#splitRightLines.length;
const isHidden = !oldDiffLine && !newDiffLine;
if ((oldDiffLine && !newDiffLine) || (!oldDiffLine && newDiffLine)) {
if (this.#composeByDiff) {
oldDiffLine && newFileLineNumber++;
newDiffLine && oldFileLineNumber++;
continue;
}
}
if (!oldDiffLine && !newRawLine && !oldDiffLine && !newDiffLine) break;
if ((oldLineHasChange && newLineHasChange) || (!oldLineHasChange && !newLineHasChange)) {
this.#splitLeftLines.push({
Expand Down
2 changes: 1 addition & 1 deletion packages/react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "@git-diff-view/react",
"author": "MrWangJustToDo",
"license": "MIT",
"version": "0.0.5",
"version": "0.0.6",
"main": "index.js",
"types": "index.d.ts",
"files": [
Expand Down
22 changes: 20 additions & 2 deletions packages/react/src/components/DiffContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const DiffString = ({
const str1 = rawLine.slice(0, range.location);
const str2 = rawLine.slice(range.location, range.location + range.length);
const str3 = rawLine.slice(range.location + range.length);
const isNewLineSymbolChanged = range.isNewLineSymbolChanged;
return (
<span className="diff-line-content-raw">
<span data-range-start={range.location} data-range-end={range.location + range.length}>
Expand All @@ -30,7 +31,15 @@ const DiffString = ({
operator === "add" ? `var(${addContentHighlightBGName})` : `var(${delContentHighlightBGName})`,
}}
>
{str2 === "\r" ? "␍" : str2 === "\n" ? "␊" : str2 === "\r\n" ? "␍␊" : str2}
{isNewLineSymbolChanged
? str2 === "\r"
? "␍"
: str2 === "\n"
? "␊"
: str2 === "\r\n"
? "␍␊"
: str2
: str2}
</span>
{str3}
</span>
Expand Down Expand Up @@ -82,6 +91,7 @@ const DiffSyntax = ({
const str3 = node.value.slice(index1 + range.length);
const isStart = str1.length || range.location === node.startIndex;
const isEnd = str3.length || node.endIndex === range.location + range.length - 1;
const isNewLineSymbolChanged = range.isNewLineSymbolChanged;
return (
<span
key={index}
Expand All @@ -101,7 +111,15 @@ const DiffSyntax = ({
borderBottomRightRadius: isEnd ? "0.2em" : undefined,
}}
>
{str2 === "\r" ? "␍" : str2 === "\n" ? "␊" : str2 === "\r\n" ? "␍␊" : str2}
{isNewLineSymbolChanged
? str2 === "\r"
? "␍"
: str2 === "\n"
? "␊"
: str2 === "\r\n"
? "␍␊"
: str2
: str2}
</span>
{str3}
</span>
Expand Down
2 changes: 1 addition & 1 deletion packages/vue/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "@git-diff-view/vue",
"author": "MrWangJustToDo",
"license": "MIT",
"version": "0.0.5",
"version": "0.0.6",
"main": "index.js",
"type": "module",
"types": "index.d.ts",
Expand Down
22 changes: 20 additions & 2 deletions packages/vue/src/components/DiffContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const DiffString = ({
const str1 = rawLine.slice(0, range.location);
const str2 = rawLine.slice(range.location, range.location + range.length);
const str3 = rawLine.slice(range.location + range.length);
const isNewLineSymbolChanged = range.isNewLineSymbolChanged;
return (
<span class="diff-line-content-raw">
<span data-range-start={range.location} data-range-end={range.location + range.length}>
Expand All @@ -29,7 +30,15 @@ const DiffString = ({
operator === "add" ? `var(${addContentHighlightBGName})` : `var(${delContentHighlightBGName})`,
}}
>
{str2 === "\r" ? "␍" : str2 === "\n" ? "␊" : str2 === "\r\n" ? "␍␊" : str2}
{isNewLineSymbolChanged
? str2 === "\r"
? "␍"
: str2 === "\n"
? "␊"
: str2 === "\r\n"
? "␍␊"
: str2
: str2}
</span>
{str3}
</span>
Expand Down Expand Up @@ -81,6 +90,7 @@ const DiffSyntax = ({
const str3 = node.value.slice(index1 + range.length);
const isStart = str1.length || range.location === node.startIndex;
const isEnd = str3.length || node.endIndex === range.location + range.length - 1;
const isNewLineSymbolChanged = range.isNewLineSymbolChanged;
return (
<span
key={index}
Expand All @@ -100,7 +110,15 @@ const DiffSyntax = ({
borderBottomRightRadius: isEnd ? "0.2em" : undefined,
}}
>
{str2 === "\r" ? "␍" : str2 === "\n" ? "␊" : str2 === "\r\n" ? "␍␊" : str2}
{isNewLineSymbolChanged
? str2 === "\r"
? "␍"
: str2 === "\n"
? "␊"
: str2 === "\r\n"
? "␍␊"
: str2
: str2}
</span>
{str3}
</span>
Expand Down
4 changes: 2 additions & 2 deletions ui/react-example/src/Example.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@ export function Example() {
</div>
)}
// use data
// data={data[v]}
diffFile={diffFileInstance}
data={data[v]}
// diffFile={diffFileInstance}
extendData={extend}
renderExtendLine={({ data }) => {
return (
Expand Down

0 comments on commit cdde872

Please sign in to comment.