Skip to content

Commit de363a1

Browse files
authored
Generate Documentation - Add remarks if available (#77783)
* add remarks to documentation if available * add named parameter for nulls
1 parent cec6e09 commit de363a1

File tree

4 files changed

+40
-8
lines changed

4 files changed

+40
-8
lines changed

src/EditorFeatures/Core/DocumentationComments/CopilotGenerateDocumentationCommentProvider.cs

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// See the LICENSE file in the project root for more information.
44

55
using System;
6+
using System.CodeDom.Compiler;
67
using System.Collections.Generic;
78
using System.Collections.Immutable;
89
using System.Text;
@@ -97,9 +98,12 @@ public async Task GenerateDocumentationProposalAsync(DocumentationCommentSnippet
9798
var summaryEndTag = comments.IndexOf("</summary>", index, StringComparison.Ordinal);
9899
if (summaryEndTag != -1 && summaryStartTag != -1)
99100
{
100-
proposedEdits.Add(new DocumentationCommentProposedEdit(new TextSpan(caret + startIndex, 0), null, DocumentationCommentTagType.Summary));
101+
proposedEdits.Add(new DocumentationCommentProposedEdit(new TextSpan(caret + startIndex, 0), symbolName: null, DocumentationCommentTagType.Summary));
101102
}
102103

104+
// We may receive remarks from the model. In that case, we want to insert the remark tags and remark directly after the summary.
105+
proposedEdits.Add(new DocumentationCommentProposedEdit(new TextSpan(summaryEndTag + "</summary>".Length + startIndex, 0), symbolName: null, DocumentationCommentTagType.Remarks));
106+
103107
while (true)
104108
{
105109
var typeParamEndTag = comments.IndexOf("</typeparam>", index, StringComparison.Ordinal);
@@ -145,7 +149,7 @@ public async Task GenerateDocumentationProposalAsync(DocumentationCommentSnippet
145149
var returnsEndTag = comments.IndexOf("</returns>", index, StringComparison.Ordinal);
146150
if (returnsEndTag != -1)
147151
{
148-
proposedEdits.Add(new DocumentationCommentProposedEdit(new TextSpan(returnsEndTag + startIndex, 0), null, DocumentationCommentTagType.Returns));
152+
proposedEdits.Add(new DocumentationCommentProposedEdit(new TextSpan(returnsEndTag + startIndex, 0), symbolName: null, DocumentationCommentTagType.Returns));
149153
}
150154

151155
while (true)
@@ -218,7 +222,7 @@ private static async Task<IReadOnlyList<ProposedEdit>> GetProposedEditsAsync(
218222
}
219223

220224
var proposedEdit = new ProposedEdit(new SnapshotSpan(oldSnapshot, textSpan.Start, textSpan.Length),
221-
AddNewLinesToCopilotText(copilotStatement, indentText, characterLimit: 120));
225+
AddNewLinesToCopilotText(copilotStatement, indentText, edit.TagType, characterLimit: 120));
222226
list.Add(proposedEdit);
223227
}
224228

@@ -230,6 +234,10 @@ private static async Task<IReadOnlyList<ProposedEdit>> GetProposedEditsAsync(
230234
{
231235
return summary;
232236
}
237+
else if (edit.TagType == DocumentationCommentTagType.Remarks && documentationCommentDictionary.TryGetValue(DocumentationCommentTagType.Remarks.ToString(), out var remarks) && !string.IsNullOrEmpty(remarks))
238+
{
239+
return remarks;
240+
}
233241
else if (edit.TagType == DocumentationCommentTagType.TypeParam && documentationCommentDictionary.TryGetValue(symbolKey!, out var typeParam) && !string.IsNullOrEmpty(typeParam))
234242
{
235243
return typeParam;
@@ -250,11 +258,13 @@ private static async Task<IReadOnlyList<ProposedEdit>> GetProposedEditsAsync(
250258
return null;
251259
}
252260

253-
static string AddNewLinesToCopilotText(string copilotText, string? indentText, int characterLimit)
261+
static string AddNewLinesToCopilotText(string copilotText, string? indentText, DocumentationCommentTagType tagType, int characterLimit)
254262
{
255263
// Double check that the resultant from Copilot does not produce any strings containing new line characters.
256264
copilotText = Regex.Replace(copilotText, @"\r?\n", " ");
257265
var builder = new StringBuilder();
266+
copilotText = BuildCopilotTextForRemarks(copilotText, indentText, tagType, builder);
267+
258268
var words = copilotText.Split(' ');
259269
var currentLineLength = 0;
260270
characterLimit -= (indentText!.Length + "/// ".Length);
@@ -279,6 +289,22 @@ static string AddNewLinesToCopilotText(string copilotText, string? indentText, i
279289
}
280290

281291
return builder.ToString();
292+
293+
static string BuildCopilotTextForRemarks(string copilotText, string? indentText, DocumentationCommentTagType tagType, StringBuilder builder)
294+
{
295+
if (tagType is DocumentationCommentTagType.Remarks)
296+
{
297+
builder.AppendLine();
298+
builder.Append(indentText);
299+
builder.Append("/// <remarks>");
300+
builder.Append(copilotText);
301+
builder.Append("</remarks>");
302+
copilotText = builder.ToString();
303+
builder.Clear();
304+
}
305+
306+
return copilotText;
307+
}
282308
}
283309
}
284310

src/Features/Core/Portable/DocumentationComments/DocumentationCommentTagType.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ namespace Microsoft.CodeAnalysis.DocumentationComments
77
internal enum DocumentationCommentTagType
88
{
99
Summary,
10+
Remarks,
1011
TypeParam,
1112
Param,
1213
Returns,
14+
Value,
1315
Exception,
1416
}
1517
}

src/Features/ExternalAccess/Copilot/GenerateDocumentation/CopilotDocumentationCommentTagType.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@ namespace Microsoft.CodeAnalysis.ExternalAccess.Copilot
99
internal enum CopilotDocumentationCommentTagType
1010
{
1111
Summary = DocumentationCommentTagType.Summary,
12+
Remarks = DocumentationCommentTagType.Remarks,
1213
TypeParam = DocumentationCommentTagType.TypeParam,
1314
Param = DocumentationCommentTagType.Param,
1415
Returns = DocumentationCommentTagType.Returns,
16+
Value = DocumentationCommentTagType.Value,
1517
Exception = DocumentationCommentTagType.Exception,
1618
}
1719
}

src/Features/ExternalAccess/Copilot/InternalAPI.Unshipped.txt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@ Microsoft.CodeAnalysis.ExternalAccess.Copilot.CopilotDocumentationCommentPropose
1313
Microsoft.CodeAnalysis.ExternalAccess.Copilot.CopilotDocumentationCommentProposedEditWrapper.SymbolName.get -> string?
1414
Microsoft.CodeAnalysis.ExternalAccess.Copilot.CopilotDocumentationCommentProposedEditWrapper.TagType.get -> Microsoft.CodeAnalysis.ExternalAccess.Copilot.CopilotDocumentationCommentTagType
1515
Microsoft.CodeAnalysis.ExternalAccess.Copilot.CopilotDocumentationCommentTagType
16-
Microsoft.CodeAnalysis.ExternalAccess.Copilot.CopilotDocumentationCommentTagType.Exception = 4 -> Microsoft.CodeAnalysis.ExternalAccess.Copilot.CopilotDocumentationCommentTagType
17-
Microsoft.CodeAnalysis.ExternalAccess.Copilot.CopilotDocumentationCommentTagType.Param = 2 -> Microsoft.CodeAnalysis.ExternalAccess.Copilot.CopilotDocumentationCommentTagType
18-
Microsoft.CodeAnalysis.ExternalAccess.Copilot.CopilotDocumentationCommentTagType.Returns = 3 -> Microsoft.CodeAnalysis.ExternalAccess.Copilot.CopilotDocumentationCommentTagType
16+
Microsoft.CodeAnalysis.ExternalAccess.Copilot.CopilotDocumentationCommentTagType.Exception = 6 -> Microsoft.CodeAnalysis.ExternalAccess.Copilot.CopilotDocumentationCommentTagType
17+
Microsoft.CodeAnalysis.ExternalAccess.Copilot.CopilotDocumentationCommentTagType.Param = 3 -> Microsoft.CodeAnalysis.ExternalAccess.Copilot.CopilotDocumentationCommentTagType
18+
Microsoft.CodeAnalysis.ExternalAccess.Copilot.CopilotDocumentationCommentTagType.Remarks = 1 -> Microsoft.CodeAnalysis.ExternalAccess.Copilot.CopilotDocumentationCommentTagType
19+
Microsoft.CodeAnalysis.ExternalAccess.Copilot.CopilotDocumentationCommentTagType.Returns = 4 -> Microsoft.CodeAnalysis.ExternalAccess.Copilot.CopilotDocumentationCommentTagType
1920
Microsoft.CodeAnalysis.ExternalAccess.Copilot.CopilotDocumentationCommentTagType.Summary = 0 -> Microsoft.CodeAnalysis.ExternalAccess.Copilot.CopilotDocumentationCommentTagType
20-
Microsoft.CodeAnalysis.ExternalAccess.Copilot.CopilotDocumentationCommentTagType.TypeParam = 1 -> Microsoft.CodeAnalysis.ExternalAccess.Copilot.CopilotDocumentationCommentTagType
21+
Microsoft.CodeAnalysis.ExternalAccess.Copilot.CopilotDocumentationCommentTagType.TypeParam = 2 -> Microsoft.CodeAnalysis.ExternalAccess.Copilot.CopilotDocumentationCommentTagType
22+
Microsoft.CodeAnalysis.ExternalAccess.Copilot.CopilotDocumentationCommentTagType.Value = 5 -> Microsoft.CodeAnalysis.ExternalAccess.Copilot.CopilotDocumentationCommentTagType
2123
Microsoft.CodeAnalysis.ExternalAccess.Copilot.CopilotOnTheFlyDocsInfoWrapper
2224
Microsoft.CodeAnalysis.ExternalAccess.Copilot.CopilotOnTheFlyDocsInfoWrapper.AdditionalContext.get -> System.Collections.Immutable.ImmutableArray<Microsoft.CodeAnalysis.ExternalAccess.Copilot.CopilotOnTheFlyDocsRelevantFileInfoWrapper?>
2325
Microsoft.CodeAnalysis.ExternalAccess.Copilot.CopilotOnTheFlyDocsInfoWrapper.CopilotOnTheFlyDocsInfoWrapper(Microsoft.CodeAnalysis.QuickInfo.OnTheFlyDocsInfo! onTheFlyDocsInfo) -> void

0 commit comments

Comments
 (0)