Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PT Run] Calculator plugin: Various improvements #18159

Merged
merged 23 commits into from
Jun 2, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ public void Interpret_ThrowError_WhenCalledNullOrEmpty(string input)
}

[DataTestMethod]
[DataRow("42")]
htcfreek marked this conversation as resolved.
Show resolved Hide resolved
[DataRow("test")]
[DataRow("pi(2)")] // Incorrect input, constant is being treated as a function.
[DataRow("e(2)")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ public CalculateResult Interpret(string input, CultureInfo cultureInfo)
}

result = TransformResult(result);
if (result is string)
{
throw new ArithmeticException(result as string);
}

if (string.IsNullOrEmpty(result?.ToString()))
{
Expand All @@ -68,7 +72,7 @@ public static decimal Round(decimal value)
return Math.Round(value, RoundingDigits, MidpointRounding.AwayFromZero);
}

private static object TransformResult(object result)
private static dynamic TransformResult(object result)
{
if (result.ToString() == "NaN")
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public static bool InputValid(string input)
}

bool singleDigitFactorial = input.EndsWith("!", StringComparison.InvariantCulture);
if (input.Length <= 2 && !singleDigitFactorial)
if (input.Length <= 0 && !singleDigitFactorial)
htcfreek marked this conversation as resolved.
Show resolved Hide resolved
{
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ public List<Result> Query(Query query)
throw new ArgumentNullException(paramName: nameof(query));
}

if (string.IsNullOrEmpty(query.Search))
{
return new List<Result>();
}

NumberTranslator translator = NumberTranslator.Create(CultureInfo.CurrentCulture, new CultureInfo("en-US"));
var input = translator.Translate(query.Search.Normalize(NormalizationForm.FormKC));

Expand Down Expand Up @@ -61,10 +66,23 @@ public List<Result> Query(Query query)
catch (Exception e)
#pragma warning restore CA1031 // Do not catch general exception types
{
Log.Exception("Exception when query for <{query}>", e, GetType());
}
// error log
Log.Exception($"Exception when query for <{query}>", e, GetType());

return new List<Result>();
// error message result on keyword queries
if (string.IsNullOrEmpty(query.ActionKeyword))
{
return new List<Result>();
}
else
{
string err = (e is Mages.Core.ParseException) ? Properties.Resources.wox_plugin_calculator_expression_not_complete : e.Message;
htcfreek marked this conversation as resolved.
Show resolved Hide resolved
return new List<Result>
{
ResultHelper.CreateErrorResult(Properties.Resources.wox_plugin_calculator_calculation_failed, err, IconPath),
};
}
}
}

public void Init(PluginInitContext context)
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@
<value>Does mathematical calculations (e.g. 5*3-2).</value>
</data>
<data name="wox_plugin_calculator_not_a_number" xml:space="preserve">
<value>Not a number (NaN)</value>
<value>Calculation result is not a valid number (NaN)</value>
</data>
<data name="wox_plugin_calculator_expression_not_complete" xml:space="preserve">
<value>Expression wrong or incomplete (Did you forget some parentheses?)</value>
Expand All @@ -135,4 +135,7 @@
<data name="wox_plugin_calculator_copy_failed" xml:space="preserve">
<value>Copy failed, please try later</value>
</data>
<data name="wox_plugin_calculator_calculation_failed" xml:space="preserve">
<value>Failed to calculate the input</value>
</data>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,17 @@ public static Result CreateResult(decimal? roundedResult, string iconPath)
};
}

public static Result CreateErrorResult(string errorMsg, string exceptMsg, string iconPath)
{
return new Result
{
Title = errorMsg,
SubTitle = exceptMsg,
IcoPath = iconPath,
Score = 300,
};
}

public static bool Action(decimal? roundedResult)
{
var ret = false;
Expand Down