Skip to content

Commit ba18f75

Browse files
committed
Code fix provider for localization: when fixing "property is not static" error, also fix it being too restrictive
1 parent 4f24b72 commit ba18f75

File tree

2 files changed

+24
-19
lines changed

2 files changed

+24
-19
lines changed

Flow.Launcher.Analyzers/AnalyzerDiagnostics.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public static class AnalyzerDiagnostics
1616
public static readonly DiagnosticDescriptor ContextIsAField = new DiagnosticDescriptor(
1717
"FLAN0002",
1818
"Plugin context is a field",
19-
"Plugin context must be a static property instead",
19+
"Plugin context must be at least internal static property",
2020
"Localization",
2121
DiagnosticSeverity.Error,
2222
isEnabledByDefault: true
@@ -25,7 +25,7 @@ public static class AnalyzerDiagnostics
2525
public static readonly DiagnosticDescriptor ContextIsNotStatic = new DiagnosticDescriptor(
2626
"FLAN0003",
2727
"Plugin context is not static",
28-
"Plugin context must be a static property",
28+
"Plugin context must be at least internal static property",
2929
"Localization",
3030
DiagnosticSeverity.Error,
3131
isEnabledByDefault: true
@@ -34,7 +34,7 @@ public static class AnalyzerDiagnostics
3434
public static readonly DiagnosticDescriptor ContextAccessIsTooRestrictive = new DiagnosticDescriptor(
3535
"FLAN0004",
3636
"Plugin context property access modifier is too restrictive",
37-
"Plugin context property must be internal or public",
37+
"Plugin context property must be at least internal static property",
3838
"Localization",
3939
DiagnosticSeverity.Error,
4040
isEnabledByDefault: true
@@ -43,7 +43,7 @@ public static class AnalyzerDiagnostics
4343
public static readonly DiagnosticDescriptor ContextIsNotDeclared = new DiagnosticDescriptor(
4444
"FLAN0005",
4545
"Plugin context is not declared",
46-
"Plugin context must be a static property of type `PluginInitContext`",
46+
"Plugin context must be at least internal static property of type `PluginInitContext`",
4747
"Localization",
4848
DiagnosticSeverity.Error,
4949
isEnabledByDefault: true

Flow.Launcher.Analyzers/Localize/ContextAvailabilityAnalyzerCodeFixProvider.cs

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ private static Document FixContextIsNotStaticError(CodeFixContext context, Synta
123123
var propertyDeclaration = GetDeclarationSyntax<PropertyDeclarationSyntax>(root, diagnosticSpan);
124124
if (propertyDeclaration is null) return context.Document;
125125

126-
var newPropertyDeclaration = propertyDeclaration.AddModifiers(SyntaxFactory.Token(SyntaxKind.StaticKeyword));
126+
var newPropertyDeclaration = FixRestrictivePropertyModifiers(propertyDeclaration).AddModifiers(SyntaxFactory.Token(SyntaxKind.StaticKeyword));
127127

128128
var newRoot = root.ReplaceNode(propertyDeclaration, newPropertyDeclaration);
129129
return context.Document.WithSyntaxRoot(newRoot);
@@ -134,20 +134,7 @@ private static Document FixContextIsTooRestricted(CodeFixContext context, Syntax
134134
var propertyDeclaration = GetDeclarationSyntax<PropertyDeclarationSyntax>(root, diagnosticSpan);
135135
if (propertyDeclaration is null) return context.Document;
136136

137-
var newModifiers = SyntaxFactory.TokenList();
138-
foreach (var modifier in propertyDeclaration.Modifiers)
139-
{
140-
if (modifier.IsKind(SyntaxKind.PrivateKeyword) || modifier.IsKind(SyntaxKind.ProtectedKeyword))
141-
{
142-
newModifiers = newModifiers.Add(SyntaxFactory.Token(SyntaxKind.InternalKeyword));
143-
}
144-
else
145-
{
146-
newModifiers = newModifiers.Add(modifier);
147-
}
148-
}
149-
150-
var newPropertyDeclaration = propertyDeclaration.WithModifiers(newModifiers);
137+
var newPropertyDeclaration = FixRestrictivePropertyModifiers(propertyDeclaration);
151138

152139
var newRoot = root.ReplaceNode(propertyDeclaration, newPropertyDeclaration);
153140
return context.Document.WithSyntaxRoot(newRoot);
@@ -172,6 +159,24 @@ private static Document FixContextIsAFieldError(CodeFixContext context, SyntaxNo
172159
return GetFormattedDocument(context, newRoot);
173160
}
174161

162+
private static PropertyDeclarationSyntax FixRestrictivePropertyModifiers(PropertyDeclarationSyntax propertyDeclaration)
163+
{
164+
var newModifiers = SyntaxFactory.TokenList();
165+
foreach (var modifier in propertyDeclaration.Modifiers)
166+
{
167+
if (modifier.IsKind(SyntaxKind.PrivateKeyword) || modifier.IsKind(SyntaxKind.ProtectedKeyword))
168+
{
169+
newModifiers = newModifiers.Add(SyntaxFactory.Token(SyntaxKind.InternalKeyword));
170+
}
171+
else
172+
{
173+
newModifiers = newModifiers.Add(modifier);
174+
}
175+
}
176+
177+
return propertyDeclaration.WithModifiers(newModifiers);
178+
}
179+
175180
private static T GetDeclarationSyntax<T>(SyntaxNode root, TextSpan diagnosticSpan) where T : SyntaxNode =>
176181
root
177182
.FindToken(diagnosticSpan.Start)

0 commit comments

Comments
 (0)