Search in sources :

Example 1 with DomEvent

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

the class DomInvocationHandler method copyFrom.

@Override
public void copyFrom(final DomElement other) {
    if (other == getProxy())
        return;
    assert other.getDomElementType().equals(myType) : "Can't copy from " + other.getDomElementType() + " to " + myType;
    if (other.getXmlElement() == null) {
        undefine();
        return;
    }
    myManager.performAtomicChange(() -> {
        ensureXmlElementExists();
        final DomInvocationHandler otherInvocationHandler = DomManagerImpl.getDomInvocationHandler(other);
        assert otherInvocationHandler != null : other;
        final DomGenericInfoEx genericInfo = otherInvocationHandler.getGenericInfo();
        for (final AttributeChildDescriptionImpl description : genericInfo.getAttributeChildrenDescriptions()) {
            description.getDomAttributeValue(this).setStringValue(description.getDomAttributeValue(other).getStringValue());
        }
        for (final DomFixedChildDescription description : genericInfo.getFixedChildrenDescriptions()) {
            final List<? extends DomElement> list = description.getValues(getProxy());
            final List<? extends DomElement> otherValues = description.getValues(other);
            for (int i = 0; i < list.size(); i++) {
                final DomElement otherValue = otherValues.get(i);
                final DomElement value = list.get(i);
                if (!DomUtil.hasXml(otherValue)) {
                    value.undefine();
                } else {
                    value.copyFrom(otherValue);
                }
            }
        }
        for (final DomCollectionChildDescription description : genericInfo.getCollectionChildrenDescriptions()) {
            for (final DomElement value : description.getValues(getProxy())) {
                value.undefine();
            }
            for (final DomElement otherValue : description.getValues(other)) {
                description.addValue(getProxy(), otherValue.getDomElementType()).copyFrom(otherValue);
            }
        }
        final String stringValue = otherInvocationHandler.getValue();
        if (StringUtil.isNotEmpty(stringValue)) {
            setValue(stringValue);
        }
    });
    if (!myManager.getSemService().isInsideAtomicChange()) {
        myManager.fireEvent(new DomEvent(getProxy(), false));
    }
}
Also used : DomEvent(com.intellij.util.xml.events.DomEvent)

Example 2 with DomEvent

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

the class FileDescriptionCachedValueProvider method _computeFileElement.

@Nullable
private DomFileElementImpl<T> _computeFileElement(final boolean fireEvents, @NotNull final XmlFileHeader rootTagName, @Nullable StringBuilder sb) {
    if (sb != null) {
        sb.append(rootTagName).append("\n");
    }
    if (!myXmlFile.isValid()) {
        return null;
    }
    if (sb != null) {
        sb.append("File is valid\n");
    }
    if (!(myXmlFile.getFileType() instanceof DomSupportEnabled)) {
        return null;
    }
    if (sb != null) {
        sb.append("File is of dom file type\n");
    }
    final DomFileDescription<T> description = findFileDescription(rootTagName, sb);
    final DomFileElementImpl oldValue = getLastValue();
    if (sb != null) {
        sb.append("last " + oldValue + "\n");
    }
    final List<DomEvent> events = fireEvents ? new SmartList<>() : Collections.<DomEvent>emptyList();
    if (oldValue != null) {
        if (fireEvents) {
            events.add(new DomEvent(oldValue, false));
        }
    }
    if (description == null) {
        return null;
    }
    final Class<T> rootElementClass = description.getRootElementClass();
    final XmlName xmlName = DomImplUtil.createXmlName(description.getRootTagName(), rootElementClass, null);
    assert xmlName != null;
    final EvaluatedXmlNameImpl rootTagName1 = EvaluatedXmlNameImpl.createEvaluatedXmlName(xmlName, xmlName.getNamespaceKey(), false);
    VirtualFile file = myXmlFile.getVirtualFile();
    FileStub stub = null;
    if (description.hasStubs() && file instanceof VirtualFileWithId && !isFileParsed()) {
        ApplicationManager.getApplication().assertReadAccessAllowed();
        if (!XmlUtil.isStubBuilding()) {
            ObjectStubTree stubTree = StubTreeLoader.getInstance().readOrBuild(myXmlFile.getProject(), file, myXmlFile);
            if (stubTree != null) {
                stub = (FileStub) stubTree.getRoot();
            }
        }
    }
    DomFileElementImpl<T> result = new DomFileElementImpl<>(myXmlFile, rootElementClass, rootTagName1, myDomManager, description, stub);
    if (sb != null) {
        sb.append("success " + result + "\n");
    }
    if (fireEvents) {
        events.add(new DomEvent(result, true));
    }
    return result;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) FileStub(com.intellij.util.xml.stubs.FileStub) DomSupportEnabled(com.intellij.ide.highlighter.DomSupportEnabled) VirtualFileWithId(com.intellij.openapi.vfs.VirtualFileWithId) DomEvent(com.intellij.util.xml.events.DomEvent) ObjectStubTree(com.intellij.psi.stubs.ObjectStubTree) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with DomEvent

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

the class AttributeChildInvocationHandler method setValue.

@Override
protected void setValue(@Nullable final String value) {
    final XmlTag tag = ensureTagExists();
    final String attributeName = getXmlElementName();
    final String namespace = getXmlApiCompatibleNamespace(getParentHandler());
    final String oldValue = StringUtil.unescapeXml(tag.getAttributeValue(attributeName, namespace));
    final String newValue = XmlStringUtil.escapeString(value);
    if (Comparing.equal(oldValue, newValue, true))
        return;
    getManager().runChange(() -> {
        try {
            XmlAttribute attribute = tag.setAttribute(attributeName, namespace, newValue);
            setXmlElement(attribute);
            getManager().cacheHandler(DomManagerImpl.DOM_ATTRIBUTE_HANDLER_KEY, attribute, this);
        } catch (IncorrectOperationException e) {
            LOG.error(e);
        }
    });
    final DomElement proxy = getProxy();
    getManager().fireEvent(oldValue != null ? new DomEvent(proxy, false) : new DomEvent(proxy, true));
}
Also used : XmlAttribute(com.intellij.psi.xml.XmlAttribute) DomElement(com.intellij.util.xml.DomElement) IncorrectOperationException(com.intellij.util.IncorrectOperationException) XmlTag(com.intellij.psi.xml.XmlTag) DomEvent(com.intellij.util.xml.events.DomEvent)

Example 4 with DomEvent

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

the class DomChildrenTest method testUndefineNotLastFixedChild.

public void testUndefineNotLastFixedChild() throws Throwable {
    new WriteCommandAction.Simple(getProject()) {

        @Override
        protected void run() throws Throwable {
            final MyElement element = createElement("<a>" + "<child>1</child>" + "<child attr=\"\">2</child>" + "</a>");
            final MyElement child = element.getChild();
            final MyElement child2 = element.getChild2();
            assertTrue(child.isValid());
            assertTrue(child2.isValid());
            child.undefine();
            assertTrue(child.isValid());
            assertTrue(child2.isValid());
            assertEquals(element, child.getParent());
            assertEquals(element, child2.getParent());
            assertEquals(child, element.getChild());
            assertEquals(child2, element.getChild2());
            myCallRegistry.putExpected(new DomEvent(child, false));
            myCallRegistry.assertResultsAndClear();
            XmlTag[] subTags = element.getXmlTag().getSubTags();
            assertEquals(2, subTags.length);
            assertCached(child, subTags[0]);
            assertCached(child2, subTags[1]);
        }
    }.execute().throwException();
}
Also used : DomEvent(com.intellij.util.xml.events.DomEvent) XmlTag(com.intellij.psi.xml.XmlTag)

Example 5 with DomEvent

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

the class DomChildrenTest method testUndefineCollectionChild.

public void testUndefineCollectionChild() throws Throwable {
    final MyElement element = createElement("<a><child-element/><child-element/><child-element/></a>");
    final MyElement child1 = element.getChildElements().get(0);
    final MyElement child2 = element.getChildElements().get(1);
    final MyElement child3 = element.getChildElements().get(2);
    final List<XmlTag> oldChildren = new ArrayList<>(Arrays.asList(element.getXmlTag().getSubTags()));
    assertTrue(child2.isValid());
    assertEquals(element, child2.getParent());
    WriteCommandAction.runWriteCommandAction(null, () -> {
        child2.undefine();
        assertFalse(child2.isValid());
        oldChildren.remove(1);
        assertEquals(oldChildren, Arrays.asList(element.getXmlTag().getSubTags()));
        assertEquals(Arrays.asList(child1, child3), element.getChildElements());
        assertCached(child1, element.getXmlTag().findSubTags("child-element")[0]);
        assertCached(child3, element.getXmlTag().findSubTags("child-element")[1]);
    });
    myCallRegistry.putExpected(new DomEvent(element, false));
    myCallRegistry.assertResultsAndClear();
}
Also used : ArrayList(java.util.ArrayList) XmlTag(com.intellij.psi.xml.XmlTag) DomEvent(com.intellij.util.xml.events.DomEvent)

Aggregations

DomEvent (com.intellij.util.xml.events.DomEvent)48 XmlTag (com.intellij.psi.xml.XmlTag)24 Result (com.intellij.openapi.application.Result)7 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)7 XmlFile (com.intellij.psi.xml.XmlFile)4 VirtualFile (com.intellij.openapi.vfs.VirtualFile)3 DomElement (com.intellij.util.xml.DomElement)3 Document (com.intellij.openapi.editor.Document)2 XmlAttribute (com.intellij.psi.xml.XmlAttribute)2 IncorrectOperationException (com.intellij.util.IncorrectOperationException)2 DomSupportEnabled (com.intellij.ide.highlighter.DomSupportEnabled)1 VirtualFileWithId (com.intellij.openapi.vfs.VirtualFileWithId)1 ObjectStubTree (com.intellij.psi.stubs.ObjectStubTree)1 FileStub (com.intellij.util.xml.stubs.FileStub)1 ArrayList (java.util.ArrayList)1 NotNull (org.jetbrains.annotations.NotNull)1 Nullable (org.jetbrains.annotations.Nullable)1