Search in sources :

Example 11 with XmlFile

use of com.intellij.psi.xml.XmlFile in project intellij-community by JetBrains.

the class InputExpressionDialog method prepareShow.

@SuppressWarnings({ "unchecked" })
private void prepareShow(XmlElement contextElement) {
    final NamespaceCollector.CollectedInfo collectedInfo;
    if (contextElement != null) {
        collectedInfo = NamespaceCollector.collectInfo((XmlFile) contextElement.getContainingFile());
        myNamespaceCache = collectedInfo.namespaces;
    } else {
        collectedInfo = NamespaceCollector.empty();
        myNamespaceCache = null;
    }
    myContextProvider = new InteractiveContextProvider(contextElement, collectedInfo, myModel);
    myContextProvider.attachTo(myXPathFile);
    final HistoryElement historyElement = myModel.getSelectedItem();
    if (historyElement != null) {
        myContextProvider.getNamespaceContext().setMap(asMap(historyElement.namespaces));
    } else {
        myContextProvider.getNamespaceContext().setMap(asMap(null));
    }
    updateOkAction();
}
Also used : HistoryElement(org.intellij.plugins.xpathView.HistoryElement) NamespaceCollector(org.intellij.plugins.xpathView.util.NamespaceCollector) XmlFile(com.intellij.psi.xml.XmlFile)

Example 12 with XmlFile

use of com.intellij.psi.xml.XmlFile in project intellij-community by JetBrains.

the class RenameXmlAttributeProcessor method doRenameXmlAttributeValue.

private static void doRenameXmlAttributeValue(@NotNull XmlAttributeValue value, String newName, UsageInfo[] infos, @Nullable RefactoringElementListener listener) throws IncorrectOperationException {
    LOG.assertTrue(value.isValid());
    renameAll(value, infos, newName, value.getValue());
    PsiManager psiManager = value.getManager();
    LOG.assertTrue(psiManager != null);
    XmlFile file = (XmlFile) PsiFileFactory.getInstance(psiManager.getProject()).createFileFromText("dummy.xml", XMLLanguage.INSTANCE, "<a attr=\"" + newName + "\"/>");
    @SuppressWarnings("ConstantConditions") PsiElement element = value.replace(file.getRootTag().getAttributes()[0].getValueElement());
    if (listener != null) {
        listener.elementRenamed(element);
    }
}
Also used : XmlFile(com.intellij.psi.xml.XmlFile) PsiManager(com.intellij.psi.PsiManager) PsiElement(com.intellij.psi.PsiElement)

Example 13 with XmlFile

use of com.intellij.psi.xml.XmlFile 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)

Example 14 with XmlFile

use of com.intellij.psi.xml.XmlFile in project intellij-community by JetBrains.

the class RngHrefConverter method fromString.

@Override
public XmlFile fromString(@Nullable @NonNls String s, ConvertContext context) {
    if (s != null) {
        final GenericAttributeValue<XmlFile> element = (GenericAttributeValue<XmlFile>) context.getInvocationElement();
        final PsiReference[] references = createReferences(element, element.getXmlAttributeValue(), context);
        if (references.length > 0) {
            PsiElement result = references[references.length - 1].resolve();
            if (result instanceof XmlFile) {
                return (XmlFile) result;
            }
        }
    }
    return null;
}
Also used : XmlFile(com.intellij.psi.xml.XmlFile) PsiReference(com.intellij.psi.PsiReference) PsiElement(com.intellij.psi.PsiElement)

Example 15 with XmlFile

use of com.intellij.psi.xml.XmlFile in project intellij-community by JetBrains.

the class XmlInsightTest method testAttributeDescriptor2.

public void testAttributeDescriptor2() throws Exception {
    XmlFile file = createFile("<root><a c='' a=''></a></root>");
    XmlNSDescriptor descriptor = createDescriptor(file);
    XmlTag rootTag = file.getDocument().getRootTag();
    XmlElementDescriptor element = descriptor.getElementDescriptor(rootTag);
    element = element.getElementsDescriptors(rootTag)[0];
    XmlAttributeDescriptor[] attributes = element.getAttributesDescriptors(rootTag);
    assertEquals("c", attributes[0].getName());
    assertTrue(attributes[0].isRequired());
    assertEquals("a", attributes[1].getName());
    assertTrue(attributes[1].isRequired());
}
Also used : XmlFile(com.intellij.psi.xml.XmlFile) XmlNSDescriptor(com.intellij.xml.XmlNSDescriptor) XmlAttributeDescriptor(com.intellij.xml.XmlAttributeDescriptor) XmlElementDescriptor(com.intellij.xml.XmlElementDescriptor) XmlTag(com.intellij.psi.xml.XmlTag)

Aggregations

XmlFile (com.intellij.psi.xml.XmlFile)409 XmlTag (com.intellij.psi.xml.XmlTag)155 PsiFile (com.intellij.psi.PsiFile)121 VirtualFile (com.intellij.openapi.vfs.VirtualFile)102 Nullable (org.jetbrains.annotations.Nullable)74 Project (com.intellij.openapi.project.Project)69 NotNull (org.jetbrains.annotations.NotNull)66 PsiElement (com.intellij.psi.PsiElement)64 XmlAttribute (com.intellij.psi.xml.XmlAttribute)39 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)34 Module (com.intellij.openapi.module.Module)34 XmlDocument (com.intellij.psi.xml.XmlDocument)32 Result (com.intellij.openapi.application.Result)28 XmlElementDescriptor (com.intellij.xml.XmlElementDescriptor)23 XmlAttributeValue (com.intellij.psi.xml.XmlAttributeValue)22 XmlNSDescriptor (com.intellij.xml.XmlNSDescriptor)21 ArrayList (java.util.ArrayList)20 Document (com.intellij.openapi.editor.Document)19 AndroidFacet (org.jetbrains.android.facet.AndroidFacet)18 Editor (com.intellij.openapi.editor.Editor)15