Пример #1
0
 public override void ExitArgList(VBAParser.ArgListContext context)
 {
     if (context.arg() != null && context.arg().Count(a => a.BYREF() != null || (a.BYREF() == null && a.BYVAL() == null)) == 1)
     {
         _contexts.Add(context);
     }
 }
        private void RewriteSignature(Declaration target, VBAParser.ArgListContext paramList, IRewriteSession rewriteSession)
        {
            var rewriter = rewriteSession.CheckOutModuleRewriter(target.QualifiedModuleName);

            var parameters = paramList.arg().Select((s, i) => new { Index = i, Text = s.GetText() }).ToList();

            for (var i = 0; i < _model.Parameters.Count; i++)
            {
                var param = paramList.arg()[i];
                rewriter.Replace(param, parameters.SingleOrDefault(s => s.Index == _model.Parameters[i].Index)?.Text);
            }
        }
        private void RewriteSignature(Declaration target, VBAParser.ArgListContext paramList)
        {
            var rewriter = _model.State.GetRewriter(target);

            var parameters = paramList.arg().Select((s, i) => new { Index = i, Text = s.GetText() }).ToList();

            for (var i = 0; i < _model.Parameters.Count; i++)
            {
                var param = paramList.arg()[i];
                rewriter.Replace(param, parameters.SingleOrDefault(s => s.Index == _model.Parameters[i].Index)?.Text);
            }

            _rewriters.Add(rewriter);
        }
Пример #4
0
        private void AddParameter(Declaration targetMethod, Declaration targetVariable, VBAParser.ArgListContext paramList, CodeModule module)
        {
            var argList   = paramList.arg();
            var lastParam = argList.LastOrDefault();

            var newContent = GetOldSignature(targetMethod);

            if (lastParam == null)
            {
                // Increase index by one because VBA is dumb enough to use 1-based indexing
                newContent = newContent.Insert(newContent.IndexOf('(') + 1, GetParameterDefinition(targetVariable));
            }
            else if (targetMethod.DeclarationType != DeclarationType.PropertyLet &&
                     targetMethod.DeclarationType != DeclarationType.PropertySet)
            {
                newContent = newContent.Replace(argList.Last().GetText(),
                                                argList.Last().GetText() + ", " + GetParameterDefinition(targetVariable));
            }
            else
            {
                newContent = newContent.Replace(argList.Last().GetText(),
                                                GetParameterDefinition(targetVariable) + ", " + argList.Last().GetText());
            }

            module.ReplaceLine(paramList.Start.Line, newContent);
            module.DeleteLines(paramList.Start.Line + 1, paramList.GetSelection().LineCount - 1);
        }
Пример #5
0
        private void RewriteSignature(Declaration target, VBAParser.ArgListContext paramList, ICodeModule module)
        {
            var parameters = paramList.arg().Select((s, i) => new { Index = i, Text = s.GetText() }).ToList();

            var reorderedParams = new List <string>();

            foreach (var param in _model.Parameters)
            {
                var parameterAtIndex = parameters.SingleOrDefault(s => s.Index == param.Index);
                if (parameterAtIndex != null)
                {
                    reorderedParams.Add(parameterAtIndex.Text);
                }
            }

            // property let/set and paramarrays
            for (var index = reorderedParams.Count; index < parameters.Count; index++)
            {
                reorderedParams.Add(parameters[index].Text);
            }

            var signature = GetOldSignature(target);

            signature = signature.Remove(signature.IndexOf('('));

            var asTypeText = target.AsTypeContext == null ? string.Empty : " " + target.AsTypeContext.GetText();

            signature += '(' + string.Join(", ", reorderedParams) + ")" + (asTypeText == " " ? string.Empty : asTypeText);

            var lineCount = paramList.GetSelection().LineCount;

            module.ReplaceLine(paramList.Start.Line, signature.Replace(" _" + Environment.NewLine, string.Empty));
            module.DeleteLines(paramList.Start.Line + 1, lineCount - 1);
        }
        public override void EnterArgList(VBAParser.ArgListContext context)
        {
            var args = context.arg();

            foreach (var argContext in args)
            {
                var asTypeClause = argContext.asTypeClause();
                var asTypeName   = asTypeClause == null
                    ? Tokens.Variant
                    : asTypeClause.type().GetText();
                var    identifier = argContext.unrestrictedIdentifier();
                string typeHint   = Identifier.GetTypeHintValue(identifier);
                bool   isArray    = argContext.LPAREN() != null;
                AddDeclaration(
                    CreateDeclaration(
                        Identifier.GetName(identifier),
                        asTypeName,
                        Accessibility.Implicit,
                        DeclarationType.Parameter,
                        argContext,
                        identifier.GetSelection(),
                        isArray,
                        asTypeClause,
                        typeHint));
            }
        }
        private void RemoveSignatureParameters(Declaration target, VBAParser.ArgListContext paramList, CodeModule module)
        {
            var paramNames = paramList.arg();

            var paramsRemoved = _model.Parameters.Where(item => item.IsRemoved).ToList();
            var signature     = GetOldSignature(target);

            foreach (var param in paramsRemoved)
            {
                try
                {
                    signature = ReplaceCommas(signature.Replace(paramNames.ElementAt(param.Index).GetText(), ""), _model.Parameters.FindIndex(item => item == param) - paramsRemoved.FindIndex(item => item == param));
                }
                catch (ArgumentOutOfRangeException)
                {
                }
            }
            var lineNum = paramList.GetSelection().LineCount;

            module.ReplaceLine(paramList.Start.Line, signature);
            for (var line = paramList.Start.Line + 1; line < paramList.Start.Line + lineNum; line++)
            {
                module.ReplaceLine(line, "");
            }
        }
Пример #8
0
        private void RewriteSignature(Declaration target, VBAParser.ArgListContext paramList, CodeModule module)
        {
            var argList = paramList.arg();

            var newContent = GetOldSignature(target);
            var lineNum    = paramList.GetSelection().LineCount;

            var parameterIndex     = 0;
            var currentStringIndex = 0;

            for (var i = parameterIndex; i < _model.Parameters.Count; i++)
            {
                var oldParam             = argList.ElementAt(parameterIndex).GetText();
                var newParam             = argList.ElementAt(_model.Parameters.ElementAt(parameterIndex).Index).GetText();
                var parameterStringIndex = newContent.IndexOf(oldParam, currentStringIndex, StringComparison.Ordinal);

                if (parameterStringIndex > -1)
                {
                    var beginningSub = newContent.Substring(0, parameterStringIndex);
                    var replaceSub   = newContent.Substring(parameterStringIndex).Replace(oldParam, newParam);

                    newContent = beginningSub + replaceSub;

                    parameterIndex++;
                    currentStringIndex = beginningSub.Length + newParam.Length;
                }
            }

            module.ReplaceLine(paramList.Start.Line, newContent);

            for (var line = paramList.Start.Line + 1; line < paramList.Start.Line + lineNum; line++)
            {
                module.ReplaceLine(line, "");
            }
        }
        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()));
        }
Пример #10
0
            public override void ExitArgList(VBAParser.ArgListContext context)
            {
                var args = context.arg();

                if (args != null && args.All(a => a.PARAMARRAY() == null && a.LPAREN() == null) && args.Count(a => a.BYREF() != null || (a.BYREF() == null && a.BYVAL() == null)) == 1)
                {
                    _contexts.Add(new QualifiedContext <ParserRuleContext>(CurrentModuleName, context));
                }
            }
Пример #11
0
            public override void ExitArgList(VBAParser.ArgListContext context)
            {
                var args = context.arg();

                if (args != null && args.All(a => a.PARAMARRAY() == null && a.LPAREN() == null) && args.Count(a => a.BYREF() != null || (a.BYREF() == null && a.BYVAL() == null)) == 1)
                {
                    _contexts.Add(context);
                }
            }
Пример #12
0
        public override void EnterArgList(VBAParser.ArgListContext context)
        {
            var args = context.arg();

            foreach (var argContext in args)
            {
                var asTypeClause = argContext.asTypeClause();
                var asTypeName   = asTypeClause == null
                    ? Tokens.Variant
                    : asTypeClause.type().GetText();

                var identifier = argContext.ambiguousIdentifier();
                _declarations.Add(CreateDeclaration(identifier.GetText(), asTypeName, Accessibility.Implicit, DeclarationType.Parameter, argContext, identifier.GetSelection()));
            }
        }
        private void RemoveSignatureParameters(Declaration target, VBAParser.ArgListContext paramList, CodeModule module)
        {
            // property set/let have one more parameter than is listed in the getter parameters
            var nonRemovedParamNames = paramList.arg().Where((a, s) => s >= _model.Parameters.Count || !_model.Parameters[s].IsRemoved).Select(s => s.GetText());
            var signature            = GetOldSignature(target);

            signature = signature.Remove(signature.IndexOf('('));

            var asTypeText = target.AsTypeContext == null ? string.Empty : " " + target.AsTypeContext.GetText();

            signature += '(' + string.Join(", ", nonRemovedParamNames) + ")" + (asTypeText == " " ? string.Empty : asTypeText);

            var lineCount = paramList.GetSelection().LineCount;

            module.ReplaceLine(paramList.Start.Line, signature.Replace(" _" + Environment.NewLine, string.Empty));
            module.DeleteLines(paramList.Start.Line + 1, lineCount - 1);
        }
        private void AddParameter(Declaration targetMethod, Declaration targetVariable, VBAParser.ArgListContext paramList, IRewriteSession rewriteSession)
        {
            var rewriter = rewriteSession.CheckOutModuleRewriter(targetMethod.QualifiedModuleName);

            var argList      = paramList.arg();
            var newParameter = $"{Tokens.ByVal} {targetVariable.IdentifierName} {Tokens.As} {targetVariable.AsTypeName}";

            if (!argList.Any())
            {
                rewriter.InsertBefore(paramList.RPAREN().Symbol.TokenIndex, newParameter);
            }
            else if (targetMethod.DeclarationType != DeclarationType.PropertyLet &&
                     targetMethod.DeclarationType != DeclarationType.PropertySet)
            {
                rewriter.InsertBefore(paramList.RPAREN().Symbol.TokenIndex, $", {newParameter}");
            }
            else
            {
                var lastParam = argList.Last();
                rewriter.InsertBefore(lastParam.Start.TokenIndex, $"{newParameter}, ");
            }
        }
        private void AddParameter(Declaration targetMethod, Declaration targetVariable, VBAParser.ArgListContext paramList)
        {
            var rewriter = _state.GetRewriter(targetMethod);

            _rewriters.Add(rewriter);

            var argList      = paramList.arg();
            var newParameter = Tokens.ByVal + " " + targetVariable.IdentifierName + " " + Tokens.As + " " + targetVariable.AsTypeName;

            if (!argList.Any())
            {
                rewriter.InsertBefore(paramList.RPAREN().Symbol.TokenIndex, newParameter);
            }
            else if (targetMethod.DeclarationType != DeclarationType.PropertyLet &&
                     targetMethod.DeclarationType != DeclarationType.PropertySet)
            {
                rewriter.InsertBefore(paramList.RPAREN().Symbol.TokenIndex, $", {newParameter}");
            }
            else
            {
                var lastParam = argList.Last();
                rewriter.InsertBefore(lastParam.Start.TokenIndex, $"{newParameter}, ");
            }
        }