Exemplo n.º 1
0
        public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            var solution = context.Document.Project.Solution;
            var model    = await context.Document.GetSemanticModelAsync(context.CancellationToken).ConfigureAwait(false) ?? throw new Exception("Semantic model is not found");

            var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false) ?? throw new Exception("Syntax root is not found");

            var location = context.Span;
            var symbol   = CodeAnalysisUtils.FindSymbol(model, root, location, context.CancellationToken);

            if (symbol == null)
            {
                return;
            }

            foreach (var diagnostic in context.Diagnostics)
            {
                if (diagnostic.Id == ExampleAnalyzer0000.Rule.Id)
                {
                    RegisterCodeFixesFor0000(context, diagnostic, solution, symbol);
                }
                if (diagnostic.Id == ExampleAnalyzer0001.Rule.Id)
                {
                    RegisterCodeFixesFor0001(context, diagnostic, solution, symbol);
                }
            }
        }
Exemplo n.º 2
0
        public sealed override async Task ComputeRefactoringsAsync(CodeRefactoringContext context)
        {
            var solution = context.Document.Project.Solution;
            var model    = await context.Document.GetSemanticModelAsync(context.CancellationToken).ConfigureAwait(false) ?? throw new Exception("Semantic model is not found");

            var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false) ?? throw new Exception("Syntax root is not found");

            var span    = context.Span;
            var symbols = CodeAnalysisUtils.FindSymbols(model, root, span, context.CancellationToken).Where(CodeAnalysisUtils.CanBeRenamed).Reverse().ToArray();

            if (!symbols.Any())
            {
                return;
            }

            context.RegisterRefactoring($"Make symbols '{symbols.Join( i => i.Name )}' start/end with underscore ({GetType().Name})", Action);
            context.RegisterRefactoring($"Make symbols '{symbols.Join( i => i.Name )}' start/end with double underscore ({GetType().Name})", Action2);

            async Task <Solution> Action(CancellationToken cancellationToken)
            {
                return(await WorkspacesUtils.WithFormattedSymbols(solution, symbols, "_{0}_", cancellationToken).ConfigureAwait(false));
            }

            async Task <Solution> Action2(CancellationToken cancellationToken)
            {
                return(await WorkspacesUtils.WithFormattedSymbols(solution, symbols, "__{0}__", cancellationToken).ConfigureAwait(false));
            }
        }
        // Struct
        //public override SyntaxNode? VisitStructDeclaration(StructDeclarationSyntax node) {
        //    return base.VisitStructDeclaration( node );
        //}
        // Record
        //public override SyntaxNode? VisitRecordDeclaration(RecordDeclarationSyntax node) {
        //    return base.VisitRecordDeclaration( node );
        //}


        // Helpers
        internal static bool IsSupported(ClassDeclarationSyntax node)
        {
            return(!CodeAnalysisUtils.IsStatic(node) && CodeAnalysisUtils.IsPartial(node));
        }