What is the recommended way to create a new Psi Expression using given two Psi Expressions.

Hi,

Lets say, given 'left', 'right' as two PsiExpressions and we would like to combine them using an operator (say, '+'). As of now, we are doing this as shown below (which seems to be hacky), is this the recommended way?

PsiFile dummyFile = createDummyFile("x = a" + operator + "b"); // This invokes PsiFileFactory.createFileFromText().
PsiAssignStatement assgn = (PsiAssignStatement) dummyFile.getFirstChild();
PsiBinaryExpression binExp = (PsiBinaryExpression) assgn.getValue();
binExp.getLeftExpression().replace(left.copy());
binExp.getRightExpression().replace(right.copy());
return binExp;

3 comments

Yes, except that you better use PsiElementFactory.createExpressionFromText() to create dummy expression.

0

Roman,

What if the PsiExpression we are dealing with are not JAVA expressions? The way you mention creates a Java Expression. What if I want to create a custom language expression. Which supplied classes should we use?

thanks
Siddharth

0

AFAIK there are now common way.
You can use PsiFileFactory.getInstance().createFileFromText() (you may need to wrap expression text to form a valid file, then to locate expression element inside a resulting tree).
Tools | View PSI Structure may be helpful.

0

Please sign in to leave a comment.