Skip to content

Commit 23ffa4c

Browse files
authored
Merge pull request #2976 from tokarzkj/bug/single-line-summary-2963
SA1642 - Fix single line summary when changing the reference
2 parents 0758449 + b18b040 commit 23ffa4c

2 files changed

Lines changed: 63 additions & 14 deletions

File tree

StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/DocumentationRules/SA1642SA1643CodeFixProvider.cs

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -117,17 +117,19 @@ internal static ImmutableArray<string> GenerateStandardText(Document document, B
117117
}
118118
}
119119

120+
internal static SyntaxList<XmlNodeSyntax> BuildStandardTextSyntaxList(BaseTypeDeclarationSyntax typeDeclaration, string preText, string postText)
121+
{
122+
TypeParameterListSyntax typeParameterList = GetTypeParameterList(typeDeclaration);
123+
124+
return XmlSyntaxFactory.List(
125+
XmlSyntaxFactory.Text(preText),
126+
BuildSeeElement(typeDeclaration.Identifier, typeParameterList),
127+
XmlSyntaxFactory.Text(postText.EndsWith(".") ? postText : (postText + ".")));
128+
}
129+
120130
internal static SyntaxList<XmlNodeSyntax> BuildStandardTextSyntaxList(BaseTypeDeclarationSyntax typeDeclaration, string newLineText, string preText, string postText)
121131
{
122-
TypeParameterListSyntax typeParameterList;
123-
if (typeDeclaration is ClassDeclarationSyntax classDeclaration)
124-
{
125-
typeParameterList = classDeclaration.TypeParameterList;
126-
}
127-
else
128-
{
129-
typeParameterList = (typeDeclaration as StructDeclarationSyntax)?.TypeParameterList;
130-
}
132+
TypeParameterListSyntax typeParameterList = GetTypeParameterList(typeDeclaration);
131133

132134
return XmlSyntaxFactory.List(
133135
XmlSyntaxFactory.NewLine(newLineText),
@@ -136,15 +138,23 @@ internal static SyntaxList<XmlNodeSyntax> BuildStandardTextSyntaxList(BaseTypeDe
136138
XmlSyntaxFactory.Text(postText.EndsWith(".") ? postText : (postText + ".")));
137139
}
138140

141+
private static TypeParameterListSyntax GetTypeParameterList(BaseTypeDeclarationSyntax typeDeclaration)
142+
{
143+
if (typeDeclaration is ClassDeclarationSyntax classDeclaration)
144+
{
145+
return classDeclaration.TypeParameterList;
146+
}
147+
148+
return (typeDeclaration as StructDeclarationSyntax)?.TypeParameterList;
149+
}
150+
139151
private static Task<Document> GetTransformedDocumentAsync(Document document, SyntaxNode root, XmlElementSyntax node, CancellationToken cancellationToken)
140152
{
141153
var typeDeclaration = node.FirstAncestorOrSelf<BaseTypeDeclarationSyntax>();
142154
var declarationSyntax = node.FirstAncestorOrSelf<BaseMethodDeclarationSyntax>();
143155

144156
var standardText = GenerateStandardText(document, declarationSyntax, typeDeclaration, cancellationToken);
145157

146-
string newLineText = document.Project.Solution.Workspace.Options.GetOption(FormattingOptions.NewLine, LanguageNames.CSharp);
147-
148158
string trailingString = string.Empty;
149159

150160
var newContent = RemoveMalformattedStandardText(node.Content, standardText[0], standardText[1], ref trailingString);
@@ -157,7 +167,17 @@ private static Task<Document> GetTransformedDocumentAsync(Document document, Syn
157167
}
158168
}
159169

160-
var list = BuildStandardTextSyntaxList(typeDeclaration, newLineText, standardText[0], standardText[1] + trailingString);
170+
SyntaxList<XmlNodeSyntax> list;
171+
if (IsMultiLine(node))
172+
{
173+
string newLineText = document.Project.Solution.Workspace.Options.GetOption(FormattingOptions.NewLine, LanguageNames.CSharp);
174+
list = BuildStandardTextSyntaxList(typeDeclaration, newLineText, standardText[0], standardText[1] + trailingString);
175+
}
176+
else
177+
{
178+
list = BuildStandardTextSyntaxList(typeDeclaration, standardText[0], standardText[1] + trailingString);
179+
}
180+
161181
newContent = newContent.InsertRange(0, list);
162182

163183
newContent = RemoveTrailingEmptyLines(newContent);
@@ -171,6 +191,12 @@ private static Task<Document> GetTransformedDocumentAsync(Document document, Syn
171191
return Task.FromResult(newDocument);
172192
}
173193

194+
private static bool IsMultiLine(XmlElementSyntax node)
195+
{
196+
var lineSpan = node.GetLineSpan();
197+
return lineSpan.StartLinePosition.Line != lineSpan.EndLinePosition.Line;
198+
}
199+
174200
private static Task<Document> GetTransformedDocumentAsync(Document document, SyntaxNode root, XmlEmptyElementSyntax node)
175201
{
176202
var typeDeclaration = node.FirstAncestorOrSelf<BaseTypeDeclarationSyntax>();

StyleCop.Analyzers/StyleCop.Analyzers.Test/DocumentationRules/SA1642UnitTests.cs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -939,8 +939,7 @@ public ClassName()
939939
{{
940940
public class ClassName
941941
{{
942-
/// <summary>
943-
/// Initializes a new instance of the <see cref=""ClassName""/> class.</summary>
942+
/// <summary>Initializes a new instance of the <see cref=""ClassName""/> class.</summary>
944943
public ClassName()
945944
{{
946945
}}
@@ -951,6 +950,30 @@ public ClassName()
951950
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
952951
}
953952

953+
[Fact]
954+
[WorkItem(2963, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/2963")]
955+
public async Task TestConstructorNoCRefDocumentationSingleLineAsync()
956+
{
957+
var testCode = @"
958+
public class TestClass
959+
{
960+
/// <summary>Initializes a new instance of the TestClass class.</summary>
961+
public TestClass() { }
962+
}
963+
";
964+
965+
var fixedCode = @"
966+
public class TestClass
967+
{
968+
/// <summary>Initializes a new instance of the <see cref=""TestClass""/> class.</summary>
969+
public TestClass() { }
970+
}
971+
";
972+
973+
var expected = Diagnostic().WithLocation(4, 9);
974+
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
975+
}
976+
954977
[Theory]
955978
[WorkItem(2686, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/2686")]
956979
[InlineData("")]

0 commit comments

Comments
 (0)