void (final @NotNull InsertionContext context, final @NotNull LookupElement item) { final XmlFile contextFile = (XmlFile)context.getFile(); final XmlExtension extension = XmlExtension.getExtension(contextFile); final XmlFile file = extension.getContainingFile(contextFile); final Project project = context.getProject(); assert file != null; final PsiElement psiElement = file.findElementAt(context.getStartOffset()); assert psiElement != null; if (isNamespaceBound(psiElement)) { doDefault(context, item); return; } final Editor editor = context.getEditor(); final Document document = editor.getDocument(); PsiDocumentManager.getInstance(project).commitDocument(document); final int caretOffset = editor.getCaretModel().getOffset(); final RangeMarker caretMarker = document.createRangeMarker(caretOffset, caretOffset); caretMarker.setGreedyToRight(true); final XmlNamespaceHelper.Runner<String, IncorrectOperationException> runAfter = new XmlNamespaceHelper.Runner<>() { @Override public void run(final String namespacePrefix) { PsiDocumentManager.getInstance(project).doPostponedOperationsAndUnblockDocument(document); final PsiElement element = file.findElementAt(context.getStartOffset()); if (element != null) { qualifyWithPrefix(namespacePrefix, element, document); PsiDocumentManager.getInstance(project).commitDocument(document); } editor.getCaretModel().moveToOffset(caretMarker.getEndOffset()); doDefault(context, item); } }; try { final String prefixByNamespace = getPrefixByNamespace(file, myNamespace); if (myNamespacePrefix != null || StringUtil.isEmpty(prefixByNamespace)) { final String nsPrefix = myNamespacePrefix == null ? suggestPrefix(file, myNamespace) : myNamespacePrefix; XmlNamespaceHelper.getHelper(file).insertNamespaceDeclaration(file, editor, Collections.singleton(myNamespace), nsPrefix, runAfter); FeatureUsageTracker.getInstance().triggerFeatureUsed(XmlCompletionContributor.TAG_NAME_COMPLETION_FEATURE); } else { runAfter.run(prefixByNamespace); // qualify && complete } } catch (IncorrectOperationException e) { LOG.error(e); } }