diff --git a/src/EditorFeatures/Core/DocumentationComments/CopilotGenerateDocumentationCommentProvider.cs b/src/EditorFeatures/Core/DocumentationComments/CopilotGenerateDocumentationCommentProvider.cs index fe1e84615b246..deea545f19253 100644 --- a/src/EditorFeatures/Core/DocumentationComments/CopilotGenerateDocumentationCommentProvider.cs +++ b/src/EditorFeatures/Core/DocumentationComments/CopilotGenerateDocumentationCommentProvider.cs @@ -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> 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; + 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 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", " "); var builder = new StringBuilder(); var words = copilotText.Split(' '); var currentLineLength = 0;