-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add initial implementation of DOC206 (Synchronize documentation)
- Loading branch information
Showing
11 changed files
with
457 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
...ationAnalyzers/DocumentationAnalyzers.CodeFixes/PortabilityRules/DOC206CodeFixProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. | ||
// Licensed under the MIT license. See LICENSE in the project root for license information. | ||
|
||
namespace DocumentationAnalyzers.PortabilityRules | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Collections.Immutable; | ||
using System.Composition; | ||
using System.Diagnostics; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using System.Xml.Linq; | ||
using DocumentationAnalyzers.Helpers; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CodeActions; | ||
using Microsoft.CodeAnalysis.CodeFixes; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
|
||
[ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(DOC206CodeFixProvider))] | ||
[Shared] | ||
internal class DOC206CodeFixProvider : CodeFixProvider | ||
{ | ||
public override ImmutableArray<string> FixableDiagnosticIds { get; } | ||
= ImmutableArray.Create(DOC206SynchronizeDocumentation.DiagnosticId); | ||
|
||
public override FixAllProvider GetFixAllProvider() | ||
=> CustomFixAllProviders.BatchFixer; | ||
|
||
public override Task RegisterCodeFixesAsync(CodeFixContext context) | ||
{ | ||
foreach (var diagnostic in context.Diagnostics) | ||
{ | ||
Debug.Assert(FixableDiagnosticIds.Contains(diagnostic.Id), "Assertion failed: FixableDiagnosticIds.Contains(diagnostic.Id)"); | ||
|
||
context.RegisterCodeFix( | ||
CodeAction.Create( | ||
PortabilityResources.DOC206CodeFix, | ||
token => GetTransformedDocumentAsync(context.Document, diagnostic, token), | ||
nameof(DOC206CodeFixProvider)), | ||
diagnostic); | ||
} | ||
|
||
return SpecializedTasks.CompletedTask; | ||
} | ||
|
||
private static async Task<Document> GetTransformedDocumentAsync(Document document, Diagnostic diagnostic, CancellationToken cancellationToken) | ||
{ | ||
SyntaxNode root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false); | ||
var xmlNode = (XmlNodeSyntax)root.FindNode(diagnostic.Location.SourceSpan, findInsideTrivia: true, getInnermostNodeForTie: true); | ||
var oldStartToken = xmlNode.GetName().LocalName; | ||
|
||
var semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false); | ||
var documentedSymbol = semanticModel.GetDeclaredSymbol(xmlNode.FirstAncestorOrSelf<SyntaxNode>(SyntaxNodeExtensionsEx.IsSymbolDeclaration), cancellationToken); | ||
var candidateSymbol = InheritdocHelper.GetCandidateSymbol(documentedSymbol); | ||
var candidateDocumentation = candidateSymbol.GetDocumentationCommentXml(expandIncludes: false, cancellationToken: cancellationToken); | ||
|
||
var xmlDocumentation = XElement.Parse(candidateDocumentation); | ||
var newLineText = Environment.NewLine; | ||
|
||
var content = new List<XmlNodeSyntax>(); | ||
content.AddRange(xmlDocumentation.Elements().Select(element => XmlSyntaxFactory.Node(newLineText, element))); | ||
content.Add(XmlSyntaxFactory.NewLine(newLineText)); | ||
content.Add(xmlNode); | ||
|
||
return document.WithSyntaxRoot(root.ReplaceNode( | ||
xmlNode.FirstAncestorOrSelf<DocumentationCommentTriviaSyntax>(), | ||
XmlSyntaxFactory.DocumentationComment(newLineText, content.ToArray()))); | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...nAnalyzers/DocumentationAnalyzers.Test.CSharp7/PortabilityRules/DOC206CSharp7UnitTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. | ||
// Licensed under the MIT license. See LICENSE in the project root for license information. | ||
|
||
namespace DocumentationAnalyzers.Test.CSharp7.PortabilityRules | ||
{ | ||
using DocumentationAnalyzers.Test.PortabilityRules; | ||
|
||
public class DOC206CSharp7UnitTests : DOC206UnitTests | ||
{ | ||
} | ||
} |
112 changes: 112 additions & 0 deletions
112
DocumentationAnalyzers/DocumentationAnalyzers.Test/PortabilityRules/DOC206UnitTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. | ||
// Licensed under the MIT license. See LICENSE in the project root for license information. | ||
|
||
namespace DocumentationAnalyzers.Test.PortabilityRules | ||
{ | ||
using System.Threading.Tasks; | ||
using Microsoft.CodeAnalysis.CSharp.Testing; | ||
using Microsoft.CodeAnalysis.Testing; | ||
using Xunit; | ||
using Verify = Microsoft.CodeAnalysis.CSharp.Testing.CSharpCodeFixVerifier<DocumentationAnalyzers.PortabilityRules.DOC206SynchronizeDocumentation, DocumentationAnalyzers.PortabilityRules.DOC206CodeFixProvider, Microsoft.CodeAnalysis.Testing.Verifiers.XUnitVerifier>; | ||
|
||
public class DOC206UnitTests | ||
{ | ||
[Fact] | ||
public async Task TestInheritSummaryAsync() | ||
{ | ||
var testCode = @" | ||
/// <summary> | ||
/// Summary text. | ||
/// </summary> | ||
/// <autoinheritdoc/> | ||
class TestClass : BaseClass | ||
{ | ||
} | ||
/// <summary> | ||
/// Summary text. | ||
/// </summary> | ||
class BaseClass | ||
{ | ||
} | ||
"; | ||
|
||
await Verify.VerifyAnalyzerAsync(testCode); | ||
} | ||
|
||
[Fact] | ||
public async Task TestIncorrectSummaryAsync() | ||
{ | ||
var testCode = @" | ||
/// <summary> | ||
/// Incorrect summary text. | ||
/// </summary> | ||
/// [|<autoinheritdoc/>|] | ||
class TestClass : BaseClass | ||
{ | ||
} | ||
/// <summary> | ||
/// Summary text. | ||
/// </summary> | ||
class BaseClass | ||
{ | ||
} | ||
"; | ||
var fixedCode = @" | ||
/// <summary> | ||
/// Summary text. | ||
/// </summary> | ||
/// <autoinheritdoc/> | ||
class TestClass : BaseClass | ||
{ | ||
} | ||
/// <summary> | ||
/// Summary text. | ||
/// </summary> | ||
class BaseClass | ||
{ | ||
} | ||
"; | ||
|
||
await Verify.VerifyCodeFixAsync(testCode, fixedCode); | ||
} | ||
|
||
[Fact] | ||
public async Task TestMissingSummaryAsync() | ||
{ | ||
var testCode = @" | ||
/// [|<autoinheritdoc/>|] | ||
class TestClass : BaseClass | ||
{ | ||
} | ||
/// <summary> | ||
/// Summary text. | ||
/// </summary> | ||
class BaseClass | ||
{ | ||
} | ||
"; | ||
var fixedCode = @" | ||
/// <summary> | ||
/// Summary text. | ||
/// </summary> | ||
/// <autoinheritdoc/> | ||
class TestClass : BaseClass | ||
{ | ||
} | ||
/// <summary> | ||
/// Summary text. | ||
/// </summary> | ||
class BaseClass | ||
{ | ||
} | ||
"; | ||
|
||
await Verify.VerifyCodeFixAsync(testCode, fixedCode); | ||
} | ||
} | ||
} |
Oops, something went wrong.