List<MatchMarker<SelectorPart>> matches = listsComparator.findMatches(lookForParts, inSelectorParts, selectorPartsComparator);
if (matches.isEmpty() || originalReplaceBy == null || originalReplaceBy.isEmpty())
return ;
SelectorPart lastRemainder = null;
MatchMarker<SelectorPart> previousMatch = null;
for (MatchMarker<SelectorPart> currentMatch : matches) {
SelectorPart firstMatch = currentMatch.getFirst();
SelectorPart lastMatch = currentMatch.getLast();
if (!inSelectorParts.contains(firstMatch) && (previousMatch==null || lastRemainder==null || previousMatch.getLast()!=currentMatch.getFirst())) { //particularly ugly condition
previousMatch = currentMatch;
continue ;
}
boolean prefixNeeded = true;
if (!inSelectorParts.contains(firstMatch) && lastRemainder!=null) { //particularly ugly code
firstMatch = ArraysUtils.last(builder.getParts());
prefixNeeded = false;
}
previousMatch = currentMatch;
List<SelectorPart> replaceBy = ArraysUtils.deeplyClonedList(originalReplaceBy);
if (firstMatch == lastMatch) {
if (lookForParts.size() != 1)
throw new BugHappened("Impossible state happened.", lookForParts.isEmpty() ? null : lookForParts.get(0));
List<SelectorPart> replaceInside = replaceInsidePart(lookForParts.get(0), lastMatch, replaceBy);
ArraysUtils.replace(lastMatch, inSelectorParts, replaceInside);
} else {
if (prefixNeeded)
builder.addUpTo(inSelectorParts, firstMatch);
ArraysUtils.chopFirst(inSelectorParts); // now we are chopping firstMatch
SelectorPart firstRemainder = selectorPartsComparator.cutSuffix(lookForParts.get(0), firstMatch);
if (firstRemainder != null) {
if (prefixNeeded)
builder.add(firstRemainder);
builder.directlyAttach(replaceBy);
} else {