How to associate a Document with a PsiFile (or vice versa)?

I am trying to add a code preview pane to the tabifier plugin, similar to the preview panes available under several Code Style tabs. Maxim Shafirov suggested:

This approach would work if all my edits could be performed against the psiFile object. However, I use the Document.replaceString() method to update psiFile. For this I need a Document.

I tried to obtain a Document from the newly created psiFile, or a psiFile from the Document, but both return null, as follows:

How do I establish a connection between a Document and a newly created PsiFile?

Thanks-
Dave

1
3 comments

Maybe you need to get the document from the psifile instead of the editor. the psifile might now have been commited yet.

PsiDocumentManager documentManager = PsiDocumentManager.getInstance(project);
Document document = documentManager.getDocument(psiFile);
//your edit here
// then you commit the document
documentManager.commitDocument(newDocument);

Jacques

1

Forget what I said. Obviously I should read more carefully ;)

0

Dave,
Unfortunately we do not have any support for synchronization between
"in the air" Documents and "in the air" PsiFile (that is, items without
associtated VirtualFile).

I am afraid your only option if you want to have the same code in preview
and for "real" reformatting is to write your own synchronization.
Something along the following lines:


Friendly,
Dmitry
Dave Kriewall wrote:

I am trying to add a code preview pane to the tabifier plugin, similar to
the preview panes available under several Code Style tabs. Maxim Shafirov
suggested:

   private Editor myEditor;
> 
>   public void init() {
>     EditorFactory editorFactory = EditorFactory.getInstance();
>     Document doc = editorFactory.createDocument("");
>     myEditor = editorFactory.createViewer(doc);
>   }
> 
>   public void dispose() {
>     EditorFactory editorFactory = EditorFactory.getInstance();
>     editorFactory.releaseEditor(myEditor);
>   }
> 
>   private String getPreviewText() {
>      return "public class Foo {}"; //sample text here
>   }
> 
>   private void updatePreview() {
>     ApplicationManager.getApplication().runWriteAction(
>         new Runnable() {
>           public void run() {
>             try {
>               PsiManager manager = PsiManager.getInstance(myProject);
>               PsiElementFactory factory = manager.getElementFactory();
>               PsiFile psiFile = factory.createFileFromText("a.java",
>               getPreviewText());
> 
>               //Do your formatting over psiFile here.  *****
> 
>               Document document = myEditor.getDocument();
>               document.replaceString(0, document.getTextLength(),
>               psiFile.getText());
>             } catch (IncorrectOperationException e) {
>               LOG.error(e); // Can't actually happen if preview text is
>               correct.
>             }
>           }
>         }
>     );
>   } ]]>

This approach would work if all my edits could be performed against the
psiFile object. However, I use the Document.replaceString()
method to update psiFile. For this I need a Document.

I tried to obtain a Document from the newly created psiFile, or a psiFile
from the Document, but both return null, as follows:

 Document document = myEditor.getDocument();
> PsiDocumentManager documentManager =
> PsiDocumentManager.getInstance(project); PsiFile f =
> documentManager.getDocument(psiFile); // returns null Document d =
> documentManager.getPsiFile(document); // also returns null
> ]]>

How do I establish a connection between a Document and a newly created
PsiFile?

Thanks-
Dave


--
Dmitry Lomov
IntelliJ Labs / JetBrains Inc.
http://www.intellij.com
"Develop with pleasure!"

0

Please sign in to leave a comment.