-
Notifications
You must be signed in to change notification settings - Fork 147
/
Copy pathThreadAbortCodeFixProvider.cs
75 lines (66 loc) · 3.21 KB
/
ThreadAbortCodeFixProvider.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// <copyright file="ThreadAbortCodeFixProvider.cs" company="Datadog">
// Unless explicitly stated otherwise all files in this repository are licensed under the Apache 2 License.
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2017 Datadog, Inc.
// </copyright>
using System.Collections.Immutable;
using System.Composition;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
namespace Datadog.Trace.Tools.Analyzers.ThreadAbortAnalyzer
{
/// <summary>
/// A CodeFixProvider for the <see cref="ThreadAbortAnalyzer"/>
/// </summary>
[ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(ThreadAbortCodeFixProvider))]
[Shared]
public class ThreadAbortCodeFixProvider : CodeFixProvider
{
/// <inheritdoc />
public sealed override ImmutableArray<string> FixableDiagnosticIds
{
get { return ImmutableArray.Create(ThreadAbortAnalyzer.DiagnosticId); }
}
/// <inheritdoc />
public sealed override FixAllProvider GetFixAllProvider()
{
// See https://github.com/dotnet/roslyn/blob/master/docs/analyzers/FixAllProvider.md for more information on Fix All Providers
return WellKnownFixAllProviders.BatchFixer;
}
/// <inheritdoc />
public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
{
var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);
var diagnostic = context.Diagnostics.First();
var diagnosticSpan = diagnostic.Location.SourceSpan;
// Find the while block catch declaration identified by the diagnostic.
var catchClause = root.FindToken(diagnosticSpan.Start)
.Parent
.AncestorsAndSelf()
.OfType<CatchClauseSyntax>().First();
// Register a code action that will invoke the fix.
context.RegisterCodeFix(
CodeAction.Create(
title: "Rethrow exception",
createChangedDocument: c => AddThrowStatement(context.Document, catchClause, c),
equivalenceKey: nameof(ThreadAbortCodeFixProvider)),
diagnostic);
}
private static async Task<Document> AddThrowStatement(Document document, CatchClauseSyntax catchBlock, CancellationToken cancellationToken)
{
// This messes with the whitespace, but it's a PITA to get that right
var throwStatement = SyntaxFactory.ThrowStatement();
var statements = catchBlock.Block.Statements.Add(throwStatement);
var newCatchBlock = catchBlock.Block.WithStatements(statements);
// replace the syntax and return updated document
var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
root = root.ReplaceNode(catchBlock.Block, newCatchBlock);
return document.WithSyntaxRoot(root);
}
}
}