/// <inheritdoc />
        public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            var editor     = new MemberInvocationCodeFixHelper(context);
            var invocation = await editor.GetDiagnosedInvocation();

            if (invocation == null)
            {
                return;
            }

            const string namespacesToBeReferenced = "System";
            var          tempInvocation           = invocation;
            var          stringComparisonOptions  = StringComparisonOptions.GetAll();

            // if it was diagnosed and overload with three or six arguments is called it must be:
            // public static int Compare(string strA, string strB, bool ignoreCase)
            // OR
            // public static int Compare(string strA, int indexA, string strB, int indexB, int length, bool ignoreCase)
            // therefore we will apply only suitable fixes
            var originalArguments = invocation.ArgumentList.Arguments;

            if (originalArguments.Count == 3 || originalArguments.Count == 6)
            {
                var  ignoreCaseAttribute = originalArguments.Last().Expression.ToString();
                bool ignoreCase;

                if (!bool.TryParse(ignoreCaseAttribute, out ignoreCase))
                {
                    // could not detect value of 'ignoreCase' argument -> no codefix provided
                    return;
                }

                // delete last argument of the previous invocation
                tempInvocation          = tempInvocation.WithArgumentList(SyntaxFactory.ArgumentList(originalArguments.RemoveAt(originalArguments.Count - 1)));
                stringComparisonOptions = ignoreCase
                    ? StringComparisonOptions.GetCaseInsensitive()
                    : StringComparisonOptions.GetCaseSensitive();
            }

            foreach (var stringComparisonOption in stringComparisonOptions)
            {
                var newInvocation = tempInvocation.AppendArguments(stringComparisonOption);

                context.RegisterCodeFix(
                    CodeAction.Create(
                        title: CodeFixMessagesProvider.GetReplaceWithMessage(newInvocation),
                        createChangedDocument:
                        c => editor.ReplaceExpressionWith(invocation, newInvocation, c, namespacesToBeReferenced),
                        equivalenceKey: $"{nameof(StringCompareStaticMethodCodeFixProvider)}-{stringComparisonOption}"),
                    context.Diagnostics.First());
            }
        }
Exemple #2
0
        /// <inheritdoc />
        public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            var editor     = new MemberInvocationCodeFixHelper(context);
            var invocation = await editor.GetDiagnosedInvocation();

            if (!IsFixable(invocation))
            {
                return;
            }

            var memberAccess = (MemberAccessExpressionSyntax)invocation.Expression;

            if (IsChainedMemberAccesses(memberAccess))
            {
                // if this is the case, we cannot apply codefix directly as it is a static method call
                // new variable needs to be introduced to keep the result of the call
                // this variable would be later used in member access chain
                return;
            }

            const string namespacesToBeReferenced = "System";
            var          firstString      = SyntaxFactory.Argument(memberAccess.Expression);
            var          secondString     = invocation.ArgumentList.Arguments.First();
            var          staticInvocation = SyntaxFactory.ParseExpression("string.Compare()") as InvocationExpressionSyntax;

            foreach (var stringComparisonOption in StringComparisonOptions.GetAll())
            {
                var newInvocation = staticInvocation.AppendArguments(firstString, secondString).AppendArguments(stringComparisonOption);

                context.RegisterCodeFix(
                    CodeAction.Create(
                        title: CodeFixMessagesProvider.GetReplaceWithMessage(newInvocation),
                        createChangedDocument: c => editor.ReplaceExpressionWith(invocation, newInvocation, c, namespacesToBeReferenced),
                        equivalenceKey: $"{nameof(StringCompareToMethodCodeFixProvider)}-{stringComparisonOption}"),
                    context.Diagnostics.First());
            }
        }
Exemple #3
0
        /// <inheritdoc />
        public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            var editor     = new MemberInvocationCodeFixHelper(context);
            var invocation = await editor.GetDiagnosedInvocation();

            if (invocation == null)
            {
                return;
            }

            const string namespacesToBeReferenced = "System";

            foreach (var stringComparisonOption in StringComparisonOptions.GetAll())
            {
                var newInvocation = invocation.AppendArguments(stringComparisonOption);

                context.RegisterCodeFix(
                    CodeAction.Create(
                        title: CodeFixMessagesProvider.GetReplaceWithMessage(newInvocation),
                        createChangedDocument: c => editor.ReplaceExpressionWith(invocation, newInvocation, c, namespacesToBeReferenced),
                        equivalenceKey: $"{nameof(StringComparisonMethodsWithModifierCodeFixProvider)}-{stringComparisonOption}"),
                    context.Diagnostics.First());
            }
        }