Example #1
0
        /// <inheritdoc/>
        public override async Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            var document = context.Document;
            var root     = await document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);

            foreach (var diagnostic in context.Diagnostics)
            {
                var token        = root.FindToken(diagnostic.Location.SourceSpan.Start);
                var newName      = char.ToUpper(token.ValueText[0]) + token.ValueText.Substring(1);
                var memberSyntax = this.GetParentTypeDeclaration(token);

                if (memberSyntax is NamespaceDeclarationSyntax)
                {
                    // namespaces are not symbols. So we are just renaming the namespace
                    Func <CancellationToken, Task <Document> > renameNamespace = cancellationToken =>
                    {
                        IdentifierNameSyntax identifierSyntax = (IdentifierNameSyntax)token.Parent;

                        var newIdentifierSyntac = identifierSyntax.WithIdentifier(SyntaxFactory.Identifier(newName));

                        var newRoot = root.ReplaceNode(identifierSyntax, newIdentifierSyntac);
                        return(Task.FromResult(context.Document.WithSyntaxRoot(newRoot)));
                    };

                    context.RegisterCodeFix(
                        CodeAction.Create(
                            string.Format(NamingResources.RenameToCodeFix, newName),
                            renameNamespace,
                            nameof(RenameToUpperCaseCodeFixProvider) + "_" + diagnostic.Id),
                        diagnostic);
                }
                else if (memberSyntax != null)
                {
                    SemanticModel semanticModel = await document.GetSemanticModelAsync(context.CancellationToken).ConfigureAwait(false);

                    var typeDeclarationSymbol = semanticModel.GetDeclaredSymbol(memberSyntax);
                    if (typeDeclarationSymbol == null)
                    {
                        continue;
                    }

                    if (!this.IsValidNewMemberName(typeDeclarationSymbol, newName))
                    {
                        newName = newName + Suffix;
                    }

                    context.RegisterCodeFix(
                        CodeAction.Create(
                            string.Format(NamingResources.RenameToCodeFix, newName),
                            cancellationToken => RenameHelper.RenameSymbolAsync(document, root, token, newName, cancellationToken),
                            nameof(RenameToUpperCaseCodeFixProvider) + "_" + diagnostic.Id),
                        diagnostic);
                }
            }
        }
Example #2
0
        public override async Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            var document = context.Document;
            var root     = await document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);

            foreach (var diagnostic in context.Diagnostics)
            {
                var token = root.FindToken(diagnostic.Location.SourceSpan.Start);
                if (string.IsNullOrEmpty(token.ValueText))
                {
                    continue;
                }

                var originalName = token.ValueText;

                var baseName = originalName.TrimStart('_');
                if (baseName.Length == 0)
                {
                    // only offer a code fix if the name does not consist of only underscores.
                    continue;
                }

                baseName = char.ToLower(baseName[0]) + baseName.Substring(1);
                int underscoreCount = originalName.Length - baseName.Length;

                var memberSyntax = RenameHelper.GetParentDeclaration(token);

                SemanticModel semanticModel = await document.GetSemanticModelAsync(context.CancellationToken).ConfigureAwait(false);

                var declaredSymbol = semanticModel.GetDeclaredSymbol(memberSyntax);
                if (declaredSymbol == null)
                {
                    continue;
                }

                // preserve the underscores, but only for fields.
                var prefix  = declaredSymbol.Kind == SymbolKind.Field ? originalName.Substring(0, underscoreCount) : string.Empty;
                var newName = prefix + baseName;

                int index = 0;
                while (!await RenameHelper.IsValidNewMemberNameAsync(semanticModel, declaredSymbol, newName, context.CancellationToken).ConfigureAwait(false))
                {
                    index++;
                    newName = prefix + baseName + index;
                }

                context.RegisterCodeFix(
                    CodeAction.Create(
                        string.Format(NamingResources.RenameToCodeFix, newName),
                        cancellationToken => RenameHelper.RenameSymbolAsync(document, root, token, newName, cancellationToken),
                        nameof(RenameToLowerCaseCodeFixProvider) + "_" + underscoreCount + "_" + index),
                    diagnostic);
            }
        }
Example #3
0
        /// <inheritdoc/>
        public override async Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            var document = context.Document;
            var root     = await document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);

            foreach (var diagnostic in context.Diagnostics)
            {
                var token = root.FindToken(diagnostic.Location.SourceSpan.Start);

                var numberOfCharsToRemove = 0;

                // If a variable contains multiple prefixes that would result in this diagnostic,
                // we detect that and remove all of the bad prefixes such that after
                // the fix is applied there are no more violations of this rule.
                for (int i = 0; i < token.ValueText.Length; i += 2)
                {
                    if (string.Compare("m_", 0, token.ValueText, i, 2, StringComparison.Ordinal) == 0 ||
                        string.Compare("s_", 0, token.ValueText, i, 2, StringComparison.Ordinal) == 0 ||
                        string.Compare("t_", 0, token.ValueText, i, 2, StringComparison.Ordinal) == 0)
                    {
                        numberOfCharsToRemove += 2;
                        continue;
                    }

                    break;
                }

                if (numberOfCharsToRemove == 0)
                {
                    continue;
                }

                // The prefix is the full variable name. In this case we cannot generate a valid variable name and thus will not offer a code fix.
                if (token.ValueText.Length == numberOfCharsToRemove)
                {
                    continue;
                }

                var newName = token.ValueText.Substring(numberOfCharsToRemove);
                context.RegisterCodeFix(
                    CodeAction.Create(
                        string.Format(NamingResources.RenameToCodeFix, newName),
                        cancellationToken => RenameHelper.RenameSymbolAsync(document, root, token, newName, cancellationToken),
                        nameof(SA1308CodeFixProvider)),
                    diagnostic);
            }
        }
Example #4
0
        /// <inheritdoc/>
        public override async Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            var document = context.Document;
            var root     = await document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);

            foreach (var diagnostic in context.Diagnostics)
            {
                var token   = root.FindToken(diagnostic.Location.SourceSpan.Start);
                var newName = "I" + token.ValueText;
                context.RegisterCodeFix(
                    CodeAction.Create(
                        string.Format(NamingResources.RenameToCodeFix, newName),
                        cancellationToken => RenameHelper.RenameSymbolAsync(document, root, token, newName, cancellationToken),
                        nameof(SA1302CodeFixProvider)),
                    diagnostic);
            }
        }
Example #5
0
        private static async Task <Solution> CreateChangedSolutionAsync(Document document, Diagnostic diagnostic, CancellationToken cancellationToken)
        {
            var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);

            var token    = root.FindToken(diagnostic.Location.SourceSpan.Start);
            var baseName = "T" + token.ValueText;
            var index    = 0;
            var newName  = baseName;

            var semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);

            var declaredSymbol = semanticModel.GetDeclaredSymbol(token.Parent, cancellationToken);

            while (!await RenameHelper.IsValidNewMemberNameAsync(semanticModel, declaredSymbol, newName, cancellationToken).ConfigureAwait(false))
            {
                index++;
                newName = baseName + index;
            }

            return(await RenameHelper.RenameSymbolAsync(document, root, token, newName, cancellationToken).ConfigureAwait(false));
        }
Example #6
0
        /// <inheritdoc/>
        public override async Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            var document = context.Document;
            var root     = await document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);

            foreach (var diagnostic in context.Diagnostics)
            {
                if (!diagnostic.Id.Equals(UseAsyncSuffixAnalyzer.DiagnosticId))
                {
                    continue;
                }

                var token   = root.FindToken(diagnostic.Location.SourceSpan.Start);
                var newName = token.ValueText + "Async";
                context.RegisterCodeFix(
                    CodeAction.Create(
                        $"Rename method to '{newName}'",
                        cancellationToken => RenameHelper.RenameSymbolAsync(document, root, token, newName, cancellationToken),
                        nameof(UseAsyncSuffixCodeFixProvider)),
                    diagnostic);
            }
        }
Example #7
0
        /// <inheritdoc/>
        public override async Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            var document = context.Document;
            var root     = await document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);

            foreach (var diagnostic in context.Diagnostics)
            {
                if (!diagnostic.Id.Equals(SA1310FieldNamesMustNotContainUnderscore.DiagnosticId))
                {
                    continue;
                }

                var    token        = root.FindToken(diagnostic.Location.SourceSpan.Start);
                string currentName  = token.ValueText;
                string proposedName = BuildProposedName(currentName);
                if (proposedName != currentName)
                {
                    context.RegisterCodeFix(CodeAction.Create(string.Format(NamingResources.RenameToCodeFix, proposedName), cancellationToken => RenameHelper.RenameSymbolAsync(document, root, token, proposedName, cancellationToken), equivalenceKey: nameof(SA1310CodeFixProvider)), diagnostic);
                }
            }
        }
        /// <inheritdoc/>
        public override async Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            foreach (var diagnostic in context.Diagnostics)
            {
                if (!diagnostic.Id.Equals(SA1307AccessibleFieldsMustBeginWithUpperCaseLetter.DiagnosticId))
                {
                    continue;
                }

                var document = context.Document;
                var root     = await document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);

                var token = root.FindToken(diagnostic.Location.SourceSpan.Start);
                if (token.IsMissing)
                {
                    continue;
                }

                if (!string.IsNullOrEmpty(token.ValueText))
                {
                    var newName = char.ToUpper(token.ValueText[0]) + token.ValueText.Substring(1);
                    context.RegisterCodeFix(CodeAction.Create($"Rename field to '{newName}'", cancellationToken => RenameHelper.RenameSymbolAsync(document, root, token, newName, cancellationToken)), diagnostic);
                }
            }
        }
        /// <inheritdoc/>
        public override async Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            var document = context.Document;
            var root     = await document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);

            foreach (var diagnostic in context.Diagnostics)
            {
                if (!diagnostic.Id.Equals(SA1309FieldNamesMustNotBeginWithUnderscore.DiagnosticId))
                {
                    continue;
                }

                var token = root.FindToken(diagnostic.Location.SourceSpan.Start);
                if (token.IsMissing)
                {
                    continue;
                }

                if (!string.IsNullOrEmpty(token.ValueText))
                {
                    var newName = token.ValueText.TrimStart(new[] { '_' });

                    if (string.IsNullOrEmpty(newName))
                    {
                        // The variable consisted of only underscores. In this case we cannot
                        // generate a valid variable name and thus will not offer a code fix.
                        continue;
                    }

                    context.RegisterCodeFix(CodeAction.Create($"Rename field to '{newName}'", cancellationToken => RenameHelper.RenameSymbolAsync(document, root, token, newName, cancellationToken)), diagnostic);
                }
            }
        }
 private async Task <Solution> AddSuffixAsync(Document document, MethodDeclarationSyntax methodDeclaration, SyntaxNode root,
                                              CancellationToken cancellationToken)
 {
     return(await RenameHelper.RenameSymbolAsync(document, root, methodDeclaration.Identifier, methodDeclaration.Identifier.Text + "Async", cancellationToken));
 }
        public override async Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            var document = context.Document;
            var root     = await document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);

            foreach (var diagnostic in context.Diagnostics)
            {
                var token        = root.FindToken(diagnostic.Location.SourceSpan.Start);
                var tokenText    = token.ValueText.TrimStart('_');
                var baseName     = char.ToUpper(tokenText[0]) + tokenText.Substring(1);
                var newName      = baseName;
                var memberSyntax = RenameHelper.GetParentDeclaration(token);

                if (BaseNamespaceDeclarationSyntaxWrapper.IsInstance(memberSyntax))
                {
                    // namespaces are not symbols. So we are just renaming the namespace
                    Task <Document> RenameNamespace(CancellationToken cancellationToken)
                    {
                        IdentifierNameSyntax identifierSyntax = (IdentifierNameSyntax)token.Parent;

                        var newIdentifierSyntax = identifierSyntax.WithIdentifier(SyntaxFactory.Identifier(newName));

                        var newRoot = root.ReplaceNode(identifierSyntax, newIdentifierSyntax);

                        return(Task.FromResult(context.Document.WithSyntaxRoot(newRoot)));
                    }

                    context.RegisterCodeFix(
                        CodeAction.Create(
                            string.Format(NamingResources.RenameToCodeFix, newName),
                            (Func <CancellationToken, Task <Document> >)RenameNamespace,
                            nameof(RenameToUpperCaseCodeFixProvider) + "_" + diagnostic.Id),
                        diagnostic);
                }
                else if (memberSyntax != null)
                {
                    SemanticModel semanticModel = await document.GetSemanticModelAsync(context.CancellationToken).ConfigureAwait(false);

                    var declaredSymbol = semanticModel.GetDeclaredSymbol(memberSyntax);
                    if (declaredSymbol == null)
                    {
                        continue;
                    }

                    bool usedSuffix = false;
                    if (declaredSymbol.Kind == SymbolKind.Field &&
                        declaredSymbol.ContainingType?.TypeKind != TypeKind.Enum &&
                        !await RenameHelper.IsValidNewMemberNameAsync(semanticModel, declaredSymbol, newName, context.CancellationToken).ConfigureAwait(false))
                    {
                        usedSuffix = true;
                        newName   += Suffix;
                    }

                    int index = 0;
                    while (!await RenameHelper.IsValidNewMemberNameAsync(semanticModel, declaredSymbol, newName, context.CancellationToken).ConfigureAwait(false))
                    {
                        usedSuffix = false;
                        index++;
                        newName = baseName + index;
                    }

                    context.RegisterCodeFix(
                        CodeAction.Create(
                            string.Format(NamingResources.RenameToCodeFix, newName),
                            cancellationToken => RenameHelper.RenameSymbolAsync(document, root, token, newName, cancellationToken),
                            nameof(RenameToUpperCaseCodeFixProvider) + "_" + diagnostic.Id + "_" + usedSuffix + "_" + index),
                        diagnostic);
                }
            }
        }
        /// <inheritdoc/>
        public override async Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            var document = context.Document;
            var root     = await document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);

            foreach (var diagnostic in context.Diagnostics)
            {
                if (!diagnostic.Id.Equals(SX1309FieldNamesMustBeginWithUnderscore.DiagnosticId) &&
                    !diagnostic.Id.Equals(SX1309SStaticFieldNamesMustBeginWithUnderscore.DiagnosticId))
                {
                    continue;
                }

                var token = root.FindToken(diagnostic.Location.SourceSpan.Start);
                if (token.IsMissing)
                {
                    continue;
                }

                if (!string.IsNullOrEmpty(token.ValueText))
                {
                    string newName = '_' + token.ValueText;
                    context.RegisterCodeFix(CodeAction.Create(string.Format(NamingResources.RenameToCodeFix, newName), cancellationToken => RenameHelper.RenameSymbolAsync(document, root, token, newName, cancellationToken), equivalenceKey: nameof(SX1309CodeFixProvider)), diagnostic);
                }
            }
        }
Example #13
0
        /// <inheritdoc/>
        public override async Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            var document = context.Document;
            var root     = await document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);

            foreach (var diagnostic in context.Diagnostics)
            {
                if (!FixableDiagnostics.Any(d => diagnostic.Id.Equals(d)))
                {
                    continue;
                }

                var token = root.FindToken(diagnostic.Location.SourceSpan.Start);
                if (token.IsMissing)
                {
                    continue;
                }

                var newName    = char.ToUpper(token.ValueText[0]) + token.ValueText.Substring(1);
                var typeSyntax = this.GetParentTypeDeclaration(token);

                if (typeSyntax != null)
                {
                    SemanticModel semanticModel = await document.GetSemanticModelAsync(context.CancellationToken).ConfigureAwait(false);

                    var typeDeclarationSymbol = semanticModel.GetDeclaredSymbol(typeSyntax) as INamedTypeSymbol;
                    if (typeDeclarationSymbol == null)
                    {
                        continue;
                    }

                    if (!this.IsValidNewMemberName(typeDeclarationSymbol, newName))
                    {
                        newName = newName + Suffix;
                    }

                    context.RegisterCodeFix(CodeAction.Create($"Rename field to '{newName}'", cancellationToken => RenameHelper.RenameSymbolAsync(document, root, token, newName, cancellationToken)), diagnostic);
                }
            }
        }
Example #14
0
        /// <inheritdoc/>
        public override async Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            var document = context.Document;
            var root     = await document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);

            foreach (var diagnostic in context.Diagnostics)
            {
                if (!diagnostic.Id.Equals(SA1302InterfaceNamesMustBeginWithI.DiagnosticId))
                {
                    continue;
                }

                var token = root.FindToken(diagnostic.Location.SourceSpan.Start);
                if (token.IsMissing)
                {
                    continue;
                }

                var newName = "I" + token.ValueText;
                context.RegisterCodeFix(CodeAction.Create(string.Format(NamingResources.RenameToCodeFix, newName), cancellationToken => RenameHelper.RenameSymbolAsync(document, root, token, newName, cancellationToken)), diagnostic);
            }
        }
Example #15
0
        /// <inheritdoc/>
        public override async Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            var document = context.Document;
            var root     = await document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);

            foreach (var diagnostic in context.Diagnostics)
            {
                if (!this.FixableDiagnosticIds.Contains(diagnostic.Id))
                {
                    continue;
                }

                var token = root.FindToken(diagnostic.Location.SourceSpan.Start);
                if (!string.IsNullOrEmpty(token.ValueText))
                {
                    var newName = char.ToLower(token.ValueText[0]) + token.ValueText.Substring(1);
                    context.RegisterCodeFix(CodeAction.Create(string.Format(NamingResources.RenameToCodeFix, newName), cancellationToken => RenameHelper.RenameSymbolAsync(document, root, token, newName, cancellationToken), equivalenceKey: nameof(RenameToLowerCaseCodeFixProvider)), diagnostic);
                }
            }
        }
Example #16
0
        /// <inheritdoc/>
        public override async Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            var document = context.Document;
            var root     = await document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);

            foreach (var diagnostic in context.Diagnostics)
            {
                if (!diagnostic.Id.Equals(SA1308VariableNamesMustNotBePrefixed.DiagnosticId))
                {
                    continue;
                }

                var token = root.FindToken(diagnostic.Location.SourceSpan.Start);

                if (token.IsMissing)
                {
                    continue;
                }

                // The variable name is the full suffix. In this case we cannot generate a valid variable name and thus will not offer a code fix.
                if (token.ValueText.Length <= 2)
                {
                    continue;
                }

                var numberOfCharsToRemove = 2;

                // If a variable contains multiple prefixes that would result in this diagnostic,
                // we detect that and remove all of the bad prefixes such that after
                // the fix is applied there are no more violations of this rule.
                for (int i = 2; i < token.ValueText.Length; i += 2)
                {
                    if (string.Compare("m_", 0, token.ValueText, i, 2, StringComparison.Ordinal) == 0 ||
                        string.Compare("s_", 0, token.ValueText, i, 2, StringComparison.Ordinal) == 0)
                    {
                        numberOfCharsToRemove += 2;
                        continue;
                    }

                    break;
                }

                if (!string.IsNullOrEmpty(token.ValueText))
                {
                    var newName = token.ValueText.Substring(numberOfCharsToRemove);
                    context.RegisterCodeFix(CodeAction.Create($"Rename field to '{newName}'", cancellationToken => RenameHelper.RenameSymbolAsync(document, root, token, newName, cancellationToken)), diagnostic);
                }
            }
        }