react-diff-view
Advanced tools
Comparing version 1.4.0-beta.1 to 1.4.0-beta.2
{ | ||
"name": "react-diff-view", | ||
"version": "1.4.0-beta.1", | ||
"version": "1.4.0-beta.2", | ||
"description": "A git diff component to consume the git unified diff output.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -52,51 +52,60 @@ import leven from 'leven'; | ||
const isInHunk = (hunk, oldLineNumber) => { | ||
const start = hunk.oldStart; | ||
const end = hunk.oldStart + hunk.oldLines; | ||
const createIsInHunkFunction = (startProperty, linesProperty) => (hunk, lineNumber) => { | ||
const start = hunk[startProperty]; | ||
const end = start + hunk[linesProperty]; | ||
return oldLineNumber >= start && oldLineNumber <= end; | ||
return lineNumber >= start && lineNumber <= end; | ||
}; | ||
const isBetweenHunks = (previousHunk, nextHunk, oldLineNumber) => { | ||
const start = previousHunk.oldStart + previousHunk.oldLines; | ||
const createIsBetweenHunksFunction = (startProperty, linesProperty) => (previousHunk, nextHunk, lineNumber) => { | ||
const start = previousHunk[startProperty] + previousHunk[linesProperty]; | ||
const end = nextHunk[startProperty]; | ||
if (!nextHunk) { | ||
return oldLineNumber > start; | ||
} | ||
return lineNumber > start && lineNumber < end; | ||
}; | ||
const end = nextHunk.oldStart; | ||
const createCorrespondingLineNumberComputeFunction = baseSide => { | ||
const anotherSide = baseSide === 'old' ? 'new' : 'old'; | ||
const baseStart = baseSide + 'Start'; | ||
const baseLines = baseSide + 'Lines'; | ||
const correspondingStart = anotherSide + 'Start'; | ||
const correspondingLines = anotherSide + 'Lines'; | ||
const isInHunk = createIsInHunkFunction(baseStart, baseLines); | ||
const isBetweenHunks = createIsBetweenHunksFunction(baseStart, baseLines); | ||
return oldLineNumber > start && oldLineNumber < end; | ||
}; | ||
return (hunks, lineNumber) => { | ||
const firstHunk = first(hunks); | ||
const getCorrespondingNewLineNumber = (hunks, oldLineNumber) => { | ||
const firstHunk = first(hunks); | ||
// Before first hunk | ||
if (lineNumber < firstHunk[baseStart]) { | ||
const spanFromStart = firstHunk[baseStart] - lineNumber; | ||
return firstHunk[correspondingStart] - spanFromStart; | ||
} | ||
// Before first hunk | ||
if (oldLineNumber < firstHunk.oldStart) { | ||
const spanFromStart = firstHunk.oldStart - oldLineNumber; | ||
return firstHunk.newStart - spanFromStart; | ||
} | ||
// After last hunk, this can be done in `for` loop, just a quick return path | ||
const lastHunk = last(hunks); | ||
if (lastHunk[baseStart] + lastHunk[baseLines] <= lineNumber) { | ||
const spanFromEnd = lineNumber - lastHunk[baseStart] - lastHunk[baseLines]; | ||
return lastHunk[correspondingStart] + lastHunk[correspondingLines] + spanFromEnd; | ||
} | ||
// After last hunk, this can be done in `for` loop, just a quick return path | ||
const lastHunk = last(hunks); | ||
if (lastHunk.oldStart + lastHunk.oldLines <= oldLineNumber) { | ||
const spanFromEnd = oldLineNumber - lastHunk.oldStart - lastHunk.oldLines; | ||
return lastHunk.newStart + lastHunk.newLines + spanFromEnd; | ||
} | ||
for (let i = 0; i < hunks.length; i++) { | ||
const currentHunk = hunks[i]; | ||
const nextHunk = hunks[i + 1]; | ||
for (let i = 0; i < hunks.length; i++) { | ||
const currentHunk = hunks[i]; | ||
const nextHunk = hunks[i + 1]; | ||
// Within current hunk or between 2 hunks | ||
if (isInHunk(currentHunk, oldLineNumber) || isBetweenHunks(currentHunk, nextHunk, oldLineNumber)) { | ||
const spanFromEnd = oldLineNumber - currentHunk.oldStart - currentHunk.oldLines; | ||
return currentHunk.newStart + currentHunk.newLines + spanFromEnd; | ||
// Within current hunk or between 2 hunks | ||
if (isInHunk(currentHunk, lineNumber) || isBetweenHunks(currentHunk, nextHunk, lineNumber)) { | ||
const spanFromEnd = lineNumber - currentHunk[baseStart] - currentHunk[baseLines]; | ||
return currentHunk[correspondingStart] + currentHunk[correspondingLines] + spanFromEnd; | ||
} | ||
} | ||
} | ||
throw new Error(`Unexpected line position ${oldLineNumber}`); | ||
throw new Error(`Unexpected line position ${lineNumber}`); | ||
}; | ||
}; | ||
export const getCorrespondingOldLineNumber = createCorrespondingLineNumberComputeFunction('new'); | ||
export const getCorrespondingNewLineNumber = createCorrespondingLineNumberComputeFunction('old'); | ||
const sliceHunk = (hunk, startOldLineNumber, endOldLineNumber) => { | ||
@@ -103,0 +112,0 @@ const isInRange = change => { |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
581123
1474