Skip to content

Commit e660b95

Browse files
authored
Merge pull request #20 from Flow-Launcher/context_diagnostic
Improve diagnostics
2 parents 8bf2747 + 5aebd62 commit e660b95

File tree

2 files changed

+28
-10
lines changed

2 files changed

+28
-10
lines changed

Flow.Launcher.Localization.SourceGenerators/Localize/LocalizeSourceGenerator.cs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -465,8 +465,9 @@ private static PluginClassInfo GetValidPluginInfo(
465465
ImmutableArray<PluginClassInfo> pluginClasses,
466466
SourceProductionContext context)
467467
{
468-
var nonNullExistClasses = pluginClasses.Where(p => p != null && p.PropertyName != null).ToArray();
469-
if (nonNullExistClasses.Length == 0)
468+
// If p is null, this class does not implement IPluginI18n
469+
var iPluginI18nClasses = pluginClasses.Where(p => p != null).ToArray();
470+
if (iPluginI18nClasses.Length == 0)
470471
{
471472
context.ReportDiagnostic(Diagnostic.Create(
472473
SourceGeneratorDiagnostics.CouldNotFindPluginEntryClass,
@@ -475,7 +476,24 @@ private static PluginClassInfo GetValidPluginInfo(
475476
return null;
476477
}
477478

478-
foreach (var pluginClass in nonNullExistClasses)
479+
// If p.PropertyName is null, this class does not have PluginInitContext property
480+
var iPluginI18nClassesWithContext = iPluginI18nClasses.Where(p => p.PropertyName != null).ToArray();
481+
if (iPluginI18nClassesWithContext.Length == 0)
482+
{
483+
foreach (var pluginClass in iPluginI18nClasses)
484+
{
485+
context.ReportDiagnostic(Diagnostic.Create(
486+
SourceGeneratorDiagnostics.CouldNotFindContextProperty,
487+
pluginClass.Location,
488+
pluginClass.ClassName
489+
));
490+
}
491+
return null;
492+
}
493+
494+
// Rest classes have implemented IPluginI18n and have PluginInitContext property
495+
// Check if the property is valid
496+
foreach (var pluginClass in iPluginI18nClassesWithContext)
479497
{
480498
if (pluginClass.IsValid == true)
481499
{

Flow.Launcher.Localization.SourceGenerators/SourceGeneratorDiagnostics.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public static class SourceGeneratorDiagnostics
88
public static readonly DiagnosticDescriptor CouldNotFindResourceDictionaries = new DiagnosticDescriptor(
99
"FLSG0001",
1010
"Could not find resource dictionaries",
11-
"Could not find resource dictionaries. There must be a file named [LANG].xaml file (for example, en.xaml), and it must be specified in <AdditionalFiles /> in your .csproj file.",
11+
"Could not find resource dictionaries. There must be a `en.xaml` file under `Language` folder.",
1212
"Localization",
1313
DiagnosticSeverity.Warning,
1414
isEnabledByDefault: true
@@ -17,7 +17,7 @@ public static class SourceGeneratorDiagnostics
1717
public static readonly DiagnosticDescriptor CouldNotFindPluginEntryClass = new DiagnosticDescriptor(
1818
"FLSG0002",
1919
"Could not find the main class of plugin",
20-
$"Could not find the main class of your plugin. It must implement {Constants.PluginInterfaceName}.",
20+
$"Could not find the main class of your plugin. It must implement `{Constants.PluginInterfaceName}`.",
2121
"Localization",
2222
DiagnosticSeverity.Warning,
2323
isEnabledByDefault: true
@@ -26,7 +26,7 @@ public static class SourceGeneratorDiagnostics
2626
public static readonly DiagnosticDescriptor CouldNotFindContextProperty = new DiagnosticDescriptor(
2727
"FLSG0003",
2828
"Could not find plugin context property",
29-
$"Could not find a property of type {Constants.PluginContextTypeName} in {{0}}. It must be a public static or internal static property of the main class of your plugin.",
29+
$"Could not find a property of type `{Constants.PluginContextTypeName}` in `{{0}}`. It must be a public static or internal static property of the main class of your plugin.",
3030
"Localization",
3131
DiagnosticSeverity.Warning,
3232
isEnabledByDefault: true
@@ -35,7 +35,7 @@ public static class SourceGeneratorDiagnostics
3535
public static readonly DiagnosticDescriptor ContextPropertyNotStatic = new DiagnosticDescriptor(
3636
"FLSG0004",
3737
"Plugin context property is not static",
38-
"Context property {0} is not static. It must be static.",
38+
"Context property `{0}` is not static. It must be static.",
3939
"Localization",
4040
DiagnosticSeverity.Warning,
4141
isEnabledByDefault: true
@@ -44,7 +44,7 @@ public static class SourceGeneratorDiagnostics
4444
public static readonly DiagnosticDescriptor ContextPropertyIsPrivate = new DiagnosticDescriptor(
4545
"FLSG0005",
4646
"Plugin context property is private",
47-
"Context property {0} is private. It must be either internal or public.",
47+
"Context property `{0}` is private. It must be either internal or public.",
4848
"Localization",
4949
DiagnosticSeverity.Warning,
5050
isEnabledByDefault: true
@@ -53,7 +53,7 @@ public static class SourceGeneratorDiagnostics
5353
public static readonly DiagnosticDescriptor ContextPropertyIsProtected = new DiagnosticDescriptor(
5454
"FLSG0006",
5555
"Plugin context property is protected",
56-
"Context property {0} is protected. It must be either internal or public.",
56+
"Context property `{0}` is protected. It must be either internal or public.",
5757
"Localization",
5858
DiagnosticSeverity.Warning,
5959
isEnabledByDefault: true
@@ -62,7 +62,7 @@ public static class SourceGeneratorDiagnostics
6262
public static readonly DiagnosticDescriptor LocalizationKeyUnused = new DiagnosticDescriptor(
6363
"FLSG0007",
6464
"Localization key is unused",
65-
$"Method '{Constants.ClassName}.{{0}}' is never used",
65+
$"Method `{Constants.ClassName}.{{0}}` is never used",
6666
"Localization",
6767
DiagnosticSeverity.Warning,
6868
isEnabledByDefault: true

0 commit comments

Comments
 (0)