Skip to content

Commit

Permalink
Add option for code fix for CS1591
Browse files Browse the repository at this point in the history
  • Loading branch information
josefpihrt committed Mar 13, 2021
1 parent 84edb52 commit 089dbed
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 18 deletions.
28 changes: 17 additions & 11 deletions src/CSharp/CSharp/Documentation/DocumentationCommentGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -312,24 +312,30 @@ private static SyntaxTriviaList Generate(
sb.AppendLine("/// </summary>");
}

foreach (TypeParameterSyntax typeParameter in typeParameters)
if (!settings.IsTagIgnored(WellKnownXmlTags.TypeParam))
{
sb.Append(settings.Indentation);
sb.Append("/// <typeparam name=\"");
sb.Append(typeParameter.Identifier.ValueText);
sb.AppendLine("\"></typeparam>");
foreach (TypeParameterSyntax typeParameter in typeParameters)
{
sb.Append(settings.Indentation);
sb.Append("/// <typeparam name=\"");
sb.Append(typeParameter.Identifier.ValueText);
sb.AppendLine("\"></typeparam>");
}
}

foreach (ParameterSyntax parameter in parameters)
if (!settings.IsTagIgnored(WellKnownXmlTags.Param))
{
sb.Append(settings.Indentation);
sb.Append("/// <param name=\"");
sb.Append(parameter.Identifier.ValueText);
sb.AppendLine("\"></param>");
foreach (ParameterSyntax parameter in parameters)
{
sb.Append(settings.Indentation);
sb.Append("/// <param name=\"");
sb.Append(parameter.Identifier.ValueText);
sb.AppendLine("\"></param>");
}
}

if (canGenerateReturns
&& settings.Returns)
&& !settings.IsTagIgnored(WellKnownXmlTags.Returns))
{
sb.Append(settings.Indentation);
sb.AppendLine("/// <returns></returns>");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
// Copyright (c) Josef Pihrt. All rights reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections.Immutable;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
Expand All @@ -11,6 +14,8 @@ namespace Roslynator.CSharp.Refactorings
{
internal static class AddDocumentationCommentRefactoring
{
private static readonly string[] _tagSeparator = new[] { "," };

public static async Task<Document> RefactorAsync(
Document document,
MemberDeclarationSyntax memberDeclaration,
Expand All @@ -34,7 +39,23 @@ public static async Task<Document> RefactorAsync(
}
}

newNode ??= memberDeclaration.WithNewSingleLineDocumentationComment();
DocumentationCommentGeneratorSettings settings = DocumentationCommentGeneratorSettings.Default;

if (document.TryGetAnalyzerOptionValue(
memberDeclaration,
CodeFixOptions.CS1591_MissingXmlCommentForPubliclyVisibleTypeOrMember_IgnoredTags,
out string value))
{
ImmutableArray<string> ignoredTags = value
.Split(_tagSeparator, StringSplitOptions.RemoveEmptyEntries)
.Select(f => f.Trim())
.Where(f => f.Length > 0)
.ToImmutableArray();

settings = new DocumentationCommentGeneratorSettings(ignoredTags: ignoredTags);
}

newNode ??= memberDeclaration.WithNewSingleLineDocumentationComment(settings);

return await document.ReplaceNodeAsync(memberDeclaration, newNode, cancellationToken).ConfigureAwait(false);
}
Expand Down
17 changes: 11 additions & 6 deletions src/Core/Documentation/DocumentationCommentGeneratorSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,38 @@ internal class DocumentationCommentGeneratorSettings
{
public DocumentationCommentGeneratorSettings(
IEnumerable<string> summary = null,
IEnumerable<string> ignoredTags = null,
string indentation = null,
bool singleLineSummary = false,
bool returns = true)
bool singleLineSummary = false)
{
Summary = (summary != null) ? ImmutableArray.CreateRange(summary) : ImmutableArray<string>.Empty;
IgnoredTags = ignoredTags?.ToImmutableArray() ?? ImmutableArray<string>.Empty;
Indentation = indentation ?? "";
SingleLineSummary = singleLineSummary;
Returns = returns;
}

public static DocumentationCommentGeneratorSettings Default { get; } = new DocumentationCommentGeneratorSettings();

public ImmutableArray<string> IgnoredTags { get; }

public ImmutableArray<string> Summary { get; }

public string Indentation { get; }

public bool SingleLineSummary { get; }

public bool Returns { get; }
public bool IsTagIgnored(string tag)
{
return IgnoredTags.Contains(tag);
}

public DocumentationCommentGeneratorSettings WithIndentation(string indentation)
{
return new DocumentationCommentGeneratorSettings(
summary: Summary,
ignoredTags: IgnoredTags,
indentation: indentation,
singleLineSummary: SingleLineSummary,
returns: Returns);
singleLineSummary: SingleLineSummary);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ private static IEnumerable<MemberDeclarationSyntax> CreateMembers(IEnumerable<Co

var settings = new DocumentationCommentGeneratorSettings(
summary: new string[] { $"{codeFix.Id} (fixes {string.Join(", ", codeFix.FixableDiagnosticIds.OrderBy(f => f))})" },
ignoredTags: new[] { "returns", "value" },
indentation: " ",
singleLineSummary: true);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ private static IEnumerable<MemberDeclarationSyntax> CreateMembers(IEnumerable<Co

var settings = new DocumentationCommentGeneratorSettings(
summary: new string[] { compilerDiagnostic.Id },
ignoredTags: new[] { "returns", "value" },
indentation: " ",
singleLineSummary: true);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ private MemberDeclarationSyntax CreateMember(
{
var settings = new DocumentationCommentGeneratorSettings(
summary: new string[] { analyzer.Id },
ignoredTags: new[] { "returns", "value" },
indentation: " ",
singleLineSummary: true);

Expand Down

0 comments on commit 089dbed

Please sign in to comment.