コード例 #1
0
ファイル: Program.cs プロジェクト: wk-j/dotnet-code-fix
        static void Test1(Document document, CSharpCompilation compilation)
        {
            var analyzer = CreateAnalyzer().ElementAt(0);

            var all   = compilation.WithAnalyzers(ImmutableArray.Create(analyzer)).GetAllDiagnosticsAsync().Result;
            var first = all.Skip(1).First();
            var span  = first.Location.SourceSpan;

            Console.WriteLine($"All = {string.Join(",", all.Select(x => x.GetMessage()))}");
            Console.WriteLine($"First = {first}");
            Console.WriteLine($"Span = {span}");

            var actions = new List <CodeAction>();

            var context = new CodeFixContext(document, first, (c, d) => {
                Console.WriteLine(c);
                Console.WriteLine(d);
                actions.Add(c);
            }, CancellationToken.None);

            var provider = new AsyncMethodWithoutAsyncSuffixCodeFix();

            provider.RegisterCodeFixesAsync(context).Wait();

            var operations = actions[0].GetOperationsAsync(CancellationToken.None).Result;
            var solution   = operations.OfType <ApplyChangesOperation>().Single().ChangedSolution;
            var newDoc     = solution.GetDocument(document.Id);

            Console.WriteLine(newDoc.GetTextAsync().Result);
        }
コード例 #2
0
    static async Task Process(Document document, Diagnostic dig)
    {
        var actions = new List <CodeAction>();
        var context = new CodeFixContext(document, dig, (c, d) => {
            actions.Add(c);
        }, CancellationToken.None);

        var provider = new AsyncMethodWithoutAsyncSuffixCodeFix();

        provider.RegisterCodeFixesAsync(context).Wait();

        foreach (var action in actions)
        {
            var operations = await action.GetOperationsAsync(CancellationToken.None);

            foreach (var operation in operations.OfType <ApplyChangesOperation>())
            {
                var solution = operation.ChangedSolution;
                var newDoc   = solution.GetDocument(document.Id);
            }
        }
    }