private static boolean isInside(int offset, PsiElement guard) {
    if (guard == null) return false;

    TextRange inside;
    if (guard instanceof PsiMethod) {
      PsiCodeBlock body = ((PsiMethod)guard).getBody();
      inside = body != null ? body.getTextRange() : null;
    }
    else if (guard instanceof PsiClassInitializer) {
      inside = ((PsiClassInitializer)guard).getBody().getTextRange();
    }
    else if (guard instanceof PsiClass) {
      PsiElement left = ((PsiClass)guard).getLBrace(), right = ((PsiClass)guard).getRBrace();
      inside = left != null && right != null ? new TextRange(left.getTextOffset(), right.getTextOffset()) : null;
    }
    else {
      inside = guard.getTextRange();
    }
    return inside != null && inside.contains(offset);
  }
private static boolean isInside(int offset, PsiElement guard)