Patch hunks with new lines

This commit is contained in:
René Pfeuffer
2020-05-29 14:00:14 +02:00
parent 2efd21d466
commit ebfc267b93
4 changed files with 165 additions and 23 deletions

View File

@@ -22,7 +22,8 @@
* SOFTWARE.
*/
import { File, Hunk } from "./DiffTypes";
import { apiClient } from "@scm-manager/ui-components";
import { Change, File, Hunk } from "./DiffTypes";
class DiffExpander {
file: File;
@@ -65,16 +66,109 @@ class DiffExpander {
return this.minLineNumber(n + 1) - this.maxLineNumber(n);
};
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("{end}", this.minLineNumber(n) - 1);
apiClient
.get(lineRequestUrl)
.then(response => response.text())
.then(text => text.split("\n"))
.then(lines => this.expandHunkAtHead(n, lines, callback));
};
expandBottom = (n: number, callback: (newFile: File) => void) => {
const lineRequestUrl = this.file._links.lines.href
.replace("{start}", this.maxLineNumber(n) + 1)
.replace("{end}", this.maxLineNumber(n) + Math.min(10, this.computeMaxExpandBottomRange(n)));
apiClient
.get(lineRequestUrl)
.then(response => response.text())
.then(text => text.split("\n"))
.then(lines => this.expandHunkAtBottom(n, lines, callback));
};
expandHunkAtHead = (n: number, lines: string[], callback: (newFile: File) => void) => {
const hunk = this.file.hunks[n];
const newChanges: Change[] = [];
let oldLineNumber = hunk.changes[0].oldLineNumber - lines.length;
let newLineNumber = hunk.changes[0].newLineNumber - lines.length;
lines.forEach(line => {
newChanges.push({
content: line,
type: "normal",
oldLineNumber,
newLineNumber,
isNormal: true
});
oldLineNumber += 1;
newLineNumber += 1;
});
hunk.changes.forEach(change => newChanges.push(change));
const newHunk = {
...hunk,
oldStart: hunk.oldStart - lines.length,
newStart: hunk.newStart - lines.length,
oldLines: hunk.oldLines + lines.length,
newLines: hunk.newLines + lines.length,
changes: newChanges
};
const newHunks: Hunk[] = [];
this.file.hunks!.forEach((oldHunk: Hunk, i: number) => {
if (i === n) {
newHunks.push(newHunk);
} else {
newHunks.push(oldHunk);
}
});
const newFile = { ...this.file, hunks: newHunks };
callback(newFile);
};
expandHunkAtBottom = (n: number, lines: string[], callback: (newFile: File) => void) => {
const hunk = this.file.hunks![n];
const newChanges = [...hunk.changes];
let oldLineNumber = newChanges[newChanges.length - 1].oldLineNumber;
let newLineNumber = newChanges[newChanges.length - 1].newLineNumber;
lines.forEach(line => {
oldLineNumber += 1;
newLineNumber += 1;
newChanges.push({
content: line,
type: "normal",
oldLineNumber,
newLineNumber,
isNormal: true
});
});
const newHunk = {
...hunk,
oldLines: hunk.oldLines + lines.length,
newLines: hunk.newLines + lines.length,
changes: newChanges
};
const newHunks: Hunk[] = [];
this.file.hunks.forEach((oldHunk: Hunk, i: number) => {
if (i === n) {
newHunks.push(newHunk);
} else {
newHunks.push(oldHunk);
}
});
const newFile = { ...this.file, hunks: newHunks };
callback(newFile);
};
getHunk: (n: number) => ExpandableHunk = (n: number) => {
return {
maxExpandHeadRange: this.computeMaxExpandHeadRange(n),
maxExpandBottomRange: this.computeMaxExpandBottomRange(n),
expandHead: () => {
return this;
},
expandBottom: () => {
return this;
},
expandHead: (callback: (newFile: File) => void) => this.expandHead(n, callback),
expandBottom: (callback: (newFile: File) => void) => this.expandBottom(n, callback),
hunk: this.file?.hunks![n]
};
};
@@ -84,8 +178,8 @@ export type ExpandableHunk = {
hunk: Hunk;
maxExpandHeadRange: number;
maxExpandBottomRange: number;
expandHead: () => DiffExpander;
expandBottom: () => DiffExpander;
expandHead: (callback: (newFile: File) => void) => void;
expandBottom: (callback: (newFile: File) => void) => void;
};
export default DiffExpander;