Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions packages/core/src/parse/diff-parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,22 @@ const DiffPrefixAdd = "+" as const;
const DiffPrefixDelete = "-" as const;
const DiffPrefixContext = " " as const;
const DiffPrefixNoNewline = "\\" as const;
// https://github.com/MrWangJustToDo/git-diff-view/issues/41
// some line only have a new line symbol without any other character
const DIffPrefixNewLine = "\n" as const;

type DiffLinePrefix =
| typeof DiffPrefixAdd
| typeof DiffPrefixDelete
| typeof DiffPrefixContext
| typeof DiffPrefixNoNewline;
| typeof DiffPrefixNoNewline
| typeof DIffPrefixNewLine;
const DiffLinePrefixChars: Set<DiffLinePrefix> = new Set([
DiffPrefixAdd,
DiffPrefixDelete,
DiffPrefixContext,
DiffPrefixNoNewline,
DIffPrefixNewLine,
]);

interface IDiffHeaderInfo {
Expand Down Expand Up @@ -127,6 +132,19 @@ export class DiffParser {

// We've succeeded if there's anything to read in between the
// start and the end
/**
* https://github.com/MrWangJustToDo/git-diff-view/issues/41
* some file content may have multiple line without any character
* such as
* ```
*
*
*
* +
* +
* ```
* this will cause ls === le, but we not in the end of file
*/
return this.ls !== this.le;
}

Expand All @@ -139,7 +157,7 @@ export class DiffParser {
if (header) {
return this.nextLine() ? this.text.substring(this.ls, this.le) : null;
} else {
return this.nextLine() ? this.text.substring(this.ls + 1, this.le + 1) : null;
return this.nextLine() ? this.text.substring(this.ls + 1, this.le + 1) : this.text.length > this.ls ? "\n" : null;
}
}

Expand Down Expand Up @@ -353,7 +371,7 @@ export class DiffParser {
diffLine = new DiffLine(line, DiffLineType.Add, diffLineNumber, null, rollingDiffAfterCounter++);
} else if (c === DiffPrefixDelete) {
diffLine = new DiffLine(line, DiffLineType.Delete, diffLineNumber, rollingDiffBeforeCounter++, null);
} else if (c === DiffPrefixContext) {
} else if (c === DiffPrefixContext || c === DIffPrefixNewLine) {
diffLine = new DiffLine(
line,
DiffLineType.Context,
Expand Down
7 changes: 7 additions & 0 deletions packages/react/src/components/DiffSplitViewNormal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
diffFontSizeName,
borderColorName,
diffAsideWidthName,
getDiffIdFromElement,
} from "@git-diff-view/utils";
import { Fragment, memo, useEffect, useRef } from "react";
import * as React from "react";
Expand Down Expand Up @@ -137,6 +138,12 @@ export const DiffSplitViewNormal = memo(({ diffFile }: { diffFile: DiffFile }) =
return;
}

const id = getDiffIdFromElement(ele as HTMLElement);

if (id && id !== `diff-root${diffFile.getId()}`) {
return;
}

while (ele && ele instanceof HTMLElement) {
const state = ele.getAttribute("data-state");
const side = ele.getAttribute("data-side");
Expand Down
8 changes: 7 additions & 1 deletion packages/react/src/components/DiffSplitViewWrap.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable @typescript-eslint/ban-ts-comment */
import { type DiffFile, getSplitContentLines } from "@git-diff-view/core";
import { diffAsideWidthName, diffFontSizeName, removeAllSelection } from "@git-diff-view/utils";
import { diffAsideWidthName, diffFontSizeName, getDiffIdFromElement, removeAllSelection } from "@git-diff-view/utils";
import { Fragment, memo, useMemo, useRef } from "react";
import * as React from "react";
// SEE https://github.com/facebook/react/pull/25231
Expand Down Expand Up @@ -65,6 +65,12 @@ export const DiffSplitViewWrap = memo(({ diffFile }: { diffFile: DiffFile }) =>
return;
}

const id = getDiffIdFromElement(ele as HTMLElement);

if (id && id !== `diff-root${diffFile.getId()}`) {
return;
}

while (ele && ele instanceof HTMLElement) {
const state = ele.getAttribute("data-state");
const side = ele.getAttribute("data-side");
Expand Down
8 changes: 7 additions & 1 deletion packages/react/src/components/DiffUnifiedView.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable @typescript-eslint/ban-ts-comment */
import { getUnifiedContentLine, SplitSide } from "@git-diff-view/core";
import { diffFontSizeName, removeAllSelection, diffAsideWidthName } from "@git-diff-view/utils";
import { diffFontSizeName, removeAllSelection, diffAsideWidthName, getDiffIdFromElement } from "@git-diff-view/utils";
import * as React from "react";
import { Fragment, memo, useEffect, useMemo, useRef } from "react";
import { useSyncExternalStore } from "use-sync-external-store/shim/index.js";
Expand Down Expand Up @@ -85,6 +85,12 @@ export const DiffUnifiedView = memo(({ diffFile }: { diffFile: DiffFile }) => {
return;
}

const id = getDiffIdFromElement(ele as HTMLElement);

if (id && id !== `diff-root${diffFile.getId()}`) {
return;
}

while (ele && ele instanceof HTMLElement) {
const state = ele.getAttribute("data-state");
if (state) {
Expand Down
7 changes: 7 additions & 0 deletions packages/solid/src/components/DiffSplitViewNormal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
borderColorName,
diffAsideWidthName,
diffFontSizeName,
getDiffIdFromElement,
removeAllSelection,
syncScroll,
} from "@git-diff-view/utils";
Expand Down Expand Up @@ -49,6 +50,12 @@ const DiffSplitViewTable = (props: {
return;
}

const id = getDiffIdFromElement(ele as HTMLElement);

if (id && id !== `diff-root${props.diffFile.getId()}`) {
return;
}

while (ele && ele instanceof HTMLElement) {
const state = ele.getAttribute("data-state");
if (state) {
Expand Down
8 changes: 7 additions & 1 deletion packages/solid/src/components/DiffSplitViewWrap.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable @typescript-eslint/ban-ts-comment */
import { getSplitContentLines, SplitSide } from "@git-diff-view/core";
import { diffAsideWidthName, diffFontSizeName, removeAllSelection } from "@git-diff-view/utils";
import { diffAsideWidthName, diffFontSizeName, getDiffIdFromElement, removeAllSelection } from "@git-diff-view/utils";
import { createEffect, createMemo, createSignal, onCleanup, For } from "solid-js";

import { useFontSize, useTextWidth } from "../hooks";
Expand Down Expand Up @@ -59,6 +59,12 @@ export const DiffSplitViewWrap = (props: { diffFile: DiffFile }) => {
return;
}

const id = getDiffIdFromElement(ele as HTMLElement);

if (id && id !== `diff-root${props.diffFile.getId()}`) {
return;
}

while (ele && ele instanceof HTMLElement) {
const state = ele.getAttribute("data-state");
const side = ele.getAttribute("data-side");
Expand Down
8 changes: 7 additions & 1 deletion packages/solid/src/components/DiffUnifiedView.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getUnifiedContentLine, type DiffFile } from "@git-diff-view/core";
import { diffAsideWidthName, diffFontSizeName, removeAllSelection } from "@git-diff-view/utils";
import { diffAsideWidthName, diffFontSizeName, getDiffIdFromElement, removeAllSelection } from "@git-diff-view/utils";
import { createEffect, createMemo, createSignal, onCleanup, For } from "solid-js";

import { useEnableWrap, useFontSize, useTextWidth } from "../hooks";
Expand Down Expand Up @@ -56,6 +56,12 @@ export const DiffUnifiedView = (props: { diffFile: DiffFile }) => {
return;
}

const id = getDiffIdFromElement(ele as HTMLElement);

if (id && id !== `diff-root${props.diffFile.getId()}`) {
return;
}

while (ele && ele instanceof HTMLElement) {
const state = ele.getAttribute("data-state");
if (state) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script lang="ts">
import { getSplitContentLines, SplitSide, type DiffFile } from '@git-diff-view/core';
import { removeAllSelection } from '$lib/utils/dom.js';
import { getDiffIdFromElement, removeAllSelection } from '$lib/utils/dom.js';

import DiffSplitContentLine from './DiffSplitContentLineNormal.svelte';
import DiffSplitExtendLine from './DiffSplitExtendLineNormal.svelte';
Expand Down Expand Up @@ -49,6 +49,12 @@
return;
}

const id = getDiffIdFromElement(ele as HTMLElement);

if (id && id !== `diff-root${props.diffFile.getId()}`) {
return;
}

while (ele && ele instanceof HTMLElement) {
const state = ele.getAttribute('data-state');
if (state) {
Expand Down
8 changes: 7 additions & 1 deletion packages/svelte/src/lib/components/DiffSplitViewWrap.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { useTextWidth } from '$lib/hooks/useTextWidth.svelte.js';
import { getSplitContentLines, SplitSide, type DiffFile } from '@git-diff-view/core';
import { diffAsideWidthName, diffFontSizeName } from '$lib/utils/size.js';
import { removeAllSelection } from '$lib/utils/dom.js';
import { getDiffIdFromElement, removeAllSelection } from '$lib/utils/dom.js';

import DiffSplitContentLine from './DiffSplitContentLineWrap.svelte';
import DiffSplitExtendLine from './DiffSplitExtendLineWrap.svelte';
Expand Down Expand Up @@ -72,6 +72,12 @@
return;
}

const id = getDiffIdFromElement(ele as HTMLElement);

if (id && id !== `diff-root${props.diffFile.getId()}`) {
return;
}

while (ele && ele instanceof HTMLElement) {
const state = ele.getAttribute('data-state');
const side = ele.getAttribute('data-side');
Expand Down
8 changes: 7 additions & 1 deletion packages/svelte/src/lib/components/DiffUnifiedView.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import { useTextWidth } from '$lib/hooks/useTextWidth.svelte.js';
import { getUnifiedContentLine, type DiffFile } from '@git-diff-view/core';
import { diffAsideWidthName, diffFontSizeName } from '$lib/utils/size.js';
import { removeAllSelection } from '$lib/utils/dom.js';
import { getDiffIdFromElement, removeAllSelection } from '$lib/utils/dom.js';

import DiffUnifiedContentLine from './DiffUnifiedContentLine.svelte';
import DiffUnifiedExtendLine from './DiffUnifiedExtendLine.svelte';
Expand Down Expand Up @@ -58,6 +58,12 @@
return;
}

const id = getDiffIdFromElement(ele as HTMLElement);

if (id && id !== `diff-root${props.diffFile.getId()}`) {
return;
}

while (ele && ele instanceof HTMLElement) {
const state = ele.getAttribute('data-state');
if (state) {
Expand Down
1 change: 1 addition & 0 deletions packages/utils/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export declare const getLineNumberBG: (isAdded: boolean, isDelete: boolean, hasD
export declare const removeAllSelection: () => void;
export declare const syncScroll: (left: HTMLElement, right: HTMLElement) => () => void;
export declare const getElementRoot: (element?: HTMLElement) => Document | ShadowRoot;
export declare const getDiffIdFromElement: (element?: HTMLElement) => string;
export declare const diffFontSizeName = "--diff-font-size--";
export declare const diffAsideWidthName = "--diff-aside-width--";
export declare const memoFunc: <T extends Function>(func: T) => T;
Expand Down
19 changes: 19 additions & 0 deletions packages/utils/src/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,22 @@ export const getElementRoot = (element?: HTMLElement) => {
}
return document;
};

export const getDiffIdFromElement = (element?: HTMLElement) => {
if (element) {
if (typeof element.closest === "function") {
const diffRoot = element.closest('[data-component="git-diff-view"]');
const ele = diffRoot.querySelector(".diff-view-wrapper");
return ele.getAttribute("id");
} else {
let el: HTMLElement | null = element;
while (el) {
if (el.getAttribute && el.getAttribute("data-component") === "git-diff-view") {
const ele = el.querySelector(".diff-view-wrapper");
return ele.getAttribute("id");
}
el = el.parentElement;
}
}
}
};
7 changes: 7 additions & 0 deletions packages/vue/src/components/DiffSplitViewNormal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
removeAllSelection,
syncScroll,
borderColorName,
getDiffIdFromElement,
} from "@git-diff-view/utils";
import { Fragment, computed, defineComponent, ref, watchPostEffect } from "vue";

Expand Down Expand Up @@ -46,6 +47,12 @@ const DiffSplitViewTable = defineComponent(
return;
}

const id = getDiffIdFromElement(ele as HTMLElement);

if (id && id !== `diff-root${props.diffFile.getId()}`) {
return;
}

while (ele && ele instanceof HTMLElement) {
const state = ele.getAttribute("data-state");
if (state) {
Expand Down
8 changes: 7 additions & 1 deletion packages/vue/src/components/DiffSplitViewWrap.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getSplitContentLines } from "@git-diff-view/core";
import { diffAsideWidthName, diffFontSizeName, removeAllSelection } from "@git-diff-view/utils";
import { diffAsideWidthName, diffFontSizeName, getDiffIdFromElement, removeAllSelection } from "@git-diff-view/utils";
import { Fragment, computed, defineComponent, ref } from "vue";

import { SplitSide } from "..";
Expand Down Expand Up @@ -46,6 +46,12 @@ export const DiffSplitViewWrap = defineComponent(
return;
}

const id = getDiffIdFromElement(ele as HTMLElement);

if (id && id !== `diff-root${props.diffFile.getId()}`) {
return;
}

while (ele && ele instanceof HTMLElement) {
const state = ele.getAttribute("data-state");
const side = ele.getAttribute("data-side");
Expand Down
8 changes: 7 additions & 1 deletion packages/vue/src/components/DiffUnifiedView.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getUnifiedContentLine } from "@git-diff-view/core";
import { diffAsideWidthName, diffFontSizeName, removeAllSelection } from "@git-diff-view/utils";
import { diffAsideWidthName, diffFontSizeName, getDiffIdFromElement, removeAllSelection } from "@git-diff-view/utils";
import { Fragment, computed, defineComponent, ref } from "vue";

import { useEnableWrap, useFontSize } from "../context";
Expand Down Expand Up @@ -54,6 +54,12 @@ export const DiffUnifiedView = defineComponent(
return;
}

const id = getDiffIdFromElement(ele as HTMLElement);

if (id && id !== `diff-root${props.diffFile.getId()}`) {
return;
}

while (ele && ele instanceof HTMLElement) {
const state = ele.getAttribute("data-state");
if (state) {
Expand Down