コード例 #1
0
        public static async Task ComputeRefactoringsAsync(RefactoringContext context, FieldDeclarationSyntax fieldDeclaration)
        {
            if (fieldDeclaration.IsConst())
            {
                if (context.IsRefactoringEnabled(RefactoringIdentifiers.ReplaceConstantWithField) &&
                    fieldDeclaration.Span.Contains(context.Span))
                {
                    context.RegisterRefactoring(
                        "Replace constant with field",
                        cancellationToken => ReplaceConstantWithFieldRefactoring.RefactorAsync(context.Document, fieldDeclaration, cancellationToken));
                }
            }
            else if (context.IsRefactoringEnabled(RefactoringIdentifiers.ReplaceFieldWithConstant) &&
                     fieldDeclaration.Modifiers.Contains(SyntaxKind.ReadOnlyKeyword) &&
                     fieldDeclaration.IsStatic() &&
                     fieldDeclaration.Span.Contains(context.Span))
            {
                if (await ReplaceFieldWithConstantRefactoring.CanRefactorAsync(context, fieldDeclaration).ConfigureAwait(false))
                {
                    context.RegisterRefactoring(
                        "Replace field with constant",
                        cancellationToken => ReplaceFieldWithConstantRefactoring.RefactorAsync(context.Document, fieldDeclaration, cancellationToken));
                }
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.MarkMemberAsStatic) &&
                fieldDeclaration.Span.Contains(context.Span) &&
                MarkMemberAsStaticRefactoring.CanRefactor(fieldDeclaration))
            {
                context.RegisterRefactoring(
                    "Mark field as static",
                    cancellationToken => MarkMemberAsStaticRefactoring.RefactorAsync(context.Document, fieldDeclaration, cancellationToken));
            }
        }
コード例 #2
0
        public static async Task ComputeRefactoringsAsync(RefactoringContext context, FieldDeclarationSyntax fieldDeclaration)
        {
            if (fieldDeclaration.Modifiers.Contains(SyntaxKind.ConstKeyword))
            {
                if (context.IsRefactoringEnabled(RefactoringIdentifiers.ReplaceConstantWithField) &&
                    fieldDeclaration.Span.Contains(context.Span))
                {
                    context.RegisterRefactoring(
                        "Replace constant with field",
                        cancellationToken => ReplaceConstantWithFieldRefactoring.RefactorAsync(context.Document, fieldDeclaration, cancellationToken),
                        RefactoringIdentifiers.ReplaceConstantWithField);
                }

                if (context.IsRefactoringEnabled(RefactoringIdentifiers.InlineConstant) &&
                    !fieldDeclaration.ContainsDiagnostics)
                {
                    VariableDeclaratorSyntax variableDeclarator = fieldDeclaration
                                                                  .Declaration?
                                                                  .Variables
                                                                  .FirstOrDefault(f => context.Span.IsEmptyAndContainedInSpanOrBetweenSpans(f.Identifier));

                    if (variableDeclarator != null)
                    {
                        context.RegisterRefactoring(
                            "Inline constant",
                            cancellationToken => InlineConstantRefactoring.RefactorAsync(context.Document, fieldDeclaration, variableDeclarator, cancellationToken),
                            RefactoringIdentifiers.InlineConstant);
                    }
                }
            }
            else if (context.IsRefactoringEnabled(RefactoringIdentifiers.UseConstantInsteadOfField) &&
                     context.Span.IsEmptyAndContainedInSpanOrBetweenSpans(fieldDeclaration))
            {
                SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false);

                if (UseConstantInsteadOfFieldAnalysis.IsFixable(fieldDeclaration, semanticModel, onlyPrivate: false, cancellationToken: context.CancellationToken))
                {
                    context.RegisterRefactoring(
                        "Use constant instead of field",
                        cancellationToken => UseConstantInsteadOfFieldRefactoring.RefactorAsync(context.Document, fieldDeclaration, cancellationToken),
                        RefactoringIdentifiers.UseConstantInsteadOfField);
                }
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.InitializeFieldFromConstructor))
            {
                InitializeFieldFromConstructorRefactoring.ComputeRefactoring(context, fieldDeclaration);
            }
        }