Mark document as modified and save.

I'm searching a way to save document. I have a custom GUI editor for xml-like file. When user modifies file, i can easy modifiy xml file DOM representation

I think i shoud catch file change event in VirtualFilemanager or FileDocument listeners. like this:

m_Document = FileDocumentManager.getInstance().getDocument(file);           m_Document.addDocumentListener(new DocumentListener() {                @Override public void beforeDocumentChange(DocumentEvent event) {}                @Override public void documentChanged(DocumentEvent event) {                     saveToFile();                }           });

But I'm realy stuck with notifing any event dispatcher about file change.

My custom editor has method isModified, but events won't fire. I set brekoint and It's look like editor never calls it.

It's strange, may be I'm doing something wrong?

P.S. There is no file rename event too. Look like i have missed something important.


If you're creating the PSI file from a real virtual file that exists on disk and modify it through the PSI, you don't need to do anything extra to save the changes. IntelliJ IDEA will take care of it itself.

To show correct modified status for editor tabs, you need to return the result of calling FileDocumentManager.isFileModified() from your implementation of FileEditor.isModified().


Thanks! Good, now I think PSI is inevitability.

I see article Using interfaces looks very promising, but what shoud I do if I have non-constant set of tags.


Can I use some DOM interface to describe that kind of structure? I need simply access <a></a> tag's text.

I get PsiFile


But it gives me a very low-level API, and i need to do a lot of iterations through getChildren() function result, which have a lot of tokens like, whitespaces, XML_START_TAG_START. I'm a bit confused. this kind of Psi usage looks like an overhead? Or I missed some useful utility methods? Sorry for so dummy question, but there isn't much documentation for it.


You can use DOM in this situation, but that's quite a bit more complicated that using the XML PSI. Actually the XmlTag interface has a number of higher-level APIs such as findSubTags() and getAttributeValue(), so you needn't use the low-level getChildren() method directly.


Hmm. And using XmlTag interface I will change PSI, so will be able to save changes. Thanks! Will try now!


Please sign in to leave a comment.