Attempt to modify PSI for non-committed Document!

i never found the error before.

but when i add the code :

D

ocument document = mPsiDocumentManager.getCachedDocument(psiFile);
if
(document != null) {
   mPsiDocumentManager.commitDocument(document);
}


it Solve the problem.

another problem is the warning:
changes have been made to "/User/kycq/dddd/Test.java" in memory and on disk.

it was so bad.

how to solve the warning?


 
private void writeFile(BeanEntry beanEntry, ArrayList<PsiFile> psiFiles) throws IOException {
   PsiDirectory directory = mSenior.getDirectory(beanEntry.getPackageName());
   
String fileName = beanEntry.getClazzName() + ".java";
   VirtualFile clazzFile = directory.getVirtualFile().findOrCreateChildData(mSenior.getProject(), fileName);

   
StringBuilder fieldBuilder = new StringBuilder();
   
StringBuilder methodBuilder = new StringBuilder();
   for
(FieldEntry fieldEntry : beanEntry.getFieldEntries().values()) {
      fieldEntry.generateField(fieldBuilder);
      
fieldEntry.generateMethod(methodBuilder);
   
}

   clazzFile.setBinaryContent(String.format("package %s;\n\npublic class %s {%s%s}",
         
beanEntry.getPackageName(),
         
beanEntry.getClazzName(),
         
fieldBuilder.toString(),
         
methodBuilder.toString()).getBytes());
   
clazzFile.refresh(false,false);


   
PsiFile psiFile = directory.findFile(fileName);
   if
(psiFile == null) {
      return;
   
}

   Document document = mPsiDocumentManager.getCachedDocument(psiFile);
   if
(document != null) {
      mPsiDocumentManager.commitDocument(document);
   
}

   mCodeStyleManager.optimizeImports(psiFile);
   mCodeStyleManager
.shortenClassReferences(psiFile);
   
psiFiles.add(psiFile);
}
1 comment
Comment actions Permalink

Thank you for the tip! I ran into this same error.


some error message actually gave a tip to use
      doPostponedOperationsAndUnblockDocument:

PsiDocumentManager.getInstance(project).doPostponedOperationsAndUnblockDocument(document);

Checking for cached document before trying to commit seems to solve the problem


Thank you

0

Please sign in to leave a comment.