use of com.intellij.diff.util.IntPair in project intellij-community by JetBrains.
the class GenericPatchApplier method testForExactMatch.
private boolean testForExactMatch(final SplitHunk splitHunk, final SplitHunk originalHunk) {
final int offset = splitHunk.getContextBefore().size();
final List<BeforeAfter<List<String>>> steps = splitHunk.getPatchSteps();
if (splitHunk.isInsertion()) {
final boolean emptyFile = myLines.isEmpty() || myLines.size() == 1 && myLines.get(0).trim().length() == 0;
if (emptyFile) {
myNotBound.add(splitHunk);
}
return emptyFile;
}
int idx = splitHunk.getStartLineBefore() + offset;
int cnt = 0;
boolean hadAlreadyApplied = false;
for (BeforeAfter<List<String>> step : steps) {
if (myLines.size() <= idx)
return false;
// can occur only in the end
if (step.getBefore().isEmpty())
continue;
final Pair<Integer, Boolean> distance = new FragmentMatcher(idx + cnt, step).find(false);
if (distance.getFirst() > 0) {
return false;
}
// fits!
int length;
if (distance.getSecond()) {
length = step.getBefore().size();
} else {
length = step.getAfter().size();
hadAlreadyApplied = true;
}
cnt += length;
//idx += length - 1;
}
putCutIntoTransformations(new TextRange(idx, idx + cnt - 1), originalHunk, new MyAppliedData(splitHunk.getAfterAll(), hadAlreadyApplied, true, true, ChangeType.REPLACE), new IntPair(originalHunk.getContextBefore().size() - splitHunk.getContextBefore().size(), originalHunk.getContextAfter().size() - splitHunk.getContextAfter().size()));
return true;
}
use of com.intellij.diff.util.IntPair in project intellij-community by JetBrains.
the class GenericPatchApplier method testForPartialContextMatch.
private boolean testForPartialContextMatch(final SplitHunk splitHunkWithExtendedContext, final MismatchSolver mismatchSolver, final int maxWalkFromBinding, @Nullable final SplitHunk originalSplitHunk) {
final List<BeforeAfter<List<String>>> steps = splitHunkWithExtendedContext.getPatchSteps();
final BetterPoint betterPoint = new BetterPoint();
if (splitHunkWithExtendedContext.isInsertion())
return false;
// if it is not just insertion, then in first step will be both parts
final Iterator<FirstLineDescriptor> iterator = mismatchSolver.getStartLineVariationsIterator();
while (iterator.hasNext() && (betterPoint.getPoint() == null || !betterPoint.getPoint().idealFound())) {
final FirstLineDescriptor descriptor = iterator.next();
final Iterator<Integer> matchingIterator = getMatchingIterator(descriptor.getLine(), splitHunkWithExtendedContext.getStartLineBefore() + descriptor.getOffset(), maxWalkFromBinding);
while (matchingIterator.hasNext() && (betterPoint.getPoint() == null || !betterPoint.getPoint().idealFound())) {
final Integer lineNumber = matchingIterator.next();
// go back and forward from point
final List<BeforeAfter<List<String>>> patchSteps = splitHunkWithExtendedContext.getPatchSteps();
final BeforeAfter<List<String>> step = patchSteps.get(descriptor.getStepNumber());
final FragmentResult fragmentResult = checkFragmented(lineNumber, descriptor.getOffsetInStep(), step, descriptor.isIsInBefore());
// we go back - if middle fragment ok
if (descriptor.getStepNumber() > 0 && fragmentResult.isStartAtEdge()) {
// not including step number here
final List<BeforeAfter<List<String>>> list = Collections.unmodifiableList(patchSteps.subList(0, descriptor.getStepNumber()));
int offsetForStart = -descriptor.getOffsetInStep() - 1;
final SequentialStepsChecker backChecker = new SequentialStepsChecker(lineNumber + offsetForStart, false);
backChecker.go(list);
fragmentResult.setContainAlreadyApplied(fragmentResult.isContainAlreadyApplied() || backChecker.isUsesAlreadyApplied());
fragmentResult.setStart(fragmentResult.getStart() - backChecker.getSizeOfFragmentToBeReplaced());
fragmentResult.addDistance(backChecker.getDistance());
fragmentResult.setStartAtEdge(backChecker.getDistance() == 0);
}
if (steps.size() > descriptor.getStepNumber() + 1 && fragmentResult.isEndAtEdge()) {
// forward
final List<BeforeAfter<List<String>>> list = Collections.unmodifiableList(patchSteps.subList(descriptor.getStepNumber() + 1, patchSteps.size()));
if (!list.isEmpty()) {
final SequentialStepsChecker checker = new SequentialStepsChecker(fragmentResult.getEnd() + 1, true);
checker.go(list);
fragmentResult.setContainAlreadyApplied(fragmentResult.isContainAlreadyApplied() || checker.isUsesAlreadyApplied());
fragmentResult.setEnd(fragmentResult.getEnd() + checker.getSizeOfFragmentToBeReplaced());
fragmentResult.addDistance(checker.getDistance());
fragmentResult.setEndAtEdge(checker.getDistance() == 0);
}
}
final TextRange textRangeInOldDocument = new UnfairTextRange(fragmentResult.getStart(), fragmentResult.getEnd());
//if (pointCanBeUsed(textRangeInOldDocument) && (! mismatchSolver.isAllowMismatch() || fragmentResult.getEnd() - fragmentResult.getStart() > 1)) {
if (pointCanBeUsed(textRangeInOldDocument)) {
final int distance = fragmentResult.myDistance;
final int commonPart = fragmentResult.getEnd() - fragmentResult.getStart() + 1;
int contextDistance = 0;
if (distance == 0 || commonPart < 2) {
final int distanceBack = getDistanceBack(fragmentResult.getStart() - 1, splitHunkWithExtendedContext.getContextBefore());
final int distanceInContextAfter = getDistance(fragmentResult.getEnd() + 1, splitHunkWithExtendedContext.getContextAfter());
contextDistance = distanceBack + distanceInContextAfter;
}
betterPoint.feed(new Point(distance, textRangeInOldDocument, fragmentResult.isContainAlreadyApplied(), contextDistance, commonPart));
}
}
}
final Point pointPoint = betterPoint.getPoint();
if (pointPoint == null)
return false;
if (!mismatchSolver.isAllowMismatch()) {
if (pointPoint.getDistance() > 0)
return false;
if (pointPoint.myCommon < 2) {
final int contextCommon = splitHunkWithExtendedContext.getContextBefore().size() + splitHunkWithExtendedContext.getContextAfter().size() - pointPoint.myContextDistance;
if (contextCommon == 0)
return false;
}
}
putCutIntoTransformations(pointPoint.getInOldDocument(), originalSplitHunk, new MyAppliedData(splitHunkWithExtendedContext.getAfterAll(), pointPoint.myUsesAlreadyApplied, false, pointPoint.getDistance() == 0, ChangeType.REPLACE), originalSplitHunk == null ? EMPTY_OFFSET : new IntPair(originalSplitHunk.getContextBefore().size() - splitHunkWithExtendedContext.getContextBefore().size(), originalSplitHunk.getContextAfter().size() - splitHunkWithExtendedContext.getContextAfter().size()));
return true;
}
Aggregations