Files
SCM-Manager/scm-ui/ui-components/src/repos/DiffExpander.ts

199 lines
6.2 KiB
TypeScript
Raw Normal View History

2020-05-27 17:23:00 +02:00
/*
* MIT License
*
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
2020-05-29 14:00:14 +02:00
import { apiClient } from "@scm-manager/ui-components";
import { Change, File, Hunk } from "./DiffTypes";
2020-05-30 20:55:02 +02:00
import { Link } from "@scm-manager/ui-types/src";
2020-05-27 17:23:00 +02:00
class DiffExpander {
file: File;
constructor(file: File) {
this.file = file;
}
hunkCount = () => {
if (this.file.hunks) {
return this.file.hunks.length;
} else {
return 0;
}
2020-05-27 17:23:00 +02:00
};
minLineNumber: (n: number) => number = (n: number) => {
return this.file.hunks![n]!.newStart!;
};
maxLineNumber: (n: number) => number = (n: number) => {
2020-05-30 15:48:51 +02:00
return this.file.hunks![n]!.newStart! + this.file.hunks![n]!.newLines! - 1;
};
computeMaxExpandHeadRange = (n: number) => {
if (this.file.type === "delete") {
return 0;
} else if (n === 0) {
return this.minLineNumber(n) - 1;
}
2020-05-30 15:48:51 +02:00
return this.minLineNumber(n) - this.maxLineNumber(n - 1) - 1;
};
computeMaxExpandBottomRange = (n: number) => {
if (this.file.type === "add" || this.file.type === "delete") {
return 0;
}
const changes = this.file.hunks![n].changes;
if (changes[changes.length - 1].type === "normal") {
if (n === this.file!.hunks!.length - 1) {
return this.file!.hunks![this.file!.hunks!.length - 1].fullyExpanded ? 0 : -1;
}
return this.minLineNumber(n + 1) - this.maxLineNumber(n) - 1;
} else {
return 0;
}
};
2020-06-09 16:53:36 +02:00
expandHead: (n: number, count: number) => Promise<File> = (n, count) => {
2020-06-09 14:39:12 +02:00
const start = this.minLineNumber(n) - Math.min(count, this.computeMaxExpandHeadRange(n)) - 1;
const end = this.minLineNumber(n) - 1;
2020-06-09 16:53:36 +02:00
return this.loadLines(start, end).then(lines => {
2020-06-09 14:39:12 +02:00
const hunk = this.file.hunks![n];
const newHunk = this.createNewHunk(
hunk.oldStart! - lines.length,
hunk.newStart! - lines.length,
lines,
lines.length
);
2020-06-09 16:53:36 +02:00
return this.addHunkToFile(newHunk, n);
2020-06-09 14:39:12 +02:00
});
2020-05-29 14:00:14 +02:00
};
2020-06-09 16:53:36 +02:00
expandBottom: (n: number, count: number) => Promise<File> = (n, count) => {
const maxExpandBottomRange = this.computeMaxExpandBottomRange(n);
2020-06-09 14:39:12 +02:00
const start = this.maxLineNumber(n);
const end =
count > 0
? start + Math.min(count, maxExpandBottomRange > 0 ? maxExpandBottomRange : Number.MAX_SAFE_INTEGER)
: -1;
2020-06-09 16:53:36 +02:00
return this.loadLines(start, end).then(lines => {
2020-06-09 14:39:12 +02:00
const hunk = this.file.hunks![n];
const newHunk: Hunk = this.createNewHunk(
this.getMaxOldLineNumber(hunk.changes) + 1,
this.getMaxNewLineNumber(hunk.changes) + 1,
lines,
count
2020-05-30 20:55:02 +02:00
);
2020-06-09 14:39:12 +02:00
2020-06-09 16:53:36 +02:00
return this.addHunkToFile(newHunk, n + 1);
2020-06-09 14:39:12 +02:00
});
};
loadLines = (start: number, end: number) => {
const lineRequestUrl = (this.file._links!.lines as Link).href
.replace("{start}", start.toString())
.replace("{end}", end.toString());
return apiClient
2020-05-29 14:00:14 +02:00
.get(lineRequestUrl)
.then(response => response.text())
.then(text => text.split("\n"))
2020-06-09 14:39:12 +02:00
.then(lines => (lines[lines.length - 1] === "" ? lines.slice(0, lines.length - 1) : lines));
2020-05-29 14:00:14 +02:00
};
2020-06-09 14:39:12 +02:00
addHunkToFile = (newHunk: Hunk, position: number) => {
2020-05-29 14:00:14 +02:00
const newHunks: Hunk[] = [];
this.file.hunks!.forEach((oldHunk: Hunk, i: number) => {
2020-06-09 14:39:12 +02:00
if (i === position) {
2020-05-29 14:00:14 +02:00
newHunks.push(newHunk);
}
newHunks.push(oldHunk);
2020-05-29 14:00:14 +02:00
});
2020-06-09 14:39:12 +02:00
if (position === newHunks.length) {
newHunks.push(newHunk);
}
return { ...this.file, hunks: newHunks };
2020-05-29 14:00:14 +02:00
};
2020-06-09 14:39:12 +02:00
createNewHunk = (oldFirstLineNumber: number, newFirstLineNumber: number, lines: string[], requestedLines: number) => {
const newChanges: Change[] = [];
2020-06-09 14:39:12 +02:00
let oldLineNumber: number = oldFirstLineNumber;
let newLineNumber: number = newFirstLineNumber;
2020-05-29 14:00:14 +02:00
lines.forEach(line => {
newChanges.push({
content: line,
type: "normal",
oldLineNumber,
newLineNumber,
isNormal: true
});
2020-06-09 14:39:12 +02:00
oldLineNumber += 1;
newLineNumber += 1;
2020-05-29 14:00:14 +02:00
});
2020-06-09 14:39:12 +02:00
return {
changes: newChanges,
content: "",
2020-06-09 14:39:12 +02:00
oldStart: oldFirstLineNumber,
newStart: newFirstLineNumber,
oldLines: lines.length,
newLines: lines.length,
2020-06-09 13:57:44 +02:00
expansion: true,
fullyExpanded: requestedLines < 0 || lines.length < requestedLines
2020-05-29 14:00:14 +02:00
};
};
getMaxOldLineNumber = (newChanges: Change[]) => {
const lastChange = newChanges[newChanges.length - 1];
return lastChange.oldLineNumber || lastChange.lineNumber!;
};
getMaxNewLineNumber = (newChanges: Change[]) => {
const lastChange = newChanges[newChanges.length - 1];
return lastChange.newLineNumber || lastChange.lineNumber!;
};
2020-06-09 16:53:36 +02:00
getHunk: (n: number) => ExpandableHunk = n => {
2020-05-27 17:23:00 +02:00
return {
maxExpandHeadRange: this.computeMaxExpandHeadRange(n),
maxExpandBottomRange: this.computeMaxExpandBottomRange(n),
2020-06-09 16:53:36 +02:00
expandHead: (count: number) => this.expandHead(n, count),
expandBottom: (count: number) => this.expandBottom(n, count),
hunk: this.file?.hunks![n]
2020-05-27 17:23:00 +02:00
};
};
}
export type ExpandableHunk = {
hunk: Hunk;
maxExpandHeadRange: number;
maxExpandBottomRange: number;
2020-06-09 16:53:36 +02:00
expandHead: (count: number) => Promise<File>;
expandBottom: (count: number) => Promise<File>;
2020-05-27 17:23:00 +02:00
};
export default DiffExpander;