Search in sources :

Example 1 with XmlTagInsertHandler

use of com.intellij.codeInsight.completion.XmlTagInsertHandler in project intellij-community by JetBrains.

the class DefaultXmlTagNameProvider method getRootTagsVariants.

private static List<LookupElement> getRootTagsVariants(final XmlTag tag, final List<LookupElement> elements) {
    elements.add(LookupElementBuilder.create("?xml version=\"1.0\" encoding=\"\" ?>").withPresentableText("<?xml version=\"1.0\" encoding=\"\" ?>").withInsertHandler(new InsertHandler<LookupElement>() {

        @Override
        public void handleInsert(InsertionContext context, LookupElement item) {
            int offset = context.getEditor().getCaretModel().getOffset();
            context.getEditor().getCaretModel().moveToOffset(offset - 4);
            AutoPopupController.getInstance(context.getProject()).scheduleAutoPopup(context.getEditor());
        }
    }));
    final FileBasedIndex fbi = FileBasedIndex.getInstance();
    Collection<String> result = new ArrayList<>();
    Processor<String> processor = Processors.cancelableCollectProcessor(result);
    fbi.processAllKeys(XmlNamespaceIndex.NAME, processor, tag.getProject());
    final GlobalSearchScope scope = new EverythingGlobalScope();
    for (final String ns : result) {
        if (ns.startsWith("file://"))
            continue;
        fbi.processValues(XmlNamespaceIndex.NAME, ns, null, new FileBasedIndex.ValueProcessor<XsdNamespaceBuilder>() {

            @Override
            public boolean process(final VirtualFile file, XsdNamespaceBuilder value) {
                List<String> tags = value.getRootTags();
                for (String s : tags) {
                    elements.add(LookupElementBuilder.create(s).withTypeText(ns).withInsertHandler(new XmlTagInsertHandler() {

                        @Override
                        public void handleInsert(InsertionContext context, LookupElement item) {
                            final Editor editor = context.getEditor();
                            final Document document = context.getDocument();
                            final int caretOffset = editor.getCaretModel().getOffset();
                            final RangeMarker caretMarker = document.createRangeMarker(caretOffset, caretOffset);
                            caretMarker.setGreedyToRight(true);
                            XmlFile psiFile = (XmlFile) context.getFile();
                            boolean incomplete = XmlUtil.getTokenOfType(tag, XmlTokenType.XML_TAG_END) == null && XmlUtil.getTokenOfType(tag, XmlTokenType.XML_EMPTY_ELEMENT_END) == null;
                            XmlNamespaceHelper.getHelper(psiFile).insertNamespaceDeclaration(psiFile, editor, Collections.singleton(ns), null, null);
                            editor.getCaretModel().moveToOffset(caretMarker.getEndOffset());
                            XmlTag rootTag = psiFile.getRootTag();
                            if (incomplete) {
                                XmlToken token = XmlUtil.getTokenOfType(rootTag, XmlTokenType.XML_EMPTY_ELEMENT_END);
                                // remove tag end added by smart attribute adder :(
                                if (token != null)
                                    token.delete();
                                PsiDocumentManager.getInstance(context.getProject()).doPostponedOperationsAndUnblockDocument(document);
                                super.handleInsert(context, item);
                            }
                        }
                    }));
                }
                return true;
            }
        }, scope);
    }
    return elements;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) XmlFile(com.intellij.psi.xml.XmlFile) EverythingGlobalScope(com.intellij.psi.search.EverythingGlobalScope) XsdNamespaceBuilder(com.intellij.xml.index.XsdNamespaceBuilder) RangeMarker(com.intellij.openapi.editor.RangeMarker) InsertionContext(com.intellij.codeInsight.completion.InsertionContext) LookupElement(com.intellij.codeInsight.lookup.LookupElement) PrioritizedLookupElement(com.intellij.codeInsight.completion.PrioritizedLookupElement) XmlTagInsertHandler(com.intellij.codeInsight.completion.XmlTagInsertHandler) Document(com.intellij.openapi.editor.Document) XmlToken(com.intellij.psi.xml.XmlToken) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) InsertHandler(com.intellij.codeInsight.completion.InsertHandler) XmlTagInsertHandler(com.intellij.codeInsight.completion.XmlTagInsertHandler) Editor(com.intellij.openapi.editor.Editor) FileBasedIndex(com.intellij.util.indexing.FileBasedIndex) XmlTag(com.intellij.psi.xml.XmlTag)

Aggregations

InsertHandler (com.intellij.codeInsight.completion.InsertHandler)1 InsertionContext (com.intellij.codeInsight.completion.InsertionContext)1 PrioritizedLookupElement (com.intellij.codeInsight.completion.PrioritizedLookupElement)1 XmlTagInsertHandler (com.intellij.codeInsight.completion.XmlTagInsertHandler)1 LookupElement (com.intellij.codeInsight.lookup.LookupElement)1 Document (com.intellij.openapi.editor.Document)1 Editor (com.intellij.openapi.editor.Editor)1 RangeMarker (com.intellij.openapi.editor.RangeMarker)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 EverythingGlobalScope (com.intellij.psi.search.EverythingGlobalScope)1 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)1 XmlFile (com.intellij.psi.xml.XmlFile)1 XmlTag (com.intellij.psi.xml.XmlTag)1 XmlToken (com.intellij.psi.xml.XmlToken)1 FileBasedIndex (com.intellij.util.indexing.FileBasedIndex)1 XsdNamespaceBuilder (com.intellij.xml.index.XsdNamespaceBuilder)1