Skip to content

Commit

Permalink
Improve Men017 formatting and display
Browse files Browse the repository at this point in the history
  • Loading branch information
menees committed Mar 29, 2024
1 parent dda815d commit 5382a78
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,18 +64,23 @@ private async Task<Document> GetTransformedDocumentAsync(

if (accessorList != null && accessorDeclaration != null)
{
// Use NormalizeWhitespace so:
// {
// get;
// private set;
// }
// is transformed into: { get; }
AccessorListSyntax? newAccessorList = accessorList
.RemoveNode(accessorDeclaration, SyntaxRemoveOptions.KeepExteriorTrivia)?
.NormalizeWhitespace()
.WithAdditionalAnnotations(Formatter.Annotation);

if (newAccessorList != null)
{
// If "private set;" was on a line by itself, then we have to jump
// through some hoops to remove the remaining blank line without
// messing up the whitespace inside or after the accessor list.
if (accessorDeclaration.GetLeadingTrivia().All(trivia => string.IsNullOrWhiteSpace(trivia.ToFullString()))
&& accessorDeclaration.GetTrailingTrivia().All(trivia => string.IsNullOrWhiteSpace(trivia.ToFullString()))
&& accessorDeclaration.GetTrailingTrivia().Any(trivia => trivia.ToFullString().Contains('\n')))
{
AccessorDeclarationSyntax lastAccessor = newAccessorList.Accessors.Last();
newAccessorList = newAccessorList.ReplaceNode(lastAccessor, lastAccessor.NormalizeWhitespace());
}

SyntaxNode newRoot = root.ReplaceNode(accessorList, newAccessorList);
result = document.WithSyntaxRoot(newRoot);
}
Expand Down
3 changes: 2 additions & 1 deletion src/Menees.Analyzers/Men017RemoveUnusedPrivateSetter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ public sealed class Men017RemoveUnusedPrivateSetter : Analyzer
new LocalizableResourceString(nameof(Resources.Men017Description), Resources.ResourceManager, typeof(Resources));

private static readonly DiagnosticDescriptor Rule =
new(DiagnosticId, Title, MessageFormat, Rules.Design, Rules.InfoSeverity, Rules.EnabledByDefault, Description);
new(DiagnosticId, Title, MessageFormat, Rules.Design, Rules.InfoSeverity, Rules.EnabledByDefault, Description,
customTags: [WellKnownDiagnosticTags.Unnecessary]); // VS shows unnecessary tokens as faded.

#endregion

Expand Down
7 changes: 5 additions & 2 deletions tests/Menees.Analyzers.Test/Men017UnitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -525,12 +525,15 @@ public bool P1
}
""";

// The call to NormalizeWhitespace will remove the blank line and reformat the accessor list.
// We have to jump through some hoops to remove the blank line without
// messing up the whitespace inside or after the accessor list.
const string Fixed = """
class C
{
public bool P1
{ get; }
{
get;
}
}
"""
;
Expand Down

0 comments on commit 5382a78

Please sign in to comment.