Posted 12 years ago by Erik Pepping - RADVenture B.V
Version: 4.0.0262
Avatar
Hi,

On our product we now have a requirement that we to "refactor" some code.

For doing this I using dotNetProjectResolver.SourceProjectContent.GetTypesForSourceKey.
I am working with the Ast classes ( ClassDeclaration, MethodDeclaration, etc).

I traverse the nodes and in certain cases I need to change it.
(get's appended to different string so i am not modifying original one).

For doing that i am using the TextRange of the IAstNode to retrieve the original source.
(see below for simple helper)

Example: If i pass to my program this c# source:

  public bool OnFlushDirty(object entity, object id, object[] currentState,
              object[] previousState, string[] propertyNames,
              NHibernate.Type.IType[] types)
        {

            Validate(entity);
            return false;

        }
        
Doing a ToStringTree (BTW, excellent , really helpful this method)
for the MethodDeclaration statements show the following:

[1082-1098] StatementExpression
[1082-1098] InvocationExpression
[1082-1090] SimpleName: Validate
[1091-1097] SimpleName: entity


[1125-1138] ReturnStatement
[1132-1137] LiteralExpression

So far, so good.
Lets look at my source "after the conversion":

#1 Validate(entity)
#2 return false;

In line #1 the ; is missing.

My question is the following: Is there a reason why the TextRange for that case
is not including the ";" . It does not look consistent with the other cases
(at least not with ReturnStatement where it's included.

Am i missing something here?


Simple helper method that retrieves the text based on the textranges.

        private string GetSource(TextRange pTextRange)
        {
            if ( pTextRange == null ) throw new ArgumentNullException("pTextRange");
            string source= _source.Substring(pTextRange.StartOffset, pTextRange.Length );
            return source;
        }

Comments (2)

Posted 12 years ago by Erik Pepping - RADVenture B.V
Avatar
Just to add more information : I can workaround it by simple identifying which statements are not including the. Maybe you already have a property or some way to identify them instead of my "fixed" way..


 /// <summary>
        /// Returns the source based on the TextRange.
        /// </summary>
        /// <param name="pTextRange"></param>
        /// <returns></returns>
        private string GetSource(IAstNode pNode )
        {
            if ( pNode == null ) throw new ArgumentNullException("pNode");

            int length = pNode.TextRange.Length;

            if (pNode is StatementExpression || pNode is LocalVariableDeclaration)
                length++;

            string source = _source.Substring(pNode.TextRange.StartOffset, length);
            return source;
        }
Posted 12 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hi Erik,

Thanks for pointing those out. I've altered the grammar for the next maintenance release to include the semi-colons in the ranges, as it should have been.


Actipro Software Support

The latest build of this product (v2020.1 build 0400) was released 24 days ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.