diff --git a/scm-ui/ui-components/src/repos/DiffExpander.test.ts b/scm-ui/ui-components/src/repos/DiffExpander.test.ts index d01de8fd09..d7d0bde027 100644 --- a/scm-ui/ui-components/src/repos/DiffExpander.test.ts +++ b/scm-ui/ui-components/src/repos/DiffExpander.test.ts @@ -204,8 +204,8 @@ describe("with hunks the diff expander", () => { it("should expand hunk with new line from api client at the bottom", async () => { expect(diffExpander.getHunk(1).hunk.changes.length).toBe(7); fetchMock.get( - "http://localhost:8081/scm/api/v2/content/abc/CommitMessage.js?start=22&end=22", - "new line 1\nnew line 2" + "http://localhost:8081/scm/api/v2/content/abc/CommitMessage.js?start=20&end=21", + "new line 1" ); let newFile; diffExpander.getHunk(1).expandBottom(file => { @@ -213,15 +213,14 @@ describe("with hunks the diff expander", () => { }); await fetchMock.flush(true); expect(fetchMock.done()).toBe(true); - expect(newFile.hunks[1].changes.length).toBe(9); + expect(newFile.hunks[1].changes.length).toBe(8); expect(newFile.hunks[1].changes[7].content).toBe("new line 1"); - expect(newFile.hunks[1].changes[8].content).toBe("new line 2"); }); it("should expand hunk with new line from api client at the top", async () => { expect(diffExpander.getHunk(1).hunk.changes.length).toBe(7); fetchMock.get( - "http://localhost:8081/scm/api/v2/content/abc/CommitMessage.js?start=9&end=13", - "new line 1\nnew line 2" + "http://localhost:8081/scm/api/v2/content/abc/CommitMessage.js?start=8&end=13", + "new line 9\nnew line 10\nnew line 11\nnew line 12\nnew line 13" ); let newFile; diffExpander.getHunk(1).expandHead(file => { @@ -229,13 +228,19 @@ describe("with hunks the diff expander", () => { }); await fetchMock.flush(true); expect(fetchMock.done()).toBe(true); - expect(newFile.hunks[1].changes.length).toBe(9); - expect(newFile.hunks[1].changes[0].content).toBe("new line 1"); - expect(newFile.hunks[1].changes[0].oldLineNumber).toBe(12); - expect(newFile.hunks[1].changes[0].newLineNumber).toBe(12); - expect(newFile.hunks[1].changes[1].content).toBe("new line 2"); - expect(newFile.hunks[1].changes[1].oldLineNumber).toBe(13); - expect(newFile.hunks[1].changes[1].newLineNumber).toBe(13); + expect(newFile.hunks[1].changes.length).toBe(12); + expect(newFile.hunks[1].changes[0].content).toBe("new line 9"); + expect(newFile.hunks[1].changes[0].oldLineNumber).toBe(9); + expect(newFile.hunks[1].changes[0].newLineNumber).toBe(9); + expect(newFile.hunks[1].changes[1].content).toBe("new line 10"); + expect(newFile.hunks[1].changes[1].oldLineNumber).toBe(10); + expect(newFile.hunks[1].changes[1].newLineNumber).toBe(10); + expect(newFile.hunks[1].changes[4].content).toBe("new line 13"); + expect(newFile.hunks[1].changes[4].oldLineNumber).toBe(13); + expect(newFile.hunks[1].changes[4].newLineNumber).toBe(13); + expect(newFile.hunks[1].changes[5].content).toBe("line"); + expect(newFile.hunks[1].changes[5].oldLineNumber).toBe(14); + expect(newFile.hunks[1].changes[5].newLineNumber).toBe(14); }); }); diff --git a/scm-ui/ui-components/src/repos/DiffExpander.ts b/scm-ui/ui-components/src/repos/DiffExpander.ts index cac5818de4..0311636d7c 100644 --- a/scm-ui/ui-components/src/repos/DiffExpander.ts +++ b/scm-ui/ui-components/src/repos/DiffExpander.ts @@ -45,7 +45,7 @@ class DiffExpander { }; maxLineNumber: (n: number) => number = (n: number) => { - return this.file.hunks![n]!.newStart! + this.file.hunks![n]!.newLines!; + return this.file.hunks![n]!.newStart! + this.file.hunks![n]!.newLines! - 1; }; computeMaxExpandHeadRange = (n: number) => { @@ -54,7 +54,7 @@ class DiffExpander { } else if (n === 0) { return this.minLineNumber(n) - 1; } - return this.minLineNumber(n) - this.maxLineNumber(n - 1); + return this.minLineNumber(n) - this.maxLineNumber(n - 1) - 1; }; computeMaxExpandBottomRange = (n: number) => { @@ -63,12 +63,12 @@ class DiffExpander { } else if (n === this.file!.hunks!.length - 1) { return Number.MAX_SAFE_INTEGER; } - return this.minLineNumber(n + 1) - this.maxLineNumber(n); + return this.minLineNumber(n + 1) - this.maxLineNumber(n) - 1; }; expandHead = (n: number, callback: (newFile: File) => void) => { const lineRequestUrl = this.file._links.lines.href - .replace("{start}", this.minLineNumber(n) - Math.min(10, this.computeMaxExpandHeadRange(n))) + .replace("{start}", this.minLineNumber(n) - Math.min(10, this.computeMaxExpandHeadRange(n)) - 1) .replace("{end}", this.minLineNumber(n) - 1); apiClient .get(lineRequestUrl) @@ -79,7 +79,7 @@ class DiffExpander { expandBottom = (n: number, callback: (newFile: File) => void) => { const lineRequestUrl = this.file._links.lines.href - .replace("{start}", this.maxLineNumber(n) + 1) + .replace("{start}", this.maxLineNumber(n)) .replace("{end}", this.maxLineNumber(n) + Math.min(10, this.computeMaxExpandBottomRange(n))); apiClient .get(lineRequestUrl) @@ -90,6 +90,9 @@ class DiffExpander { expandHunkAtHead = (n: number, lines: string[], callback: (newFile: File) => void) => { const hunk = this.file.hunks[n]; + if (lines[lines.length - 1] === "") { + lines.pop(); + } const newChanges: Change[] = []; let oldLineNumber = hunk.changes[0].oldLineNumber - lines.length; let newLineNumber = hunk.changes[0].newLineNumber - lines.length; @@ -129,6 +132,9 @@ class DiffExpander { expandHunkAtBottom = (n: number, lines: string[], callback: (newFile: File) => void) => { const hunk = this.file.hunks![n]; + if (lines[lines.length - 1] === "") { + lines.pop(); + } const newChanges = [...hunk.changes]; let oldLineNumber = newChanges[newChanges.length - 1].oldLineNumber; let newLineNumber = newChanges[newChanges.length - 1].newLineNumber;