private static @Nullable <T extends PsiElement> T findElementInRange(@NotNull PsiFile file,
                                                                       int startOffset,
                                                                       int endOffset,
                                                                       @NotNull Class<T> klass,
                                                                       @NotNull Language language,
                                                                       @Nullable PsiElement initialElement) {
    PsiElement element1 = file.getViewProvider().findElementAt(startOffset, language);
    PsiElement element2 = file.getViewProvider().findElementAt(endOffset - 1, language);
    if (element1 instanceof PsiWhiteSpace) {
      startOffset = element1.getTextRange().getEndOffset();
      element1 = file.getViewProvider().findElementAt(startOffset, language);
    }
    if (element2 instanceof PsiWhiteSpace) {
      endOffset = element2.getTextRange().getStartOffset();
      element2 = file.getViewProvider().findElementAt(endOffset - 1, language);
    }
    if (element2 == null || element1 == null) return null;
    final PsiElement commonParent = PsiTreeUtil.findCommonParent(element1, element2);
    final T element =
      ReflectionUtil.isAssignable(klass, commonParent.getClass())
      ? (T)commonParent : PsiTreeUtil.getParentOfType(commonParent, klass);

    if (element == initialElement) {
      return element;
    }
    
    if (element == null || element.getTextRange().getStartOffset() != startOffset || element.getTextRange().getEndOffset() != endOffset) {
      return null;
    }
    return element;
  }
private static @Nullable <T extends PsiElement> T findElementInRange(@NotNull PsiFile file,
                                                                       int startOffset,
                                                                       int endOffset,
                                                                       @NotNull Class<T> klass,
                                                                       @NotNull Language language,
                                                                       @Nullable PsiElement initialElement)