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

Generate Documentation - Bug Fixes #77641

Merged
merged 2 commits into from
Mar 18, 2025
Merged
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
@@ -6,6 +6,7 @@
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Copilot;
@@ -199,46 +200,60 @@ private static async Task<IReadOnlyList<ProposedEdit>> GetProposedEditsAsync(

foreach (var edit in proposal.ProposedEdits)
{
string? copilotStatement = null;
var textSpan = edit.SpanToReplace;

string? symbolKey = null;

if (edit.SymbolName is not null)
{
symbolKey = edit.TagType.ToString() + "- " + edit.SymbolName;
Copy link
Member Author

Choose a reason for hiding this comment

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

This was silly, the space is unnecessary and the JsonConvert I am doing on the Conversations side was removing the space at times.

symbolKey = edit.TagType.ToString() + "-" + edit.SymbolName;
}

var copilotStatement = GetCopilotStatement(documentationCommentDictionary, edit, symbolKey);

// Just skip this piece of the documentation comment if, for some reason, it is not found.
if (copilotStatement is null)
{
continue;
}

var proposedEdit = new ProposedEdit(new SnapshotSpan(oldSnapshot, textSpan.Start, textSpan.Length),
AddNewLinesToCopilotText(copilotStatement, indentText, characterLimit: 120));
list.Add(proposedEdit);
}

return list;

static string? GetCopilotStatement(Dictionary<string, string> documentationCommentDictionary, DocumentationCommentProposedEdit edit, string? symbolKey)
{
if (edit.TagType == DocumentationCommentTagType.Summary && documentationCommentDictionary.TryGetValue(DocumentationCommentTagType.Summary.ToString(), out var summary) && !string.IsNullOrEmpty(summary))
{
copilotStatement = summary;
return summary;
}
if (edit.TagType == DocumentationCommentTagType.TypeParam && documentationCommentDictionary.TryGetValue(symbolKey!, out var typeParam) && !string.IsNullOrEmpty(typeParam))
else if (edit.TagType == DocumentationCommentTagType.TypeParam && documentationCommentDictionary.TryGetValue(symbolKey!, out var typeParam) && !string.IsNullOrEmpty(typeParam))
{
copilotStatement = typeParam;
return typeParam;
}
else if (edit.TagType == DocumentationCommentTagType.Param && documentationCommentDictionary.TryGetValue(symbolKey!, out var param) && !string.IsNullOrEmpty(param))
{
copilotStatement = param;
return param;
}
else if (edit.TagType == DocumentationCommentTagType.Returns && documentationCommentDictionary.TryGetValue(DocumentationCommentTagType.Returns.ToString(), out var returns) && !string.IsNullOrEmpty(returns))
{
copilotStatement = returns;
return returns;
}
else if (edit.TagType == DocumentationCommentTagType.Exception && documentationCommentDictionary.TryGetValue(symbolKey!, out var exception) && !string.IsNullOrEmpty(exception))
{
copilotStatement = exception;
return exception;
}

var proposedEdit = new ProposedEdit(new SnapshotSpan(oldSnapshot, textSpan.Start, textSpan.Length),
AddNewLinesToCopilotText(copilotStatement!, indentText, characterLimit: 120));
list.Add(proposedEdit);
return null;
}

return list;

static string AddNewLinesToCopilotText(string copilotText, string? indentText, int characterLimit)
{
// Double check that the resultant from Copilot does not produce any strings containing new line characters.
copilotText = Regex.Replace(copilotText, @"\r?\n", " ");
Copy link
Member Author

Choose a reason for hiding this comment

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

This is just a secondary check that the model does not return text that contains any new line trivia since, if it does, it gets inserted incorrectly.

Copy link
Member

Choose a reason for hiding this comment

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

Could we add tests to cover some basic scenarios?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah will do in a follow-up!

var builder = new StringBuilder();
var words = copilotText.Split(' ');
var currentLineLength = 0;