Skip to content

Commit c40a992

Browse files
authored
Merge pull request #56 from sharwell/inline-code-resolver
Add inline code analyzers
2 parents d33668c + 9e41513 commit c40a992

25 files changed

+1884
-0
lines changed

DocumentationAnalyzers/DocumentationAnalyzers.CodeFixes/Helpers/XmlSyntaxFactory.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,11 @@ public static XmlEmptyElementSyntax ParamRefElement(string parameterName)
268268
return EmptyElement("paramref").AddAttributes(NameAttribute(parameterName));
269269
}
270270

271+
public static XmlEmptyElementSyntax TypeParamRefElement(string parameterName)
272+
{
273+
return EmptyElement(XmlCommentHelper.TypeParamRefXmlTag).AddAttributes(NameAttribute(parameterName));
274+
}
275+
271276
public static XmlEmptyElementSyntax SeeElement(CrefSyntax cref)
272277
{
273278
return EmptyElement("see").AddAttributes(CrefAttribute(cref));
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
2+
// Licensed under the MIT license. See LICENSE in the project root for license information.
3+
4+
namespace DocumentationAnalyzers.StyleRules
5+
{
6+
using System.Collections.Immutable;
7+
using System.Composition;
8+
using System.Diagnostics;
9+
using System.Threading;
10+
using System.Threading.Tasks;
11+
using DocumentationAnalyzers.Helpers;
12+
using Microsoft.CodeAnalysis;
13+
using Microsoft.CodeAnalysis.CodeActions;
14+
using Microsoft.CodeAnalysis.CodeFixes;
15+
using Microsoft.CodeAnalysis.CSharp.Syntax;
16+
17+
[ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(DOC104CodeFixProvider))]
18+
[Shared]
19+
internal class DOC104CodeFixProvider : CodeFixProvider
20+
{
21+
public override ImmutableArray<string> FixableDiagnosticIds { get; }
22+
= ImmutableArray.Create(DOC104UseSeeLangword.DiagnosticId);
23+
24+
public override FixAllProvider GetFixAllProvider()
25+
=> CustomFixAllProviders.BatchFixer;
26+
27+
public override Task RegisterCodeFixesAsync(CodeFixContext context)
28+
{
29+
foreach (var diagnostic in context.Diagnostics)
30+
{
31+
Debug.Assert(FixableDiagnosticIds.Contains(diagnostic.Id), "Assertion failed: FixableDiagnosticIds.Contains(diagnostic.Id)");
32+
33+
context.RegisterCodeFix(
34+
CodeAction.Create(
35+
StyleResources.DOC104CodeFix,
36+
token => GetTransformedDocumentAsync(context.Document, diagnostic, token),
37+
nameof(DOC104CodeFixProvider)),
38+
diagnostic);
39+
}
40+
41+
return SpecializedTasks.CompletedTask;
42+
}
43+
44+
private static async Task<Document> GetTransformedDocumentAsync(Document document, Diagnostic diagnostic, CancellationToken cancellationToken)
45+
{
46+
SyntaxNode root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
47+
var xmlElement = (XmlElementSyntax)root.FindNode(diagnostic.Location.SourceSpan, findInsideTrivia: true, getInnermostNodeForTie: true);
48+
49+
var newXmlElement = XmlSyntaxFactory.EmptyElement(XmlCommentHelper.SeeXmlTag)
50+
.AddAttributes(XmlSyntaxFactory.TextAttribute("langword", xmlElement.Content.ToFullString()))
51+
.WithTriviaFrom(xmlElement);
52+
53+
return document.WithSyntaxRoot(root.ReplaceNode(xmlElement, newXmlElement));
54+
}
55+
}
56+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
2+
// Licensed under the MIT license. See LICENSE in the project root for license information.
3+
4+
namespace DocumentationAnalyzers.StyleRules
5+
{
6+
using System.Collections.Immutable;
7+
using System.Composition;
8+
using System.Diagnostics;
9+
using System.Threading;
10+
using System.Threading.Tasks;
11+
using DocumentationAnalyzers.Helpers;
12+
using Microsoft.CodeAnalysis;
13+
using Microsoft.CodeAnalysis.CodeActions;
14+
using Microsoft.CodeAnalysis.CodeFixes;
15+
using Microsoft.CodeAnalysis.CSharp.Syntax;
16+
17+
[ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(DOC105CodeFixProvider))]
18+
[Shared]
19+
internal class DOC105CodeFixProvider : CodeFixProvider
20+
{
21+
public override ImmutableArray<string> FixableDiagnosticIds { get; }
22+
= ImmutableArray.Create(DOC105UseParamref.DiagnosticId);
23+
24+
public override FixAllProvider GetFixAllProvider()
25+
=> CustomFixAllProviders.BatchFixer;
26+
27+
public override Task RegisterCodeFixesAsync(CodeFixContext context)
28+
{
29+
foreach (var diagnostic in context.Diagnostics)
30+
{
31+
Debug.Assert(FixableDiagnosticIds.Contains(diagnostic.Id), "Assertion failed: FixableDiagnosticIds.Contains(diagnostic.Id)");
32+
33+
context.RegisterCodeFix(
34+
CodeAction.Create(
35+
StyleResources.DOC105CodeFix,
36+
token => GetTransformedDocumentAsync(context.Document, diagnostic, token),
37+
nameof(DOC105CodeFixProvider)),
38+
diagnostic);
39+
}
40+
41+
return SpecializedTasks.CompletedTask;
42+
}
43+
44+
private static async Task<Document> GetTransformedDocumentAsync(Document document, Diagnostic diagnostic, CancellationToken cancellationToken)
45+
{
46+
SyntaxNode root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
47+
var xmlElement = (XmlElementSyntax)root.FindNode(diagnostic.Location.SourceSpan, findInsideTrivia: true, getInnermostNodeForTie: true);
48+
49+
var newXmlElement = XmlSyntaxFactory.ParamRefElement(xmlElement.Content.ToFullString()).WithTriviaFrom(xmlElement);
50+
51+
return document.WithSyntaxRoot(root.ReplaceNode(xmlElement, newXmlElement));
52+
}
53+
}
54+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
2+
// Licensed under the MIT license. See LICENSE in the project root for license information.
3+
4+
namespace DocumentationAnalyzers.StyleRules
5+
{
6+
using System.Collections.Immutable;
7+
using System.Composition;
8+
using System.Diagnostics;
9+
using System.Threading;
10+
using System.Threading.Tasks;
11+
using DocumentationAnalyzers.Helpers;
12+
using Microsoft.CodeAnalysis;
13+
using Microsoft.CodeAnalysis.CodeActions;
14+
using Microsoft.CodeAnalysis.CodeFixes;
15+
using Microsoft.CodeAnalysis.CSharp.Syntax;
16+
17+
[ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(DOC106CodeFixProvider))]
18+
[Shared]
19+
internal class DOC106CodeFixProvider : CodeFixProvider
20+
{
21+
public override ImmutableArray<string> FixableDiagnosticIds { get; }
22+
= ImmutableArray.Create(DOC106UseTypeparamref.DiagnosticId);
23+
24+
public override FixAllProvider GetFixAllProvider()
25+
=> CustomFixAllProviders.BatchFixer;
26+
27+
public override Task RegisterCodeFixesAsync(CodeFixContext context)
28+
{
29+
foreach (var diagnostic in context.Diagnostics)
30+
{
31+
Debug.Assert(FixableDiagnosticIds.Contains(diagnostic.Id), "Assertion failed: FixableDiagnosticIds.Contains(diagnostic.Id)");
32+
33+
context.RegisterCodeFix(
34+
CodeAction.Create(
35+
StyleResources.DOC106CodeFix,
36+
token => GetTransformedDocumentAsync(context.Document, diagnostic, token),
37+
nameof(DOC106CodeFixProvider)),
38+
diagnostic);
39+
}
40+
41+
return SpecializedTasks.CompletedTask;
42+
}
43+
44+
private static async Task<Document> GetTransformedDocumentAsync(Document document, Diagnostic diagnostic, CancellationToken cancellationToken)
45+
{
46+
SyntaxNode root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
47+
var xmlElement = (XmlElementSyntax)root.FindNode(diagnostic.Location.SourceSpan, findInsideTrivia: true, getInnermostNodeForTie: true);
48+
49+
var newXmlElement = XmlSyntaxFactory.TypeParamRefElement(xmlElement.Content.ToFullString()).WithTriviaFrom(xmlElement);
50+
51+
return document.WithSyntaxRoot(root.ReplaceNode(xmlElement, newXmlElement));
52+
}
53+
}
54+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
2+
// Licensed under the MIT license. See LICENSE in the project root for license information.
3+
4+
namespace DocumentationAnalyzers.StyleRules
5+
{
6+
using System.Collections.Immutable;
7+
using System.Composition;
8+
using System.Diagnostics;
9+
using System.Threading;
10+
using System.Threading.Tasks;
11+
using DocumentationAnalyzers.Helpers;
12+
using Microsoft.CodeAnalysis;
13+
using Microsoft.CodeAnalysis.CodeActions;
14+
using Microsoft.CodeAnalysis.CodeFixes;
15+
using Microsoft.CodeAnalysis.CSharp;
16+
using Microsoft.CodeAnalysis.CSharp.Syntax;
17+
18+
[ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(DOC107CodeFixProvider))]
19+
[Shared]
20+
internal class DOC107CodeFixProvider : CodeFixProvider
21+
{
22+
public override ImmutableArray<string> FixableDiagnosticIds { get; }
23+
= ImmutableArray.Create(DOC107UseSeeCref.DiagnosticId);
24+
25+
public override FixAllProvider GetFixAllProvider()
26+
=> CustomFixAllProviders.BatchFixer;
27+
28+
public override Task RegisterCodeFixesAsync(CodeFixContext context)
29+
{
30+
foreach (var diagnostic in context.Diagnostics)
31+
{
32+
Debug.Assert(FixableDiagnosticIds.Contains(diagnostic.Id), "Assertion failed: FixableDiagnosticIds.Contains(diagnostic.Id)");
33+
34+
context.RegisterCodeFix(
35+
CodeAction.Create(
36+
StyleResources.DOC107CodeFix,
37+
token => GetTransformedDocumentAsync(context.Document, diagnostic, token),
38+
nameof(DOC107CodeFixProvider)),
39+
diagnostic);
40+
}
41+
42+
return SpecializedTasks.CompletedTask;
43+
}
44+
45+
private static async Task<Document> GetTransformedDocumentAsync(Document document, Diagnostic diagnostic, CancellationToken cancellationToken)
46+
{
47+
SyntaxNode root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
48+
var xmlElement = (XmlElementSyntax)root.FindNode(diagnostic.Location.SourceSpan, findInsideTrivia: true, getInnermostNodeForTie: true);
49+
50+
var newXmlElement = XmlSyntaxFactory.EmptyElement(XmlCommentHelper.SeeXmlTag)
51+
.AddAttributes(XmlSyntaxFactory.TextAttribute(
52+
"cref",
53+
SyntaxFactory.Token(SyntaxTriviaList.Empty, SyntaxKind.XmlTextLiteralToken, xmlElement.Content.ToFullString(), xmlElement.Content.ToFullString(), SyntaxTriviaList.Empty)))
54+
.WithTriviaFrom(xmlElement);
55+
56+
return document.WithSyntaxRoot(root.ReplaceNode(xmlElement, newXmlElement));
57+
}
58+
}
59+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
2+
// Licensed under the MIT license. See LICENSE in the project root for license information.
3+
4+
namespace DocumentationAnalyzers.Test.CSharp7.StyleRules
5+
{
6+
using DocumentationAnalyzers.Test.StyleRules;
7+
8+
public class DOC104CSharp7UnitTests : DOC104UnitTests
9+
{
10+
}
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
2+
// Licensed under the MIT license. See LICENSE in the project root for license information.
3+
4+
namespace DocumentationAnalyzers.Test.CSharp7.StyleRules
5+
{
6+
using DocumentationAnalyzers.Test.StyleRules;
7+
8+
public class DOC105CSharp7UnitTests : DOC105UnitTests
9+
{
10+
}
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
2+
// Licensed under the MIT license. See LICENSE in the project root for license information.
3+
4+
namespace DocumentationAnalyzers.Test.CSharp7.StyleRules
5+
{
6+
using DocumentationAnalyzers.Test.StyleRules;
7+
8+
public class DOC106CSharp7UnitTests : DOC106UnitTests
9+
{
10+
}
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
2+
// Licensed under the MIT license. See LICENSE in the project root for license information.
3+
4+
namespace DocumentationAnalyzers.Test.CSharp7.StyleRules
5+
{
6+
using DocumentationAnalyzers.Test.StyleRules;
7+
8+
public class DOC107CSharp7UnitTests : DOC107UnitTests
9+
{
10+
}
11+
}

0 commit comments

Comments
 (0)