What parameter values are required to create an IReferenceExpression

I am writting a code formatting modulue that converts types to and from alias types (String -> string -> String for example). I have encountered an issue where the code is something like String value = String.Empty. To convert this to string value = string.Empty, I need to create a reference expression because String in String.Empty is an IReferenceExpressionNode. I am guessing that I can create an IReferenceExpressionNode using something like this:

string newTypeName = "string";

IReferenceExpression expression = CSharpElementFactory.GetInstance(project).CreateReferenceExpression(newTypeName, null);

if (expression == null)
{
return;
}

newTypeUsage = expression.ToTreeNode();

// replace AST fragment in actual file
using (WriteLockCookie.Create(node.Parent.IsPhysical()))
{
ModificationUtil.ReplaceChild(node, newTypeUsage);
}

The problem is that I don't know what values need to be passed into CreateReferenceExpression(). By passing in "string" or "String", null is returned.

Any pointers?

Cheers

2 comments
Comment actions Permalink

You're going wring way
You can't replace ITypeUsageNode by IReferenceExpressionNode - these are 2
different nodes, and thus you can't replace one by another. This is the main
reason why usage of "ModificationUtil.ReplaceChild" is not recommended - no
type compatibility checking is made there.

1) You have to create not reference expression, but TypeUsageNode via
ElementFactory
2) Use higher-level API to replace AST:

MultipleLocalVariableDeclarationNodeNavigator.GetByDeclarator(declaretion).SetTypeUsage(your
new type usage node)


--
Eugene Pasynkov
Developer
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"
"Rory Primrose" <no_reply@jetbrains.com> wrote in message
news:5506696.20981213875352065.JavaMail.jive@app4.labs.intellij.net...
>I am writting a code formatting modulue that converts types to and from
>alias types (String -> string -> String for example). I have encountered an
>issue where the code is something like String value = String.Empty. To
>convert this to string value = string.Empty, I need to create a reference
>expression because String in String.Empty is an IReferenceExpressionNode.
>I am guessing that I can create an IReferenceExpressionNode using something
>like this:
>

string newTypeName = "string";

>

IReferenceExpression expression =
CSharpElementFactory.GetInstance(project).CreateReferenceExpression(newTypeName,
null);

>

if (expression == null)
{
return;
}

>

newTypeUsage = expression.ToTreeNode();

>

// replace AST fragment in actual file
using (WriteLockCookie.Create(node.Parent.IsPhysical()))
{
ModificationUtil.ReplaceChild(node, newTypeUsage);
}

>

The problem is that I don't know what values need to be passed into
CreateReferenceExpression(). By passing in "string" or "String", null is
returned.

>

Any pointers?

>

Cheers



0
Comment actions Permalink

Hi Eugene,

Thanks for the reply. In this post, you say to use a lower level api because I want write out Boolean rather than bool for example. The higher level api will write bool even if I specify Boolean.

Cheers

0

Please sign in to leave a comment.