void (@NotNull InsertionContext context, @NotNull T item) { final PsiElement currentElement = context.getFile().findElementAt(context.getStartOffset()); assert currentElement != null : "no element at " + context.getStartOffset(); YAMLKeyValue parent = PsiTreeUtil.getParentOfType(currentElement, YAMLKeyValue.class); final YAMLDocument holdingDocument = PsiTreeUtil.getParentOfType(currentElement, YAMLDocument.class); assert holdingDocument != null; YAMLValue oldValue = (holdingDocument.getTopLevelValue() instanceof YAMLMapping) ? deleteLookupTextAndRetrieveOldValue(context, currentElement) : null; // Inheritors must handle lookup text removal since otherwise holdingDocument may become invalid. if (oldValue instanceof YAMLSequence) { trimSequenceItemIndents((YAMLSequence)oldValue); } final YAMLKeyValue created = createNewEntry(holdingDocument, item, parent != null && parent.isValid() ? parent : null); YAMLValue createdValue = created.getValue(); if (createdValue != null) { context.getEditor().getCaretModel().moveToOffset(createdValue.getTextRange().getStartOffset() - 1); } else { context.getEditor().getCaretModel().moveToOffset(created.getTextRange().getEndOffset()); } if (oldValue != null) { WriteCommandAction.runWriteCommandAction(context.getProject(), YAMLBundle.message("YamlKeyCompletionInsertHandler.insert.value"), null, () -> created.setValue(oldValue)); } PsiDocumentManager.getInstance(context.getProject()).doPostponedOperationsAndUnblockDocument(context.getDocument()); createdValue = created.getValue(); // retrieve restored value YAMLSequenceItem valueItem = createdValue instanceof YAMLSequence ? ContainerUtil.getFirstItem(((YAMLSequence)createdValue).getItems()) : null; if (valueItem != null) { context.getEditor().getCaretModel().moveToOffset(valueItem.getTextOffset() + 1); } if (!isCharAtCaret(context.getEditor(), ' ')) { EditorModificationUtil.insertStringAtCaret(context.getEditor(), " "); } else { context.getEditor().getCaretModel().moveCaretRelatively(1, 0, false, false, true); } }