Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions Flow.Launcher.Localization.Analyzers/AnalyzerDiagnostics.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.CodeAnalysis;
using Flow.Launcher.Localization.Shared;
using Microsoft.CodeAnalysis;

namespace Flow.Launcher.Localization.Analyzers
{
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
),
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
5 changes: 5 additions & 0 deletions Flow.Launcher.Localization.Shared/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -355,14 +355,14 @@ private static (string Summary, ImmutableArray<LocalizableStringParam> Parameter
try
{
var doc = XDocument.Parse($"<root>{comment.Value}</root>");
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))
Expand Down Expand Up @@ -518,7 +518,7 @@ private static Location GetLocation(SyntaxTree syntaxTree, CSharpSyntaxNode clas
return Location.Create(syntaxTree, classDeclaration.GetLocation().SourceSpan);
}

#endregion
#endregion

#region Generate Source

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.CodeAnalysis;
using Flow.Launcher.Localization.Shared;
using Microsoft.CodeAnalysis;

namespace Flow.Launcher.Localization.SourceGenerators
{
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
Loading