PSI and Cursor offets...

Whenever I insert some code/text via PSI the cursor moves in my code. Or
more accurately ( IMO ), the cursor offset remains the same, but as theirs
new text in the buffer, the apparant location has changed.

I tried to get around this using:

fileEditorManager.getSelectedTextEditor().getCaretModel().moveToOffset(
documentEvent.getOffset() + element[0].getLength());

Where element[0] is what I just inserted. This does move the cursor
further along in the document, but it doesn't move it back to where the
cursor originally was ( due to padding and spacing also being inserted
that's not part of the PsiDocElement.

Any solutions to this? Also, I'm inserting a javadoc block, how can I get
this lined up/formatted properly with the current indentation of the
element I'm addBefore()'ing to?

Mark

4 comments

On 1/31/04 7:26 AM, in article pan.2004.01.31.04.26.19.239098@talios.com,
"Mark Derricutt" <mark@talios.com> wrote:

Generally cursor handles all modifications made to the document
automatically and tries whenever it is possible to stay in its position. The
only problem occurs when some text block caret is actually in is replaced in
the document. In this case caret gets moved to the start offset of the
changed block.

If something goes wrong this way could you please attach a code snippet that
shows the problem.

--
Maxim Shafirov
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"


0

Attached is the code I'm using, it first checks if the file can contain java, then walks up the PsiElement tree till it finds a PsiMethod element, and if there isn't a comment attached, addBefore() a new one with what I want.



Attachment(s):
codeblock.txt
0

On 1/31/04 10:37 PM, in article
8095584.1075577870804.JavaMail.itn@is.intellij.net, "Mark Derricutt"
<mark@talios.com> wrote:

Attached is the code I'm using, it first checks if the file can contain java,
then walks up the PsiElement tree till it finds a PsiMethod element, and if
there isn't a comment attached, addBefore() a new one with what I want.


Well I see now what the problem is. It is not a good idea to perform any
modifications of the document inside processing notification of the document
change. Changing PSI modifies the document synchronously in fact.

I'd suggest using LaterInvocator.invokeLater() to postpone necessary
modifications.

--
Maxim Shafirov
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"


0

On Sun, 01 Feb 2004 15:27:01 +0300, Maxim Shafirov (JetBrains) wrote:

I'd suggest using LaterInvocator.invokeLater() to postpone necessary
modifications.


Excellent - that seems to be working a treat. Now to work out why I'm
getting my comment added twice ;p

0

Please sign in to leave a comment.