diff --git a/src/modules/launcher/Plugins/Microsoft.Plugin.Calculator/Main.cs b/src/modules/launcher/Plugins/Microsoft.Plugin.Calculator/Main.cs index fb848b8e6c..29cda2497d 100644 --- a/src/modules/launcher/Plugins/Microsoft.Plugin.Calculator/Main.cs +++ b/src/modules/launcher/Plugins/Microsoft.Plugin.Calculator/Main.cs @@ -6,6 +6,7 @@ using System.Text.RegularExpressions; using System.Threading; using System.Windows; using Mages.Core; +using Wox.Infrastructure.Logger; using Wox.Plugin; namespace Microsoft.Plugin.Calculator @@ -22,18 +23,18 @@ namespace Microsoft.Plugin.Calculator @"[ei]|[0-9]|[\+\-\*\/\^\., ""]|[\(\)\|\!\[\]]" + @")+$", RegexOptions.Compiled); private static readonly Regex RegBrackets = new Regex(@"[\(\)\[\]]", RegexOptions.Compiled); - private static readonly Engine MagesEngine; + private static readonly Engine MagesEngine = new Engine(); private PluginInitContext Context { get; set; } private string IconPath { get; set; } private bool _disposed = false; - static Main() - { - MagesEngine = new Engine(); - } - public List Query(Query query) { + if(query == null) + { + throw new ArgumentNullException(paramName: nameof(query)); + } + if (query.Search.Length <= 2 // don't affect when user only input "e" or "i" keyword || !RegValidExpressChar.IsMatch(query.Search) || !IsBracketComplete(query.Search)) return new List(); @@ -88,10 +89,13 @@ namespace Microsoft.Plugin.Calculator } }; } - } - catch + } +//We want to keep the process alive if any the mages library throws any exceptions. +#pragma warning disable CA1031 // Do not catch general exception types + catch(Exception e) +#pragma warning restore CA1031 // Do not catch general exception types { - // ignored + Log.Exception($"|Microsoft.Plugin.Calculator.Main.Query|Exception when query for <{query}>", e); } return new List(); @@ -118,6 +122,11 @@ namespace Microsoft.Plugin.Calculator public void Init(PluginInitContext context) { + if(context == null) + { + throw new ArgumentNullException(paramName: nameof(context)); + } + Context = context; Context.API.ThemeChanged += OnThemeChanged; UpdateIconPath(Context.API.GetCurrentTheme()); diff --git a/src/modules/launcher/Plugins/Microsoft.Plugin.Calculator/Microsoft.Plugin.Calculator.csproj b/src/modules/launcher/Plugins/Microsoft.Plugin.Calculator/Microsoft.Plugin.Calculator.csproj index 62e2a9cbf9..1ffec522fc 100644 --- a/src/modules/launcher/Plugins/Microsoft.Plugin.Calculator/Microsoft.Plugin.Calculator.csproj +++ b/src/modules/launcher/Plugins/Microsoft.Plugin.Calculator/Microsoft.Plugin.Calculator.csproj @@ -23,6 +23,7 @@ MinimumRecommendedRules.ruleset 4 false + true @@ -97,7 +98,13 @@ - + + NU1701 + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/src/modules/launcher/Plugins/Microsoft.Plugin.Calculator/NumberTranslator.cs b/src/modules/launcher/Plugins/Microsoft.Plugin.Calculator/NumberTranslator.cs index c808eafb08..32b7cac5b8 100644 --- a/src/modules/launcher/Plugins/Microsoft.Plugin.Calculator/NumberTranslator.cs +++ b/src/modules/launcher/Plugins/Microsoft.Plugin.Calculator/NumberTranslator.cs @@ -36,6 +36,16 @@ namespace Microsoft.Plugin.Calculator /// public static NumberTranslator Create(CultureInfo sourceCulture, CultureInfo targetCulture) { + if (sourceCulture == null) + { + throw new ArgumentNullException(paramName:nameof(sourceCulture)); + } + + if (targetCulture == null) + { + throw new ArgumentNullException(paramName: nameof(sourceCulture)); + } + bool conversionRequired = sourceCulture.NumberFormat.NumberDecimalSeparator != targetCulture.NumberFormat.NumberDecimalSeparator || sourceCulture.NumberFormat.PercentGroupSeparator != targetCulture.NumberFormat.PercentGroupSeparator || sourceCulture.NumberFormat.NumberGroupSizes != targetCulture.NumberFormat.NumberGroupSizes; @@ -51,7 +61,7 @@ namespace Microsoft.Plugin.Calculator /// public string Translate(string input) { - return this.Translate(input, this.sourceCulture, this.targetCulture, this.splitRegexForSource); + return Translate(input, this.sourceCulture, this.targetCulture, this.splitRegexForSource); } /// @@ -61,10 +71,10 @@ namespace Microsoft.Plugin.Calculator /// public string TranslateBack(string input) { - return this.Translate(input, this.targetCulture, this.sourceCulture, this.splitRegexForTarget); + return Translate(input, this.targetCulture, this.sourceCulture, this.splitRegexForTarget); } - private string Translate(string input, CultureInfo cultureFrom, CultureInfo cultureTo, Regex splitRegex) + private static string Translate(string input, CultureInfo cultureFrom, CultureInfo cultureTo, Regex splitRegex) { var outputBuilder = new StringBuilder();