Posted 11 years ago by BorodaAlex
Version: 4.0.0275
Avatar
Hi, all.

I have the class on С# (body) that is in correct state.
How can I parse its body to have the corresponding nodes(properties, methods, constructors) via SyntaxEditor? I need to delete, rename, edit these language items hereafter(generally by name).
What samples should I investigate?

Thanks in advance.

Comments (7)

Posted 11 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
If the document has been parsed, there will be a CompilationUnit in Document.SemanticParseData. The CompilationUnit is an AST node tree that you can examine recursively. It will all the types and member declarations in it, along with their offsets.


Actipro Software Support

Posted 11 years ago by BorodaAlex
Avatar
Thanks for your quick answering my question.

Assume, I have been found the PropertyDeclaration I need. My goal is replace type of Property with new one. How can I find the type of property within range of PropertyDeclaration ? Is it possible via regexp or manual search only or there are some ability to achieve that in SyntaxEditor? Then I need to replace type of private field that is used within Property.

Thanks in advance.
Posted 11 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hello I believe you'd want to look at the PropertyDeclaration.ReturnType property.


Actipro Software Support

Posted 11 years ago by BorodaAlex
Avatar
Thanks.
I can find the necessary field and replace with new one like this:

document_.ReplaceText(DocumentModificationType.Replace, fieldNode.ReturnType.TextRange,
                                                      newTypeName);
It works fine.
But there is problem with replacing the SimpleName item within Property body.
I am doing the next:

Expression getExpression =((ReturnStatement) (node.GetAccessor.BlockStatement.Statements[0])).Expression;                                    SimpleName getSN = ((SimpleName)getExpression);
document_.ReplaceText(DocumentModificationType.Replace, getSN.TextRange, newPrivateFieldName);
The same for setter.
document_.ReplaceText works incorrect in this case :
oldValue: "isExpensive_"
newValue : "test"

Before:
public bool IsExpensive
{
get { return isExpensive_; }
set { isExpensive_ = value; }
}

After:
public System.Boolean test
{
get { retutest_ve_; }
set { isExtest_alue; }
}
As far as I understood TextRange for setter and getter(SimpleName) is correct, but
document_.ReplaceText works incorrect.

How can I resolve my problem ?
Posted 11 years ago by BorodaAlex
Avatar
I understood...
After replacing Property and Type by
document_.ReplaceText
the real position of offset was changed and the next calling of
document_.ReplaceText for getter and setter is not valid.

But i still able to change names of items directly
in CompilationUnit like this :

CompilationUnit cu = (CompilationUnit) document_.SemanticParseData;

            foreach (TypeDeclaration typeDeclaration in cu.Types)
            {
                if (typeDeclaration.Name.Text == member.ClassName)
                {
                    foreach (AstNodeBase childNode in typeDeclaration.ChildNodes)
                    {
                        PropertyDeclaration node = childNode as PropertyDeclaration;
                        if (node != null)
                        {
                           //change the correspomding property
                        }
                    }
                }
             }
e.g. for SimpleName

 Expression getExpression = getRS.Expression;
 SimpleName getSN = ((SimpleName)getExpression);
 getSN.Name = newPrivateFieldName;
The same for other nodes.
How can I get text of my class from CompilationUnit after all changes ?
Posted 11 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Unfortunately right now the AST model doesn't emit code back out. Perhaps that is something we can add in the future though.


Actipro Software Support

Posted 11 years ago by BorodaAlex
Avatar
Thanks for your explanation. I have resolved this question in the following way: I parse class body each time after calling of document.Replace() to have topical values of Text ranges of AST items.
The latest build of this product (v2018.1 build 0342) was released 11 days ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.