Skip to content

Commit 0d88ae3

Browse files
committed
Fix single line summary when changing the reference
The issue resulted from always passing in a newline character when using the code fix provider. This was resolved by determining if the summary was multiline or not. If it isn't multiline the newLineText is no longer passed.
1 parent 29cd207 commit 0d88ae3

2 files changed

Lines changed: 61 additions & 5 deletions

File tree

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

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,24 @@ 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;
123+
if (typeDeclaration is ClassDeclarationSyntax classDeclaration)
124+
{
125+
typeParameterList = classDeclaration.TypeParameterList;
126+
}
127+
else
128+
{
129+
typeParameterList = (typeDeclaration as StructDeclarationSyntax)?.TypeParameterList;
130+
}
131+
132+
return XmlSyntaxFactory.List(
133+
XmlSyntaxFactory.Text(preText),
134+
BuildSeeElement(typeDeclaration.Identifier, typeParameterList),
135+
XmlSyntaxFactory.Text(postText.EndsWith(".") ? postText : (postText + ".")));
136+
}
137+
120138
internal static SyntaxList<XmlNodeSyntax> BuildStandardTextSyntaxList(BaseTypeDeclarationSyntax typeDeclaration, string newLineText, string preText, string postText)
121139
{
122140
TypeParameterListSyntax typeParameterList;
@@ -143,8 +161,6 @@ private static Task<Document> GetTransformedDocumentAsync(Document document, Syn
143161

144162
var standardText = GenerateStandardText(document, declarationSyntax, typeDeclaration, cancellationToken);
145163

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

150166
var newContent = RemoveMalformattedStandardText(node.Content, standardText[0], standardText[1], ref trailingString);
@@ -157,7 +173,18 @@ private static Task<Document> GetTransformedDocumentAsync(Document document, Syn
157173
}
158174
}
159175

160-
var list = BuildStandardTextSyntaxList(typeDeclaration, newLineText, standardText[0], standardText[1] + trailingString);
176+
SyntaxList<XmlNodeSyntax> list;
177+
if (IsMultiLine(node))
178+
{
179+
string newLineText = document.Project.Solution.Workspace.Options.GetOption(FormattingOptions.NewLine, LanguageNames.CSharp);
180+
list = BuildStandardTextSyntaxList(typeDeclaration, newLineText, standardText[0], standardText[1] + trailingString);
181+
}
182+
else
183+
{
184+
list = BuildStandardTextSyntaxList(typeDeclaration, standardText[0], standardText[1] + trailingString);
185+
}
186+
187+
161188
newContent = newContent.InsertRange(0, list);
162189

163190
newContent = RemoveTrailingEmptyLines(newContent);
@@ -171,6 +198,12 @@ private static Task<Document> GetTransformedDocumentAsync(Document document, Syn
171198
return Task.FromResult(newDocument);
172199
}
173200

201+
private static bool IsMultiLine(XmlElementSyntax node)
202+
{
203+
var lineSpan = node.GetLineSpan();
204+
return lineSpan.StartLinePosition.Line != lineSpan.EndLinePosition.Line;
205+
}
206+
174207
private static Task<Document> GetTransformedDocumentAsync(Document document, SyntaxNode root, XmlEmptyElementSyntax node)
175208
{
176209
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)