Beispiel #1
0
        public override object VisitLambdaExpression(CsharpSubsetParser.LambdaExpressionContext context)
        {
            string body    = null;
            bool   isTyped = false;

            if (context.lambdaBody() != null)
            {
                body    = GetContextSource(context.lambdaBody());
                isTyped = context.lambdaBody().returnStmnt() != null;
            }
            else
            {
                body    = "\nreturn " + GetContextSource(context.mathExpression()) + ";\n";
                isTyped = true;
            }


            var          lambdaExpressionInterval = new Interval(context.Start.StartIndex, context.Stop.StopIndex);
            var          argList = GetArgList(context);
            var          classDefinitionContext = FindClassDefinitionContext(context);
            var          clasStartIndex         = classDefinitionContext.Start.StartIndex;
            RefactorData data = new RefactorData(argList, body, lambdaExpressionInterval, clasStartIndex, isTyped);

            RefactorDataList.Add(data);


            return(base.VisitLambdaExpression(context));
        }
Beispiel #2
0
        private string RefactorSingleLambda(StringBuilder refactorBuilder, RefactorData data, int index)
        {
            string methodName = "refactoredLambda" + index;

            string method = "\n    public static " + (data.IsTyped ? "int " : "void ") + methodName
                            + "(" + data.ArgumentList + ")" + "\n    {\n        "
                            + data.LambdaBody.Replace("\n", "\n        ") + "\n    }\n";

            refactorBuilder.Remove(data.LambdaInterval.a, data.LambdaInterval.b - data.LambdaInterval.a + 1);
            refactorBuilder.Insert(data.LambdaInterval.a, methodName);

            var methodPrintPos = refactorBuilder.ToString().IndexOf('{', data.ClassStartIndex) + 1;

            refactorBuilder.Insert(methodPrintPos, method);

            Console.WriteLine(refactorBuilder.ToString());

            return(refactorBuilder.ToString());
        }