How to replace text with tag in XML file while preserving whitespace with PSI?

A string of text and whitespace is represented in XmlFile as single XmlText
element. I want to replace only the actual text with a new XML tag, and leave
the surrounding whitespace. I have tried several ways to do this and I can't do
any of them; they all throw exceptions or corrupt the PSI & caches. I've tried:
1. Create tag, copy PsiWhiteSpace objects, insert them before and after tag
2. Create tag, create new XmlText objects containing only whitespace, insert
those appropriately

Neither of these work; they throw exceptions. Is there no way to create
whitespace in XML file with PSI?

3 comments
Comment actions Permalink

It looks like I can't do it using Document.insertString either because the newly
inserted element's getTextRange returns even though it is definitely not
at the top of the file. This shouldn't be this hard.

Keith Lea wrote:

A string of text and whitespace is represented in XmlFile as single
XmlText element. I want to replace only the actual text with a new XML
tag, and leave the surrounding whitespace. I have tried several ways to
do this and I can't do any of them; they all throw exceptions or corrupt
the PSI & caches. I've tried:
1. Create tag, copy PsiWhiteSpace objects, insert them before and after tag
2. Create tag, create new XmlText objects containing only whitespace,
insert those appropriately

Neither of these work; they throw exceptions. Is there no way to create
whitespace in XML file with PSI?

0
Comment actions Permalink

I didn't mean can't, I mean I have to use ugly workaround because this basic
feature of PSI seems to not work. Even after I call commitDocument, the
TextRange is still invalid. Is this expected behavior?

Keith Lea wrote:

It looks like I can't do it using Document.insertString either because
the newly inserted element's getTextRange returns even though it
is definitely not at the top of the file. This shouldn't be this hard.

Keith Lea wrote:

>> A string of text and whitespace is represented in XmlFile as single
>> XmlText element. I want to replace only the actual text with a new XML
>> tag, and leave the surrounding whitespace. I have tried several ways
>> to do this and I can't do any of them; they all throw exceptions or
>> corrupt the PSI & caches. I've tried:
>> 1. Create tag, copy PsiWhiteSpace objects, insert them before and
>> after tag
>> 2. Create tag, create new XmlText objects containing only whitespace,
>> insert those appropriately
>>
>> Neither of these work; they throw exceptions. Is there no way to
>> create whitespace in XML file with PSI?

0
Comment actions Permalink

Hello Keith,

KL> A string of text and whitespace is represented in XmlFile as single XmlText
KL> element. I want to replace only the actual text with a new XML tag, and
leave
KL> the surrounding whitespace.

Have you tried XmlText.setValue()? This should work. Just include the whitespace
characters in the tag text you pass to it.

--
Dmitry Jemerov
Software Developer
JetBrains, Inc.
http://www.jetbrains.com
"Develop with pleasure!"


0

Please sign in to leave a comment.