commitAllDocuments() vs SaveAll

Hi,

1/
I have a problem with saving an editor modified contents with

PsiDocumentManager.getInstance(p).commitDocument(document);
, or even
PsiDocumentManager.getInstance(p).commitAllDocuments();
, and reading its virtualFile contents afterwards, and getting the old
(pre-modif) contents.

If I trigger 'SaveAll' manually, it works fine (ie: reading the
virtualFile returns the modified data)


Any idea?

2/
Could somebody explain a little about the - important - differences between
- File, psiFile, virtualFile..
- Editor, FileEditor, TextEditor..

It's not completely clear in my mind; I'm still guessing.

Alain

4 comments

Well, a breif explanation of model data layers in IDEA.
1. VFS (Virtual File System) mirrors physical file system and represents
raw data being stored on hard disk/network drive/zip or jar file
2. Document stores raw character data of the text files being edited by means
of Editor (user typings etc.) or PSI (refactorings for example). Doesn't
have to be intact with VFS until saved (action that propogates data from
document to file system) or reload (action that propogates data from filesystem
to the document).
3. PSI (Program Structure Interface) mostly represents AST (abstract syntax
tree). Changes made to PSI (through various setters and other write interface)
are immediately propogated to the Document while changes made to the Document
(by means other than PSI, Editor for example) are propogated to PSI via commitDocument
action (an AST getting reparsed that time).

Feel free to request additional comments if something still left unclear.

Editor stuff is much simpler actually.
The Editor itself is just text editor component which is able to edit data
contained in the Document. Thus editor is a View and Controller of the Document
(model). In fact, this pair not only used for editing files but in many other
places like console view, diff panel, live and file template configuration
and even various text fields that support completion and validation stuff.

FileEditor is something that represent abstaction of the editor tab.
TextEditor is a special case of the FileEditor that wraps Editor thus representing
editor tab for editing text files. Other cases of the FileEditors come into
my mind are: UI Designer forms editor, various J2EE deployment descriptior
visual editors.


Hi,

1/
I have a problem with saving an editor modified contents with
PsiDocumentManager.getInstance(p).commitDocument(document);
, or even
PsiDocumentManager.getInstance(p).commitAllDocuments();
, and reading its virtualFile contents afterwards, and getting the old
(pre-modif) contents.

If I trigger 'SaveAll' manually, it works fine (ie: reading the
virtualFile returns the modified data)

Any idea?

2/
Could somebody explain a little about the - important - differences
between
- File, psiFile, virtualFile..
- Editor, FileEditor, TextEditor..
It's not completely clear in my mind; I'm still guessing.

Alain



0

Maxim,

Thanks, your explanations make it clearer, and I think I've found the
cause of my trouble:
- commitDocument(..) saves the document changes to the psi
structure, but not to the file system. (Am I right? )

Q/ how do you save the doc changes to the file system?

Q/ for plain text editors, does commitDocument() have an effect, as
there is no backing AST/psi structure?


Alain

(Note: here is the code where I have some problems

1: modify the editor document
document.replaceString(..)
2: commit the document
PsiDocumentManager.getInstance(p).commitDocument(document);

a little later
3: vf = LocalFileSystem.getInstance().findFileByPath (docPath);
4:
StringBuffer b = new StringBuffer();
b.append(vf.contentsToCharArray()) ;
String documentText = b.toString();

problem: despite "2:commitDocument()", the text found in documentText
doesn't reflect the committed changes.

0

- commitDocument(..) saves the document changes to the psi
structure, but not to the file system. (Am I right? )

Exactly...

Q/ how do you save the doc changes to the file system?

FileDocumentManager.getInstance().save[All]Document[s]()

Q/ for plain text editors, does commitDocument() have an effect, as
there is no backing AST/psi structure?

Even for plain text files there's an AST structure though completely dumb
in the case (just one tree element representing whole file text).


Though totally doable your plan is somewhat undesired way of functioning.
Instead of quering a VFS for file content yourself you are encouraged to
get its text from the corresponding document. This way you shouldn't bother
saving.

Whenever you've got a virtual file you can get corresponding document by
calling the same manager:
FileDocumentManager.getInstance().getDocument(vFile);

Be aware one can return null if that's not possible to get a document for
a particular file if the file (for example) is binary or its file type is
not known.


BTW: You do not have to commit anything to PSI util you're not using PSI
API yourself.

So a little bit changed your code would look like:
1. modify the editor document

a little later
2. VirtualFile vf = LocalFileSystem.getInstance().findFileByPath (docPath);
3. Document doc = FileDocumentManager.getInstance().getDocument(vf);
4. String docText = doc.getText();

(Note: here is the code where I have some problems

1: modify the editor document
document.replaceString(..)
2: commit the document
PsiDocumentManager.getInstance(p).commitDocument(document);
a little later
3: vf = LocalFileSystem.getInstance().findFileByPath (docPath);
4:
StringBuffer b = new StringBuffer();
b.append(vf.contentsToCharArray()) ;
String documentText = b.toString();
problem: despite "2:commitDocument()", the text found in documentText
doesn't reflect the committed changes.



0

I just realized that I could save about three days work if I had such an explanation before I started working on my plugin! ;)

I think this explanation is a mandatory read for many plugins authors - perhaps you could post that somewhere (plugins wiki) or in the plugins dev kit?

cheers,
Arik.

0

Please sign in to leave a comment.