Skip to content

Commit

Permalink
Getting const, override, and full working all together
Browse files Browse the repository at this point in the history
  • Loading branch information
bc3tech committed Jan 7, 2024
1 parent 7ec4812 commit 5f02d24
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 15 deletions.
12 changes: 8 additions & 4 deletions DocGpt.Roslyn/DocGptCodeAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,13 @@ protected override async Task<IEnumerable<CodeActionOperation>> ComputePreviewOp
SyntaxNode root = await _doc.GetSyntaxRootAsync(cancellationToken);
SyntaxNode node = root.FindNode(diagnosticSpan);

return !HasOverrideModifier(node)
? DocGptCodeActionPreviewOperation.InstanceArray
: await base.ComputePreviewOperationsAsync(cancellationToken);
if (HasOverrideModifier(node) || IsConstantLiteral(ref node))
{
return await base.ComputePreviewOperationsAsync(cancellationToken);

}

return DocGptCodeActionPreviewOperation.InstanceArray;
}

/// <inheritdoc />
Expand All @@ -65,7 +69,7 @@ private class DocGptCodeActionPreviewOperation : PreviewOperation
private DocGptCodeActionPreviewOperation() { }

/// <inheritdoc />
public override string Title { get; } = "Sends this entire member's definition to the defined OpenAI endpoint for summary text generation and applies the result.";
public override string Title { get; } = "Sends this entire member's definition (and body) to the defined OpenAI endpoint for summary text generation and applies the result.";

/// <inheritdoc />
public override Task<object> GetPreviewAsync(CancellationToken cancellationToken) => Task.FromResult<object>(Title);
Expand Down
1 change: 1 addition & 0 deletions DocGpt.Roslyn/DocGptCodeFixProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public sealed override ImmutableArray<string> FixableDiagnosticIds
get
{
return ImmutableArray.Create("CS1591", // The diagnostic id of the XML Documentation an analyzer fired when XML doc gen is turned on but missing from a visible member
"CD1606", // Diagnostic from "CodeDocumentor" analyzer (https://marketplace.visualstudio.com/items?itemName=DanTurco.CodeDocumentor)
DocGptAnalyzer.DiagnosticId);
}
}
Expand Down
6 changes: 2 additions & 4 deletions DocGpt.Roslyn/DocGptExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,17 +88,15 @@ public static async Task<Document> AddXmlDocumentationAsync(Document document, L
You are to give back only the XML documentation wrapped in a code block (```), do not respond with any other text."));

SyntaxNode syntaxRoot = await document.GetSyntaxRootAsync();
SyntaxNode diagNode = syntaxRoot.FindNode(location.SourceSpan);
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, diagNode.GetLeadingTrivia());
SyntaxTriviaList commentTrivia = SyntaxFactory.ParseLeadingTrivia(comment).InsertRange(0, node.GetLeadingTrivia());
// Add the comment to the start of the node found by the analyzer
SyntaxNode newRoot = syntaxRoot.ReplaceNode(diagNode, diagNode.WithLeadingTrivia(commentTrivia /*.Insert(0, SyntaxFactory.CarriageReturnLineFeed).Add(SyntaxFactory.CarriageReturnLineFeed)*/));
SyntaxNode newRoot = root.ReplaceNode(node, node.WithLeadingTrivia(commentTrivia /*.Insert(0, SyntaxFactory.CarriageReturnLineFeed).Add(SyntaxFactory.CarriageReturnLineFeed)*/));

// return a document with the new syntax root
document = document.WithSyntaxRoot(Formatter.Format(newRoot, document.Project.Solution.Workspace));
Expand Down
9 changes: 2 additions & 7 deletions DocGpt.Roslyn/Helpers.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
namespace DocGpt
{
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -57,14 +56,10 @@ public static bool ShouldSkip(SyntaxNode node)

public static bool IsConstantLiteral(ref SyntaxNode node)
{
if (node is VariableDeclaratorSyntax)
if (node is VariableDeclaratorSyntax
&& node.Parent?.Parent is FieldDeclarationSyntax field)
{
Debug.Assert(node.Parent.Parent is FieldDeclarationSyntax);
node = node.Parent.Parent;
}

if (node is FieldDeclarationSyntax field)
{
if (field?.Modifiers.Any(SyntaxKind.ConstKeyword) is true
&& field.Declaration.Variables.FirstOrDefault()?.Initializer?.Value is LiteralExpressionSyntax)
{
Expand Down
3 changes: 3 additions & 0 deletions DocGpt.Test/CodeFixTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,16 @@
[TestClass]
public class CodeFixTests
{
public static TestContext Context { get; private set; }

[ClassInitialize]
public static void ClassInit(TestContext context)
{
DocGptOptions.Instance.Endpoint = new System.Uri("http://localhost:5000");
DocGptOptions.Instance.ApiKey = "foo";
DocGptOptions.Instance.ModelDeploymentName = "foo";

Context = context;
}

/// <summary>
Expand Down

0 comments on commit 5f02d24

Please sign in to comment.