private static void AddByRefIdentifier(IModuleRewriter rewriter, VBAParser.ArgContext context)
 {
     if (context.BYREF() == null)
     {
         rewriter.InsertBefore(context.unrestrictedIdentifier().Start.TokenIndex, "ByRef ");
     }
 }
 public override void ExitArg([NotNull] VBAParser.ArgContext context)
 {
     if (context.Start.Line != context.Stop.Line)
     {
         _contexts.Add(new QualifiedContext <ParserRuleContext>(CurrentModuleName, context));
     }
 }
示例#3
0
 public override void ExitArg(VBAParser.ArgContext context)
 {
     if (context.BYREF() != null)
     {
         _contexts.Add(new QualifiedContext <ParserRuleContext>(CurrentModuleName, context));
     }
 }
示例#4
0
        private string DeclareExplicitVariant(VBAParser.ArgContext context, out string instruction)
        {
            if (context == null)
            {
                instruction = null;
                return(null);
            }

            instruction = context.GetText();
            if (!context.children.Select(s => s.GetType()).Contains(typeof(VBAParser.ArgDefaultValueContext)))
            {
                return(instruction + ' ' + Tokens.As + ' ' + Tokens.Variant);
            }

            var fix = string.Empty;
            var hasArgDefaultValue = false;

            foreach (var child in context.children)
            {
                if (child.GetType() == typeof(VBAParser.ArgDefaultValueContext))
                {
                    fix += Tokens.As + ' ' + Tokens.Variant + ' ';
                    hasArgDefaultValue = true;
                }

                fix += child.GetText();
            }

            return(hasArgDefaultValue ? fix : fix + ' ' + Tokens.As + ' ' + Tokens.Variant);
        }
示例#5
0
 private static void RemoveByRefIdentifier(IModuleRewriter rewriter, VBAParser.ArgContext context)
 {
     if (context.BYREF() != null)
     {
         rewriter.Remove(context.BYREF());
         rewriter.Remove(context.whiteSpace().First());
     }
 }
        private string DeclareExplicitVariant(VBAParser.ArgContext context, out string instruction)
        {
            if (context == null)
            {
                instruction = null;
                return(null);
            }

            instruction = context.GetText();
            return(instruction + ' ' + Tokens.As + ' ' + Tokens.Variant);
        }
示例#7
0
        private void FixMethod(VBAParser.ArgContext context, QualifiedSelection qualifiedSelection)
        {
            var selectionLength = context.BYREF() == null ? 0 : 6;

            var module = qualifiedSelection.QualifiedName.Component.CodeModule;
            var lines  = module.Lines[context.Start.Line, 1];

            var result = lines.Remove(context.Start.Column, selectionLength).Insert(context.Start.Column, Tokens.ByVal + ' ');

            module.ReplaceLine(context.Start.Line, result);
        }
        private void FixMethod(VBAParser.ArgContext context, QualifiedSelection qualifiedSelection, IRewriteSession rewriteSession)
        {
            var rewriter = rewriteSession.CheckOutModuleRewriter(qualifiedSelection.QualifiedName);

            if (context.BYREF() != null)
            {
                rewriter.Replace(context.BYREF(), Tokens.ByVal);
            }
            else
            {
                rewriter.InsertBefore(context.unrestrictedIdentifier().Start.TokenIndex, "ByVal ");
            }
        }
        private string DeclareExplicitVariant(VBAParser.ArgContext context, out string instruction, out Selection selection)
        {
            if (context == null)
            {
                instruction = null;
                selection   = VBEditor.Selection.Empty;
                return(null);
            }

            var memberContext = (ParserRuleContext)context.Parent.Parent;

            selection   = memberContext.GetSelection();
            instruction = memberContext.GetText();

            var fix = string.Empty;

            foreach (var child in memberContext.children)
            {
                if (child is VBAParser.ArgListContext)
                {
                    foreach (var tree in ((VBAParser.ArgListContext)child).children)
                    {
                        if (tree.Equals(context))
                        {
                            foreach (var part in context.children)
                            {
                                if (part is VBAParser.UnrestrictedIdentifierContext)
                                {
                                    fix += part.GetText() + ' ' + Tokens.As + ' ' + Tokens.Variant;
                                }
                                else
                                {
                                    fix += part.GetText();
                                }
                            }
                        }
                        else
                        {
                            fix += tree.GetText();
                        }
                    }
                }
                else
                {
                    fix += child.GetText();
                }
            }

            return(fix);
        }
        private static RewriterInfo GetRewriterInfo(VBAParser.ArgContext arg, VBAParser.ArgListContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context), @"Context is null. Expecting a VBAParser.ArgListContext instance.");
            }

            var items     = context.arg();
            var itemIndex = items.ToList().IndexOf(arg);

            if (items.Length == 1)
            {
                return(new RewriterInfo(context.LPAREN().Symbol.TokenIndex + 1, context.RPAREN().Symbol.TokenIndex - 1));
            }

            return(GetRewriterInfoForTargetRemovedFromListStmt(arg.Start, itemIndex, context.arg()));
        }
        private void FixMethod(VBAParser.ArgContext context, QualifiedSelection qualifiedSelection)
        {
            var parameter = context.GetText();
            var argList   = context.parent.GetText();

            var module = qualifiedSelection.QualifiedName.Component.CodeModule;
            {
                string result;
                if (context.BYREF() != null)
                {
                    result = parameter.Replace(Tokens.ByRef, Tokens.ByVal);
                }
                else if (context.OPTIONAL() != null)
                {
                    result = parameter.Replace(Tokens.Optional, Tokens.Optional + ' ' + Tokens.ByVal);
                }
                else
                {
                    result = Tokens.ByVal + ' ' + parameter;
                }

                var startLine = 0;
                var stopLine  = 0;
                try
                {
                    dynamic proc = context.parent.parent;
                    startLine = proc.GetType().GetProperty("Start").GetValue(proc).Line;
                    stopLine  = proc.GetType().GetProperty("Stop").GetValue(proc).Line;
                }
                catch { return; }

                var code = module.GetLines(startLine, stopLine - startLine + 1);
                result = code.Replace(argList, argList.Replace(parameter, result));

                foreach (var line in result.Split(new[] { "\r\n" }, StringSplitOptions.None))
                {
                    module.ReplaceLine(startLine++, line);
                }
            }
        }
示例#12
0
 private static int GetParameterIndex(VBAParser.ArgContext context)
 {
     return(Array.IndexOf(((VBAParser.ArgListContext)context.Parent).arg().ToArray(), context));
 }
示例#13
0
        public static string GetName(VBAParser.ArgContext context, out Interval tokenInterval)
        {
            var nameContext = context.unrestrictedIdentifier();

            return(GetName(nameContext, out tokenInterval));
        }
示例#14
0
 public PassParameterByReferenceQuickFix(Declaration target, QualifiedSelection selection)
     : base(target.Context, selection, InspectionsUI.PassParameterByReferenceQuickFix)
 {
     _argContext = target.Context as VBAParser.ArgContext;
     _codeModule = Selection.QualifiedName.Component.CodeModule;
 }
示例#15
0
 public override void EnterArg(VBAParser.ArgContext context)
 {
     _members.Add(new QualifiedContext <ParserRuleContext>(_qualifiedName, context));
 }