int (@NotNull Side touchSide, int equalForward, int equalBackward, @NotNull Range range1, @NotNull Range range2) { List<? extends InlineChunk> touchWords = touchSide.select(myData1, myData2); CharSequence touchText = touchSide.select(myText1, myText2); int touchStart = touchSide.select(range2.start1, range2.start2); // check if chunks are already separated by whitespaces if (isSeparatedWithWhitespace(touchText, touchWords.get(touchStart - 1), touchWords.get(touchStart))) return 0; // shift chunks left [X]A Y[A ZA] -> [XA] YA [ZA] // [X][A ZA] -> [XA] [ZA] int leftShift = findSequenceEdgeShift(touchText, touchWords, touchStart, equalForward, true); if (leftShift > 0) return leftShift; // shift chunks right [AX A]Y A[Z] -> [AX] AY [AZ] // [AX A][Z] -> [AX] [AZ] int rightShift = findSequenceEdgeShift(touchText, touchWords, touchStart - 1, equalBackward, false); if (rightShift > 0) return -rightShift; // nothing to do return 0; }