Skip to content

Commit

Permalink
fixing bug in adding comment on to vars/constants
Browse files Browse the repository at this point in the history
  • Loading branch information
bc3tech committed Feb 7, 2024
1 parent 00e2d3d commit ccd5f7a
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 49 deletions.
4 changes: 2 additions & 2 deletions DocGpt.Roslyn/DocGptCodeAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ protected override async Task<Document> GetChangedDocumentAsync(CancellationToke
SyntaxNode root = await _doc.GetSyntaxRootAsync(cancellationToken);
SyntaxNode node = root.FindNode(diagnosticSpan);

var newNode = await DocGptExecutor.AddXmlDocumentationAsync(node, cancellationToken);
var (newNode, nodeToReplace) = await DocGptExecutor.AddXmlDocumentationAsync(node, cancellationToken);

SyntaxNode newRoot = root.ReplaceNode(node, newNode);
SyntaxNode newRoot = root.ReplaceNode(nodeToReplace, newNode);

// return a document with the new syntax root
return _doc.WithSyntaxRoot(Formatter.Format(newRoot, _doc.Project.Solution.Workspace));
Expand Down
84 changes: 40 additions & 44 deletions DocGpt.Roslyn/DocGptExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,35 +42,34 @@ internal static class DocGptExecutor
/// <param name="node">The node to add XML documentation to.</param>
/// <param name="cancellationToken">A cancellation token for the operation.</param>
/// <returns>A Task returning a Document with the new XML documentation added.</returns>
public static async Task<SyntaxNode> AddXmlDocumentationAsync(SyntaxNode node, CancellationToken cancellationToken)
public static async Task<(SyntaxNode newNode, SyntaxNode nodeToReplace)> AddXmlDocumentationAsync(SyntaxNode node, CancellationToken cancellationToken)
{
// Find the node at the diagnostic span
if (node != null)
_ = node ?? throw new ArgumentNullException(nameof(node));

if (HasOverrideModifier(node) && DocGptOptions.Instance.OverridesBehavior is OverrideBehavior.UseInheritDoc)
{
if (HasOverrideModifier(node) && DocGptOptions.Instance.OverridesBehavior is OverrideBehavior.UseInheritDoc)
{
return DecorateWithInheritDoc(node);
}
return (DecorateWithInheritDoc(node), node);
}

if (IsConstantLiteral(node, out var parentField) && DocGptOptions.Instance.UseValueForLiteralConstants is true)
{
return DecorateWithValueAsSummary(parentField);
}
if (IsConstantLiteral(node, out var parentField) && DocGptOptions.Instance.UseValueForLiteralConstants is true)
{
return (DecorateWithValueAsSummary(parentField), parentField);
}

// Get the body of the method
string code = node.GetText().ToString();
// Get the body of the method
string code = node.GetText().ToString();

try
{
OpenAIClient client = DocGptOptions.Instance.GetClient();
try
{
OpenAIClient client = DocGptOptions.Instance.GetClient();

ChatCompletionsOptions completionOptions = new ChatCompletionsOptions();
if (!string.IsNullOrWhiteSpace(DocGptOptions.Instance.ModelDeploymentName))
{
completionOptions.DeploymentName = DocGptOptions.Instance.ModelDeploymentName;
}
ChatCompletionsOptions completionOptions = new ChatCompletionsOptions();
if (!string.IsNullOrWhiteSpace(DocGptOptions.Instance.ModelDeploymentName))
{
completionOptions.DeploymentName = DocGptOptions.Instance.ModelDeploymentName;
}

completionOptions.Messages.Add(new ChatRequestUserMessage($@"You are to take the C# code below and create a valid XML Documentation summary block for it according to .NET specifications. Use the following steps to determine what you compute for the answer:
completionOptions.Messages.Add(new ChatRequestUserMessage($@"You are to take the C# code below and create a valid XML Documentation summary block for it according to .NET specifications. Use the following steps to determine what you compute for the answer:
1. If the given code is not a complete C# type or member declaration, stop computing and return nothing.
2. If you're not able to discern the purpose of the code with reasonable certainty, just return `/// <summary />`
Expand All @@ -81,33 +80,30 @@ public static async Task<SyntaxNode> AddXmlDocumentationAsync(SyntaxNode node, C
You are to give back only the XML documentation wrapped in a code block (```), do not respond with any other text."));

try
{
Azure.Response<ChatCompletions> completion = await client.GetChatCompletionsAsync(completionOptions, cancellationToken);
string comment = completion.Value.Choices[0].Message.Content;
ExtractXmlDocComment(ref comment);

SyntaxTriviaList commentTrivia = SyntaxFactory.ParseLeadingTrivia(comment).InsertRange(0, node.GetLeadingTrivia());
// Add the comment to the start of the node found by the analyzer
return node.WithLeadingTrivia(commentTrivia);
}
catch (Exception e)
{
if (!(e is TaskCanceledException) && !(e is OperationCanceledException))
{
System.Diagnostics.Debugger.Break();
}
try
{
Azure.Response<ChatCompletions> completion = await client.GetChatCompletionsAsync(completionOptions, cancellationToken);
string comment = completion.Value.Choices[0].Message.Content;
ExtractXmlDocComment(ref comment);

throw;
}
SyntaxTriviaList commentTrivia = SyntaxFactory.ParseLeadingTrivia(comment).InsertRange(0, node.GetLeadingTrivia());
// Add the comment to the start of the node found by the analyzer
return (node.WithLeadingTrivia(commentTrivia), node);
}
catch (ArgumentNullException e)
catch (Exception e)
{
return node.WithLeadingTrivia(SyntaxFactory.Comment($"// Missing {e.ParamName} - Make sure you've entered the necessary information in Tools | Options | Doc GPT and try again.\r\n"));
if (!(e is TaskCanceledException) && !(e is OperationCanceledException))
{
System.Diagnostics.Debugger.Break();
}

throw;
}
}

return node;
catch (ArgumentNullException e)
{
return (node.WithLeadingTrivia(SyntaxFactory.Comment($"// Missing {e.ParamName} - Make sure you've entered the necessary information in Tools | Options | Doc GPT and try again.\r\n")), node);
}
}

internal static bool NodeTriggersGpt(SyntaxNode node)
Expand Down
4 changes: 2 additions & 2 deletions DocGpt.Test/AnalyzerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class MyClass
/// </summary>
/// <returns>A Task.</returns>
[TestMethod]
public async Task AnalyzerThrows_ConstLiteralMember()
public async Task AnalyzerThrows_ConstLiteralMember_UseValueForComment()
{
DocGptOptions.Instance.UseValueForLiteralConstants = true;

Expand Down Expand Up @@ -114,7 +114,7 @@ internal class MyClass
/// </summary>
/// <returns>A Task.</returns>
[TestMethod]
public async Task AnalyzerThrows_ConstLiteralMember_DoNotUseInheritDoc()
public async Task AnalyzerThrows_ConstLiteralMember_DoNotUseValueForComment()
{
DocGptOptions.Instance.UseValueForLiteralConstants = false;

Expand Down
2 changes: 2 additions & 0 deletions DocGpt.Test/CodeFixTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public static void AsmInit(TestContext _)
DocGptOptions.Instance.ModelDeploymentName = "foo";
}

private const string ApiKey = "foo";

/// <summary>
/// Analyzers the throws class decl.
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion DocGpt.Vsix/source.extension.vsixmanifest
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" ?>
<PackageManifest Version="2.0.0" xmlns="http://schemas.microsoft.com/developer/vsx-schema/2011" xmlns:d="http://schemas.microsoft.com/developer/vsx-schema-design/2011">
<Metadata>
<Identity Id="DocGPT.b5edf920-0ac9-4255-9182-ed263a6e9b18" Version="1.0.0" Language="en-US" Publisher="Brandon H" />
<Identity Id="DocGPT.b5edf920-0ac9-4255-9182-ed263a6e9b18" Version="1.0.1" Language="en-US" Publisher="Brandon H" />
<DisplayName>Doc GPT</DisplayName>
<Description xml:space="preserve">Adds XML Documentation derived from an OpenAI GPT endpoint to .NET members. Can be used with either Azure OpenAI service or OpenAI.com account.</Description>
<MoreInfo>https://github.com/bc3tech/docgpt</MoreInfo>
Expand Down

0 comments on commit ccd5f7a

Please sign in to comment.