diff --git a/Flow.Launcher.Localization.Analyzers/AnalyzerDiagnostics.cs b/Flow.Launcher.Localization.Analyzers/AnalyzerDiagnostics.cs index 2f910d3..8317454 100644 --- a/Flow.Launcher.Localization.Analyzers/AnalyzerDiagnostics.cs +++ b/Flow.Launcher.Localization.Analyzers/AnalyzerDiagnostics.cs @@ -1,4 +1,5 @@ -using Microsoft.CodeAnalysis; +using Flow.Launcher.Localization.Shared; +using Microsoft.CodeAnalysis; namespace Flow.Launcher.Localization.Analyzers { @@ -7,7 +8,7 @@ public static class AnalyzerDiagnostics public static readonly DiagnosticDescriptor OldLocalizationApiUsed = new DiagnosticDescriptor( "FLAN0001", "Old localization API used", - "Use `Localize.{0}({1})` instead", + $"Use `{Constants.ClassName}.{{0}}({{1}})` instead", "Localization", DiagnosticSeverity.Warning, isEnabledByDefault: true @@ -43,7 +44,7 @@ public static class AnalyzerDiagnostics public static readonly DiagnosticDescriptor ContextIsNotDeclared = new DiagnosticDescriptor( "FLAN0005", "Plugin context is not declared", - "Plugin context must be at least internal static property of type `PluginInitContext`", + $"Plugin context must be at least internal static property of type `{Constants.PluginContextTypeName}`", "Localization", DiagnosticSeverity.Error, isEnabledByDefault: true diff --git a/Flow.Launcher.Localization.Analyzers/Localize/ContextAvailabilityAnalyzerCodeFixProvider.cs b/Flow.Launcher.Localization.Analyzers/Localize/ContextAvailabilityAnalyzerCodeFixProvider.cs index e009c03..54df68a 100644 --- a/Flow.Launcher.Localization.Analyzers/Localize/ContextAvailabilityAnalyzerCodeFixProvider.cs +++ b/Flow.Launcher.Localization.Analyzers/Localize/ContextAvailabilityAnalyzerCodeFixProvider.cs @@ -2,6 +2,7 @@ using System.Composition; using System.Linq; using System.Threading.Tasks; +using Flow.Launcher.Localization.Shared; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CodeActions; using Microsoft.CodeAnalysis.CodeFixes; @@ -83,7 +84,7 @@ public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context) private static MemberDeclarationSyntax GetStaticContextPropertyDeclaration(string propertyName = "Context") => SyntaxFactory.ParseMemberDeclaration( - $"internal static PluginInitContext {propertyName} {{ get; private set; }} = null!;" + $"internal static {Constants.PluginContextTypeName} {propertyName} {{ get; private set; }} = null!;" ); private static Document GetFormattedDocument(CodeFixContext context, SyntaxNode root) diff --git a/Flow.Launcher.Localization.Analyzers/Localize/OldGetTranslateAnalyzerCodeFixProvider.cs b/Flow.Launcher.Localization.Analyzers/Localize/OldGetTranslateAnalyzerCodeFixProvider.cs index 1040270..47cf329 100644 --- a/Flow.Launcher.Localization.Analyzers/Localize/OldGetTranslateAnalyzerCodeFixProvider.cs +++ b/Flow.Launcher.Localization.Analyzers/Localize/OldGetTranslateAnalyzerCodeFixProvider.cs @@ -2,6 +2,7 @@ using System.Composition; using System.Linq; using System.Threading.Tasks; +using Flow.Launcher.Localization.Shared; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CodeActions; using Microsoft.CodeAnalysis.CodeFixes; @@ -28,7 +29,7 @@ public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context) context.RegisterCodeFix( CodeAction.Create( - title: "Replace with 'Localize.localization_key(...args)'", + title: $"Replace with '{Constants.ClassName}.localization_key(...args)'", createChangedDocument: _ => Task.FromResult(FixOldTranslation(context, root, diagnostic)), equivalenceKey: AnalyzerDiagnostics.OldLocalizationApiUsed.Id ), @@ -75,7 +76,7 @@ private static Document FixOldTranslationWithoutStringFormat( CodeFixContext context, string translationKey, SyntaxNode root, InvocationExpressionSyntax invocationExpr ) { var newInvocationExpr = SyntaxFactory.ParseExpression( - $"Localize.{translationKey}()" + $"{Constants.ClassName}.{translationKey}()" ); var newRoot = root.ReplaceNode(invocationExpr, newInvocationExpr); @@ -104,7 +105,7 @@ private static Document FixOldTranslationWithStringFormat( InvocationExpressionSyntax invocationExpr ) { var newArguments = string.Join(", ", argumentList.Skip(1).Select(a => a.Expression)); - var newInnerInvocationExpr = SyntaxFactory.ParseExpression($"Localize.{translationKey2}({newArguments})"); + var newInnerInvocationExpr = SyntaxFactory.ParseExpression($"{Constants.ClassName}.{translationKey2}({newArguments})"); var newRoot = root.ReplaceNode(invocationExpr, newInnerInvocationExpr); return context.Document.WithSyntaxRoot(newRoot); diff --git a/Flow.Launcher.Localization.Shared/Constants.cs b/Flow.Launcher.Localization.Shared/Constants.cs index beac8aa..51a4168 100644 --- a/Flow.Launcher.Localization.Shared/Constants.cs +++ b/Flow.Launcher.Localization.Shared/Constants.cs @@ -12,6 +12,11 @@ public static class Constants public const string XamlPrefixUri = "http://schemas.microsoft.com/winfx/2006/xaml"; public const string XamlTag = "String"; public const string KeyAttribute = "Key"; + public const string SummaryElementName = "summary"; + public const string ParamElementName = "param"; + public const string IndexAttribute = "index"; + public const string NameAttribute = "name"; + public const string TypeAttribute = "type"; public static readonly Regex LanguagesXamlRegex = new Regex(@"\\Languages\\[^\\]+\.xaml$", RegexOptions.IgnoreCase); } diff --git a/Flow.Launcher.Localization.SourceGenerators/Localize/LocalizeSourceGenerator.cs b/Flow.Launcher.Localization.SourceGenerators/Localize/LocalizeSourceGenerator.cs index 583ea8d..5dc3058 100644 --- a/Flow.Launcher.Localization.SourceGenerators/Localize/LocalizeSourceGenerator.cs +++ b/Flow.Launcher.Localization.SourceGenerators/Localize/LocalizeSourceGenerator.cs @@ -355,14 +355,14 @@ private static (string Summary, ImmutableArray Parameter try { var doc = XDocument.Parse($"{comment.Value}"); - var summary = doc.Descendants("summary").FirstOrDefault()?.Value.Trim(); + var summary = doc.Descendants(Constants.SummaryElementName).FirstOrDefault()?.Value.Trim(); // Update parameter names and types of the format string - foreach (var p in doc.Descendants("param")) + foreach (var p in doc.Descendants(Constants.ParamElementName)) { - var index = int.TryParse(p.Attribute("index").Value, out var intValue) ? intValue : -1; - var name = p.Attribute("name").Value; - var type = p.Attribute("type").Value; + var index = int.TryParse(p.Attribute(Constants.IndexAttribute).Value, out var intValue) ? intValue : -1; + var name = p.Attribute(Constants.NameAttribute).Value; + var type = p.Attribute(Constants.TypeAttribute).Value; if (index >= 0 && index < parameters.Count) { if (!string.IsNullOrEmpty(name)) @@ -518,7 +518,7 @@ private static Location GetLocation(SyntaxTree syntaxTree, CSharpSyntaxNode clas return Location.Create(syntaxTree, classDeclaration.GetLocation().SourceSpan); } -#endregion + #endregion #region Generate Source diff --git a/Flow.Launcher.Localization.SourceGenerators/SourceGeneratorDiagnostics.cs b/Flow.Launcher.Localization.SourceGenerators/SourceGeneratorDiagnostics.cs index 710600d..e74c915 100644 --- a/Flow.Launcher.Localization.SourceGenerators/SourceGeneratorDiagnostics.cs +++ b/Flow.Launcher.Localization.SourceGenerators/SourceGeneratorDiagnostics.cs @@ -1,4 +1,5 @@ -using Microsoft.CodeAnalysis; +using Flow.Launcher.Localization.Shared; +using Microsoft.CodeAnalysis; namespace Flow.Launcher.Localization.SourceGenerators { @@ -16,7 +17,7 @@ public static class SourceGeneratorDiagnostics public static readonly DiagnosticDescriptor CouldNotFindPluginEntryClass = new DiagnosticDescriptor( "FLSG0002", "Could not find the main class of plugin", - "Could not find the main class of your plugin. It must implement IPluginI18n.", + $"Could not find the main class of your plugin. It must implement {Constants.PluginInterfaceName}.", "Localization", DiagnosticSeverity.Warning, isEnabledByDefault: true @@ -25,7 +26,7 @@ public static class SourceGeneratorDiagnostics public static readonly DiagnosticDescriptor CouldNotFindContextProperty = new DiagnosticDescriptor( "FLSG0003", "Could not find plugin context property", - "Could not find a property of type PluginInitContext in {0}. It must be a public static or internal static property of the main class of your plugin.", + $"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.", "Localization", DiagnosticSeverity.Warning, isEnabledByDefault: true @@ -61,7 +62,7 @@ public static class SourceGeneratorDiagnostics public static readonly DiagnosticDescriptor LocalizationKeyUnused = new DiagnosticDescriptor( "FLSG0007", "Localization key is unused", - "Method 'Localize.{0}' is never used", + $"Method '{Constants.ClassName}.{{0}}' is never used", "Localization", DiagnosticSeverity.Warning, isEnabledByDefault: true