예제 #1
0
        public override async Task ComputeRefactoringsAsync(CodeRefactoringContext context)
        {
            var(document, classDeclarationSyntax) = await context.GetClassIfOnClassIdentifier();

            if (classDeclarationSyntax == null)
            {
                return;
            }

            if (!ClassDeclarationSyntaxAnalysis.CanBeDiscriminatedUnionBaseType(classDeclarationSyntax))
            {
                return;
            }

            if (classDeclarationSyntax.Parent == null ||
                (!classDeclarationSyntax.Parent.IsKind(SyntaxKind.NamespaceDeclaration) &&
                 !classDeclarationSyntax.Parent.IsKind(SyntaxKind.CompilationUnit)))
            {
                return;
            }

            var duMembers = GetCandidateMethods(classDeclarationSyntax);

            if (duMembers.Count == 0)
            {
                return;
            }

            context.RegisterRefactoring(
                new DelegateCodeAction(
                    "Generate discriminated union from static methods",
                    (c) => GenerateDiscriminatedUnion(document, classDeclarationSyntax, c)));
        }