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).

0
3 comments

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?

0

I'm wondering why you are writing text to virtual file instead of a document. Can you try modifying the document?

This looks suspicious:

vfsFile.writeText(content)
PsiDocumentManager.getInstance(project).commitDocument(vfsDoc);

You write to VirtualFile directly, but commit the document. Why not operate on the document in both steps?

1

Because I didn't find any write or save method in document, after using vfsDoc.setText(content) it works perfectly. Thanks!

0

Please sign in to leave a comment.