private void addWhen() {
    if (!PsiUtil.isAvailable(JavaFeature.PATTERN_GUARDS_AND_RECORD_PATTERNS, myPosition)) {
      return;
    }
    PsiElement element = PsiTreeUtil.skipWhitespacesAndCommentsForward(myPrevLeaf);
    if (element instanceof PsiErrorElement) {
      return;
    }

    element = PsiTreeUtil.skipWhitespacesAndCommentsBackward(PsiTreeUtil.prevLeaf(myPosition));
    if (element instanceof PsiErrorElement) {
      return;
    }

    PsiPattern psiPattern =
      PsiTreeUtil.getParentOfType(myPrevLeaf, PsiPattern.class, true, PsiStatement.class, PsiMember.class, PsiClass.class);
    if (psiPattern == null ||
        (psiPattern instanceof PsiTypeTestPattern testPattern &&
         testPattern.getPatternVariable() != null &&
         testPattern.getPatternVariable().getNameIdentifier() == myPosition)) {
      return;
    }
    PsiElement parentOfPattern = PsiTreeUtil.skipParentsOfType(psiPattern, PsiPattern.class, PsiDeconstructionList.class);
    if (!(parentOfPattern instanceof PsiCaseLabelElementList)) {
      return;
    }
    addKeyword(new OverridableSpace(createKeyword(PsiKeyword.WHEN), TailTypes.insertSpaceType()));
  }
private void addWhen()