ksanjeev284 commented on code in PR #9:
URL: 
https://github.com/apache/lucenenet-codeanalysis-dev/pull/9#discussion_r2404464652


##########
src/Lucene.Net.CodeAnalysis.Dev.CodeFixes/LuceneDev1xxx/LuceneDev1001_FloatingPointFormattingCSCodeFixProvider.cs:
##########
@@ -36,94 +36,233 @@ namespace Lucene.Net.CodeAnalysis.Dev.CodeFixes
     public class LuceneDev1001_FloatingPointFormattingCSCodeFixProvider : 
CodeFixProvider
     {
         public override ImmutableArray<string> FixableDiagnosticIds =>
-            [Descriptors.LuceneDev1001_FloatingPointFormatting.Id];
+        [
+            Descriptors.LuceneDev1001_FloatingPointFormatting.Id,
+            Descriptors.LuceneDev1006_FloatingPointFormatting.Id
+        ];
 
         public override FixAllProvider GetFixAllProvider() =>
             WellKnownFixAllProviders.BatchFixer;
 
         public override async Task RegisterCodeFixesAsync(CodeFixContext 
context)
         {
-            Diagnostic? diagnostic = context.Diagnostics.FirstOrDefault();
-            if (diagnostic is null)
-                return;
-
             SyntaxNode? root = await 
context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);
             if (root is null)
                 return;
 
-            // the diagnostic in the analyzer is reported on the member access 
(e.g. "x.ToString")
-            // but we need the whole invocation (e.g. "x.ToString(...)").  So 
find the invocation
-            // by walking ancestors if needed.
-            SyntaxNode? node = root.FindNode(diagnostic.Location.SourceSpan);
-            if (node is null)
+            SemanticModel? semanticModel = await 
context.Document.GetSemanticModelAsync(context.CancellationToken).ConfigureAwait(false);
+            if (semanticModel is null)
                 return;
 
-            if (node is not ExpressionSyntax exprNode)
+            foreach (Diagnostic diagnostic in context.Diagnostics)
+            {
+                SyntaxNode? node = 
root.FindNode(diagnostic.Location.SourceSpan, getInnermostNodeForTie: true);
+                if (node is null)
+                    continue;
+
+                if (diagnostic.Id == 
Descriptors.LuceneDev1001_FloatingPointFormatting.Id)
+                {
+                    RegisterExplicitToStringFix(context, semanticModel, 
diagnostic, node);
+                }
+                else if (diagnostic.Id == 
Descriptors.LuceneDev1006_FloatingPointFormatting.Id)
+                {
+                    RegisterStringEmbeddingFix(context, semanticModel, 
diagnostic, node);
+                }
+            }
+        }
+
+        private void RegisterExplicitToStringFix(
+            CodeFixContext context,
+            SemanticModel semanticModel,
+            Diagnostic diagnostic,
+            SyntaxNode node)
+        {
+            if (node is not ExpressionSyntax expression)
                 return;
 
-            SemanticModel? semanticModel = await 
context.Document.GetSemanticModelAsync(context.CancellationToken).ConfigureAwait(false);
-            if (semanticModel is null)
+            if (!TryGetJ2NTypeAndMember(semanticModel, expression, out var 
j2nTypeName, out var memberAccess))
                 return;
 
-            if (!TryGetJ2NTypeAndMember(semanticModel, exprNode, out var 
j2nTypeName, out var memberAccess))
+            string codeElement = $"J2N.Numerics.{j2nTypeName}.ToString(...)";
+
+            context.RegisterCodeFix(
+                CodeActionHelper.CreateFromResource(
+                    CodeFixResources.UseX,
+                    c => ReplaceExplicitToStringAsync(context.Document, 
memberAccess, j2nTypeName, c),
+                    "UseJ2NToString",
+                    codeElement),
+                diagnostic);
+        }
+
+        private void RegisterStringEmbeddingFix(
+            CodeFixContext context,
+            SemanticModel semanticModel,
+            Diagnostic diagnostic,
+            SyntaxNode node)
+        {
+            ExpressionSyntax? expression = node as ExpressionSyntax ?? 
node.AncestorsAndSelf().OfType<ExpressionSyntax>().FirstOrDefault();
+            if (expression is null)
+                return;
+
+            if 
(!TryGetFloatingPointTypeName(semanticModel.GetTypeInfo(expression, 
context.CancellationToken), out var j2nTypeName))
                 return;
 
             string codeElement = $"J2N.Numerics.{j2nTypeName}.ToString(...)";
 
+            InterpolationSyntax? interpolation = 
expression.AncestorsAndSelf().OfType<InterpolationSyntax>().FirstOrDefault();
+            if (interpolation is not null)
+            {
+                context.RegisterCodeFix(
+                    CodeActionHelper.CreateFromResource(
+                        CodeFixResources.UseX,
+                        c => 
ReplaceInterpolationExpressionAsync(context.Document, interpolation, 
expression, j2nTypeName, c),
+                        "UseJ2NToString",
+                        codeElement),
+                    diagnostic);
+
+                return;
+            }
+
             context.RegisterCodeFix(
                 CodeActionHelper.CreateFromResource(
                     CodeFixResources.UseX,
-                    c => ReplaceWithJ2NToStringAsync(context.Document, 
memberAccess, c),
+                    c => ReplaceConcatenationExpressionAsync(context.Document, 
expression, j2nTypeName, c),
                     "UseJ2NToString",
                     codeElement),
                 diagnostic);
         }
 
-        private async Task<Document> ReplaceWithJ2NToStringAsync(
+        private async Task<Document> ReplaceExplicitToStringAsync(
             Document document,
             MemberAccessExpressionSyntax memberAccess,
+            string j2nTypeName,
             CancellationToken cancellationToken)
         {
-            SemanticModel? semanticModel = await 
document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);
-            if (semanticModel is null)
+            if (memberAccess.Parent is not InvocationExpressionSyntax 
invocation)
                 return document;
 
-            if (!TryGetJ2NTypeAndMember(semanticModel, memberAccess, out var 
j2nTypeName, out _))
-                return document;
+            var newArguments = new List<ArgumentSyntax>
+            {
+                SyntaxFactory.Argument(memberAccess.Expression.WithoutTrivia())
+            };
+
+            if (invocation.ArgumentList is not null)
+                newArguments.AddRange(invocation.ArgumentList.Arguments);
+
+            InvocationExpressionSyntax replacement = 
CreateJ2NToStringInvocation(j2nTypeName, newArguments)
+                .WithTriviaFrom(invocation);
+
+            DocumentEditor editor = await DocumentEditor.CreateAsync(document, 
cancellationToken).ConfigureAwait(false);
+            editor.ReplaceNode(invocation, replacement);
+
+            return editor.GetChangedDocument();
+        }
+
+        private async Task<Document> ReplaceConcatenationExpressionAsync(
+            Document document,
+            ExpressionSyntax expression,
+            string j2nTypeName,
+            CancellationToken cancellationToken)
+        {
+            var arguments = new List<ArgumentSyntax>
+            {
+                SyntaxFactory.Argument(expression.WithoutTrivia())
+            };
+
+            InvocationExpressionSyntax replacement = 
CreateJ2NToStringInvocation(j2nTypeName, arguments)
+                .WithLeadingTrivia(expression.GetLeadingTrivia())
+                .WithTrailingTrivia(expression.GetTrailingTrivia());
+
+            DocumentEditor editor = await DocumentEditor.CreateAsync(document, 
cancellationToken).ConfigureAwait(false);
+            editor.ReplaceNode(expression, replacement);
+
+            return editor.GetChangedDocument();
+        }
+
+        private async Task<Document> ReplaceInterpolationExpressionAsync(

Review Comment:
   Thanks for the feedback! I’ll fix the alignment clause issue and disable 
LuceneDev1006 until the custom interpolation handler is ready.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to