Beispiel #1
0
        public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            SyntaxNode root = await context.GetSyntaxRootAsync().ConfigureAwait(false);

            EndRegionDirectiveTriviaSyntax endRegionDirective = root
                                                                .FindNode(context.Span, findInsideTrivia: true, getInnermostNodeForTie: true)?
                                                                .FirstAncestorOrSelf <EndRegionDirectiveTriviaSyntax>();

            Debug.Assert(endRegionDirective != null, $"{nameof(endRegionDirective)} is null");

            if (endRegionDirective == null)
            {
                return;
            }

            RegionDirectiveTriviaSyntax regionDirective = endRegionDirective.GetRegionDirective();

            SyntaxTrivia trivia = regionDirective.GetPreprocessingMessageTrivia();

            CodeAction codeAction = CodeAction.Create(
                (trivia.IsKind(SyntaxKind.PreprocessingMessageTrivia))
                    ? "Add region name to #endregion"
                    : "Remove region name from #endregion",
                cancellationToken => AddOrRemoveRegionNameRefactoring.RefactorAsync(context.Document, endRegionDirective, trivia, cancellationToken),
                DiagnosticIdentifiers.AddOrRemoveRegionName + EquivalenceKeySuffix);

            context.RegisterCodeFix(codeAction, context.Diagnostics);
        }
        private static void AnalyzeEndRegionDirectiveTrivia(SyntaxNodeAnalysisContext context)
        {
            var endRegionDirective = (EndRegionDirectiveTriviaSyntax)context.Node;

            RegionDirectiveTriviaSyntax regionDirective = endRegionDirective.GetRegionDirective();

            if (regionDirective == null)
            {
                return;
            }

            SyntaxTrivia trivia = regionDirective.GetPreprocessingMessageTrivia();

            SyntaxTrivia endTrivia = endRegionDirective.GetPreprocessingMessageTrivia();

            if (trivia.IsKind(SyntaxKind.PreprocessingMessageTrivia))
            {
                if (!endTrivia.IsKind(SyntaxKind.PreprocessingMessageTrivia) ||
                    !string.Equals(trivia.ToString(), endTrivia.ToString(), StringComparison.Ordinal))
                {
                    DiagnosticHelpers.ReportDiagnostic(context, DiagnosticRules.AddOrRemoveRegionName, endRegionDirective, "Add", "to");
                }
            }
            else if (endTrivia.IsKind(SyntaxKind.PreprocessingMessageTrivia))
            {
                DiagnosticHelpers.ReportDiagnostic(context, DiagnosticRules.AddOrRemoveRegionName, endRegionDirective, "Remove", "from");
            }
        }
        public static void AnalyzeEndRegionDirectiveTrivia(SyntaxNodeAnalysisContext context)
        {
            var endRegionDirective = (EndRegionDirectiveTriviaSyntax)context.Node;

            RegionDirectiveTriviaSyntax regionDirective = endRegionDirective.GetRegionDirective();

            if (regionDirective == null)
            {
                return;
            }

            SyntaxTrivia trivia = regionDirective.GetPreprocessingMessageTrivia();

            SyntaxTrivia endTrivia = endRegionDirective.GetPreprocessingMessageTrivia();

            if (trivia.Kind() == SyntaxKind.PreprocessingMessageTrivia)
            {
                if (endTrivia.Kind() != SyntaxKind.PreprocessingMessageTrivia ||
                    !string.Equals(trivia.ToString(), endTrivia.ToString(), StringComparison.Ordinal))
                {
                    context.ReportDiagnostic(DiagnosticDescriptors.AddOrRemoveRegionName, endRegionDirective, "Add", "to");
                }
            }
            else if (endTrivia.Kind() == SyntaxKind.PreprocessingMessageTrivia)
            {
                context.ReportDiagnostic(DiagnosticDescriptors.AddOrRemoveRegionName, endRegionDirective, "Remove", "from");
            }
        }
        public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            SyntaxNode root = await context.GetSyntaxRootAsync().ConfigureAwait(false);

            if (!TryFindFirstAncestorOrSelf(root, context.Span, out EndRegionDirectiveTriviaSyntax endRegionDirective, findInsideTrivia: true))
            {
                return;
            }

            RegionDirectiveTriviaSyntax regionDirective = endRegionDirective.GetRegionDirective();

            SyntaxTrivia trivia = regionDirective.GetPreprocessingMessageTrivia();

            CodeAction codeAction = CodeAction.Create(
                (trivia.IsKind(SyntaxKind.PreprocessingMessageTrivia))
                    ? "Add region name to #endregion"
                    : "Remove region name from #endregion",
                cancellationToken => AddOrRemoveRegionNameRefactoring.RefactorAsync(context.Document, endRegionDirective, trivia, cancellationToken),
                GetEquivalenceKey(DiagnosticIdentifiers.AddOrRemoveRegionName));

            context.RegisterCodeFix(codeAction, context.Diagnostics);
        }