Repository: MessagePack-CSharp/MessagePack-CSharp

Issue:
MessagePackAnalyzer MessagePackCodeFix Will overwrite comments
#### Bug description

Using Message PackCodeFix to fix code may result in missing comments

#### Repro steps

Execute Message PackCodeFix

#### Expected behavior

Just adding new attributes without affecting comments

#### Actual behavior

Code comments are overwritten

- Version used: 2.5.140
- Runtime: Unity 2021.3.38


Hints:
![image](https://github.com/MessagePack-CSharp/MessagePack-CSharp/assets/19683667/130f44e6-4138-4b26-a98e-c25887d3e0bf)
Actual behavior：
![image](https://github.com/MessagePack-CSharp/MessagePack-CSharp/assets/19683667/3dde48dc-c2a9-4f81-9bd2-8e9b68b01725)
Expected behavior：
![image](https://github.com/MessagePack-CSharp/MessagePack-CSharp/assets/19683667/4be9a9cb-a7fd-43cc-a6b7-c0f3378b1a6c)

Do you have any improvement ideas? I am not familiar with RoslynAnalyzer and tried to make some changes myself, but it was not successful.

If there is any solution, I can quickly test it.
Thank you for your report. Yes, I've dealt with bugs like this with other code fixes and plan to fix this.
Interesting... it only repros with _fields_. Properties retain their comments.

Patch:
diff --git a/src/MessagePack.Analyzers.CodeFixes/CodeFixes/MessagePackCodeFixProvider.cs b/src/MessagePack.Analyzers.CodeFixes/CodeFixes/MessagePackCodeFixProvider.cs
index 428f711aa..0e9d0bb96 100644
--- a/src/MessagePack.Analyzers.CodeFixes/CodeFixes/MessagePackCodeFixProvider.cs
+++ b/src/MessagePack.Analyzers.CodeFixes/CodeFixes/MessagePackCodeFixProvider.cs
@@ -1,12 +1,8 @@
 // Copyright (c) All contributors. All rights reserved.
 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
 
-using System;
 using System.Collections.Immutable;
 using System.Composition;
-using System.Linq;
-using System.Threading;
-using System.Threading.Tasks;
 using MessagePack.SourceGenerator;
 using MessagePack.SourceGenerator.Analyzers;
 using Microsoft.CodeAnalysis;
@@ -168,6 +164,13 @@ private static async Task<Solution> AddKeyAttributeAsync(Document document, INam
                 var node = await member.DeclaringSyntaxReferences[0].GetSyntaxAsync(cancellationToken).ConfigureAwait(false);
                 var documentEditor = await solutionEditor.GetDocumentEditorAsync(document.Project.Solution.GetDocumentId(node.SyntaxTree), cancellationToken).ConfigureAwait(false);
                 var syntaxGenerator = SyntaxGenerator.GetGenerator(documentEditor.OriginalDocument);
+
+                // Preserve comments on fields.
+                if (node is VariableDeclaratorSyntax)
+                {
+                    node = node.Parent;
+                }
+
                 documentEditor.AddAttribute(node, syntaxGenerator.Attribute("MessagePack.KeyAttribute", syntaxGenerator.LiteralExpression(startOrder++)));
             }
         }


Test patch:
diff --git a/tests/MessagePack.SourceGenerator.Tests/MessagePackAnalyzerTests.cs b/tests/MessagePack.SourceGenerator.Tests/MessagePackAnalyzerTests.cs
index c8b3bab9f..c77bb52ab 100644
--- a/tests/MessagePack.SourceGenerator.Tests/MessagePackAnalyzerTests.cs
+++ b/tests/MessagePack.SourceGenerator.Tests/MessagePackAnalyzerTests.cs
@@ -120,6 +120,36 @@ public class Foo
         }.RunAsync();
     }
 
+    [Fact]
+    public async Task AddAttributeToField_WithComment()
+    {
+        string input = Preamble + @"
+[MessagePackObject]
+public class Foo
+{
+    // comment
+    public string {|MsgPack004:Member1|};
+}
+";
+
+        string output = Preamble + @"
+[MessagePackObject]
+public class Foo
+{
+    // comment
+    [Key(0)]
+    public string Member1;
+}
+";
+
+        await new VerifyCS.Test
+        {
+            TestCode = input,
+            FixedCode = output,
+            MarkupOptions = MarkupOptions.UseFirstDescriptor,
+        }.RunAsync();
+    }
+
     [Fact]
     public async Task AddAttributeToType()
     {