Trying to force a code reformat

We have created a plugin that edits HTML files in an editor.
When we move from the editor to the IDEA HTML text file we update the file using:


      ApplicationManager.getApplication().runWriteAction(new Runnable()
      {
        public void run()
        {
          FileDocumentManager fileDocManager = FileDocumentManager.getInstance();
          Document doc = fileDocManager.getDocument(file);
          String fileContents = htmlEditor.getFileContents();
          doc.setText(fileContents);
          fileDocManager.saveDocument(doc);
        }
      });


This works like a dream.

But now we are trying to force a reformat of the HTML code in the IDEA HTML editor.
We use the following code in the VirtualFileListener (under contentsChanged and inside ApplicationManager.getApplication().runWriteAction()) for the specific file:


            if (virtualFileEvent.getFile().equals(file))
            {
              PsiElement element = PsiManager.getInstance(project).findFile(virtualFileEvent.getFile());
              if (element != null)
                PsiManager.getInstance(project).getCodeStyleManager().reformat(element);
            }

But this crashes with the following error:

Attempt to modify PSI for non-committed Document!
java.lang.Throwable
 at com.intellij.openapi.diagnostic.Logger.error(Logger.java:48)
 at com.intellij.psi.impl.PsiToDocumentSynchronizer.a(PsiToDocumentSynchronizer.java:73)
 at com.intellij.psi.impl.PsiToDocumentSynchronizer.childReplaced(PsiToDocumentSynchronizer.java:114)
 at com.intellij.psi.impl.PsiManagerImpl.a(PsiManagerImpl.java:631)
 at com.intellij.psi.impl.PsiManagerImpl.childReplaced(PsiManagerImpl.java:530)
 at com.intellij.pom.wrappers.PsiEventWrapperAspect.update(PsiEventWrapperAspect.java:106)
 at com.intellij.pom.core.impl.PomModelImpl.runTransaction(PomModelImpl.java:164)
 at com.intellij.psi.impl.source.tree.ChangeUtil.prepareAndRunChangeAction(ChangeUtil.java:187)
 at com.intellij.psi.impl.source.tree.CompositeElement.replaceChild(CompositeElement.java:446)
 at com.intellij.psi.formatter.FormatterUtil.replaceWhiteSpace(FormatterUtil.java:129)
 at com.intellij.lang.xml.XmlFormattingModel.replaceWithPsiInLeaf(XmlFormattingModel.java:109)
 at com.intellij.psi.formatter.PsiBasedFormattingModel.a(PsiBasedFormattingModel.java:90)
 at com.intellij.psi.formatter.PsiBasedFormattingModel.replaceWhiteSpace(PsiBasedFormattingModel.java:55)
 at com.intellij.formatting.FormatProcessor.a(FormatProcessor.java:224)
 at com.intellij.formatting.FormatProcessor.a(FormatProcessor.java:187)
 at com.intellij.formatting.FormatProcessor.performModifications(FormatProcessor.java:168)
 at com.intellij.formatting.FormatProcessor.format(FormatProcessor.java:122)
 at com.intellij.formatting.FormatterImpl.format(FormatterImpl.java:129)
...

What are we doing wrong?

Thanks,
Paul.


3 comments

You should commit the document with PsiDocumentManager.commitDocument().
BTW jetbrains.intellij.openapi would be the right place for the question.

"Paul Rau" <no_reply@jetbrains.com> сообщил(а) в новостях
следующее:22000730.10941288100162193.JavaMail.devnet@domU-12-31-39-18-36-57.compute-1.internal...

We have created a plugin that edits HTML files in an editor.
When we move from the editor to the IDEA HTML text file we update the file
using:

>

      ApplicationManager.getApplication().runWriteAction(new Runnable()
      {
        public void run()
        {
          FileDocumentManager fileDocManager =
FileDocumentManager.getInstance();
          Document doc = fileDocManager.getDocument(file);
          String fileContents = htmlEditor.getFileContents();
          doc.setText(fileContents);
          fileDocManager.saveDocument(doc);
        }
      });

>

This works like a dream.

>

But now we are trying to force a reformat of the HTML code in the IDEA
HTML editor.
We use the following code in the VirtualFileListener (under
contentsChanged and inside
ApplicationManager.getApplication().runWriteAction()) for the specific
file:

>

            if (virtualFileEvent.getFile().equals(file))
            {
              PsiElement element =
PsiManager.getInstance(project).findFile(virtualFileEvent.getFile());
              if (element != null)

PsiManager.getInstance(project).getCodeStyleManager().reformat(element);
            }

>

But this crashes with the following error:

>

Attempt to modify PSI for non-committed Document!
java.lang.Throwable
at com.intellij.openapi.diagnostic.Logger.error(Logger.java:48)
at
com.intellij.psi.impl.PsiToDocumentSynchronizer.a(PsiToDocumentSynchronizer.java:73)
at
com.intellij.psi.impl.PsiToDocumentSynchronizer.childReplaced(PsiToDocumentSynchronizer.java:114)
at com.intellij.psi.impl.PsiManagerImpl.a(PsiManagerImpl.java:631)
at
com.intellij.psi.impl.PsiManagerImpl.childReplaced(PsiManagerImpl.java:530)
at
com.intellij.pom.wrappers.PsiEventWrapperAspect.update(PsiEventWrapperAspect.java:106)
at
com.intellij.pom.core.impl.PomModelImpl.runTransaction(PomModelImpl.java:164)
at
com.intellij.psi.impl.source.tree.ChangeUtil.prepareAndRunChangeAction(ChangeUtil.java:187)
at
com.intellij.psi.impl.source.tree.CompositeElement.replaceChild(CompositeElement.java:446)
at
com.intellij.psi.formatter.FormatterUtil.replaceWhiteSpace(FormatterUtil.java:129)
at
com.intellij.lang.xml.XmlFormattingModel.replaceWithPsiInLeaf(XmlFormattingModel.java:109)
at
com.intellij.psi.formatter.PsiBasedFormattingModel.a(PsiBasedFormattingModel.java:90)
at
com.intellij.psi.formatter.PsiBasedFormattingModel.replaceWhiteSpace(PsiBasedFormattingModel.java:55)
at com.intellij.formatting.FormatProcessor.a(FormatProcessor.java:224)
at com.intellij.formatting.FormatProcessor.a(FormatProcessor.java:187)
at
com.intellij.formatting.FormatProcessor.performModifications(FormatProcessor.java:168)
at
com.intellij.formatting.FormatProcessor.format(FormatProcessor.java:122)
at com.intellij.formatting.FormatterImpl.format(FormatterImpl.java:129)
..

>

What are we doing wrong?

>

Thanks,
Paul.

>
>
>
>

---
Original message URL: http://devnet.jetbrains.net/message/5275873#5275873


0

Hello Paul,

Please refer to http://confluence.jetbrains.net/display/IDEADEV/PluginDevelopment
for information on committing PSI.

Note that trying to call a synchronous reformat from a contents changed event
handler is wrong for several reasons. One is that this will also be fired
when I update code from VCS, for example, and I wouldn't want my IDE to auto-reformat
changes done by my colleagues. Second, reformatting a large HTML file is
a time-consuming operation (up to several seconds), and performing it synchronously
in the event dispatch thread (which is where VFS events are dispatched) without
any explicit user request will hurt the perceived performance of the IDE
a lot.

We have created a plugin that edits HTML files in an editor. When we
move from the editor to the IDEA HTML text file we update the file
using:

ApplicationManager.getApplication().runWriteAction(new
Runnable()
{
public void run()
{
FileDocumentManager fileDocManager =
FileDocumentManager.getInstance();
Document doc = fileDocManager.getDocument(file);
String fileContents = htmlEditor.getFileContents();
doc.setText(fileContents);
fileDocManager.saveDocument(doc);
}
});
This works like a dream.

But now we are trying to force a reformat of the HTML code in the IDEA
HTML editor.

We use the following code in the VirtualFileListener (under
contentsChanged and inside
ApplicationManager.getApplication().runWriteAction()) for the specific
file:

if (virtualFileEvent.getFile().equals(file))
{
PsiElement element =
PsiManager.getInstance(project).findFile(virtualFileEvent.getFile());
if (element != null)

PsiManager.getInstance(project).getCodeStyleManager().reformat(element
);
}
But this crashes with the following error:

Attempt to modify PSI for non-committed Document!
java.lang.Throwable
at com.intellij.openapi.diagnostic.Logger.error(Logger.java:48)
at
com.intellij.psi.impl.PsiToDocumentSynchronizer.a(PsiToDocumentSynchro
nizer.java:73)
at
com.intellij.psi.impl.PsiToDocumentSynchronizer.childReplaced(PsiToDoc
umentSynchronizer.java:114)
at com.intellij.psi.impl.PsiManagerImpl.a(PsiManagerImpl.java:631)
at
com.intellij.psi.impl.PsiManagerImpl.childReplaced(PsiManagerImpl.java
:530)
at
com.intellij.pom.wrappers.PsiEventWrapperAspect.update(PsiEventWrapper
Aspect.java:106)
at
com.intellij.pom.core.impl.PomModelImpl.runTransaction(PomModelImpl.ja
va:164)
at
com.intellij.psi.impl.source.tree.ChangeUtil.prepareAndRunChangeAction
(ChangeUtil.java:187)
at
com.intellij.psi.impl.source.tree.CompositeElement.replaceChild(Compos
iteElement.java:446)
at
com.intellij.psi.formatter.FormatterUtil.replaceWhiteSpace(FormatterUt
il.java:129)
at
com.intellij.lang.xml.XmlFormattingModel.replaceWithPsiInLeaf(XmlForma
ttingModel.java:109)
at
com.intellij.psi.formatter.PsiBasedFormattingModel.a(PsiBasedFormattin
gModel.java:90)
at
com.intellij.psi.formatter.PsiBasedFormattingModel.replaceWhiteSpace(P
siBasedFormattingModel.java:55)
at
com.intellij.formatting.FormatProcessor.a(FormatProcessor.java:224)
at
com.intellij.formatting.FormatProcessor.a(FormatProcessor.java:187)
at
com.intellij.formatting.FormatProcessor.performModifications(FormatPro
cessor.java:168)
at
com.intellij.formatting.FormatProcessor.format(FormatProcessor.java:12
2)
at
com.intellij.formatting.FormatterImpl.format(FormatterImpl.java:129)
..
What are we doing wrong?

Thanks,
Paul.
---
Original message URL:
http://devnet.jetbrains.net/message/5275873#5275873


--
Dmitry Jemerov
Development Lead
JetBrains, Inc.
http://www.jetbrains.com/
"Develop with Pleasure!"


0

Thanks Dmitry. Point well made and taken.

0

Please sign in to leave a comment.