PsiElement () { PsiElement parent = getParent(); if (parent == null) return this; if (parent instanceof PsiParameterList) { return parent.getParent(); } if (parent instanceof PsiForeachStatementBase) { return parent; } if (parent instanceof PsiCatchSection) { return parent; } PsiElement[] children = parent.getChildren(); for (int i = 0; i < children.length; i++) { if (children[i].equals(this)) { for (int j = i + 1; j < children.length; j++) { if (children[j] instanceof PsiCodeBlock) return children[j]; } break; } } StringBuilder ancestors = new StringBuilder(); for (PsiElement e = parent; e != null; e = e.getParent()) ancestors.append(' ').append(e); throw new IllegalStateException("Parameter: " + this + "; siblings: " + Arrays.asList(children) + "; ancestors:" + ancestors); }