File content saving

Answered

Hello, I have a problem about file saving, and psi operation is used.Take the example of saving an xml type file.

1. I get the document of the file and save the contents of the file

val document = readAction(file::findDocument)
val documentManager = FileDocumentManager.getInstance()
document.setText(strPayload)
documentManager.saveDocument(document)

2. After saving I look for the psi file of the current file and parse it

val findFile = PsiManager.getInstance(project).findFile(file)
val xmlFile = findFile as XmlFile
xmlFile.accept(object : XmlRecursiveElementVisitor() {
   override fun visitXmlTag(tag: XmlTag) {
 })

The problem is that when the xml file is saved and parsed again with psi, it is not the latest file content,I need your help

0
2 comments

Hi,

Try to commit your document with PsiDocumentManager:

val document = readAction(file::findDocument)
document.setText(strPayload)
PsiDocumentManager.getInstance(project).commitDocument(document)
0

Thank you very much for your answer, the problem has been solved

0

Please sign in to leave a comment.