Skip to content

Commit 8805539

Browse files
committed
Support string.Format culture info parameter code fixer
1 parent f12be6d commit 8805539

File tree

1 file changed

+29
-11
lines changed

1 file changed

+29
-11
lines changed

Flow.Launcher.Localization.Analyzers/Localize/OldGetTranslateAnalyzerCodeFixProvider.cs

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,22 +49,38 @@ private static Document FixOldTranslation(CodeFixContext context, SyntaxNode roo
4949
{
5050
var diagnosticSpan = diagnostic.Location.SourceSpan;
5151

52+
if (root is null) return context.Document;
53+
5254
var invocationExpr = root
5355
?.FindToken(diagnosticSpan.Start).Parent
5456
?.AncestorsAndSelf()
5557
.OfType<InvocationExpressionSyntax>()
56-
.First();
58+
.FirstOrDefault();
5759

58-
if (invocationExpr is null || root is null) return context.Document;
60+
if (invocationExpr is null) return context.Document;
5961

6062
var argumentList = invocationExpr.ArgumentList.Arguments;
61-
var argument = argumentList.First().Expression;
62-
63-
if (GetTranslationKey(argument) is string translationKey)
64-
return FixOldTranslationWithoutStringFormat(context, translationKey, root, invocationExpr);
6563

66-
if (GetTranslationKeyFromInnerInvocation(argument) is string translationKeyInside)
67-
return FixOldTranslationWithStringFormat(context, argumentList, translationKeyInside, root, invocationExpr);
64+
// Loop through the arguments to find the translation key.
65+
for (int i = 0; i < argumentList.Count; i++)
66+
{
67+
var argument = argumentList[i].Expression;
68+
69+
// Case 1: The argument is a literal (direct GetTranslation("key"))
70+
if (GetTranslationKey(argument) is string translationKey)
71+
return FixOldTranslationWithoutStringFormat(context, translationKey, root, invocationExpr);
72+
73+
// Case 2: The argument is itself an invocation (nested GetTranslation)
74+
if (GetTranslationKeyFromInnerInvocation(argument) is string translationKeyInside)
75+
{
76+
// If there are arguments following this translation call, treat as a Format call.
77+
if (i < argumentList.Count - 1)
78+
return FixOldTranslationWithStringFormat(context, argumentList, translationKeyInside, root, invocationExpr, i);
79+
80+
// Otherwise, treat it as a direct translation call.
81+
return FixOldTranslationWithoutStringFormat(context, translationKeyInside, root, invocationExpr);
82+
}
83+
}
6884

6985
return context.Document;
7086
}
@@ -94,7 +110,7 @@ private static Document FixOldTranslationWithoutStringFormat(
94110
private static string GetTranslationKeyFromInnerInvocation(ExpressionSyntax syntax)
95111
{
96112
if (syntax is InvocationExpressionSyntax invocationExpressionSyntax &&
97-
invocationExpressionSyntax.ArgumentList.Arguments.Count is 1)
113+
invocationExpressionSyntax.ArgumentList.Arguments.Count == 1)
98114
{
99115
var firstArgument = invocationExpressionSyntax.ArgumentList.Arguments.First().Expression;
100116
return GetTranslationKey(firstArgument);
@@ -107,9 +123,11 @@ private static Document FixOldTranslationWithStringFormat(
107123
SeparatedSyntaxList<ArgumentSyntax> argumentList,
108124
string translationKey2,
109125
SyntaxNode root,
110-
InvocationExpressionSyntax invocationExpr)
126+
InvocationExpressionSyntax invocationExpr,
127+
int translationArgIndex)
111128
{
112-
var newArguments = string.Join(", ", argumentList.Skip(1).Select(a => a.Expression));
129+
// Skip all arguments before and including the translation call
130+
var newArguments = string.Join(", ", argumentList.Skip(translationArgIndex + 1).Select(a => a.Expression));
113131
var newInnerInvocationExpr = SyntaxFactory.ParseExpression($"{Constants.ClassName}.{translationKey2}({newArguments})");
114132

115133
var newRoot = root.ReplaceNode(invocationExpr, newInnerInvocationExpr);

0 commit comments

Comments
 (0)