How to modify a PsiPlainTextFile?

Hi all,

I've got a PsiPlainTextFile object for a .properties file which I would
like to modify. However, the only way I've been able to get to this work is:

outputStream = mFile.getVirtualFile().getOutputStream(this);
mProperties.store(outputStream, "Log Resource Bundle");

Where mFile is the PsiPlainTextFile.

The problem with this method is that the new contents of the file don't
appear in the editor until I use the Synchronize command from the File
menu or toolbar.

The first method that I came up with didn't work but it seemed like it
should work with some tweaking:

ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
mProperties.store(outputStream, "Log Resource Bundle");
String newFileText = outputStream.toString();
PsiElementFactory elementFactory =
mFile.getManager().getElementFactory();
PsiFile newFile = elementFactory.createFile(newFileText);
mFile.replace(newFile);

This method caused an assertion failure somewhere inside the
PsiFile.replace() call. But without documentation I can't figure out
what needs to be changed.

What is the recommended way of modifying a PsiPlainTextFile?

Thanks,
Gordon

--
Gordon Tyler (Software Developer)
Quest Software <http://java.quest.com/>
260 King Street East, Toronto, Ontario M5A 4L5, Canada
Voice: 416-643-4846 | Fax: 416-594-1919

6 comments

You know, you can also synchronize from the API.


Gordon Tyler wrote:

Hi all,

I've got a PsiPlainTextFile object for a .properties file which I would
like to modify. However, the only way I've been able to get to this work
is:

outputStream = mFile.getVirtualFile().getOutputStream(this);
mProperties.store(outputStream, "Log Resource Bundle");

Where mFile is the PsiPlainTextFile.

The problem with this method is that the new contents of the file don't
appear in the editor until I use the Synchronize command from the File
menu or toolbar.

The first method that I came up with didn't work but it seemed like it
should work with some tweaking:

ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
mProperties.store(outputStream, "Log Resource Bundle");
String newFileText = outputStream.toString();
PsiElementFactory elementFactory =
mFile.getManager().getElementFactory();
PsiFile newFile = elementFactory.createFile(newFileText);
mFile.replace(newFile);

This method caused an assertion failure somewhere inside the
PsiFile.replace() call. But without documentation I can't figure out
what needs to be changed.

What is the recommended way of modifying a PsiPlainTextFile?

Thanks,
Gordon



--

Erb

==============================================================
"Most of you are familiar with the virtues of a programmer.
There are three, of course: laziness, impatience, and hubris."
- Larry Wall
==============================================================

0

Erb wrote:

You know, you can also synchronize from the API.


Can I synchronize just one file? I don't want to kick off a heavy synch
of the whole project each time the user chooses an item from a drop-down
box in my toolwindow.

Ciao,
Gordon

--
Gordon Tyler (Software Developer)
Quest Software <http://java.quest.com/>
260 King Street East, Toronto, Ontario M5A 4L5, Canada
Voice: 416-643-4846 | Fax: 416-594-1919

0

Gordon Tyler wrote:

Erb wrote:

>> You know, you can also synchronize from the API.


Can I synchronize just one file? I don't want to kick off a heavy synch
of the whole project each time the user chooses an item from a drop-down
box in my toolwindow.


Allow me to answer my own question: You can synch a single file by
calling refresh on the VirtualFile. This seems to work well enough my
purposes with one snag: if changes have been made to the properties file
in an IDEA editor window and then I use my IDEA plugin to modify the
properties file, the refresh call pops up a dialog asking if I want to
overwrite changes in memory with the new contents from disk.

Hmm... Any advice from IntelliJ would be greatly appreciated.

Ciao,
Gordon

--
Gordon Tyler (Software Developer)
Quest Software <http://java.quest.com/>
260 King Street East, Toronto, Ontario M5A 4L5, Canada
Voice: 416-643-4846 | Fax: 416-594-1919

0

Try


As far as modifying the file I would use instead the Document interface
operations:


Jacques
"Gordon Tyler" <gordon.tyler@sitraka.com> wrote in message
news:b4tbd6$9eh$1@is.intellij.net...

Gordon Tyler wrote:

Erb wrote:

>
>> You know, you can also synchronize from the API.
>
>

Can I synchronize just one file? I don't want to kick off a heavy synch
of the whole project each time the user chooses an item from a drop-down
box in my toolwindow.

>

Allow me to answer my own question: You can synch a single file by
calling refresh on the VirtualFile. This seems to work well enough my
purposes with one snag: if changes have been made to the properties file
in an IDEA editor window and then I use my IDEA plugin to modify the
properties file, the refresh call pops up a dialog asking if I want to
overwrite changes in memory with the new contents from disk.

>

Hmm... Any advice from IntelliJ would be greatly appreciated.

>

Ciao,
Gordon

>

--
Gordon Tyler (Software Developer)
Quest Software <http://java.quest.com/>
260 King Street East, Toronto, Ontario M5A 4L5, Canada
Voice: 416-643-4846 | Fax: 416-594-1919

>


0
 PsiPlainTextFile file = ...
> PsiDocumentManager docManager =
> PsiDocumentManager.getInstance();
> docManager.commitDocument(docManager.getDocument(file);
]]>


I can't find the PsiDocumentManager class. It doesn't appear in the Class Autocomplete list and I can't find it in idea.jar.

As far as modifying the file I would use instead the
Document interface
operations:

    void insertString(int i,String s);
>    void deleteString(int i,int i1);
>    void replaceString(int i,int i1,String s);
]]>


Thanks, I'll look into that. It appears I can get a Document instance for a VirtualFile using the FileEditorManager's fileToDocument method?

Ciao,
Gordon

0

Aurora: PsiDocumentManager
3.0: CodeEditorManager

Kirk

0

Please sign in to leave a comment.