private void RewriteCall(VBAParser.ArgumentListContext argList, QualifiedModuleName module, IRewriteSession rewriteSession) { var rewriter = rewriteSession.CheckOutModuleRewriter(module); var args = argList.argument().Select((s, i) => new { Index = i, Text = s.GetText() }).ToList(); for (var i = 0; i < _model.Parameters.Count; i++) { if (argList.argument().Length <= i) { break; } var arg = argList.argument()[i]; rewriter.Replace(arg, args.Single(s => s.Index == _model.Parameters[i].Index).Text); } }
private void RemoveCallArguments(VBAParser.ArgumentListContext argList, QualifiedModuleName module) { var rewriter = _model.State.GetRewriter(module); _rewriters.Add(rewriter); var args = argList.children.OfType <VBAParser.ArgumentContext>().ToList(); for (var i = 0; i < _model.Parameters.Count; i++) { // only remove params from RemoveParameters if (!_model.RemoveParameters.Contains(_model.Parameters[i])) { continue; } if (_model.Parameters[i].IsParamArray) { //The following code works because it is neither allowed to use both named arguments //and a ParamArray nor optional arguments and a ParamArray. var index = i == 0 ? 0 : argList.children.IndexOf(args[i - 1]) + 1; for (var j = index; j < argList.children.Count; j++) { rewriter.Remove((dynamic)argList.children[j]); } break; } if (args.Count > i && (args[i].positionalArgument() != null || args[i].missingArgument() != null)) { rewriter.Remove(args[i]); } else { var arg = args.Where(a => a.namedArgument() != null) .SingleOrDefault(a => a.namedArgument().unrestrictedIdentifier().GetText() == _model.Parameters[i].Declaration.IdentifierName); if (arg != null) { rewriter.Remove(arg); } } } }
private static RewriterInfo GetRewriterInfo(VBAParser.ArgumentContext arg, VBAParser.ArgumentListContext argList) { if (argList == null) { throw new ArgumentNullException(nameof(argList), @"Context is null. Expecting a VBAParser.ArgumentListContext instance."); } var items = argList.argument(); var itemIndex = items.ToList().IndexOf(arg); var count = items.Count; if (count == 1) { return(new RewriterInfo(argList.Start.TokenIndex, argList.Stop.TokenIndex)); } return(GetRewriterInfoForTargetRemovedFromListStmt(arg.Start, itemIndex, argList.argument())); }
//Issue 4319. If there are 3 or more arguments and the user elects to remove 2 or more of //the last arguments, then we need to specifically remove the trailing comma from //the last 'kept' argument. private void RemoveTrailingComma(IModuleRewriter rewriter, VBAParser.ArgumentListContext argList = null, bool usesNamedParams = false) { var commaLocator = RetrieveTrailingCommaInfo(_model.RemoveParameters, _model.Parameters); if (!commaLocator.RequiresTrailingCommaRemoval) { return; } var tokenStart = 0; var tokenStop = 0; if (argList is null) { //Handle Signatures only tokenStart = commaLocator.LastRetainedArg.Param.Declaration.Context.Stop.TokenIndex + 1; tokenStop = commaLocator.FirstOfRemovedArgSeries.Param.Declaration.Context.Start.TokenIndex - 1; rewriter.RemoveRange(tokenStart, tokenStop); return; } //Handles References var args = argList.children.OfType <VBAParser.ArgumentContext>().ToList(); if (usesNamedParams) { var lastKeptArg = args.Where(a => a.namedArgument() != null) .SingleOrDefault(a => a.namedArgument().unrestrictedIdentifier().GetText() == commaLocator.LastRetainedArg.Identifier); var firstOfRemovedArgSeries = args.Where(a => a.namedArgument() != null) .SingleOrDefault(a => a.namedArgument().unrestrictedIdentifier().GetText() == commaLocator.FirstOfRemovedArgSeries.Identifier); tokenStart = lastKeptArg.Stop.TokenIndex + 1; tokenStop = firstOfRemovedArgSeries.Start.TokenIndex - 1; rewriter.RemoveRange(tokenStart, tokenStop); return; } tokenStart = args[commaLocator.LastRetainedArg.Index].Stop.TokenIndex + 1; tokenStop = args[commaLocator.FirstOfRemovedArgSeries.Index].Start.TokenIndex - 1; rewriter.RemoveRange(tokenStart, tokenStop); }
private void RewriteCall(VBAParser.ArgumentListContext argList, ICodeModule module) { var rewriter = _model.State.GetRewriter(module.Parent); var args = argList.argument().Select((s, i) => new { Index = i, Text = s.GetText() }).ToList(); for (var i = 0; i < _model.Parameters.Count; i++) { if (argList.argument().Count <= i) { break; } var arg = argList.argument()[i]; rewriter.Replace(arg, args.Single(s => s.Index == _model.Parameters[i].Index).Text); } _rewriters.Add(rewriter); }
private void AdjustReferences(IEnumerable <IdentifierReference> references, Declaration method) { foreach (var reference in references.Where(item => item.Context != method.Context)) { var proc = (dynamic)reference.Context; var module = reference.QualifiedModuleName.Component.CodeModule; VBAParser.ArgumentListContext argumentList = null; var callStmt = ParserRuleContextHelper.GetParent <VBAParser.CallStmtContext>(reference.Context); if (callStmt != null) { argumentList = CallStatement.GetArgumentList(callStmt); } if (argumentList == null) { continue; } RemoveCallParameter(argumentList, module); } }
private void RemoveCallArguments(VBAParser.ArgumentListContext argList, ICodeModule module) { var rewriter = _model.State.GetRewriter(module.Parent); var args = argList.children.OfType <VBAParser.ArgumentContext>().ToList(); for (var i = 0; i < _model.Parameters.Count; i++) { if (!_model.Parameters[i].IsRemoved) { continue; } if (_model.Parameters[i].IsParamArray) { var index = i == 0 ? 0 : argList.children.IndexOf(args[i - 1]) + 1; for (var j = index; j < argList.children.Count; j++) { rewriter.Remove((dynamic)argList.children[j]); } break; } if (args.Count > i && args[i].positionalArgument() != null) { rewriter.Remove(args[i]); } else { var arg = args.Where(a => a.namedArgument() != null) .SingleOrDefault(a => a.namedArgument().unrestrictedIdentifier().GetText() == _model.Parameters[i].Declaration.IdentifierName); if (arg != null) { rewriter.Remove(arg); } } } }
private void AdjustReferences(IEnumerable <IdentifierReference> references, Declaration method, IRewriteSession rewriteSession) { foreach (var reference in references.Where(item => item.Context != method.Context)) { VBAParser.ArgumentListContext argumentList = null; var callStmt = reference.Context.GetAncestor <VBAParser.CallStmtContext>(); if (callStmt != null) { argumentList = CallStatement.GetArgumentList(callStmt); } if (argumentList == null) { var indexExpression = reference.Context.GetAncestor <VBAParser.IndexExprContext>(); if (indexExpression != null) { argumentList = indexExpression.GetChild <VBAParser.ArgumentListContext>(); } } if (argumentList == null) { var whitespaceIndexExpression = reference.Context.GetAncestor <VBAParser.WhitespaceIndexExprContext>(); if (whitespaceIndexExpression != null) { argumentList = whitespaceIndexExpression.GetChild <VBAParser.ArgumentListContext>(); } } if (argumentList == null) { continue; } RemoveCallArguments(argumentList, reference.QualifiedModuleName, rewriteSession); } }
public static VBAParser.ArgumentListContext GetArgumentList(VBAParser.CallStmtContext callStmt) { VBAParser.ArgumentListContext argList = null; if (callStmt.CALL() != null) { var lExpr = callStmt.lExpression(); if (lExpr is VBAParser.IndexExprContext) { var indexExpr = (VBAParser.IndexExprContext)lExpr; argList = indexExpr.argumentList(); } else if (lExpr is VBAParser.WhitespaceIndexExprContext) { var indexExpr = (VBAParser.WhitespaceIndexExprContext)lExpr; argList = indexExpr.argumentList(); } } else { argList = callStmt.argumentList(); } return(argList); }
private void RemoveCallParameter(VBAParser.ArgumentListContext paramList, CodeModule module) { var paramNames = new List <string>(); if (paramList.positionalOrNamedArgumentList().positionalArgumentOrMissing() != null) { paramNames.AddRange(paramList.positionalOrNamedArgumentList().positionalArgumentOrMissing().Select(p => { if (p is VBAParser.SpecifiedPositionalArgumentContext) { return(((VBAParser.SpecifiedPositionalArgumentContext)p).positionalArgument().GetText()); } return(string.Empty); }).ToList()); } if (paramList.positionalOrNamedArgumentList().namedArgumentList() != null) { paramNames.AddRange(paramList.positionalOrNamedArgumentList().namedArgumentList().namedArgument().Select(p => p.GetText()).ToList()); } if (paramList.positionalOrNamedArgumentList().requiredPositionalArgument() != null) { paramNames.Add(paramList.positionalOrNamedArgumentList().requiredPositionalArgument().GetText()); } var lineCount = paramList.Stop.Line - paramList.Start.Line + 1; // adjust for total line count var newContent = module.Lines[paramList.Start.Line, lineCount]; newContent = newContent.Remove(paramList.Start.Column, paramList.GetText().Length); var savedParamNames = paramNames; for (var index = _model.Parameters.Count - 1; index >= 0; index--) { var param = _model.Parameters[index]; if (!param.IsRemoved) { continue; } if (param.Name.Contains("ParamArray")) { // handle param arrays while (savedParamNames.Count > index) { savedParamNames.RemoveAt(index); } } else { if (index < savedParamNames.Count && !savedParamNames[index].StripStringLiterals().Contains(":=")) { savedParamNames.RemoveAt(index); } else { var paramIndex = savedParamNames.FindIndex(s => s.StartsWith(param.Declaration.IdentifierName + ":=")); if (paramIndex != -1 && paramIndex < savedParamNames.Count) { savedParamNames.RemoveAt(paramIndex); } } } } newContent = newContent.Insert(paramList.Start.Column, string.Join(", ", savedParamNames)); module.ReplaceLine(paramList.Start.Line, newContent.Replace(" _" + Environment.NewLine, string.Empty)); module.DeleteLines(paramList.Start.Line + 1, lineCount - 1); }
private ArgumentList VisitArgumentList(Declaration module, Declaration parent, VBAParser.ArgumentListContext argumentList, IBoundExpression withBlockVariable, StatementResolutionContext statementContext) { var convertedList = new ArgumentList(); if (argumentList == null) { return(convertedList); } var list = argumentList.positionalOrNamedArgumentList(); // TODO: positionalArgumentOrMissing is there as preparation for argument compatibility checking. if (list.positionalArgumentOrMissing() != null) { foreach (var expr in list.positionalArgumentOrMissing()) { if (expr is VBAParser.SpecifiedPositionalArgumentContext) { convertedList.AddArgument(new ArgumentListArgument( VisitArgumentBinding(module, parent, ((VBAParser.SpecifiedPositionalArgumentContext)expr).positionalArgument().argumentExpression(), withBlockVariable, StatementResolutionContext.Undefined), ArgumentListArgumentType.Positional)); } } } if (list.requiredPositionalArgument() != null) { convertedList.AddArgument(new ArgumentListArgument( VisitArgumentBinding(module, parent, list.requiredPositionalArgument().argumentExpression(), withBlockVariable, StatementResolutionContext.Undefined), ArgumentListArgumentType.Positional)); } if (list.namedArgumentList() != null) { foreach (var expr in list.namedArgumentList().namedArgument()) { convertedList.AddArgument(new ArgumentListArgument( VisitArgumentBinding(module, parent, expr.argumentExpression(), withBlockVariable, StatementResolutionContext.Undefined), ArgumentListArgumentType.Named, CreateNamedArgumentExpressionCreator(expr.unrestrictedIdentifier().GetText(), expr.unrestrictedIdentifier()))); } } return(convertedList); }
private void UpdateCalls() { var procedureName = Identifier.GetName(_subStmtQualifiedContext.Context.subroutineName().identifier()); var procedure = _state.AllDeclarations.SingleOrDefault(d => !d.IsBuiltIn && d.IdentifierName == procedureName && d.Context is VBAParser.SubStmtContext && d.QualifiedName.QualifiedModuleName.Equals(_subStmtQualifiedContext.ModuleName)); if (procedure == null) { return; } foreach (var reference in procedure.References.OrderByDescending(o => o.Selection.StartLine).ThenByDescending(d => d.Selection.StartColumn)) { var startLine = reference.Selection.StartLine; if (procedure.ComponentName == reference.QualifiedModuleName.ComponentName && procedure.Selection.EndLine < reference.Selection.StartLine) { startLine += _lineOffset; } var module = reference.QualifiedModuleName.Component.CodeModule; var referenceParent = ParserRuleContextHelper.GetParent <VBAParser.CallStmtContext>(reference.Context); if (referenceParent == null) { continue; } VBAParser.ArgumentListContext argList = CallStatement.GetArgumentList(referenceParent); List <string> paramNames = new List <string>(); string argsCall = string.Empty; int argsCallOffset = 0; if (argList != null) { argsCallOffset = argList.GetSelection().EndColumn - reference.Context.GetSelection().EndColumn; argsCall = argList.GetText(); if (argList.positionalOrNamedArgumentList().positionalArgumentOrMissing() != null) { paramNames.AddRange(argList.positionalOrNamedArgumentList().positionalArgumentOrMissing().Select(p => { if (p is VBAParser.SpecifiedPositionalArgumentContext) { return(((VBAParser.SpecifiedPositionalArgumentContext)p).positionalArgument().GetText()); } else { return(string.Empty); } }).ToList()); } if (argList.positionalOrNamedArgumentList().namedArgumentList() != null) { paramNames.AddRange(argList.positionalOrNamedArgumentList().namedArgumentList().namedArgument().Select(p => p.GetText()).ToList()); } if (argList.positionalOrNamedArgumentList().requiredPositionalArgument() != null) { paramNames.Add(argList.positionalOrNamedArgumentList().requiredPositionalArgument().GetText()); } } var referenceText = reference.Context.GetText(); var newCall = paramNames.ToList().ElementAt(_argListQualifiedContext.Context.arg().ToList().IndexOf(_argQualifiedContext.Context)) + " = " + _subStmtQualifiedContext.Context.subroutineName().GetText() + "(" + argsCall + ")"; var oldLines = module.Lines[startLine, reference.Selection.LineCount]; var newText = oldLines.Remove(reference.Selection.StartColumn - 1, referenceText.Length + argsCallOffset) .Insert(reference.Selection.StartColumn - 1, newCall); module.DeleteLines(startLine, reference.Selection.LineCount); module.InsertLines(startLine, newText); } }
private ArgumentList VisitArgumentList(Declaration module, Declaration parent, VBAParser.ArgumentListContext argumentList, IBoundExpression withBlockVariable) { var convertedList = new ArgumentList(); if (argumentList == null) { return(convertedList); } var list = argumentList; // TODO: positionalArgumentOrMissing is there as preparation for argument compatibility checking. if (list.argument() != null) { foreach (var expr in list.argument()) { if (expr.positionalArgument() != null) { var(binding, context, isAddressOfArgument) = VisitArgumentBinding(module, parent, expr.positionalArgument().argumentExpression(), withBlockVariable); convertedList.AddArgument(new ArgumentListArgument( binding, context, argumentList, ArgumentListArgumentType.Positional, isAddressOfArgument)); } else if (expr.namedArgument() != null) { var(binding, context, isAddressOfArgument) = VisitArgumentBinding(module, parent, expr.namedArgument().argumentExpression(), withBlockVariable); convertedList.AddArgument(new ArgumentListArgument( binding, context, argumentList, ArgumentListArgumentType.Named, CreateNamedArgumentExpressionCreator(expr.namedArgument().unrestrictedIdentifier().GetText(), expr.namedArgument().unrestrictedIdentifier()), isAddressOfArgument)); } else if (expr.missingArgument() != null) { var missingArgumentContext = expr.missingArgument(); var binding = new MissingArgumentBinding(missingArgumentContext); convertedList.AddArgument(new ArgumentListArgument( binding, missingArgumentContext, argumentList, ArgumentListArgumentType.Missing, false)); } } } return(convertedList); }
private static (int?Position, string ArgumentName) GetParameterLocatorAttributes(VBAParser.ArgumentListContext argListContext, string identifier) { (int?Position, string ArgumentName)result = (null, null); var argumentContexts = argListContext.GetDescendents <VBAParser.ArgumentContext>(); for (var idx = 0; idx < argumentContexts.Count(); idx++) { var argumentContext = argumentContexts.ElementAt(idx); if (!argumentContext.GetText().Contains(identifier)) { continue; } if (argumentContext.TryGetChildContext <VBAParser.PositionalArgumentContext>(out _) && argumentContext.GetText().Equals(identifier)) { result.Position = idx; break; } if (argumentContext.TryGetChildContext <VBAParser.NamedArgumentContext>(out var namedArgCtxt) && namedArgCtxt.GetChild <VBAParser.ArgumentExpressionContext>().GetText().Equals(identifier)) { result.ArgumentName = namedArgCtxt.GetChild <VBAParser.UnrestrictedIdentifierContext>().GetText(); break; } } return(result); }
private static string GetArgListString(VBAParser.ArgumentListContext context) { return(context == null ? null : "(" + context.GetText() + ")"); }
private void RemoveCallParameter(VBAParser.ArgumentListContext paramList, CodeModule module) { List <string> paramNames = new List <string>(); if (paramList.positionalOrNamedArgumentList().positionalArgumentOrMissing() != null) { paramNames.AddRange(paramList.positionalOrNamedArgumentList().positionalArgumentOrMissing().Select(p => { if (p is VBAParser.SpecifiedPositionalArgumentContext) { return(((VBAParser.SpecifiedPositionalArgumentContext)p).positionalArgument().GetText()); } else { return(string.Empty); } }).ToList()); } if (paramList.positionalOrNamedArgumentList().namedArgumentList() != null) { paramNames.AddRange(paramList.positionalOrNamedArgumentList().namedArgumentList().namedArgument().Select(p => p.GetText()).ToList()); } if (paramList.positionalOrNamedArgumentList().requiredPositionalArgument() != null) { paramNames.Add(paramList.positionalOrNamedArgumentList().requiredPositionalArgument().GetText()); } var lineCount = paramList.Stop.Line - paramList.Start.Line + 1; // adjust for total line count var newContent = module.Lines[paramList.Start.Line, lineCount].Replace(" _" + Environment.NewLine, string.Empty).RemoveExtraSpacesLeavingIndentation(); var currentStringIndex = 0; foreach ( var param in _model.Parameters.Where(item => item.IsRemoved && item.Index < paramNames.Count) .Select(item => item.Declaration)) { var paramIndex = _model.Parameters.FindIndex(item => item.Declaration.Context.GetText() == param.Context.GetText()); if (paramIndex >= paramNames.Count) { return; } do { var paramToRemoveName = paramNames.ElementAt(0).Contains(":=") ? paramNames.Find(item => item.Contains(param.IdentifierName + ":=")) : paramNames.ElementAt(paramIndex); if (paramToRemoveName == null || !newContent.Contains(paramToRemoveName)) { continue; } var valueToRemove = paramToRemoveName != paramNames.Last() ? paramToRemoveName + "," : paramToRemoveName; var parameterStringIndex = newContent.IndexOf(valueToRemove, currentStringIndex, StringComparison.Ordinal); if (parameterStringIndex <= -1) { continue; } newContent = newContent.Remove(parameterStringIndex, valueToRemove.Length); currentStringIndex = parameterStringIndex; if (paramToRemoveName == paramNames.Last() && newContent.LastIndexOf(',') != -1) { newContent = newContent.Remove(newContent.LastIndexOf(','), 1); } } while (paramIndex >= _model.Parameters.Count - 1 && ++paramIndex < paramNames.Count && newContent.Contains(paramNames.ElementAt(paramIndex))); } module.ReplaceLine(paramList.Start.Line, newContent); module.DeleteLines(paramList.Start.Line + 1, lineCount - 1); }