Skip to content

Commit

Permalink
fix vue component & fix some bug
Browse files Browse the repository at this point in the history
  • Loading branch information
MrWangJustToDo committed Feb 23, 2024
1 parent 73e6b3a commit 0c455a0
Show file tree
Hide file tree
Showing 20 changed files with 273 additions and 430 deletions.
25 changes: 0 additions & 25 deletions packages/core/src/diff-file-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ export enum DiffFileLineType {
content = 2,
widget = 3,
extend = 4,
lastHunk = 5,
}

export type DiffSplitContentLineItem = {
Expand Down Expand Up @@ -83,18 +82,6 @@ export const getSplitLines = (diffFile: DiffFile, options: Options): DiffSplitLi
splitLines.push({ type: DiffFileLineType.extend, index, lineNumber: index + 1, extendLine: extendLine });
});

const lastHunkLine = diffFile.getNeedShowExpandAll("split");

const expandEnabled = diffFile.getExpandEnabled();

if (lastHunkLine && expandEnabled) {
splitLines.push({
type: DiffFileLineType.lastHunk,
index: splitLineLength,
lineNumber: splitLineLength + 1,
});
}

return splitLines;
};

Expand Down Expand Up @@ -129,18 +116,6 @@ export const getUnifiedLines = (diffFile: DiffFile, options: Options): DiffUnifi
unifiedLines.push({ type: DiffFileLineType.extend, index, lineNumber: index + 1, extendLine: extendLine });
});

const lastHunkLine = diffFile.getNeedShowExpandAll("unified");

const expandEnabled = diffFile.getExpandEnabled();

if (lastHunkLine && expandEnabled) {
unifiedLines.push({
type: DiffFileLineType.lastHunk,
index: unifiedLineLength,
lineNumber: unifiedLineLength + 1,
});
}

return unifiedLines;
};

Expand Down
178 changes: 91 additions & 87 deletions packages/core/src/diff-file.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
/* eslint-disable @typescript-eslint/ban-ts-comment */
/* eslint-disable max-lines */
import { DiffLineType } from "./diff-line";
import { DiffLineType, DiffLine } from "./diff-line";
import { parseInstance } from "./diff-parse";
import { getFile } from "./file";
import { getDiffRange, getLang } from "./tool";

import type { DiffLine } from "./diff-line";
import type { File } from "./file";
import type { highlighter } from "./highlighter";
import type { IRawDiff } from "./raw-diff";
Expand Down Expand Up @@ -34,7 +33,7 @@ export interface DiffLineItem extends DiffLine {
}

export interface DiffHunkItem extends DiffLineItem {
isAppendLast: boolean;
isLast: boolean;
hunkInfo: {
oldStartIndex: number;
newStartIndex: number;
Expand Down Expand Up @@ -88,14 +87,10 @@ export class DiffFile {

#splitHunksLines?: Record<string, DiffHunkItem>;

#splitLastStartIndex?: number;

#unifiedLines: UnifiedLineItem[] = [];

#unifiedHunksLines?: Record<string, DiffHunkItem>;

#unifiedLastStartIndex?: number;

#listeners: ((() => void) & { isSyncExternal?: boolean })[] = [];

#hasInitRaw: boolean = false;
Expand Down Expand Up @@ -547,9 +542,29 @@ export class DiffFile {
}
}

this.splitLineLength = this.#splitRightLines.length;
// have last hunk
if (Number.isFinite(hideStart)) {
const lastDiff = new DiffLine("", DiffLineType.Hunk, null, null, null);
const lastHunk = lastDiff as DiffHunkItem;
lastHunk.isLast = true;
lastHunk.splitInfo = {
startHiddenIndex: hideStart,
endHiddenIndex: this.#splitRightLines.length,
// just for placeholder
plainText: "",
oldStartIndex: 0,
newStartIndex: 0,
oldLength: 0,
newLength: 0,
};
this.#splitHunksLines = {
...this.#splitHunksLines,
[this.#splitRightLines.length]: lastHunk,
};
hideStart = Infinity;
}

this.#splitLastStartIndex = hideStart;
this.splitLineLength = this.#splitRightLines.length;

this.#hasBuildSplit = true;

Expand Down Expand Up @@ -639,9 +654,29 @@ export class DiffFile {
}
}

this.unifiedLineLength = this.#unifiedLines.length;
// have last hunk
if (Number.isFinite(hideStart)) {
const lastDiff = new DiffLine("", DiffLineType.Hunk, null, null, null);
const lastHunk = lastDiff as DiffHunkItem;
lastHunk.isLast = true;
lastHunk.unifiedInfo = {
startHiddenIndex: hideStart,
endHiddenIndex: this.#unifiedLines.length,
// just for placeholder
plainText: "",
oldStartIndex: 0,
newStartIndex: 0,
oldLength: 0,
newLength: 0,
};
this.#unifiedHunksLines = {
...this.#unifiedHunksLines,
[this.#unifiedLines.length]: lastHunk,
};
hideStart = Infinity;
}

this.#unifiedLastStartIndex = hideStart;
this.unifiedLineLength = this.#unifiedLines.length;

this.#hasBuildUnified = true;

Expand All @@ -660,7 +695,7 @@ export class DiffFile {
return this.#splitHunksLines?.[index];
};

onSplitHunkExpand = (dir: "up" | "down" | "all", index: number) => {
onSplitHunkExpand = (dir: "up" | "down" | "all", index: number, needTrigger = true) => {
const current = this.#splitHunksLines?.[index];
if (!current) return;

Expand All @@ -671,7 +706,6 @@ export class DiffFile {
if (leftLine?.isHidden) leftLine.isHidden = false;
if (rightLine?.isHidden) rightLine.isHidden = false;
}
current.splitInfo.plainText = current.text;
current.splitInfo = {
...current.splitInfo,
...current.hunkInfo,
Expand All @@ -685,12 +719,23 @@ export class DiffFile {
if (leftLine?.isHidden) leftLine.isHidden = false;
if (rightLine?.isHidden) rightLine.isHidden = false;
}
current.splitInfo = {
...current.splitInfo,
startHiddenIndex: current.splitInfo.startHiddenIndex + composeLen,
plainText: `@@ -${current.splitInfo.oldStartIndex},${current.splitInfo.oldLength} +${current.splitInfo.newStartIndex},${current.splitInfo.newLength}`,
};
if (current.isLast) {
current.splitInfo = {
...current.splitInfo,
startHiddenIndex: current.splitInfo.startHiddenIndex + composeLen,
};
} else {
current.splitInfo = {
...current.splitInfo,
startHiddenIndex: current.splitInfo.startHiddenIndex + composeLen,
plainText: `@@ -${current.splitInfo.oldStartIndex},${current.splitInfo.oldLength} +${current.splitInfo.newStartIndex},${current.splitInfo.newLength}`,
};
}
} else {
if (current.isLast) {
__DEV__ && console.error("the last hunk can not expand up!");
return;
}
for (let i = current.splitInfo.endHiddenIndex - composeLen; i < current.splitInfo.endHiddenIndex; i++) {
const leftLine = this.#splitLeftLines[i];
const rightLine = this.#splitRightLines[i];
Expand All @@ -716,29 +761,7 @@ export class DiffFile {
this.#splitHunksLines![current.splitInfo.endHiddenIndex] = current;
}

this.notifyAll();
};

onSplitLastExpand = (expandAll?: boolean) => {
if (!this.#splitLastStartIndex || !Number.isFinite(this.#splitLastStartIndex)) return;

const start = this.#splitLastStartIndex;

const end = expandAll ? this.splitLineLength : this.#splitLastStartIndex + composeLen;

for (let i = start; i < end; i++) {
const leftLine = this.#splitLeftLines[i];
const rightLine = this.#splitRightLines[i];
if (leftLine?.isHidden) leftLine.isHidden = false;
if (rightLine?.isHidden) rightLine.isHidden = false;
}

this.#splitLastStartIndex = end;

this.#splitLastStartIndex =
this.#splitLastStartIndex >= this.splitLineLength ? Infinity : this.#splitLastStartIndex;

this.notifyAll();
needTrigger && this.notifyAll();
};

getUnifiedLine = (index: number) => {
Expand All @@ -749,7 +772,7 @@ export class DiffFile {
return this.#unifiedHunksLines?.[index];
};

onUnifiedHunkExpand = (dir: "up" | "down" | "all", index: number) => {
onUnifiedHunkExpand = (dir: "up" | "down" | "all", index: number, needTrigger = true) => {
const current = this.#unifiedHunksLines?.[index];
if (!current) return;

Expand All @@ -760,7 +783,6 @@ export class DiffFile {
unifiedLine.isHidden = false;
}
}
current.unifiedInfo.plainText = current.text;
current.unifiedInfo = {
...current.unifiedInfo,
...current.hunkInfo,
Expand All @@ -772,12 +794,23 @@ export class DiffFile {
const unifiedLine = this.#unifiedLines[i];
if (unifiedLine?.isHidden) unifiedLine.isHidden = false;
}
current.unifiedInfo = {
...current.unifiedInfo,
startHiddenIndex: current.unifiedInfo.startHiddenIndex + composeLen,
plainText: `@@ -${current.unifiedInfo.oldStartIndex},${current.unifiedInfo.oldLength} +${current.unifiedInfo.newStartIndex},${current.unifiedInfo.newLength}`,
};
if (current.isLast) {
current.unifiedInfo = {
...current.unifiedInfo,
startHiddenIndex: current.unifiedInfo.startHiddenIndex + composeLen,
};
} else {
current.unifiedInfo = {
...current.unifiedInfo,
startHiddenIndex: current.unifiedInfo.startHiddenIndex + composeLen,
plainText: `@@ -${current.unifiedInfo.oldStartIndex},${current.unifiedInfo.oldLength} +${current.unifiedInfo.newStartIndex},${current.unifiedInfo.newLength}`,
};
}
} else {
if (current.isLast) {
__DEV__ && console.error("the last hunk can not expand up!");
return;
}
for (let i = current.unifiedInfo.endHiddenIndex - composeLen; i < current.unifiedInfo.endHiddenIndex; i++) {
const unifiedLine = this.#unifiedLines[i];
if (unifiedLine?.isHidden) unifiedLine.isHidden = false;
Expand All @@ -801,26 +834,20 @@ export class DiffFile {
this.#unifiedHunksLines![current.unifiedInfo.endHiddenIndex] = current;
}

this.notifyAll();
needTrigger && this.notifyAll();
};

onUnifiedLastExpand = (expandAll?: boolean) => {
if (!this.#unifiedLastStartIndex || !Number.isFinite(this.#unifiedLastStartIndex)) return;

const start = this.#unifiedLastStartIndex;

const end = expandAll ? this.unifiedLineLength : this.#unifiedLastStartIndex + composeLen;

for (let i = start; i < end; i++) {
const unifiedLine = this.#unifiedLines[i];
if (unifiedLine?.isHidden) unifiedLine.isHidden = false;
onAllExpand = (mode: "split" | "unified") => {
if (mode === "split") {
Object.keys(this.#splitHunksLines).forEach((key) => {
this.onSplitHunkExpand("all", +key, false);
})
} else {
Object.keys(this.#unifiedHunksLines).forEach((key) => {
this.onUnifiedHunkExpand("all", +key, false);
})
}

this.#unifiedLastStartIndex = end;

this.#unifiedLastStartIndex =
this.#unifiedLastStartIndex >= this.unifiedLineLength ? Infinity : this.#unifiedLastStartIndex;

this.notifyAll();
};

Expand Down Expand Up @@ -852,23 +879,6 @@ export class DiffFile {

getUpdateCount = () => this.#updateCount;

getNeedShowExpandAll = (mode: "split" | "unified") => {
if (mode === "split") {
return (
this.#splitLastStartIndex &&
Number.isFinite(this.#splitLastStartIndex) &&
(this.getSplitLeftLine(this.splitLineLength - 1)?.isHidden ||
this.getSplitRightLine(this.splitLineLength - 1)?.isHidden)
);
} else {
return (
this.#unifiedLastStartIndex &&
Number.isFinite(this.#unifiedLastStartIndex) &&
this.getUnifiedLine(this.unifiedLineLength - 1)?.isHidden
);
}
};

getExpandEnabled = () => !this.#composeByDiff;

getBundle = () => {
Expand All @@ -891,12 +901,10 @@ export class DiffFile {
const splitLeftLines = this.#splitLeftLines;
const splitRightLines = this.#splitRightLines;
const splitHunkLines = this.#splitHunksLines;
const splitLastStartIndex = this.#splitLastStartIndex;

// unified
const unifiedLines = this.#unifiedLines;
const unifiedHunkLines = this.#unifiedHunksLines;
const unifiedLastStartIndex = this.#unifiedLastStartIndex;

return {
hasInitRaw,
Expand All @@ -914,10 +922,8 @@ export class DiffFile {
splitLeftLines,
splitRightLines,
splitHunkLines,
splitLastStartIndex,
unifiedLines,
unifiedHunkLines,
unifiedLastStartIndex,

composeByDiff,
};
Expand All @@ -942,11 +948,9 @@ export class DiffFile {
this.#splitLeftLines = data.splitLeftLines;
this.#splitRightLines = data.splitRightLines;
this.#splitHunksLines = data.splitHunkLines;
this.#splitLastStartIndex = data.splitLastStartIndex;

this.#unifiedLines = data.unifiedLines;
this.#unifiedHunksLines = data.unifiedHunkLines;
this.#unifiedLastStartIndex = data.unifiedLastStartIndex;

this.notifyAll();
};
Expand Down
Loading

0 comments on commit 0c455a0

Please sign in to comment.