PsiElement (@NotNull PsiElement elementToReplace, PsiElement @NotNull [] patternElements, @NotNull String replacementToMake, @NotNull PsiElement elementParent) { int i = 0; while (true) { // if it goes out of bounds then deep error happens if (!(patternElements[i] instanceof PsiComment || patternElements[i] instanceof PsiWhiteSpace)) { break; } ++i; if (patternElements.length == i) { break; } } if (patternElements.length == i) { Logger logger = Logger.getInstance(StructuralSearchProfile.class.getName()); logger.error("Unexpected replacement structure:" + replacementToMake); } if (i != 0) { elementParent.addRangeBefore(patternElements[0], patternElements[i - 1], elementToReplace); } return patternElements[i]; }