PsiElement (@NotNull PsiElement original, TextRange fitToRange) { PsiElement currElement = original; PsiElement prevElement = original; while (currElement != null) { if ((currElement instanceof PsiFile) || !fitToRange.contains(currElement.getTextRange())) { if (!fitToRange.contains(prevElement.getTextRange())) { return original; } return prevElement; } prevElement = currElement; currElement = currElement.getParent(); } return original; }