Add PsiWhiteSpace before XmlAttribute

If I write the following code:

PsiElement whiteSpaceFromText = PsiParserFacade.SERVICE.getInstance(project).createWhiteSpaceFromText("\n");
xmlTag.addBefore(whiteSpaceFromText, xmlAttribute);

IDEA writes error to log:

java.lang.Throwable: Assertion failed
at com.intellij.openapi.diagnostic.Logger.assertTrue(Logger.java:163)
at com.intellij.openapi.diagnostic.Logger.assertTrue(Logger.java:173)
at com.intellij.psi.impl.source.xml.XmlTagImpl.addInternal(XmlTagImpl.java:1116)
at com.intellij.psi.impl.source.xml.XmlTagImpl.addInternal(XmlTagImpl.java:1074)
at com.intellij.psi.impl.source.tree.CompositePsiElement.addInnerBefore(CompositePsiElement.java:320)
at com.intellij.psi.impl.source.tree.CompositePsiElement.addBefore(CompositePsiElement.java:156)

 

I edit the user xml file in the editor and after I rewrite the xml file, it is important to keep the file formatting.

4 comments

Adding whitespace elements is not supported here. Please use  Document.insertString() instead.

0

At the time of processing, the xmlTag is not associated with the psi file and is a temporary element created through com.intellij.psi.xml.XmlTag#createChildTag, respectively there is no document either.

xmlTag.getContainingFile() = XmlFile:dummy.xml
0

Ok, so add the tag to where it belongs, commit psi and then use Document.

0

Please sign in to leave a comment.