Implement option to load rest of files completely

This commit is contained in:
René Pfeuffer
2020-05-30 18:12:03 +02:00
parent b86c025b37
commit 751a2bfa9b
5 changed files with 38 additions and 7 deletions

View File

@@ -199,7 +199,7 @@ describe("with hunks the diff expander", () => {
}); });
it("should return a really bix number for the expand bottom range of the last hunk", () => { it("should return a really bix number for the expand bottom range of the last hunk", () => {
expect(diffExpander.getHunk(3).maxExpandBottomRange).toBeGreaterThan(99999); expect(diffExpander.getHunk(3).maxExpandBottomRange).toBe(-1);
}); });
it("should expand hunk with new line from api client at the bottom", async () => { it("should expand hunk with new line from api client at the bottom", async () => {
expect(diffExpander.getHunk(1).hunk.changes.length).toBe(7); expect(diffExpander.getHunk(1).hunk.changes.length).toBe(7);
@@ -251,6 +251,18 @@ describe("with hunks the diff expander", () => {
await fetchMock.flush(true); await fetchMock.flush(true);
expect(newFile.hunks[3].fullyExpanded).toBe(true); expect(newFile.hunks[3].fullyExpanded).toBe(true);
}); });
it("should set end to -1 if requested to expand to the end", async () => {
fetchMock.get(
"http://localhost:8081/scm/api/v2/content/abc/CommitMessage.js?start=40&end=-1",
"new line 40\nnew line 41\nnew line 42"
);
let newFile;
diffExpander.getHunk(3).expandBottom(-1, file => {
newFile = file;
});
await fetchMock.flush(true);
expect(newFile.hunks[3].fullyExpanded).toBe(true);
});
}); });
describe("for a new file with text input the diff expander", () => { describe("for a new file with text input the diff expander", () => {

View File

@@ -61,7 +61,7 @@ class DiffExpander {
if (this.file.type === "add" || this.file.type === "delete") { if (this.file.type === "add" || this.file.type === "delete") {
return 0; return 0;
} else if (n === this.file!.hunks!.length - 1) { } else if (n === this.file!.hunks!.length - 1) {
return this.file!.hunks![this.file!.hunks!.length - 1].fullyExpanded ? 0 : Number.MAX_SAFE_INTEGER; return this.file!.hunks![this.file!.hunks!.length - 1].fullyExpanded ? 0 : -1;
} }
return this.minLineNumber(n + 1) - this.maxLineNumber(n) - 1; return this.minLineNumber(n + 1) - this.maxLineNumber(n) - 1;
}; };
@@ -78,9 +78,10 @@ class DiffExpander {
}; };
expandBottom = (n: number, count: number, callback: (newFile: File) => void) => { expandBottom = (n: number, count: number, callback: (newFile: File) => void) => {
const maxExpandBottomRange = this.computeMaxExpandBottomRange(n);
const lineRequestUrl = this.file._links.lines.href const lineRequestUrl = this.file._links.lines.href
.replace("{start}", this.maxLineNumber(n)) .replace("{start}", this.maxLineNumber(n))
.replace("{end}", this.maxLineNumber(n) + Math.min(count, this.computeMaxExpandBottomRange(n))); .replace("{end}", count > 0 ? this.maxLineNumber(n) + Math.min(count, maxExpandBottomRange > 0? maxExpandBottomRange:Number.MAX_SAFE_INTEGER) : -1);
apiClient apiClient
.get(lineRequestUrl) .get(lineRequestUrl)
.then(response => response.text()) .then(response => response.text())
@@ -156,7 +157,7 @@ class DiffExpander {
oldLines: hunk.oldLines + lines.length, oldLines: hunk.oldLines + lines.length,
newLines: hunk.newLines + lines.length, newLines: hunk.newLines + lines.length,
changes: newChanges, changes: newChanges,
fullyExpanded: lines.length < requestedLines fullyExpanded: requestedLines < 0 || lines.length < requestedLines
}; };
const newHunks: Hunk[] = []; const newHunks: Hunk[] = [];
this.file.hunks.forEach((oldHunk: Hunk, i: number) => { this.file.hunks.forEach((oldHunk: Hunk, i: number) => {

View File

@@ -212,7 +212,7 @@ class DiffFile extends React.Component<Props, State> {
}; };
createLastHunkFooter = (expandableHunk: ExpandableHunk) => { createLastHunkFooter = (expandableHunk: ExpandableHunk) => {
if (expandableHunk.maxExpandBottomRange > 0) { if (expandableHunk.maxExpandBottomRange != 0) {
return ( return (
<Decoration> <Decoration>
<HunkDivider> <HunkDivider>

View File

@@ -115,10 +115,16 @@ public class ContentResource {
} }
private StreamingOutput createStreamingOutput(String namespace, String name, String revision, String path, Integer start, Integer end) { private StreamingOutput createStreamingOutput(String namespace, String name, String revision, String path, Integer start, Integer end) {
Integer effectiveEnd;
if (end != null && end < 0) {
effectiveEnd = null;
} else {
effectiveEnd = end;
}
return os -> { return os -> {
OutputStream sourceOut; OutputStream sourceOut;
if (start != null || end != null) { if (start != null || effectiveEnd != null) {
sourceOut = new LineFilteredOutputStream(os, start, end); sourceOut = new LineFilteredOutputStream(os, start, effectiveEnd);
} else { } else {
sourceOut = os; sourceOut = os;
} }

View File

@@ -109,6 +109,18 @@ public class ContentResourceTest {
assertEquals("line 2\nline 3\n", baos.toString()); assertEquals("line 2\nline 3\n", baos.toString());
} }
@Test
public void shouldNotLimitOutputWhenEndLessThanZero() throws Exception {
mockContent("file", "line 1\nline 2\nline 3\nline 4".getBytes());
Response response = contentResource.get(NAMESPACE, REPO_NAME, REV, "file", 1, -1);
assertEquals(200, response.getStatus());
ByteArrayOutputStream baos = readOutputStream(response);
assertEquals("line 2\nline 3\nline 4", baos.toString());
}
@Test @Test
public void shouldHandleMissingFile() { public void shouldHandleMissingFile() {
Response response = contentResource.get(NAMESPACE, REPO_NAME, REV, "doesNotExist", null, null); Response response = contentResource.get(NAMESPACE, REPO_NAME, REV, "doesNotExist", null, null);