protected @Nullable YAMLValue deleteLookupTextAndRetrieveOldValue(InsertionContext context, @NotNull PsiElement elementAtCaret) {
    final YAMLValue oldValue;
    if (elementAtCaret.getNode().getElementType() != YAMLTokenTypes.SCALAR_KEY) {
      deleteLookupPlain(context);
      return null;
    }

    final YAMLKeyValue keyValue = PsiTreeUtil.getParentOfType(elementAtCaret, YAMLKeyValue.class);
    assert keyValue != null;
    assert keyValue.getParentMapping() != null || keyValue.getParent() instanceof YAMLCompoundValue;

    Document document = context.getDocument();
    String text = document.getText(TextRange.create(context.getStartOffset(), context.getTailOffset()));
    if (!keyValue.getKeyText().equals(text)) {
      // Insertion was performed at the start of existing keyValue, we should delete only inserted text from the key.
      document.deleteString(context.getStartOffset(), context.getTailOffset());
      context.commitDocument();
      return null;
    }
    context.commitDocument();
    if (keyValue.getValue() != null) {
      // Save old value somewhere
      final YAMLKeyValue dummyKV = YAMLElementGenerator.getInstance(context.getProject()).createYamlKeyValue("foo", "b");
      dummyKV.setValue(keyValue.getValue());
      oldValue = dummyKV.getValue();
    }
    else {
      oldValue = null;
    }

    context.setTailOffset(keyValue.getTextRange().getEndOffset());
    WriteCommandAction.runWriteCommandAction(context.getProject(),
                                             YAMLBundle.message("YamlKeyCompletionInsertHandler.remove.key"),
                                             null,
                                             () -> {
                                               PsiElement parent = keyValue.getParent();
                                               PsiElement substitute = null;
                                               boolean delete = parent.getNode().getChildren(null).length == 1;
                                               if (parent instanceof YAMLMapping parentMapping) {
                                                 parentMapping.deleteKeyValue(keyValue);
                                                 ASTNode[] children = parent.getNode().getChildren(null);
                                                 if (children.length == 1 && children[0] instanceof LeafPsiElement) {
                                                   substitute = children[0].getPsi();
                                                 }
                                               }
                                               else {
                                                 YAMLUtil.deleteSurroundingWhitespace(keyValue);
                                                 keyValue.delete();
                                               }
                                               if (delete) {
                                                 parent.delete();
                                               }
                                               else if (substitute != null) {
                                                 parent.replace(substitute);
                                               }
                                             });
    return oldValue;
  }
protected @Nullable YAMLValue deleteLookupTextAndRetrieveOldValue(InsertionContext context, @NotNull PsiElement elementAtCaret)