When are PsiElement text offsets recalculated?

If the insertString() method is used on the Document available from the Editor are the text offsets for all following PsiElements automatically recalculated? If not is there a way to get them to recalculate?

I am trying to get the text "this." in front of all member variables. I find the text offsets of all PsiReferenceExpressions and if "this." isn't there already I add it with document.insertString(offset, charsequence). This actually works somewhat, but it seems like if it adds more than a couple of strings the offsets for the following elements are not correct anymore, after a couple of insertString the "this." isn't added in the right location.

This actually seems odd to me because it seems like anything after the first one would be off. However, it doesn't really start to get off until after the 3rd one (or so) is inserted.

It would be neat if PsiElement had a setText() method. However, I can understand why it doesn't as in most cases setting the text would probably make it a different element (if I am understanding the document model correctly).

2 comments
Comment actions Permalink

Hello Michael,

The process of updating the PSI based on document changes is called committing
the document, and it doesn't happen on every document change. To invoke it
manually, use PsiDocumentManager.getInstance(project).commitDocument(document).

Please refer to http://www.jetbrains.net/confluence/display/IDEADEV/IntelliJIDEAArchitectural+Overview
for more information.

If the insertString() method is used on the Document available from
the Editor are the text offsets for all following PsiElements
automatically recalculated? If not is there a way to get them to
recalculate?

I am trying to get the text "this." in front of all member variables.
I find the text offsets of all PsiReferenceExpressions and if "this."
isn't there already I add it with document.insertString(offset,
charsequence). This actually works somewhat, but it seems like if it
adds more than a couple of strings the offsets for the following
elements are not correct anymore, after a couple of insertTexts the
"this." isn't added in the right location.

This actually seems odd to me because it seems like anything after the
first one would be off. However, it doesn't really start to get off
until after the 3rd one is inserted or so.

It would be neat if PsiElement had a setText() method. However, I can
understand why it doesn't as in most cases setting the text would
probably make it a different element (if I am understanding the
document model correctly).

---
Original message URL:
http://www.jetbrains.net/devnet/message/5233140#5233140

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


0
Comment actions Permalink

That did the trick.

I had read the document you linked to and I didn't remember seeing anything about a "commit" in it. I read it again and indeed it isn't mentioned.

There are a couple of comments that allude to the fact that edits aren't applied until after the WriteAction is complete. However, the comments don't mention the solution either.

I'll probably add a quick comment there with the solution.

Thanks for the information! (and sorry I have been such a pest the past couple of days)

0

Please sign in to leave a comment.