private void RewriteCall(VBAParser.ArgsCallContext paramList, CodeModule module) { var paramNames = paramList.argCall().Select(arg => arg.GetText()).ToList(); 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 RemoveCallParameter(VBAParser.ArgsCallContext paramList, CodeModule module) { var paramNames = paramList.argCall().Select(arg => arg.GetText()).ToList(); var lineCount = paramList.Stop.Line - paramList.Start.Line + 1; // adjust for total line count var newContent = module.Lines[paramList.Start.Line, lineCount].Replace(" _", "").RemoveExtraSpaces(); 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); for (var line = paramList.Start.Line + 1; line < paramList.Start.Line + lineCount; line++) { module.ReplaceLine(line, ""); } }