private static @Nullable PsiElement getPrevious(@NotNull PsiElement element) {
    PsiElement result = element.getPrevSibling();
    if (result != null) {
      return result;
    } 
    
    PsiElement parent = getParentWithinFile(element);
    if (parent == null) {
      return null;
    }

    PsiElement parentSibling = null;
    for (; parent != null && parentSibling == null; parent = getParentWithinFile(parent)) {
      parentSibling = parent.getPrevSibling();
    }

    if (parentSibling == null) {
      return null;
    }

    result = parentSibling.getLastChild();
    return result == null ? parentSibling : result;
  }
private static @Nullable PsiElement getPrevious(@NotNull PsiElement element)