Search in sources :

Example 81 with DomElement

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

the class MockDomElementsEditor method initFileEditor.

protected final DomFileEditor initFileEditor(final Project project, final VirtualFile virtualFile, final String name, final Factory<? extends BasicDomElementComponent> component) {
    myFileEditor = new DomFileEditor<BasicDomElementComponent>(project, virtualFile, name, component) {

        @Override
        public JComponent getPreferredFocusedComponent() {
            return null;
        }

        @Override
        @NotNull
        protected JComponent createCustomComponent() {
            final JComponent customComponent = super.createCustomComponent();
            myContents = getDomComponent();
            return customComponent;
        }

        @Override
        public void reset() {
            for (final Map.Entry<EditedElementDescription<? extends DomElement>, DomElement> entry : myDomElements.entrySet()) {
                final DomElement newValue = entry.getKey().find();
                final DomElement oldValue = entry.getValue();
                if (newValue != null && !newValue.equals(oldValue) || newValue == null && !oldValue.getManager().isMockElement(oldValue)) {
                    ((StableElement) oldValue).revalidate();
                }
            }
            super.reset();
        }

        @Override
        public void commit() {
            super.commit();
            final List<EditedElementDescription> descriptions = new ArrayList<>();
            final Set<PsiFile> changedFiles = new HashSet<>();
            for (final Map.Entry<EditedElementDescription<? extends DomElement>, DomElement> entry : myDomElements.entrySet()) {
                final EditedElementDescription description = entry.getKey();
                final DomElement editedElement = entry.getValue();
                if (description.find() == null && editedElement.getXmlTag() != null) {
                    descriptions.add(description);
                    final XmlFile xmlFile = description.getEditedFile();
                    if (xmlFile != null) {
                        changedFiles.add(xmlFile);
                    }
                }
            }
            new WriteCommandAction(project, PsiUtilCore.toPsiFileArray(changedFiles)) {

                @Override
                protected void run(@NotNull Result result) throws Throwable {
                    for (EditedElementDescription description : descriptions) {
                        final DomElement editedElement = myDomElements.get(description);
                        DomElement element = description.addElement();
                        element.copyFrom(editedElement);
                        description.initialize(element);
                        removeWatchedElement(editedElement);
                        ((StableElement) editedElement).invalidate();
                    }
                }
            }.execute();
        }
    };
    final DomManager domManager = DomManager.getDomManager(project);
    for (final DomElement element : myDomElements.values()) {
        if (domManager.isMockElement(element)) {
            myFileEditor.addWatchedElement(element);
        }
    }
    return myFileEditor;
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) XmlFile(com.intellij.psi.xml.XmlFile) DomManager(com.intellij.util.xml.DomManager) NotNull(org.jetbrains.annotations.NotNull) Result(com.intellij.openapi.application.Result) StableElement(com.intellij.util.xml.StableElement) DomElement(com.intellij.util.xml.DomElement)

Example 82 with DomElement

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

the class ApplicationLoader method registerMetaData.

private static void registerMetaData() {
    final MetaDataRegistrar registrar = MetaDataRegistrar.getInstance();
    registrar.registerMetaData(new AndFilter(new NamespaceFilter(RNG_NAMESPACE), new ClassFilter(XmlDocument.class)), RngNsDescriptor.class);
    registrar.registerMetaData(new ClassFilter(RncDocument.class), RngNsDescriptor.class);
    registrar.registerMetaData(new ElementFilter() {

        @Override
        public boolean isAcceptable(Object element, PsiElement context) {
            if (element instanceof XmlTag) {
                final XmlTag tag = (XmlTag) element;
                final DomElement domElement = DomManager.getDomManager(tag.getProject()).getDomElement(tag);
                return domElement instanceof RngDefine;
            }
            return false;
        }

        @Override
        public boolean isClassAcceptable(Class hintClass) {
            return XmlTag.class.isAssignableFrom(hintClass);
        }
    }, RngDefineMetaData.class);
}
Also used : AndFilter(com.intellij.psi.filters.AndFilter) RngDefine(org.intellij.plugins.relaxNG.xml.dom.RngDefine) DomElement(com.intellij.util.xml.DomElement) NamespaceFilter(com.intellij.psi.filters.position.NamespaceFilter) MetaDataRegistrar(com.intellij.psi.meta.MetaDataRegistrar) ClassFilter(com.intellij.psi.filters.ClassFilter) ElementFilter(com.intellij.psi.filters.ElementFilter) PsiElement(com.intellij.psi.PsiElement) RncDocument(org.intellij.plugins.relaxNG.compact.psi.impl.RncDocument) XmlTag(com.intellij.psi.xml.XmlTag)

Example 83 with DomElement

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

the class DomStructureViewBuilder method createStructureView.

@Override
@NotNull
public StructureView createStructureView(final FileEditor fileEditor, @NotNull final Project project) {
    return new StructureViewComponent(fileEditor, createStructureViewModel(fileEditor instanceof TextEditor ? ((TextEditor) fileEditor).getEditor() : null), project, true) {

        @Override
        public AsyncResult<AbstractTreeNode> expandPathToElement(final Object element) {
            if (element instanceof XmlElement && ((XmlElement) element).isValid()) {
                final XmlElement xmlElement = (XmlElement) element;
                XmlTag tag = PsiTreeUtil.getParentOfType(xmlElement, XmlTag.class, false);
                while (tag != null) {
                    final DomElement domElement = DomManager.getDomManager(xmlElement.getProject()).getDomElement(tag);
                    if (domElement != null) {
                        for (DomElement curElement = domElement; curElement != null; curElement = curElement.getParent()) {
                            if (myDescriptor.fun(curElement) == DomService.StructureViewMode.SHOW) {
                                return super.expandPathToElement(curElement.getXmlElement());
                            }
                        }
                    }
                    tag = PsiTreeUtil.getParentOfType(tag, XmlTag.class, true);
                }
            }
            return super.expandPathToElement(element);
        }
    };
}
Also used : TextEditor(com.intellij.openapi.fileEditor.TextEditor) DomElement(com.intellij.util.xml.DomElement) AbstractTreeNode(com.intellij.ide.util.treeView.AbstractTreeNode) XmlElement(com.intellij.psi.xml.XmlElement) StructureViewComponent(com.intellij.ide.structureView.newStructureView.StructureViewComponent) XmlTag(com.intellij.psi.xml.XmlTag) NotNull(org.jetbrains.annotations.NotNull)

Example 84 with DomElement

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

the class AttributeChildInvocationHandler method ensureXmlElementExists.

@Override
public final XmlAttribute ensureXmlElementExists() {
    XmlAttribute attribute = (XmlAttribute) getXmlElement();
    if (attribute != null)
        return attribute;
    final DomManagerImpl manager = getManager();
    final boolean b = manager.setChanging(true);
    try {
        attribute = ensureTagExists().setAttribute(getXmlElementName(), getXmlApiCompatibleNamespace(getParentHandler()), "");
        setXmlElement(attribute);
        getManager().cacheHandler(DomManagerImpl.DOM_ATTRIBUTE_HANDLER_KEY, attribute, this);
        final DomElement element = getProxy();
        manager.fireEvent(new DomEvent(element, true));
        return attribute;
    } catch (IncorrectOperationException e) {
        LOG.error(e);
        return null;
    } finally {
        manager.setChanging(b);
    }
}
Also used : XmlAttribute(com.intellij.psi.xml.XmlAttribute) DomElement(com.intellij.util.xml.DomElement) IncorrectOperationException(com.intellij.util.IncorrectOperationException) DomEvent(com.intellij.util.xml.events.DomEvent)

Example 85 with DomElement

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

the class CollectionElementInvocationHandler method createPathStableCopy.

@Override
public DomElement createPathStableCopy() {
    final AbstractDomChildDescriptionImpl description = getChildDescription();
    final DomElement parent = getParent();
    assert parent != null;
    final DomElement parentCopy = parent.createStableCopy();
    final int index = description.getValues(parent).indexOf(getProxy());
    return getManager().createStableValue((Factory<DomElement>) () -> {
        if (parentCopy.isValid()) {
            final List<? extends DomElement> list = description.getValues(parentCopy);
            if (list.size() > index) {
                return list.get(index);
            }
        }
        return null;
    });
}
Also used : DomElement(com.intellij.util.xml.DomElement) List(java.util.List)

Aggregations

DomElement (com.intellij.util.xml.DomElement)97 XmlTag (com.intellij.psi.xml.XmlTag)34 PsiElement (com.intellij.psi.PsiElement)19 Nullable (org.jetbrains.annotations.Nullable)19 NotNull (org.jetbrains.annotations.NotNull)15 Project (com.intellij.openapi.project.Project)11 PsiFile (com.intellij.psi.PsiFile)11 XmlFile (com.intellij.psi.xml.XmlFile)11 XmlAttribute (com.intellij.psi.xml.XmlAttribute)10 XmlAttributeValue (com.intellij.psi.xml.XmlAttributeValue)9 ArrayList (java.util.ArrayList)8 LayoutViewElement (org.jetbrains.android.dom.layout.LayoutViewElement)7 ResourceElement (org.jetbrains.android.dom.resources.ResourceElement)6 DomManager (com.intellij.util.xml.DomManager)5 Style (org.jetbrains.android.dom.resources.Style)5 AntDomTarget (com.intellij.lang.ant.dom.AntDomTarget)4 XmlElement (com.intellij.psi.xml.XmlElement)4 DomElementProblemDescriptor (com.intellij.util.xml.highlighting.DomElementProblemDescriptor)4 LookupElement (com.intellij.codeInsight.lookup.LookupElement)3 TextRange (com.intellij.openapi.util.TextRange)3