Assert Failed after addAfter () (build 1162)

Hello, i was trying to add some comment block after a specific PsiElement. I use PsiElement.addAfter (PsiElement, PsiElement) and i get Assert Failed msg, which follows. But nevertheless, the comment block is added into th? file perfectly.

I've tried several approaches so far: without runWriteAction(), with


and finally with


Here's the error reported by IDEA:

Error message: Assertion failed
java.lang.Throwable

at com.intellij.openapi.diagnostic.Logger.assertTrue(Logger.java:82)

at com.intellij.openapi.diagnostic.Logger.assertTrue(Logger.java:136)

at com.intellij.psi.impl.source.a.c.a(c.java:352)

at com.intellij.psi.impl.source.c.e.a(e.java:144)

at com.intellij.psi.impl.source.a.r.a(r.java:63)

at com.intellij.psi.impl.source.a.s.addAfter(s.java:53)

at com.netcracker.standalone.nls.externalization.ideaplugin.LocalizationPlugin$1.run(LocalizationPlugin.java:376)

at com.intellij.openapi.application.a.b.runWriteAction(b.java:68)

at com.netcracker.standalone.nls.externalization.ideaplugin.LocalizationPlugin$FileProcessor$AddSkipComment.run(LocalizationPlugin.java:368)

at com.intellij.openapi.command.impl.a.executeCommand(a.java:62)

at com.netcracker.standalone.nls.externalization.ideaplugin.LocalizationPlugin$FileProcessor.markForSkip(LocalizationPlugin.java:351)

at com.netcracker.standalone.nls.externalization.ideaplugin.LocalizationPlugin$SkipThisElementAction.actionPerformed(LocalizationPlugin.java:151)

at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1786)

at javax.swing.AbstractButton$ForwardActionEvents.actionPerformed(AbstractButton.java:1839)

at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:420)

at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:258)

at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:245)

at java.awt.Component.processMouseEvent(Component.java:5100)

at java.awt.Component.processEvent(Component.java:4897)

at java.awt.Container.processEvent(Container.java:1569)

at java.awt.Component.dispatchEventImpl(Component.java:3615)

at java.awt.Container.dispatchEventImpl(Container.java:1627)

at java.awt.Component.dispatchEvent(Component.java:3477)

at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:3483)

at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3198)

at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3128)

at java.awt.Container.dispatchEventImpl(Container.java:1613)

at java.awt.Window.dispatchEventImpl(Window.java:1606)

at java.awt.Component.dispatchEvent(Component.java:3477)

at java.awt.EventQueue.dispatchEvent(EventQueue.java:456)

at com.intellij.ide.q.b(q.java:80)

at com.intellij.ide.q.a(q.java:91)

at com.intellij.ide.q.dispatchEvent(q.java:63)

at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:201)

at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:145)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:137)

at java.awt.EventDispatchThread.run(EventDispatchThread.java:100)


0
6 comments

i've changed it to


and that helped...

but one more question arose! )

if a get a reference to some PsiElement in a Document, and then alter the text of the document, will such fields as getTextOffset() of my PsiElement automatically updated or i have to call some synchronization method ?

0

Anatoliy S. Knyazev wrote:

i've changed it to



and that helped...

but one more question arose! )

if a get a reference to some PsiElement in a Document, and then alter the
text of the document, will such fields as getTextOffset() of my PsiElement
automatically updated or i have to call some synchronization method ?


A rule of thumb is: after you modified a document, you should start with PSI
all over again. During reparsing of a changed document, some, or all,
PsiElements in that document will become invalid (their isValid() method
will return false and most methods will result in failed assertions).

After you've changed the document, you must initiate its reparsing by
calling PsiDocumentManager.commitDocument() or friends.

Friendly,
Dmitry

--
Dmitry Lomov
Software Developer
JetBrains Inc.
http://www.jetbrains.com
"Develop with pleasure!"

0

So, after reparsing the Document, some of my PsiElement references may become invalid, not all ?

Say i want to make a plugin for easy navigation through PsiLiteralExpressions in the Document, with the ability for a user to change some of them. I get an ArraySet of all PsiLiteralExpressions through some custom recursive function. Should i refill the ArraySet after every modification of code ? Providing that the modification will only change the value of the PsiLiteralExpression, and nothing else will be altered...

0

Anatoliy S. Knyazev wrote:

So, after reparsing the Document, some of my PsiElement references may
become invalid, not all ?

Say i want to make a plugin for easy navigation through
PsiLiteralExpressions in the Document, with the ability for a user to
change some of them. I get an ArraySet of all PsiLiteralExpressions
through some custom recursive function. Should i refill the ArraySet after
every modification of code ? Providing that the modification will only
change the value of the PsiLiteralExpression, and nothing else will be
altered...


You shouldn't do modification via Document. Do them via PSI API.
Create a modified PsiLiteralExpression using
PsiElementFactory.createExpressionFromText() and then replace() your
PsiLiteralExpression in code. This won't invalidate any elements except the
one you have replaced.

Hope this helps,
Friendly,
Dmitry
--
Dmitry Lomov
Software Developer
JetBrains Inc.
http://www.jetbrains.com
"Develop with pleasure!"

0

Dmitriy, of course i do all modifications through PSI - that's really a convenient way...

And, hopefully, the last question in this topic - thanx in advance!

If i use not replace(), but addAfter() - will it invalidate some of my PsiElements ?

0

Anatoliy S. Knyazev wrote:

Dmitriy, of course i do all modifications through PSI - that's really a
convenient way...

And, hopefully, the last question in this topic - thanx in advance!

If i use not replace(), but addAfter() - will it invalidate some of my
PsiElements ?


No.

Friendly,
Dmitry

--
Dmitry Lomov
Software Developer
JetBrains Inc.
http://www.jetbrains.com
"Develop with pleasure!"

0

Please sign in to leave a comment.