mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-14 09:25:43 +01:00
Extract common code
This commit is contained in:
@@ -74,95 +74,77 @@ class DiffExpander {
|
|||||||
};
|
};
|
||||||
|
|
||||||
expandHead = (n: number, count: number, callback: (newFile: File) => void) => {
|
expandHead = (n: number, count: number, callback: (newFile: File) => void) => {
|
||||||
const lineRequestUrl = (this.file._links!.lines as Link).href
|
const start = this.minLineNumber(n) - Math.min(count, this.computeMaxExpandHeadRange(n)) - 1;
|
||||||
.replace("{start}", (this.minLineNumber(n) - Math.min(count, this.computeMaxExpandHeadRange(n)) - 1).toString())
|
const end = this.minLineNumber(n) - 1;
|
||||||
.replace("{end}", (this.minLineNumber(n) - 1).toString());
|
this.loadLines(start, end).then(lines => {
|
||||||
apiClient
|
const hunk = this.file.hunks![n];
|
||||||
.get(lineRequestUrl)
|
|
||||||
.then(response => response.text())
|
const newHunk = this.createNewHunk(
|
||||||
.then(text => text.split("\n"))
|
hunk.oldStart! - lines.length,
|
||||||
.then(lines => this.expandHunkAtHead(n, lines, callback));
|
hunk.newStart! - lines.length,
|
||||||
|
lines,
|
||||||
|
lines.length
|
||||||
|
);
|
||||||
|
|
||||||
|
const newFile = this.addHunkToFile(newHunk, n);
|
||||||
|
callback(newFile);
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
expandBottom = (n: number, count: number, callback: (newFile: File) => void) => {
|
expandBottom = (n: number, count: number, callback: (newFile: File) => void) => {
|
||||||
const maxExpandBottomRange = this.computeMaxExpandBottomRange(n);
|
const maxExpandBottomRange = this.computeMaxExpandBottomRange(n);
|
||||||
const lineRequestUrl = (this.file._links!.lines as Link).href
|
const start = this.maxLineNumber(n);
|
||||||
.replace("{start}", this.maxLineNumber(n).toString())
|
const end =
|
||||||
.replace(
|
|
||||||
"{end}",
|
|
||||||
count > 0
|
count > 0
|
||||||
? (
|
? start + Math.min(count, maxExpandBottomRange > 0 ? maxExpandBottomRange : Number.MAX_SAFE_INTEGER)
|
||||||
this.maxLineNumber(n) +
|
: -1;
|
||||||
Math.min(count, maxExpandBottomRange > 0 ? maxExpandBottomRange : Number.MAX_SAFE_INTEGER)
|
this.loadLines(start, end).then(lines => {
|
||||||
).toString()
|
const hunk = this.file.hunks![n];
|
||||||
: "-1"
|
|
||||||
|
const newHunk: Hunk = this.createNewHunk(
|
||||||
|
this.getMaxOldLineNumber(hunk.changes) + 1,
|
||||||
|
this.getMaxNewLineNumber(hunk.changes) + 1,
|
||||||
|
lines,
|
||||||
|
count
|
||||||
);
|
);
|
||||||
apiClient
|
|
||||||
|
const newFile = this.addHunkToFile(newHunk, n + 1);
|
||||||
|
callback(newFile);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
loadLines = (start: number, end: number) => {
|
||||||
|
const lineRequestUrl = (this.file._links!.lines as Link).href
|
||||||
|
.replace("{start}", start.toString())
|
||||||
|
.replace("{end}", end.toString());
|
||||||
|
return apiClient
|
||||||
.get(lineRequestUrl)
|
.get(lineRequestUrl)
|
||||||
.then(response => response.text())
|
.then(response => response.text())
|
||||||
.then(text => text.split("\n"))
|
.then(text => text.split("\n"))
|
||||||
.then(lines => this.expandHunkAtBottom(n, count, lines, callback));
|
.then(lines => (lines[lines.length - 1] === "" ? lines.slice(0, lines.length - 1) : lines));
|
||||||
};
|
};
|
||||||
|
|
||||||
expandHunkAtHead = (n: number, lines: string[], callback: (newFile: File) => void) => {
|
addHunkToFile = (newHunk: Hunk, position: number) => {
|
||||||
const hunk = this.file.hunks![n];
|
|
||||||
if (lines[lines.length - 1] === "") {
|
|
||||||
lines.pop();
|
|
||||||
}
|
|
||||||
const newChanges: Change[] = [];
|
|
||||||
const minOldLineNumberOfNewHunk = hunk.oldStart! - lines.length;
|
|
||||||
const minNewLineNumberOfNewHunk = hunk.newStart! - lines.length;
|
|
||||||
let oldLineNumber = minOldLineNumberOfNewHunk;
|
|
||||||
let newLineNumber = minNewLineNumberOfNewHunk;
|
|
||||||
|
|
||||||
lines.forEach(line => {
|
|
||||||
newChanges.push({
|
|
||||||
content: line,
|
|
||||||
type: "normal",
|
|
||||||
oldLineNumber,
|
|
||||||
newLineNumber,
|
|
||||||
isNormal: true
|
|
||||||
});
|
|
||||||
oldLineNumber += 1;
|
|
||||||
newLineNumber += 1;
|
|
||||||
});
|
|
||||||
|
|
||||||
const newHunk: Hunk = {
|
|
||||||
content: "",
|
|
||||||
oldStart: minOldLineNumberOfNewHunk,
|
|
||||||
newStart: minNewLineNumberOfNewHunk,
|
|
||||||
oldLines: lines.length,
|
|
||||||
newLines: lines.length,
|
|
||||||
changes: newChanges,
|
|
||||||
expansion: true
|
|
||||||
};
|
|
||||||
const newHunks: Hunk[] = [];
|
const newHunks: Hunk[] = [];
|
||||||
this.file.hunks!.forEach((oldHunk: Hunk, i: number) => {
|
this.file.hunks!.forEach((oldHunk: Hunk, i: number) => {
|
||||||
if (i === n) {
|
if (i === position) {
|
||||||
newHunks.push(newHunk);
|
newHunks.push(newHunk);
|
||||||
}
|
}
|
||||||
newHunks.push(oldHunk);
|
newHunks.push(oldHunk);
|
||||||
});
|
});
|
||||||
const newFile = { ...this.file, hunks: newHunks };
|
if (position === newHunks.length) {
|
||||||
callback(newFile);
|
newHunks.push(newHunk);
|
||||||
|
}
|
||||||
|
return { ...this.file, hunks: newHunks };
|
||||||
};
|
};
|
||||||
|
|
||||||
expandHunkAtBottom = (n: number, requestedLines: number, lines: string[], callback: (newFile: File) => void) => {
|
createNewHunk = (oldFirstLineNumber: number, newFirstLineNumber: number, lines: string[], requestedLines: number) => {
|
||||||
const hunk = this.file.hunks![n];
|
|
||||||
if (lines[lines.length - 1] === "") {
|
|
||||||
lines.pop();
|
|
||||||
}
|
|
||||||
const newChanges: Change[] = [];
|
const newChanges: Change[] = [];
|
||||||
|
|
||||||
const maxOldLineNumberFromPrecedingHunk = this.getMaxOldLineNumber(hunk.changes);
|
let oldLineNumber: number = oldFirstLineNumber;
|
||||||
const maxNewLineNumberFromPrecedingHunk = this.getMaxNewLineNumber(hunk.changes);
|
let newLineNumber: number = newFirstLineNumber;
|
||||||
|
|
||||||
let oldLineNumber: number = maxOldLineNumberFromPrecedingHunk;
|
|
||||||
let newLineNumber: number = maxNewLineNumberFromPrecedingHunk;
|
|
||||||
|
|
||||||
lines.forEach(line => {
|
lines.forEach(line => {
|
||||||
oldLineNumber += 1;
|
|
||||||
newLineNumber += 1;
|
|
||||||
newChanges.push({
|
newChanges.push({
|
||||||
content: line,
|
content: line,
|
||||||
type: "normal",
|
type: "normal",
|
||||||
@@ -170,28 +152,20 @@ class DiffExpander {
|
|||||||
newLineNumber,
|
newLineNumber,
|
||||||
isNormal: true
|
isNormal: true
|
||||||
});
|
});
|
||||||
|
oldLineNumber += 1;
|
||||||
|
newLineNumber += 1;
|
||||||
});
|
});
|
||||||
|
|
||||||
const newHunk: Hunk = {
|
return {
|
||||||
changes: newChanges,
|
changes: newChanges,
|
||||||
content: "",
|
content: "",
|
||||||
oldStart: maxOldLineNumberFromPrecedingHunk + 1,
|
oldStart: oldFirstLineNumber,
|
||||||
newStart: maxNewLineNumberFromPrecedingHunk + 1,
|
newStart: newFirstLineNumber,
|
||||||
oldLines: lines.length,
|
oldLines: lines.length,
|
||||||
newLines: lines.length,
|
newLines: lines.length,
|
||||||
expansion: true,
|
expansion: true,
|
||||||
fullyExpanded: requestedLines < 0 || lines.length < requestedLines
|
fullyExpanded: requestedLines < 0 || lines.length < requestedLines
|
||||||
};
|
};
|
||||||
|
|
||||||
const newHunks: Hunk[] = [];
|
|
||||||
this.file.hunks!.forEach((oldHunk: Hunk, i: number) => {
|
|
||||||
newHunks.push(oldHunk);
|
|
||||||
if (i === n) {
|
|
||||||
newHunks.push(newHunk);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
const newFile = { ...this.file, hunks: newHunks };
|
|
||||||
callback(newFile);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
getMaxOldLineNumber = (newChanges: Change[]) => {
|
getMaxOldLineNumber = (newChanges: Change[]) => {
|
||||||
|
|||||||
Reference in New Issue
Block a user