private void RewriteCall(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 parameterIndex = 0; var currentStringIndex = 0; for (var i = 0; i < paramNames.Count && parameterIndex < _model.Parameters.Count; i++) { var parameterStringIndex = newContent.IndexOf(paramNames.ElementAt(i), currentStringIndex, StringComparison.Ordinal); if (parameterStringIndex <= -1) { continue; } var oldParameterString = paramNames.ElementAt(i); var newParameterString = paramNames.ElementAt(_model.Parameters.ElementAt(parameterIndex).Index); var beginningSub = newContent.Substring(0, parameterStringIndex); var replaceSub = newContent.Substring(parameterStringIndex).Replace(oldParameterString, newParameterString); newContent = beginningSub + replaceSub; parameterIndex++; currentStringIndex = beginningSub.Length + newParameterString.Length; } module.ReplaceLine(paramList.Start.Line, newContent); module.DeleteLines(paramList.Start.Line + 1, lineCount - 1); }
private void RewriteCall(VBAParser.ArgumentListContext paramList, ICodeModule module) { var argValues = new List <string>(); if (paramList.positionalOrNamedArgumentList().positionalArgumentOrMissing() != null) { argValues.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) { argValues.AddRange(paramList.positionalOrNamedArgumentList().namedArgumentList().namedArgument().Select(p => p.GetText()).ToList()); } if (paramList.positionalOrNamedArgumentList().requiredPositionalArgument() != null) { argValues.Add(paramList.positionalOrNamedArgumentList().requiredPositionalArgument().GetText()); } var lineCount = paramList.Stop.Line - paramList.Start.Line + 1; // adjust for total line count var newContent = module.GetLines(paramList.Start.Line, lineCount); newContent = newContent.Remove(paramList.Start.Column, paramList.GetText().Length); var reorderedArgValues = new List <string>(); foreach (var param in _model.Parameters) { var argAtIndex = argValues.ElementAtOrDefault(param.Index); if (argAtIndex != null) { reorderedArgValues.Add(argAtIndex); } } // property let/set and paramarrays for (var index = reorderedArgValues.Count; index < argValues.Count; index++) { reorderedArgValues.Add(argValues[index]); } newContent = newContent.Insert(paramList.Start.Column, string.Join(", ", reorderedArgValues)); 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 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 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 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); }