createExpressionFromText doesn't accept \n


This piece of code works fine:
PsiElementFactory factory = PsiManager.getInstance
(__project).getElementFactory ();
PsiExpression exp = factory.createExpressionFromText ("\"one line\"",
__replacee.getContext ());

, but if you put a '\n' in the string to create an expression from,
...
PsiExpression exp = factory.createExpressionFromText ("\"two
\nlines\"", __replacee.getContext ());

it fails: (see stacktrace below)


I need this for a string editor plugin, that eases complex strings editing.
For example,
before:
String s = "one line" ; action: trigger the StringEditorPlugin, and change the text to "two\nlines" After: String s = "]]>two\nlines" ;




com.intellij.util.IncorrectOperationException: Incorrect expression ""one
line"".
at com.intellij.psi.impl.x.createExpressionFromText(x.java:205)
at
com.ravet.ideaplugins.stringEditor.ExpressionReplacer.run(ExpressionReplacer.java:24)
at com.intellij.openapi.command.a.a.executeCommand(a.java:92)
at com.intellij.openapi.command.a.a.executeCommand(a.java:113)
at
com.ravet.ideaplugins.stringEditor.ExpressionReplacer$1.run(ExpressionReplacer.java:45)
at
com.intellij.openapi.application.impl.ApplicationImpl.runWriteAction(ApplicationImpl.java:36)
at
com.ravet.ideaplugins.stringEditor.ExpressionReplacer.replace(ExpressionReplacer.java:41)

3 comments

Alain Ravet wrote:

, but if you put a '\n' in the string to create an expression from,
...
PsiExpression exp = factory.createExpressionFromText ("\"two
\nlines\"", __replacee.getContext ());

it fails: (see stacktrace below)


Alain,

this is because the expression you're passing to the factory looks like this:

String s = "two
lines";

Of course that's not a valid expression. What you want to do is to escape the
backslash because you're having a String inside a String:

PsiExpression exp = factory.createExpressionFromText ("\"two
nlines\"", __replacee.getContext ());

Then it'll work.


Sascha

PS: Merry Christmas :)

0

Java doesn't allow newlines in string constants. Maybe this is why - it's
invalid syntax.

-Keith

This piece of code works fine:
PsiElementFactory factory = PsiManager.getInstance
(__project).getElementFactory ();
PsiExpression exp = factory.createExpressionFromText ("\"one
line\"",
__replacee.getContext ());
, but if you put a '\n' in the string to create an expression from,
...
PsiExpression exp = factory.createExpressionFromText ("\"two
\nlines\"", __replacee.getContext ());
it fails: (see stacktrace below)

I need this for a string editor plugin, that eases complex strings
editing.
For example,
before:
String s = "one <caret>line" ;
action:
trigger the StringEditorPlugin, and change the text to
"two\nlines"
After:
String s = "<caret>two\nlines" ;
com.intellij.util.IncorrectOperationException: Incorrect expression
""one
line"".
at com.intellij.psi.impl.x.createExpressionFromText(x.java:205)
at
com.ravet.ideaplugins.stringEditor.ExpressionReplacer.run(ExpressionRe
placer.java:24)
at com.intellij.openapi.command.a.a.executeCommand(a.java:92)
at com.intellij.openapi.command.a.a.executeCommand(a.java:113)
at
com.ravet.ideaplugins.stringEditor.ExpressionReplacer$1.run(Expression
Replacer.java:45)
at
com.intellij.openapi.application.impl.ApplicationImpl.runWriteAction(A
pplicationImpl.java:36)
at
com.ravet.ideaplugins.stringEditor.ExpressionReplacer.replace(Expressi
onReplacer.java:41)



0

Sascha

What you want to do is to escape the backslash because you're having a
String inside a String:

>

PsiExpression exp = factory.createExpressionFromText ("\"two

nlines\"", __replacee.getContext ());

>

Then it'll work.



It does, indeed. A bigh thanks.

PS: Merry Christmas :)



The same to you (writing this with one hand, the other holding a glas of
champagne).
.. and to - almost - everybody else.

Alain

0

Please sign in to leave a comment.