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 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 static string GetArgListString(VBAParser.ArgumentListContext context) { return(context == null ? null : "(" + context.GetText() + ")"); }