This commit is contained in:
Mateusz Gruszczyński
2026-07-25 17:54:34 +02:00
parent 2d8bb24fcc
commit 4c4c15495d
8 changed files with 89 additions and 24 deletions
+41
View File
@@ -91,6 +91,47 @@ export function replaceAuthorshipOwner(model, matcher, replacement, contentLengt
};
}
export function lineAuthors(content, model) {
const starts = [0];
for (let i = 0; i < content.length; i++) if (content.charCodeAt(i) === 10) starts.push(i + 1);
return starts.map((start, index) => {
const end = index + 1 < starts.length ? starts[index + 1] : content.length;
const totals = new Map();
for (const span of model?.spans || []) {
const overlap = Math.max(0, Math.min(end, span.end) - Math.max(start, span.start));
if (overlap) totals.set(span.owner, (totals.get(span.owner) || 0) + overlap);
}
return [...totals.entries()].sort((a, b) => b[1] - a[1]).map(([owner]) => owner);
});
}
export function mapSelectionThroughEdit(previousText, nextText, start, end = start) {
if (previousText === nextText) return { start, end };
let prefix = 0;
const shared = Math.min(previousText.length, nextText.length);
while (prefix < shared && previousText.charCodeAt(prefix) === nextText.charCodeAt(prefix)) prefix++;
let oldSuffix = previousText.length;
let newSuffix = nextText.length;
while (oldSuffix > prefix && newSuffix > prefix && previousText.charCodeAt(oldSuffix - 1) === nextText.charCodeAt(newSuffix - 1)) {
oldSuffix--;
newSuffix--;
}
const insertedLength = newSuffix - prefix;
const delta = insertedLength - (oldSuffix - prefix);
const map = position => {
if (position < prefix) return position;
if (position > oldSuffix) return position + delta;
if (position === prefix && oldSuffix === prefix) return prefix + insertedLength;
return prefix + insertedLength;
};
return {
start: Math.max(0, Math.min(nextText.length, map(start))),
end: Math.max(0, Math.min(nextText.length, map(end))),
};
}
export function lineOwners(content, model) {
const starts = [0];
for (let i = 0; i < content.length; i++) if (content.charCodeAt(i) === 10) starts.push(i + 1);