|
1 | | - |
| 1 | +//Michel Posseth 2025-05-17 last mod |
| 2 | +//multiple code fixes due to feedback from the community |
| 3 | +using System.Linq; |
2 | 4 | using System.Composition; |
3 | 5 | using System.Threading; |
4 | 6 | using System.Threading.Tasks; |
5 | 7 | using Microsoft.CodeAnalysis; |
6 | | -using Microsoft.CodeAnalysis.Text; |
| 8 | +using System.Collections.Generic; |
7 | 9 | using System.Collections.Immutable; |
8 | | -using Microsoft.CodeAnalysis.Rename; |
| 10 | +using Microsoft.CodeAnalysis.CSharp; |
9 | 11 | using Microsoft.CodeAnalysis.CodeFixes; |
10 | | -using Microsoft.CodeAnalysis.CSharp.Syntax; |
11 | 12 | using Microsoft.CodeAnalysis.CodeActions; |
12 | | -using Microsoft.CodeAnalysis.CSharp; |
13 | | -using System.Linq; |
14 | | -using System.Xml.Linq; |
| 13 | +using Microsoft.CodeAnalysis.CSharp.Syntax; |
15 | 14 | namespace Posseth.NamedArguments.AnalyzerAndFixer |
16 | 15 | { |
17 | | - [ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(NamedArgumentsCodeFixProvider)), Shared] |
| 16 | + [ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(NamedArgumentsCodeFixProvider)), Shared] |
18 | 17 | public class NamedArgumentsCodeFixProvider : CodeFixProvider |
19 | 18 | { |
20 | 19 | private const string Title = "Use named argument"; |
@@ -43,41 +42,148 @@ public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context) |
43 | 42 | private async Task<Document> UseNamedArgumentAsync(Document document, ArgumentSyntax argument, CancellationToken cancellationToken) |
44 | 43 | { |
45 | 44 | var semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false); |
| 45 | + var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false); |
| 46 | + |
| 47 | + // First, process any nested invocations within this argument |
| 48 | + var processedRoot = ProcessNestedInvocations(root, argument, semanticModel); |
| 49 | + |
| 50 | + // Then process the current argument |
46 | 51 | var argumentList = argument.Parent as ArgumentListSyntax; |
47 | | - var invocation = argumentList?.Parent as InvocationExpressionSyntax; |
48 | | - |
49 | | - if (invocation == null) |
| 52 | + if (argumentList == null) |
| 53 | + return document.WithSyntaxRoot(processedRoot); |
| 54 | + |
| 55 | + var invocation = argumentList.Parent; |
| 56 | + |
| 57 | + // Handle both direct invocations and object creation expressions |
| 58 | + if (invocation == null || |
| 59 | + !(invocation is InvocationExpressionSyntax || |
| 60 | + invocation is ObjectCreationExpressionSyntax)) |
50 | 61 | { |
51 | | - // If the parent is not an InvocationExpressionSyntax, we cannot apply the fix |
52 | | - return document; |
| 62 | + return document.WithSyntaxRoot(processedRoot); |
53 | 63 | } |
54 | 64 |
|
55 | | - var methodSymbol = semanticModel.GetSymbolInfo(invocation).Symbol as IMethodSymbol; |
| 65 | + // Find the corresponding argument in the processed root |
| 66 | + var currentArgument = FindCorrespondingNode(processedRoot, argument); |
| 67 | + if (currentArgument == null) |
| 68 | + return document.WithSyntaxRoot(processedRoot); |
| 69 | + |
| 70 | + IMethodSymbol methodSymbol = null; |
| 71 | + |
| 72 | + if (invocation is InvocationExpressionSyntax invocationExpr) |
| 73 | + { |
| 74 | + methodSymbol = semanticModel.GetSymbolInfo(invocationExpr).Symbol as IMethodSymbol; |
| 75 | + } |
| 76 | + else if (invocation is ObjectCreationExpressionSyntax creationExpr) |
| 77 | + { |
| 78 | + methodSymbol = semanticModel.GetSymbolInfo(creationExpr).Symbol as IMethodSymbol; |
| 79 | + } |
56 | 80 |
|
57 | 81 | if (methodSymbol == null) |
58 | 82 | { |
59 | | - // If methodSymbol is null, we cannot apply the fix |
60 | | - return document; |
| 83 | + return document.WithSyntaxRoot(processedRoot); |
| 84 | + } |
| 85 | + |
| 86 | + int parameterIndex = argumentList.Arguments.IndexOf(argument); |
| 87 | + if (parameterIndex >= methodSymbol.Parameters.Length) |
| 88 | + { |
| 89 | + return document.WithSyntaxRoot(processedRoot); |
61 | 90 | } |
62 | 91 |
|
63 | | - var parameter = methodSymbol.Parameters[argumentList.Arguments.IndexOf(argument)]; |
| 92 | + var parameter = methodSymbol.Parameters[parameterIndex]; |
64 | 93 |
|
65 | 94 | if (parameter == null) |
66 | 95 | { |
67 | | - // If parameter is null, we cannot apply the fix |
68 | | - return document; |
| 96 | + return document.WithSyntaxRoot(processedRoot); |
69 | 97 | } |
70 | 98 |
|
71 | 99 | // Create a new argument node with a NameColon |
72 | 100 | var namedArgument = SyntaxFactory.Argument( |
73 | | - SyntaxFactory.NameColon(parameter.Name), // Here you add the name of the parameter |
74 | | - argument.RefOrOutKeyword, |
75 | | - argument.Expression); |
| 101 | + SyntaxFactory.NameColon(parameter.Name), |
| 102 | + currentArgument.RefOrOutKeyword, |
| 103 | + currentArgument.Expression); |
76 | 104 |
|
77 | | - var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false); |
78 | | - var newRoot = root.ReplaceNode(argument, namedArgument); |
| 105 | + var newRoot = processedRoot.ReplaceNode(currentArgument, namedArgument); |
79 | 106 |
|
80 | 107 | return document.WithSyntaxRoot(newRoot); |
81 | 108 | } |
| 109 | + |
| 110 | + // Helper method to find a corresponding node in a new syntax tree |
| 111 | + private ArgumentSyntax FindCorrespondingNode(SyntaxNode root, ArgumentSyntax originalNode) |
| 112 | + { |
| 113 | + // Find the node at the same position |
| 114 | + var nodeAtSamePosition = root.FindNode(originalNode.Span); |
| 115 | + if (nodeAtSamePosition is ArgumentSyntax arg) |
| 116 | + return arg; |
| 117 | + |
| 118 | + // If location-based search failed, try finding by structure |
| 119 | + var parentList = originalNode.Parent as ArgumentListSyntax; |
| 120 | + if (parentList != null) |
| 121 | + { |
| 122 | + int index = parentList.Arguments.IndexOf(originalNode); |
| 123 | + var newParentList = root.DescendantNodes() |
| 124 | + .OfType<ArgumentListSyntax>() |
| 125 | + .FirstOrDefault(a => a.Span.Contains(parentList.Span)); |
| 126 | + |
| 127 | + if (newParentList != null && index >= 0 && index < newParentList.Arguments.Count) |
| 128 | + return newParentList.Arguments[index]; |
| 129 | + } |
| 130 | + |
| 131 | + return null; |
| 132 | + } |
| 133 | + |
| 134 | + // New method to process nested invocations |
| 135 | + private SyntaxNode ProcessNestedInvocations(SyntaxNode root, ArgumentSyntax argument, SemanticModel semanticModel) |
| 136 | + { |
| 137 | + // Find all nested invocations within this argument |
| 138 | + var nestedInvocations = argument.DescendantNodes() |
| 139 | + .OfType<InvocationExpressionSyntax>() |
| 140 | + .ToList(); |
| 141 | + |
| 142 | + if (nestedInvocations.Count == 0) |
| 143 | + return root; |
| 144 | + |
| 145 | + // Process each nested invocation |
| 146 | + return root.ReplaceNodes( |
| 147 | + nestedInvocations, |
| 148 | + (original, _) => ProcessNestedInvocation(original, original, semanticModel)); |
| 149 | + } |
| 150 | + |
| 151 | + private SyntaxNode ProcessNestedInvocation(SyntaxNode original, SyntaxNode rewritten, SemanticModel semanticModel) |
| 152 | + { |
| 153 | + var invocation = rewritten as InvocationExpressionSyntax; |
| 154 | + if (invocation == null) |
| 155 | + return rewritten; |
| 156 | + |
| 157 | + var methodSymbol = semanticModel.GetSymbolInfo(invocation).Symbol as IMethodSymbol; |
| 158 | + if (methodSymbol == null) |
| 159 | + return rewritten; |
| 160 | + |
| 161 | + var argList = invocation.ArgumentList; |
| 162 | + var newArgs = new List<ArgumentSyntax>(); |
| 163 | + bool changed = false; |
| 164 | + |
| 165 | + for (int i = 0; i < argList.Arguments.Count; i++) |
| 166 | + { |
| 167 | + var arg = argList.Arguments[i]; |
| 168 | + if (arg.NameColon == null && i < methodSymbol.Parameters.Length) |
| 169 | + { |
| 170 | + changed = true; |
| 171 | + newArgs.Add(SyntaxFactory.Argument( |
| 172 | + SyntaxFactory.NameColon(methodSymbol.Parameters[i].Name), |
| 173 | + arg.RefOrOutKeyword, |
| 174 | + arg.Expression)); |
| 175 | + } |
| 176 | + else |
| 177 | + { |
| 178 | + newArgs.Add(arg); |
| 179 | + } |
| 180 | + } |
| 181 | + |
| 182 | + if (!changed) |
| 183 | + return rewritten; |
| 184 | + |
| 185 | + return invocation.WithArgumentList( |
| 186 | + SyntaxFactory.ArgumentList(SyntaxFactory.SeparatedList(newArgs))); |
| 187 | + } |
82 | 188 | } |
83 | 189 | } |
0 commit comments