From 718e72557182df210a476964c4f13cbb377b47f6 Mon Sep 17 00:00:00 2001 From: Yu Leng <42196638+moooyo@users.noreply.github.com> Date: Mon, 26 May 2025 11:37:38 +0800 Subject: [PATCH] [cmdpal] Fix calculator error when pressing Space (#39679) Co-authored-by: Yu Leng (from Dev Box) --- .../Microsoft.CmdPal.Ext.Calc/Helper/CalculateHelper.cs | 2 +- .../ext/Microsoft.CmdPal.Ext.Calc/Helper/QueryHelper.cs | 5 +++++ .../Properties/Resources.Designer.cs | 9 +++++++++ .../Microsoft.CmdPal.Ext.Calc/Properties/Resources.resx | 3 +++ 4 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Calc/Helper/CalculateHelper.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Calc/Helper/CalculateHelper.cs index acab3d7b96..deb441cfe2 100644 --- a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Calc/Helper/CalculateHelper.cs +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Calc/Helper/CalculateHelper.cs @@ -35,7 +35,7 @@ public static class CalculateHelper { if (string.IsNullOrWhiteSpace(input)) { - throw new ArgumentNullException(paramName: nameof(input)); + return false; } if (!RegValidExpressChar.IsMatch(input)) diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Calc/Helper/QueryHelper.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Calc/Helper/QueryHelper.cs index 6b1e62ae03..5805a6a0ad 100644 --- a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Calc/Helper/QueryHelper.cs +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Calc/Helper/QueryHelper.cs @@ -32,6 +32,11 @@ public static partial class QueryHelper NumberTranslator translator = NumberTranslator.Create(inputCulture, new CultureInfo("en-US")); var input = translator.Translate(query.Normalize(NormalizationForm.FormKC)); + if (string.IsNullOrWhiteSpace(input)) + { + return ErrorHandler.OnError(isFallbackSearch, query, Properties.Resources.calculator_expression_empty); + } + if (!CalculateHelper.InputValid(input)) { return null; diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Calc/Properties/Resources.Designer.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Calc/Properties/Resources.Designer.cs index 8cd385be32..8759c1209f 100644 --- a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Calc/Properties/Resources.Designer.cs +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Calc/Properties/Resources.Designer.cs @@ -132,6 +132,15 @@ namespace Microsoft.CmdPal.Ext.Calc.Properties { } } + /// + /// Looks up a localized string similar to Please enter an expression. + /// + public static string calculator_expression_empty { + get { + return ResourceManager.GetString("calculator_expression_empty", resourceCulture); + } + } + /// /// Looks up a localized string similar to Expression wrong or incomplete. /// diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Calc/Properties/Resources.resx b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Calc/Properties/Resources.resx index 3c50d3a1c5..dba2ba2067 100644 --- a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Calc/Properties/Resources.resx +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Calc/Properties/Resources.resx @@ -199,4 +199,7 @@ Copy binary + + Please enter an expression + \ No newline at end of file