Undo not present after vfs.writeText on non edited file
Answered
I've got simple action which writes file content as:
val file = File("/asd/asd/asd/asd.txt") <- (any project file)
vfsFile = VfsUtil.findFileByIoFile(file, true)
vfsDoc = vfsFile.findDocument()
CommandProcessor.getInstance().executeCommand(
project,
{
WriteCommandAction.runWriteCommandAction(project) {
vfsFile.writeText(content)
PsiDocumentManager.getInstance(project).commitDocument(vfsDoc);
}
},
"write-file",
DocCommandGroupId.noneGroupId(vfsDoc),
UndoConfirmationPolicy.DO_NOT_REQUEST_CONFIRMATION
)
File is properly written.
Undo IS NOT present in following case:
1. Open IDE, trigger action
Undo IS present when:
1. Open IDE, edit file (type anything), trigger action
Difference in UndoManager is that for not working case there are 2 nonundoable events, and for working case there is 1 nonundoable event and Editor Action with file content change (that one is later put on Undo menu, which is perfect).
Please sign in to leave a comment.
Hi Marcin,
What does it mean that there are 1/2 undoable events in UndoManager? What are they?
Are there any exceptions in the logs? Are you sure file and documents are not null?
I'm wondering why you are writing text to virtual file instead of a document. Can you try modifying the document?
This looks suspicious:
You write to VirtualFile directly, but commit the document. Why not operate on the document in both steps?
Because I didn't find any write or save method in document, after using
vfsDoc.setText(content)
it works perfectly. Thanks!