public void insertKeyValueAtOffset(@NotNull YAMLKeyValue keyValue, int offset) {
    int indent = YAMLUtil.getIndentToThisElement(this);

    if (offset < getTextRange().getStartOffset()) {
      offset = getTextRange().getStartOffset();
    }

    YAMLElementGenerator generator = YAMLElementGenerator.getInstance(getProject());
    if (offset == getTextRange().getStartOffset()) {
      boolean pasteAtEmptyFirstMappingLine = PsiUtilCore.getElementType(getPrevSibling()) == YAMLTokenTypes.INDENT &&
                                             PsiUtilCore.getElementType(getFirstChild()) == YAMLTokenTypes.EOL;
      PsiElement newElement = addBefore(keyValue, getFirstChild());
      if (!pasteAtEmptyFirstMappingLine) {
        newElement = addAfter(generator.createEol(), newElement);
        addAfter(generator.createIndent(indent), newElement);
      }
      return;
    }

    if (offset == getTextRange().getEndOffset()) {
      addNewKeyToTheEnd(keyValue);
      return;
    }

    if (offset > getTextRange().getEndOffset()) {
      PsiElement nextLeaf = PsiTreeUtil.nextLeaf(this);
      List<PsiElement> toBeRemoved = new SmartList<>();
      while (YAMLElementTypes.SPACE_ELEMENTS.contains(PsiUtilCore.getElementType(nextLeaf))) {
        if (offset >= nextLeaf.getTextRange().getStartOffset()) {
          toBeRemoved.add(nextLeaf);
        }
        nextLeaf = PsiTreeUtil.nextLeaf(nextLeaf);
      }
      for (PsiElement leaf : toBeRemoved) {
        add(leaf);
      }
      for (PsiElement leaf : toBeRemoved) {
        leaf.delete();
      }

      addNewKeyToTheEnd(keyValue);
      return;
    }

    PsiElement child = getFirstChild();
    for (; child != null; child = child.getNextSibling()) {
      if (PsiUtilCore.getElementType(child) == YAMLTokenTypes.INDENT && offset <= child.getTextRange().getEndOffset()) {
        if (PsiUtilCore.getElementType(child.getNextSibling()) == YAMLTokenTypes.EOL) {
          addAfter(keyValue, child);
          return;
        }
        PsiElement newElement = addBefore(generator.createIndent(indent), child);
        newElement = addAfter(keyValue, newElement);
        addAfter(generator.createEol(), newElement);
        return;
      }
      if (offset <= child.getTextRange().getEndOffset()) {
        break;
      }
    }
    for (; child != null; child = child.getNextSibling()) {
      if (PsiUtilCore.getElementType(child) == YAMLTokenTypes.EOL) {
        PsiElement element = child;
        if (indent != 0) {
          element = addAfter(generator.createIndent(indent), element);
        }
        element = addAfter(keyValue, element);
        if (PsiUtilCore.getElementType(child) != YAMLTokenTypes.EOL) {
          addAfter(generator.createEol(), element);
        }
        return;
      }
    }
    addNewKeyToTheEnd(keyValue);
  }
public void insertKeyValueAtOffset(@NotNull YAMLKeyValue keyValue, int offset)