How do people keep track f specific PsiELements between VMs?

I want to continue to be able to reference specific PsiElements after closing and reopening the project.

Normally, I would serialize / deserialize the information needed to recreate a reference, but it seems as though any PsiElement added and which I kept a reference to continues to exist and is re-realized when the java file is reparsed. So that element is in that tree- somewhere. The question is, which element is it and how to I assign another reference to it after the project is opened....

How do other people do this I wonder... are you tying into the parser and listening/ checking for events?

7 comments

Hello softwarevisualization,

I want to continue to be able to reference specific PsiElements after
closing and reopening the project.

Normally, I would serialize / deserialize the information needed to
recreate a reference, but it seems as though any PsiElement added and
which I kept a reference to continues to exist and is re-realized when
the java file is reparsed. So that element is in that tree- somewhere.
The question is, which element is it and how to I assign another
reference to it after the project is opened....

How do other people do this I wonder... are you tying into the parser
and listening/ checking for events?


What exactly do you need to achieve? In general it is not recommended to
keep references to PSI elements outside the scope of your action, as that's
a very easy way to get memory leaks.

--
Dmitry Jemerov
Development Lead
JetBrains, Inc.
http://www.jetbrains.com/
"Develop with Pleasure!"


0

I just want to keep a memory of elements I have injected, just statements really, so I can present them as a group to the user.

So the plugin writes a statement. Now that statement, aside form whatever other properties it has, has the property of having been written by my plugin. All such statements with that property are Interesting. I want to present a list of such Interesting statements.

While the VM lives, of course I can maintain a reference, but when the project is restarted, I have the problem of finding my Interesting statements again.

I can think of strategies to try, but I thought maybe there's be a best practices way that everyone knew about.

If not then jolly well and back to work I go !

0

Hello softwarevisualization,

I just want to keep a memory of elements I have injected, just
statements really, so I can present them as a group to the user.

So the plugin writes a statement. Now that statement, aside form
whatever other properties it has, has the property of having been
written by my plugin. All such statements with that property are
Interesting. I want to present a list of such Interesting statements.

While the VM lives, of course I can maintain a reference, but when
the project is restarted, I have the problem of finding my Interesting
statements again.


I still do not quite understand your end goal. Is your Interesting property
only relevant to you, or to other users working on the same project as well?

The most robust way is to mark these statements explicitly with a source
code comment, and to locate these comments after project restart.

--
Dmitry Jemerov
Development Lead
JetBrains, Inc.
http://www.jetbrains.com/
"Develop with Pleasure!"


0

To answer your question- my Interesting property is a POJ Statement with no particular distinguishing characteristics save one- it was generated by my plugin.

So after the project is closed and reopened, I have an interest to once again find all these "generated by my plugin statements".

I am getting to know the Psi and it does appear that, unless the statement contains a distinguishing String, it would be hard to pick it out from all other equivalent statements.

For instance, what's to distinguish one

public static void main(String args[])
{
}

statement from any other one ever written? Not much I'd say !

It's all about undo. Undo that which my plugin has done without reverting all other later changes.

0

I did notice a UserData class and also that statements (PsiStatement) is outfitted with a getUserData method. This might be something in the context of this problem, but I am not sure of the following-

what is the UserData intended for?
is that userData on a per-instance basis?
Is it intended to be persisted and is it persisted?

Cheers!

0

Hello softwarevisualization,

I did notice a UserData class and also that statements (PsiStatement)
is outfitted with a getUserData method. This might be something in the
context of this problem, but I am not sure of the following-

what is the UserData intended for?


For anything you like. In particular, using it for marking statements created
by your plugin is quite acceptable.

is that userData on a per-instance basis?


Yes.

Is it intended to be persisted and is it persisted?


No, it's not persisted.

--
Dmitry Jemerov
Development Lead
JetBrains, Inc.
http://www.jetbrains.com/
"Develop with Pleasure!"


0

hey Dmitry

So I found in the OpenAPI where it's possible to create a "RangeMarker" . Here is the relevant documentation-

from class Document at
http://www.jetbrains.com/idea/openapi/5.0/com/intellij/openapi/editor/Document.html

RangeMarker createRangeMarker(int startOffset, int endOffset)
Creates a range marker which points to the specified range of text in the document and is automatically adjusted when the document text is changed.

I want to keep track of the statements my plugin generates in the plug-in user's code (it generates plain old java statements in code as if the user had written them).


If I associate a RangeMarker with the statements and save off the information at closing,

question 1 )
what can go wrong?

question 2)
Is this how one would implement this functionality?

question 3)
Is this the API that is in fact used to implement Bookmarks?

question 4)
The promise of the RangeMarker surviving refactorings sounds too good to be true. In the extreme case, (not a use-case I am anticipating for my plug in, but in general) the statement itself could either be mutated so much it's hard to say whether it's even the same statement anymore or not. Do you know how that eventuality is dealt with?

Thanks!!!!

0

Please sign in to leave a comment.