react-diff-view
Advanced tools
Comparing version 1.4.0-beta.2 to 1.4.0-beta.3
{ | ||
"name": "react-diff-view", | ||
"version": "1.4.0-beta.2", | ||
"version": "1.4.0-beta.3", | ||
"description": "A git diff component to consume the git unified diff output.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -66,2 +66,23 @@ import leven from 'leven'; | ||
const createFindChangeByLineNumberFunction = side => { | ||
const computeLineNumber = side === 'old' ? computeOldLineNumber : computeNewLineNumber; | ||
const startProperty = side + 'Start'; | ||
const linesProperty = side + 'Lines'; | ||
const isInHunk = createIsInHunkFunction(startProperty, linesProperty); | ||
return (hunks, lineNumber) => { | ||
const containerHunk = hunks.find(hunk => isInHunk(hunk, lineNumber)); | ||
if (!containerHunk) { | ||
return undefined; | ||
} | ||
return containerHunk.changes.find(change => computeLineNumber(change) === lineNumber); | ||
}; | ||
}; | ||
export const findChangeByOldLineNumber = createFindChangeByLineNumberFunction('old'); | ||
export const findChangeByNewLineNumber = createFindChangeByLineNumberFunction('new'); | ||
const createCorrespondingLineNumberComputeFunction = baseSide => { | ||
@@ -73,2 +94,4 @@ const anotherSide = baseSide === 'old' ? 'new' : 'old'; | ||
const correspondingLines = anotherSide + 'Lines'; | ||
const baseLineNumber = baseSide === 'old' ? computeOldLineNumber : computeNewLineNumber; | ||
const correspondingLineNumber = baseSide === 'old' ? computeOldLineNumber : computeNewLineNumber; | ||
const isInHunk = createIsInHunkFunction(baseStart, baseLines); | ||
@@ -97,4 +120,33 @@ const isBetweenHunks = createIsBetweenHunksFunction(baseStart, baseLines); | ||
// Within current hunk or between 2 hunks | ||
if (isInHunk(currentHunk, lineNumber) || isBetweenHunks(currentHunk, nextHunk, lineNumber)) { | ||
// Within current hunk | ||
if (isInHunk(currentHunk, lineNumber)) { | ||
const changeIndex = currentHunk.changes.findIndex(change => baseLineNumber(change) === lineNumber); | ||
const change = currentHunk.changes[changeIndex]; | ||
if (change.isNormal) { | ||
return correspondingLineNumber(change); | ||
} | ||
// For changes of type "insert" and "delete", the sibling change can be the corresponding one, | ||
// or they can have no corresponding change | ||
// | ||
// Git diff always put delete change before insert change | ||
// | ||
// Note that `nearbySequences: "zip"` option can affect this function | ||
const possibleCorrespondingChangeIndex = change.isDelete ? changeIndex + 1 : changeIndex - 1; | ||
const possibleCorrespondingChange = currentHunk.changes[possibleCorrespondingChangeIndex]; | ||
if (!possibleCorrespondingChange) { | ||
return -1; | ||
} | ||
const negativeChangeType = change.isInsert ? 'delete' : 'insert'; | ||
return possibleCorrespondingChange.type === negativeChangeType | ||
? correspondingLineNumber(possibleCorrespondingChange) | ||
: -1; | ||
} | ||
// Between 2 hunks | ||
if (isBetweenHunks(currentHunk, nextHunk, lineNumber)) { | ||
const spanFromEnd = lineNumber - currentHunk[baseStart] - currentHunk[baseLines]; | ||
@@ -101,0 +153,0 @@ return currentHunk[correspondingStart] + currentHunk[correspondingLines] + spanFromEnd; |
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
587430
1516