コード例 #1
0
        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, "");
            }
        }
コード例 #2
0
        private void FixTypeHintUsage(string hint, CodeModule module, Selection selection, bool isDeclaration = false)
        {
            var line = module.Lines[selection.StartLine, 1];

            var asTypeClause = ' ' + Tokens.As + ' ' + TypeHints[hint];

            string fix;

            if (isDeclaration && (Context is VBAParser.FunctionStmtContext || Context is VBAParser.PropertyGetStmtContext))
            {
                var typeHint  = (ParserRuleContext)Context.children.First(c => c is VBAParser.TypeHintContext);
                var argList   = (ParserRuleContext)Context.children.First(c => c is VBAParser.ArgListContext);
                var endLine   = argList.Stop.Line;
                var endColumn = argList.Stop.Column;

                var oldLine = module.Lines[endLine, selection.LineCount];
                fix = oldLine.Insert(endColumn + 1, asTypeClause).Remove(typeHint.Start.Column, 1);  // adjust for VBA 0-based indexing

                module.ReplaceLine(endLine, fix);
            }
            else
            {
                var pattern = "\\b" + _declaration.IdentifierName + "\\" + hint;
                fix = Regex.Replace(line, pattern, _declaration.IdentifierName + (isDeclaration ? asTypeClause : string.Empty));
                module.ReplaceLine(selection.StartLine, fix);
            }
        }
コード例 #3
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, "");
            }
        }
コード例 #4
0
        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, "");
            }
        }
コード例 #5
0
        private void RewriteSignature(Declaration target, VBAParser.ArgListContext paramList, CodeModule 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);
        }
コード例 #6
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);
        }
コード例 #7
0
        private void editQueryObjectInfo(string objectName, string objInfo, int objType)
        {                        //TODO: �������� objType
            int countOfCase = 0; // ���� 1 ������ ����� ��� ���� 2 ������ ����� ������ ������

            for (int j = 1; j <= codeModule.CountOfLines; j++)
            {
                string bufString = codeModule.get_Lines(j, 1);
                if (bufString.IndexOf(string.Format("Case \"{0}\"", objectName)) > 0)
                {
                    countOfCase++; // ����� ���
                }
                if (countOfCase > 0)
                {
                    if (bufString.IndexOf("objInfo") > 0)
                    {
                        countOfCase++; // ����� ������ � objInfo
                    }
                }
                if (countOfCase > 1)
                {
                    // �������� ������
                    string[] array = bufString.Split(new char[] { '"' });
                    array[1] = objInfo;
                    codeModule.ReplaceLine(j, string.Join("\"", array));
                    return;
                }
            }
        }
コード例 #8
0
        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);
        }
コード例 #9
0
        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);
        }
コード例 #10
0
        private void FixTypeHintUsage(string hint, CodeModule module, Selection selection, bool isDeclaration = false)
        {
            var line = module.get_Lines(selection.StartLine, 1);

            var asTypeClause = ' ' + Tokens.As + ' ' + TypeHints[hint];
            var pattern = "\\b" + _declaration.IdentifierName + "\\" + hint;
            var fix = Regex.Replace(line, pattern, _declaration.IdentifierName + (isDeclaration ? asTypeClause : string.Empty));

            module.ReplaceLine(selection.StartLine, fix);
        }
コード例 #11
0
        private void FixTypeHintUsage(string hint, CodeModule module, Selection selection, bool isDeclaration = false)
        {
            var line = module.get_Lines(selection.StartLine, 1);

            var asTypeClause = ' ' + Tokens.As + ' ' + TypeHints[hint];
            var pattern      = "\\b" + _declaration.IdentifierName + "\\" + hint;
            var fix          = Regex.Replace(line, pattern, _declaration.IdentifierName + (isDeclaration ? asTypeClause : string.Empty));

            module.ReplaceLine(selection.StartLine, fix);
        }
コード例 #12
0
        private void RewriteCall(VBAParser.ArgumentListContext paramList, CodeModule 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.Lines[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);
        }
コード例 #13
0
        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);
        }
コード例 #14
0
        private void FixTypeHintUsage(string hint, CodeModule module, Selection selection, bool isDeclaration = false)
        {
            var line = module.Lines[selection.StartLine, 1];

            var asTypeClause = ' ' + Tokens.As + ' ' + TypeHints[hint];

            string fix;

            if (isDeclaration && (Context is VBAParser.FunctionStmtContext || Context is VBAParser.PropertyGetStmtContext))
            {
                var typeHint = (ParserRuleContext)Context.children.First(c => c is VBAParser.TypeHintContext);
                var argList = (ParserRuleContext) Context.children.First(c => c is VBAParser.ArgListContext);
                var endLine = argList.Stop.Line;
                var endColumn = argList.Stop.Column;

                var oldLine = module.Lines[endLine, selection.LineCount];
                fix = oldLine.Insert(endColumn + 1, asTypeClause).Remove(typeHint.Start.Column, 1);  // adjust for VBA 0-based indexing

                module.ReplaceLine(endLine, fix);
            }
            else
            {
                var pattern = "\\b" + _declaration.IdentifierName + "\\" + hint;
                fix = Regex.Replace(line, pattern, _declaration.IdentifierName + (isDeclaration ? asTypeClause : string.Empty));
                module.ReplaceLine(selection.StartLine, fix);
            }
        }
コード例 #15
0
        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);
        }
コード例 #16
0
        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);
        }
コード例 #17
0
 public void ReplaceLine(int Line, string String)
 {
     _codeModule.ReplaceLine(Line, String);
 }
コード例 #18
0
        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, "");
            }
        }
コード例 #19
0
        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, "");
            }
        }
コード例 #20
0
        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);
        }
コード例 #21
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.Replace(" _" + Environment.NewLine, string.Empty));
            module.DeleteLines(paramList.Start.Line + 1, lineNum - 1);
        }