コード例 #1
0
        /// <inheritdoc />
        public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            var classDeclarationCodeFixHelper = new ClassDeclarationCodeFixHelper(context);

            var diagnostic       = classDeclarationCodeFixHelper.GetFirstDiagnostic(FixableDiagnosticIds.ToArray());
            var classDeclaration = await classDeclarationCodeFixHelper.GetDiagnosedClassDeclarationSyntax(diagnostic);

            if (classDeclaration == null)
            {
                return;
            }

            var className = classDeclaration.Identifier.ToString();

            var containingNamespaceNode = classDeclaration
                                          .AncestorsAndSelf()
                                          .OfType <NamespaceDeclarationSyntax>()
                                          .FirstOrDefault();

            var containingNamespaceName = containingNamespaceNode?.Name.ToString();

            var attributeToBeAdded = GetAttributeListToBeAdded(containingNamespaceName, className);

            context.RegisterCodeFix(
                CodeAction.Create(
                    title: string.Format(CmsBaseClassesResources.ModuleRegistration_CodeFix, className),
                    createChangedDocument: c => classDeclarationCodeFixHelper.ApplyRootModification(oldRoot => oldRoot.AddAttributeLists(attributeToBeAdded), c, "CMS"),
                    equivalenceKey: nameof(ModuleRegistrationCodeFixProvider)),
                diagnostic);
        }
コード例 #2
0
        /// <inheritdoc />
        public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            var baseTypeCodeFixHelper = new ClassDeclarationCodeFixHelper(context);

            var diagnostic       = baseTypeCodeFixHelper.GetFirstDiagnostic(FixableDiagnosticIds.ToArray());
            var classDeclaration = await baseTypeCodeFixHelper.GetDiagnosedClassDeclarationSyntax(diagnostic);

            if (classDeclaration == null)
            {
                return;
            }

            var suggestions = diagnostic.Id == DiagnosticIds.UIWebPartBase
                ? UiWebPartBaseClasses
                : WebPartBaseClasses;

            foreach (var classAndItsNamespace in suggestions)
            {
                var newClassDeclaration = classDeclaration.WithBaseClass(classAndItsNamespace.ClassName);
                context.RegisterCodeFix(
                    CodeAction.Create(
                        title: CodeFixMessagesProvider.GetInheritFromMessage(classAndItsNamespace.ClassName),
                        createChangedDocument: c => baseTypeCodeFixHelper.ReplaceExpressionWith(classDeclaration, newClassDeclaration, c, classAndItsNamespace.ClassNamespace),
                        equivalenceKey: nameof(WebPartBaseCodeFixProvider) + classAndItsNamespace.ClassName),
                    diagnostic);
            }
        }