Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge doc comments across multiple parts #76013

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,25 @@ await TestAsync(markup,
Documentation("summary for interface IGoo"));
}

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/60725")]
public async Task TestMergedRemarks()
{
var markup =
"""
/// <remarks>hello1</remarks>
public partial class C {
}

/// <remarks>hello2</remarks>
public partial class $$C {
}
""";

await TestAsync(markup,
MainDescription("class C"),
Remarks("hello1 hello2"));
CyrusNajmabadi marked this conversation as resolved.
Show resolved Hide resolved
}

[Fact, WorkItem("http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/991466")]
public async Task TestDocumentationInUsingDirectiveWithAlias3()
{
Expand Down Expand Up @@ -516,7 +535,9 @@ void M()

// SingleLine doc comment with '\r' line separators
await TestAsync("""
///<summary>Hello! ///</summary> class C { void M() { $$C obj; } }
///<summary>Hello!
///</summary>
class C { void M() { $$C obj; } }
""",
MainDescription("class C"),
Documentation("Hello!"));
Expand Down Expand Up @@ -632,7 +653,12 @@ void M()

// Multiline doc comment with '\r' line separators
await TestAsync("""
/** * <summary> * Hello! * </summary> */ class C { void M() { $$C obj; } }
/**
* <summary>
* Hello!
* </summary>
*/
class C { void M() { $$C obj; } }
""",
MainDescription("class C"),
Documentation("Hello!"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@ Imports Microsoft.CodeAnalysis.Host
Imports Microsoft.CodeAnalysis.LanguageService

Namespace Microsoft.CodeAnalysis.Editor.UnitTests.Workspaces

<[UseExportProvider]>
Public Class SymbolDescriptionServiceTests

Private Shared Async Function TestAsync(languageServiceProvider As HostLanguageServices, workspace As EditorTestWorkspace, expectedDescription As String) As Task

Dim solution = workspace.CurrentSolution
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#nullable disable

using System.Composition;
using System.Diagnostics.CodeAnalysis;
using Microsoft.CodeAnalysis.DocumentationComments;
Expand All @@ -12,11 +10,6 @@
namespace Microsoft.CodeAnalysis.CSharp.DocumentationComments;

[ExportLanguageService(typeof(IDocumentationCommentFormattingService), LanguageNames.CSharp), Shared]
internal class CSharpDocumentationCommentFormattingService : AbstractDocumentationCommentFormattingService
{
[ImportingConstructor]
[SuppressMessage("RoslynDiagnosticsReliability", "RS0033:Importing constructor should be [Obsolete]", Justification = "Used in test code: https://github.com/dotnet/roslyn/issues/42814")]
public CSharpDocumentationCommentFormattingService()
{
}
}
[method: ImportingConstructor]
[method: SuppressMessage("RoslynDiagnosticsReliability", "RS0033:Importing constructor should be [Obsolete]", Justification = "Used in test code: https://github.com/dotnet/roslyn/issues/42814")]
internal sealed class CSharpDocumentationCommentFormattingService() : AbstractDocumentationCommentFormattingService;
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
using System.Collections.Immutable;
using System.Xml;
using Microsoft.CodeAnalysis.PooledObjects;
using XmlNames = Roslyn.Utilities.DocumentationCommentXmlNames;
using Microsoft.CodeAnalysis.Shared.Extensions;
using XmlNames = Roslyn.Utilities.DocumentationCommentXmlNames;

CyrusNajmabadi marked this conversation as resolved.
Show resolved Hide resolved
namespace Microsoft.CodeAnalysis.Shared.Utilities;

Expand Down Expand Up @@ -227,89 +227,98 @@ private static string TrimEachLine(string text)

private void ParseCallback(XmlReader reader)
{
if (reader.NodeType == XmlNodeType.Element)
if (reader.NodeType != XmlNodeType.Element)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

inverted.

view with whitespace off.

{
var localName = reader.LocalName;
if (XmlNames.ElementEquals(localName, XmlNames.ExampleElementName) && _comment.ExampleText == null)
{
_comment.ExampleText = TrimEachLine(reader.ReadInnerXml());
}
else if (XmlNames.ElementEquals(localName, XmlNames.SummaryElementName) && _comment.SummaryText == null)
{
_comment.SummaryText = TrimEachLine(reader.ReadInnerXml());
}
else if (XmlNames.ElementEquals(localName, XmlNames.ReturnsElementName) && _comment.ReturnsText == null)
{
_comment.ReturnsText = TrimEachLine(reader.ReadInnerXml());
}
else if (XmlNames.ElementEquals(localName, XmlNames.ValueElementName) && _comment.ValueText == null)
{
_comment.ValueText = TrimEachLine(reader.ReadInnerXml());
}
else if (XmlNames.ElementEquals(localName, XmlNames.RemarksElementName) && _comment.RemarksText == null)
// We came across something that isn't a start element, like a block of text. Skip it.
reader.Read();
return;
}

var localName = reader.LocalName;
if (XmlNames.ElementEquals(localName, XmlNames.ExampleElementName))
{
_comment.ExampleText = InitializeValue(_comment.ExampleText, TrimEachLine(reader.ReadInnerXml()));
}
else if (XmlNames.ElementEquals(localName, XmlNames.SummaryElementName))
{
_comment.SummaryText = InitializeValue(_comment.SummaryText, TrimEachLine(reader.ReadInnerXml()));
}
else if (XmlNames.ElementEquals(localName, XmlNames.ReturnsElementName))
{
_comment.ReturnsText = InitializeValue(_comment.ReturnsText, TrimEachLine(reader.ReadInnerXml()));
}
else if (XmlNames.ElementEquals(localName, XmlNames.ValueElementName))
{
_comment.ValueText = InitializeValue(_comment.ValueText, TrimEachLine(reader.ReadInnerXml()));
}
else if (XmlNames.ElementEquals(localName, XmlNames.RemarksElementName))
{
_comment.RemarksText = InitializeValue(_comment.RemarksText, TrimEachLine(reader.ReadInnerXml()));
}
else if (XmlNames.ElementEquals(localName, XmlNames.ParameterElementName))
{
var name = reader.GetAttribute(XmlNames.NameAttributeName);
var paramText = reader.ReadInnerXml();

if (!string.IsNullOrWhiteSpace(name) && !_comment._parameterTexts.ContainsKey(name))
{
_comment.RemarksText = TrimEachLine(reader.ReadInnerXml());
(_parameterNamesBuilder ??= ImmutableArray.CreateBuilder<string>()).Add(name);
_comment._parameterTexts.Add(name, TrimEachLine(paramText));
}
else if (XmlNames.ElementEquals(localName, XmlNames.ParameterElementName))
{
var name = reader.GetAttribute(XmlNames.NameAttributeName);
var paramText = reader.ReadInnerXml();
}
else if (XmlNames.ElementEquals(localName, XmlNames.TypeParameterElementName))
{
var name = reader.GetAttribute(XmlNames.NameAttributeName);
var typeParamText = reader.ReadInnerXml();

if (!string.IsNullOrWhiteSpace(name) && !_comment._parameterTexts.ContainsKey(name))
{
(_parameterNamesBuilder ??= ImmutableArray.CreateBuilder<string>()).Add(name);
_comment._parameterTexts.Add(name, TrimEachLine(paramText));
}
}
else if (XmlNames.ElementEquals(localName, XmlNames.TypeParameterElementName))
if (!string.IsNullOrWhiteSpace(name) && !_comment._typeParameterTexts.ContainsKey(name))
{
var name = reader.GetAttribute(XmlNames.NameAttributeName);
var typeParamText = reader.ReadInnerXml();

if (!string.IsNullOrWhiteSpace(name) && !_comment._typeParameterTexts.ContainsKey(name))
{
(_typeParameterNamesBuilder ??= ImmutableArray.CreateBuilder<string>()).Add(name);
_comment._typeParameterTexts.Add(name, TrimEachLine(typeParamText));
}
(_typeParameterNamesBuilder ??= ImmutableArray.CreateBuilder<string>()).Add(name);
_comment._typeParameterTexts.Add(name, TrimEachLine(typeParamText));
}
else if (XmlNames.ElementEquals(localName, XmlNames.ExceptionElementName))
{
var type = reader.GetAttribute(XmlNames.CrefAttributeName);
var exceptionText = reader.ReadInnerXml();

if (!string.IsNullOrWhiteSpace(type))
{
if (_exceptionTextBuilders == null || !_exceptionTextBuilders.ContainsKey(type))
{
(_exceptionTypesBuilder ??= ImmutableArray.CreateBuilder<string>()).Add(type);
(_exceptionTextBuilders ??= []).Add(type, ImmutableArray.CreateBuilder<string>());
}
}
else if (XmlNames.ElementEquals(localName, XmlNames.ExceptionElementName))
{
var type = reader.GetAttribute(XmlNames.CrefAttributeName);
var exceptionText = reader.ReadInnerXml();

_exceptionTextBuilders[type].Add(exceptionText);
}
}
else if (XmlNames.ElementEquals(localName, XmlNames.CompletionListElementName))
if (!string.IsNullOrWhiteSpace(type))
{
var cref = reader.GetAttribute(XmlNames.CrefAttributeName);
if (!string.IsNullOrWhiteSpace(cref))
if (_exceptionTextBuilders == null || !_exceptionTextBuilders.ContainsKey(type))
{
_comment.CompletionListCref = cref;
(_exceptionTypesBuilder ??= ImmutableArray.CreateBuilder<string>()).Add(type);
(_exceptionTextBuilders ??= []).Add(type, ImmutableArray.CreateBuilder<string>());
}

reader.ReadInnerXml();
_exceptionTextBuilders[type].Add(exceptionText);
}
else
}
else if (XmlNames.ElementEquals(localName, XmlNames.CompletionListElementName))
{
var cref = reader.GetAttribute(XmlNames.CrefAttributeName);
if (!string.IsNullOrWhiteSpace(cref))
{
// This is an element we don't handle. Skip it.
reader.Read();
_comment.CompletionListCref = cref;
}

reader.ReadInnerXml();
}
else
{
// We came across something that isn't a start element, like a block of text.
// Skip it.
// This is an element we don't handle. Skip it.
reader.Read();
}

return;

static string InitializeValue(string? existingValue, string newValue)
{
// The use of Environment.NewLine matches the behavior of StringBuilder.AppendLine used in multiple
// places in this file.
return existingValue is null or ""
? newValue
: $"{existingValue}{Environment.NewLine}{newValue}";
}
}
}

Expand Down
Loading
Loading