Search in sources :

Example 6 with XmlElementDescriptor

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

the class GenerateXmlTagAction method invoke.

@Override
public void invoke(@NotNull final Project project, @NotNull final Editor editor, @NotNull final PsiFile file) {
    if (!EditorModificationUtil.checkModificationAllowed(editor))
        return;
    try {
        final XmlTag contextTag = getContextTag(editor, file);
        if (contextTag == null) {
            throw new CommonRefactoringUtil.RefactoringErrorHintException("Caret should be positioned inside a tag");
        }
        XmlElementDescriptor currentTagDescriptor = contextTag.getDescriptor();
        assert currentTagDescriptor != null;
        final XmlElementDescriptor[] descriptors = currentTagDescriptor.getElementsDescriptors(contextTag);
        Arrays.sort(descriptors, Comparator.comparing(PsiMetaData::getName));
        final JBList list = new JBList(descriptors);
        list.setCellRenderer(new MyListCellRenderer());
        Runnable runnable = () -> {
            final XmlElementDescriptor selected = (XmlElementDescriptor) list.getSelectedValue();
            new WriteCommandAction.Simple(project, "Generate XML Tag", file) {

                @Override
                protected void run() {
                    if (selected == null)
                        return;
                    XmlTag newTag = createTag(contextTag, selected);
                    PsiElement anchor = getAnchor(contextTag, editor, selected);
                    if (anchor == null) {
                        // insert it in the cursor position
                        int offset = editor.getCaretModel().getOffset();
                        Document document = editor.getDocument();
                        document.insertString(offset, newTag.getText());
                        PsiDocumentManager.getInstance(project).commitDocument(document);
                        newTag = PsiTreeUtil.getParentOfType(file.findElementAt(offset + 1), XmlTag.class, false);
                    } else {
                        newTag = (XmlTag) contextTag.addAfter(newTag, anchor);
                    }
                    if (newTag != null) {
                        generateTag(newTag, editor);
                    }
                }
            }.execute();
        };
        if (ApplicationManager.getApplication().isUnitTestMode()) {
            XmlElementDescriptor descriptor = ContainerUtil.find(descriptors, xmlElementDescriptor -> xmlElementDescriptor.getName().equals(TEST_THREAD_LOCAL.get()));
            list.setSelectedValue(descriptor, false);
            runnable.run();
        } else {
            JBPopupFactory.getInstance().createListPopupBuilder(list).setTitle("Choose Tag Name").setItemChoosenCallback(runnable).setFilteringEnabled(o -> ((XmlElementDescriptor) o).getName()).createPopup().showInBestPositionFor(editor);
        }
    } catch (CommonRefactoringUtil.RefactoringErrorHintException e) {
        HintManager.getInstance().showErrorHint(editor, e.getMessage());
    }
}
Also used : Document(com.intellij.openapi.editor.Document) CommonRefactoringUtil(com.intellij.refactoring.util.CommonRefactoringUtil) JBList(com.intellij.ui.components.JBList) XmlElementDescriptor(com.intellij.xml.XmlElementDescriptor) PsiElement(com.intellij.psi.PsiElement)

Example 7 with XmlElementDescriptor

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

the class Xsd2InstanceUtils method addVariantsFromRootTag.

public static List<String> addVariantsFromRootTag(XmlTag rootTag) {
    PsiMetaData metaData = rootTag.getMetaData();
    if (metaData instanceof XmlNSDescriptorImpl) {
        XmlNSDescriptorImpl nsDescriptor = (XmlNSDescriptorImpl) metaData;
        List<String> elementDescriptors = new ArrayList<>();
        XmlElementDescriptor[] rootElementsDescriptors = nsDescriptor.getRootElementsDescriptors(PsiTreeUtil.getParentOfType(rootTag, XmlDocument.class));
        for (XmlElementDescriptor e : rootElementsDescriptors) {
            elementDescriptors.add(e.getName());
        }
        return elementDescriptors;
    }
    return Collections.emptyList();
}
Also used : PsiMetaData(com.intellij.psi.meta.PsiMetaData) XmlDocument(com.intellij.psi.xml.XmlDocument) XmlNSDescriptorImpl(com.intellij.xml.impl.schema.XmlNSDescriptorImpl) XmlElementDescriptor(com.intellij.xml.XmlElementDescriptor)

Example 8 with XmlElementDescriptor

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

Example 9 with XmlElementDescriptor

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

the class XmlInsightTest method testAttributeDescriptor1.

public void testAttributeDescriptor1() throws Exception {
    XmlFile file = createFile("<root><a attr1=''></a><a attr2='' attr1=''></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("attr1", attributes[0].getName());
    assertTrue(attributes[0].isRequired());
    assertEquals("attr2", 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)

Example 10 with XmlElementDescriptor

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

the class XmlInsightTest method testElementDescriptor2.

public void testElementDescriptor2() throws Exception {
    XmlFile file = createFile("<root><a><b/></a><a><c/></a></root>");
    XmlNSDescriptor descriptor = createDescriptor(file);
    XmlTag rootTag = file.getDocument().getRootTag();
    XmlElementDescriptor element = descriptor.getElementDescriptor(rootTag);
    element = element.getElementsDescriptors(rootTag)[0];
    XmlElementDescriptor[] elements = element.getElementsDescriptors(rootTag.getSubTags()[0]);
    assertEquals(2, elements.length);
    assertEquals("b", elements[0].getName());
    assertEquals("c", elements[1].getName());
}
Also used : XmlFile(com.intellij.psi.xml.XmlFile) XmlNSDescriptor(com.intellij.xml.XmlNSDescriptor) XmlElementDescriptor(com.intellij.xml.XmlElementDescriptor) XmlTag(com.intellij.psi.xml.XmlTag)

Aggregations

XmlElementDescriptor (com.intellij.xml.XmlElementDescriptor)159 XmlTag (com.intellij.psi.xml.XmlTag)88 XmlNSDescriptor (com.intellij.xml.XmlNSDescriptor)60 XmlAttributeDescriptor (com.intellij.xml.XmlAttributeDescriptor)54 PsiElement (com.intellij.psi.PsiElement)23 XmlFile (com.intellij.psi.xml.XmlFile)23 Nullable (org.jetbrains.annotations.Nullable)23 AnyXmlElementDescriptor (com.intellij.xml.impl.schema.AnyXmlElementDescriptor)22 PsiFile (com.intellij.psi.PsiFile)11 ArrayList (java.util.ArrayList)11 XmlAttribute (com.intellij.psi.xml.XmlAttribute)10 NotNull (org.jetbrains.annotations.NotNull)10 ClassBackedElementDescriptor (com.intellij.javascript.flex.mxml.schema.ClassBackedElementDescriptor)7 JSClass (com.intellij.lang.javascript.psi.ecmal4.JSClass)7 Project (com.intellij.openapi.project.Project)7 HtmlTag (com.intellij.psi.html.HtmlTag)7 AnnotationBackedDescriptor (com.intellij.lang.javascript.flex.AnnotationBackedDescriptor)5 XmlDocument (com.intellij.psi.xml.XmlDocument)5 DElementPattern (org.kohsuke.rngom.digested.DElementPattern)5 InvalidPropertyException (com.intellij.flex.uiDesigner.InvalidPropertyException)4