Skip to content

Commit fe4d8df

Browse files
committed
Replace more strings with constants
1 parent 61fed01 commit fe4d8df

File tree

6 files changed

+26
-17
lines changed

6 files changed

+26
-17
lines changed

Flow.Launcher.Localization.Analyzers/AnalyzerDiagnostics.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Microsoft.CodeAnalysis;
1+
using Flow.Launcher.Localization.Shared;
2+
using Microsoft.CodeAnalysis;
23

34
namespace Flow.Launcher.Localization.Analyzers
45
{
@@ -7,7 +8,7 @@ public static class AnalyzerDiagnostics
78
public static readonly DiagnosticDescriptor OldLocalizationApiUsed = new DiagnosticDescriptor(
89
"FLAN0001",
910
"Old localization API used",
10-
"Use `Localize.{0}({1})` instead",
11+
$"Use `{Constants.ClassName}.{{0}}({{1}})` instead",
1112
"Localization",
1213
DiagnosticSeverity.Warning,
1314
isEnabledByDefault: true
@@ -43,7 +44,7 @@ public static class AnalyzerDiagnostics
4344
public static readonly DiagnosticDescriptor ContextIsNotDeclared = new DiagnosticDescriptor(
4445
"FLAN0005",
4546
"Plugin context is not declared",
46-
"Plugin context must be at least internal static property of type `PluginInitContext`",
47+
$"Plugin context must be at least internal static property of type `{Constants.PluginContextTypeName}`",
4748
"Localization",
4849
DiagnosticSeverity.Error,
4950
isEnabledByDefault: true

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Composition;
33
using System.Linq;
44
using System.Threading.Tasks;
5+
using Flow.Launcher.Localization.Shared;
56
using Microsoft.CodeAnalysis;
67
using Microsoft.CodeAnalysis.CodeActions;
78
using Microsoft.CodeAnalysis.CodeFixes;
@@ -83,7 +84,7 @@ public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
8384

8485
private static MemberDeclarationSyntax GetStaticContextPropertyDeclaration(string propertyName = "Context") =>
8586
SyntaxFactory.ParseMemberDeclaration(
86-
$"internal static PluginInitContext {propertyName} {{ get; private set; }} = null!;"
87+
$"internal static {Constants.PluginContextTypeName} {propertyName} {{ get; private set; }} = null!;"
8788
);
8889

8990
private static Document GetFormattedDocument(CodeFixContext context, SyntaxNode root)

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Composition;
33
using System.Linq;
44
using System.Threading.Tasks;
5+
using Flow.Launcher.Localization.Shared;
56
using Microsoft.CodeAnalysis;
67
using Microsoft.CodeAnalysis.CodeActions;
78
using Microsoft.CodeAnalysis.CodeFixes;
@@ -28,7 +29,7 @@ public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
2829

2930
context.RegisterCodeFix(
3031
CodeAction.Create(
31-
title: "Replace with 'Localize.localization_key(...args)'",
32+
title: $"Replace with '{Constants.ClassName}.localization_key(...args)'",
3233
createChangedDocument: _ => Task.FromResult(FixOldTranslation(context, root, diagnostic)),
3334
equivalenceKey: AnalyzerDiagnostics.OldLocalizationApiUsed.Id
3435
),
@@ -75,7 +76,7 @@ private static Document FixOldTranslationWithoutStringFormat(
7576
CodeFixContext context, string translationKey, SyntaxNode root, InvocationExpressionSyntax invocationExpr
7677
) {
7778
var newInvocationExpr = SyntaxFactory.ParseExpression(
78-
$"Localize.{translationKey}()"
79+
$"{Constants.ClassName}.{translationKey}()"
7980
);
8081

8182
var newRoot = root.ReplaceNode(invocationExpr, newInvocationExpr);
@@ -104,7 +105,7 @@ private static Document FixOldTranslationWithStringFormat(
104105
InvocationExpressionSyntax invocationExpr
105106
) {
106107
var newArguments = string.Join(", ", argumentList.Skip(1).Select(a => a.Expression));
107-
var newInnerInvocationExpr = SyntaxFactory.ParseExpression($"Localize.{translationKey2}({newArguments})");
108+
var newInnerInvocationExpr = SyntaxFactory.ParseExpression($"{Constants.ClassName}.{translationKey2}({newArguments})");
108109

109110
var newRoot = root.ReplaceNode(invocationExpr, newInnerInvocationExpr);
110111
return context.Document.WithSyntaxRoot(newRoot);

Flow.Launcher.Localization.Shared/Constants.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ public static class Constants
1212
public const string XamlPrefixUri = "http://schemas.microsoft.com/winfx/2006/xaml";
1313
public const string XamlTag = "String";
1414
public const string KeyAttribute = "Key";
15+
public const string SummaryElementName = "summary";
16+
public const string ParamElementName = "param";
17+
public const string IndexAttribute = "index";
18+
public const string NameAttribute = "name";
19+
public const string TypeAttribute = "type";
1520

1621
public static readonly Regex LanguagesXamlRegex = new Regex(@"\\Languages\\[^\\]+\.xaml$", RegexOptions.IgnoreCase);
1722
}

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -355,14 +355,14 @@ private static (string Summary, ImmutableArray<LocalizableStringParam> Parameter
355355
try
356356
{
357357
var doc = XDocument.Parse($"<root>{comment.Value}</root>");
358-
var summary = doc.Descendants("summary").FirstOrDefault()?.Value.Trim();
358+
var summary = doc.Descendants(Constants.SummaryElementName).FirstOrDefault()?.Value.Trim();
359359

360360
// Update parameter names and types of the format string
361-
foreach (var p in doc.Descendants("param"))
361+
foreach (var p in doc.Descendants(Constants.ParamElementName))
362362
{
363-
var index = int.TryParse(p.Attribute("index").Value, out var intValue) ? intValue : -1;
364-
var name = p.Attribute("name").Value;
365-
var type = p.Attribute("type").Value;
363+
var index = int.TryParse(p.Attribute(Constants.IndexAttribute).Value, out var intValue) ? intValue : -1;
364+
var name = p.Attribute(Constants.NameAttribute).Value;
365+
var type = p.Attribute(Constants.TypeAttribute).Value;
366366
if (index >= 0 && index < parameters.Count)
367367
{
368368
if (!string.IsNullOrEmpty(name))
@@ -518,7 +518,7 @@ private static Location GetLocation(SyntaxTree syntaxTree, CSharpSyntaxNode clas
518518
return Location.Create(syntaxTree, classDeclaration.GetLocation().SourceSpan);
519519
}
520520

521-
#endregion
521+
#endregion
522522

523523
#region Generate Source
524524

Flow.Launcher.Localization.SourceGenerators/SourceGeneratorDiagnostics.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Microsoft.CodeAnalysis;
1+
using Flow.Launcher.Localization.Shared;
2+
using Microsoft.CodeAnalysis;
23

34
namespace Flow.Launcher.Localization.SourceGenerators
45
{
@@ -16,7 +17,7 @@ public static class SourceGeneratorDiagnostics
1617
public static readonly DiagnosticDescriptor CouldNotFindPluginEntryClass = new DiagnosticDescriptor(
1718
"FLSG0002",
1819
"Could not find the main class of plugin",
19-
"Could not find the main class of your plugin. It must implement IPluginI18n.",
20+
$"Could not find the main class of your plugin. It must implement {Constants.PluginInterfaceName}.",
2021
"Localization",
2122
DiagnosticSeverity.Warning,
2223
isEnabledByDefault: true
@@ -25,7 +26,7 @@ public static class SourceGeneratorDiagnostics
2526
public static readonly DiagnosticDescriptor CouldNotFindContextProperty = new DiagnosticDescriptor(
2627
"FLSG0003",
2728
"Could not find plugin context property",
28-
"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.",
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.",
2930
"Localization",
3031
DiagnosticSeverity.Warning,
3132
isEnabledByDefault: true
@@ -61,7 +62,7 @@ public static class SourceGeneratorDiagnostics
6162
public static readonly DiagnosticDescriptor LocalizationKeyUnused = new DiagnosticDescriptor(
6263
"FLSG0007",
6364
"Localization key is unused",
64-
"Method 'Localize.{0}' is never used",
65+
$"Method '{Constants.ClassName}.{{0}}' is never used",
6566
"Localization",
6667
DiagnosticSeverity.Warning,
6768
isEnabledByDefault: true

0 commit comments

Comments
 (0)