Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: for issue-144 page hanging on large files #172

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 69 additions & 6 deletions src/compute-lines.ts
Original file line number Diff line number Diff line change
@@ -146,15 +146,52 @@ const computeLineInformation = (
compareMethod: string = DiffMethod.CHARS,
linesOffset: number = 0,
): ComputedLineInformation => {
const diffArray = diff.diffLines(
oldString.trimRight(),
newString.trimRight(),
{
let diffArray: diff.Change[] = [];
let long = false;
let num = 100;
if (oldString.length > 1000 || newString.length > 1000) {
long = true;
}

if (long) {
const oldArr = getArr(oldString, {
newlineIsToken: true,
ignoreWhitespace: false,
ignoreCase: false,
},
);
});

const newArr = getArr(newString, {
newlineIsToken: true,
ignoreWhitespace: false,
ignoreCase: false,
});

let oa = oldArr.splice(0, num);
let na = newArr.splice(0, num);

while (oa.length > 0 || na.length > 0) {
const o = oa.join("");
const n = na.join("");
diffArray = diffArray.concat(
diff.diffLines(o, n, {
newlineIsToken: true,
ignoreWhitespace: false,
ignoreCase: false,
})
);
oa = oldArr.splice(0, num);
na = newArr.splice(0, num);
}
Comment on lines +169 to +184

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This approach causes the result to be incorrect. Because you're chunking the call to diffLines into units of 100 lines additions or deletions will cause equal lines to get pushed into different chunks, where the underlying diff algorithm can no longer match them up.

} else {
diffArray = diffArray.concat(
diff.diffLines(oldString, newString, {
newlineIsToken: true,
ignoreWhitespace: false,
ignoreCase: false,
})
);
}

let rightLineNumber = linesOffset;
let leftLineNumber = linesOffset;
let lineInformation: LineInformation[] = [];
@@ -268,4 +305,30 @@ const computeLineInformation = (
};
};

function getArr(value: string, options: diff.LinesOptions) {
let retLines = [],
linesAndNewlines = value.split(/(\n|\r\n)/);

// Ignore the final empty token that occurs if the string ends with a new line
if (!linesAndNewlines[linesAndNewlines.length - 1]) {
linesAndNewlines.pop();
}

// Merge the content and line separators into single tokens
for (let i = 0; i < linesAndNewlines.length; i++) {
let line = linesAndNewlines[i];

if (i % 2 && !options.newlineIsToken) {
retLines[retLines.length - 1] += line;
} else {
if (options.ignoreWhitespace) {
line = line.trim();
}
retLines.push(line);
}
}

return retLines;
}

export { computeLineInformation };