public override void Fix()
        {
            dynamic functionContext    = Context as VBAParser.FunctionStmtContext;
            dynamic propertyGetContext = Context as VBAParser.PropertyGetStmtContext;

            var context = functionContext ?? propertyGetContext;

            if (context == null)
            {
                throw new InvalidOperationException(string.Format(InspectionsUI.InvalidContextTypeInspectionFix, Context.GetType(), GetType()));
            }


            VBAParser.FunctionNameContext functionName = null;
            if (Context is VBAParser.FunctionStmtContext)
            {
                functionName = ((VBAParser.FunctionStmtContext)Context).functionName();
            }
            else
            {
                functionName = ((VBAParser.PropertyGetStmtContext)Context).functionName();
            }

            string token = functionContext != null
                ? Tokens.Function
                : Tokens.Property + ' ' + Tokens.Get;
            string endToken = token == Tokens.Function
                ? token
                : Tokens.Property;

            string visibility  = context.visibility() == null ? string.Empty : context.visibility().GetText() + ' ';
            string name        = ' ' + Identifier.GetName(functionName.identifier());
            bool   hasTypeHint = Identifier.GetTypeHintValue(functionName.identifier()) != null;

            string args   = context.argList().GetText();
            string asType = context.asTypeClause() == null ? string.Empty : ' ' + context.asTypeClause().GetText();

            string oldSignature = visibility + token + name + (hasTypeHint ? Identifier.GetTypeHintValue(functionName.identifier()) : string.Empty) + args + asType;
            string newSignature = visibility + Tokens.Sub + name + args;

            string procedure          = Context.GetText();
            string noReturnStatements = procedure;

            _returnStatements.ToList().ForEach(returnStatement =>
                                               noReturnStatements = Regex.Replace(noReturnStatements, @"[ \t\f]*" + returnStatement + @"[ \t\f]*\r?\n?", ""));
            string result = noReturnStatements.Replace(oldSignature, newSignature)
                            .Replace(Tokens.End + ' ' + endToken, Tokens.End + ' ' + Tokens.Sub)
                            .Replace(Tokens.Exit + ' ' + endToken, Tokens.Exit + ' ' + Tokens.Sub);

            CodeModule module    = Selection.QualifiedName.Component.CodeModule;
            Selection  selection = Context.GetSelection();

            module.DeleteLines(selection.StartLine, selection.LineCount);
            module.InsertLines(selection.StartLine, result);
        }
Ejemplo n.º 2
0
        public static string GetName(VBAParser.FunctionNameContext context, out Interval tokenInterval)
        {
            var nameContext = context.identifier();

            tokenInterval = Interval.Of(nameContext.Start.TokenIndex, nameContext.Stop.TokenIndex);
            return(GetName(context));
        }
Ejemplo n.º 3
0
 public static string GetName(VBAParser.FunctionNameContext context)
 {
     return(GetName(context.identifier()));
 }