mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-06 19:26:39 +02:00
CmdPal: Make Calculator Great Again (#44594)
## Summary of the Pull Request This PR continues the tradition of alphabetical progress. After [MBGA](#41961), we move on to **MCBA — Make Calculator Better Again!** - Introduces limited automatic correction and completion of expressions. - The goal is to allow uninterrupted typing and avoid disruptions when a partially entered expression is temporarily invalid (which previously caused the result to be replaced by an error message or hidden by the fallback). - The implementation intentionally aims for a sweet spot: - Ignores trailing binary operators. - Automatically closes all opened parentheses. - It is not exhaustive; for example, incomplete constants or functions may still result in an invalid query. - Copy current result to the search bar. - Adds an option to copy the current result to the search bar when the user types `=` at the end of the expression. - Adds a new menu item for the same action. - Fixes the **Save** command to also copy the result to the query. - Adds support for the `factorial(x)` function and the `x!` expression. - Factorial calculations are supported up to `170!` (limited by `double`), but display is constrained by decimal conversion and allows direct display of results up to `20!`. - Adds support for the `sign(x)` function. - Adds support for the `π` symbol as an alternative to the `pi` constant. - Adds a context menu item to the result list item and fallback that displays the octal representation of the result. - Implements beautification of the query: - Converts technical symbols such as `*` or `/` to `×` or `÷`, respectively. - Not enabled for fallbacks for now, since the item text should match the query to keep the score intact. - Implements additional normalization of symbols in the query: - Percent: `%`, `%`, `﹪` - Minus: `−`, `-`, `–`, `—` - Factorial: `!`, `!` - Multiplication: `*`, `×`, `∗`, `·`, `⋅`, `✕`, `✖`, `\u2062` (invisible times) - Division: `/`, `÷`, `➗`, `:` - Allows use of `²` and `³` as alternatives to `^2` and `^3`. - Updates the unit test that was culture sensitive to force en-US output (not an actual fix, but at least it clears false positive for now) - Fixes pre-parsing of scientific notation to prevent capturing minus sign as part of it. - Fixes normalization/rounding of the result, so it can display small values (the current solution turned it into a string with scientific notation and couldn't parse it back). - Updates test with new cases ## Pictures? Moving! Previous behavior: https://github.com/user-attachments/assets/ebcdcd85-797a-44f9-a8b1-a0f2f33c6b42 New behavior: https://github.com/user-attachments/assets/5bd94663-a0d0-4d7d-8032-1030e79926c3 <!-- Please review the items on the PR checklist before submitting--> ## PR Checklist - [x] Closes: #43481 - [x] Closes: #43460 - [x] Closes: #42078 - [x] Closes: #41839 - [x] Closes: #39659 - [x] Closes: #40502 - [x] Related to: #41715 <!-- - [ ] Closes: #yyy (add separate lines for additional resolved issues) --> - [ ] **Communication:** I've discussed this with core contributors already. If the work hasn't been agreed, this work might be rejected - [x] **Tests:** Added/updated and all pass - [ ] **Localization:** All end-user-facing strings can be localized - [ ] **Dev docs:** Added/updated - [ ] **New binaries:** Added on the required places - [ ] [JSON for signing](https://github.com/microsoft/PowerToys/blob/main/.pipelines/ESRPSigning_core.json) for new binaries - [ ] [WXS for installer](https://github.com/microsoft/PowerToys/blob/main/installer/PowerToysSetup/Product.wxs) for new binaries and localization folder - [ ] [YML for CI pipeline](https://github.com/microsoft/PowerToys/blob/main/.pipelines/ci/templates/build-powertoys-steps.yml) for new test projects - [ ] [YML for signed pipeline](https://github.com/microsoft/PowerToys/blob/main/.pipelines/release.yml) - [ ] **Documentation updated:** If checked, please file a pull request on [our docs repo](https://github.com/MicrosoftDocs/windows-uwp/tree/docs/hub/powertoys) and link it here: #xxx <!-- Provide a more detailed description of the PR, other things fixed, or any additional comments/features here --> ## Detailed Description of the Pull Request / Additional comments <!-- Describe how you validated the behavior. Add automated tests wherever possible, but list manual validation steps taken as well --> ## Validation Steps Performed
This commit is contained in:
@@ -3,11 +3,12 @@
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace Microsoft.CmdPal.Ext.Calc.Helper;
|
||||
|
||||
public static class CalculateHelper
|
||||
public static partial class CalculateHelper
|
||||
{
|
||||
private static readonly Regex RegValidExpressChar = new Regex(
|
||||
@"^(" +
|
||||
@@ -19,7 +20,7 @@ public static class CalculateHelper
|
||||
@"rad\s*\(|deg\s*\(|grad\s*\(|" + /* trigonometry unit conversion macros */
|
||||
@"pi|" +
|
||||
@"==|~=|&&|\|\||" +
|
||||
@"((-?(\d+(\.\d*)?)|-?(\.\d+))[Ee](-?\d+))|" + /* expression from CheckScientificNotation between parenthesis */
|
||||
@"((\d+(?:\.\d*)?|\.\d+)[eE](-?\d+))|" + /* expression from CheckScientificNotation between parenthesis */
|
||||
@"e|[0-9]|0[xX][0-9a-fA-F]+|0[bB][01]+|0[oO][0-7]+|[\+\-\*\/\^\., ""]|[\(\)\|\!\[\]]" +
|
||||
@")+$",
|
||||
RegexOptions.Compiled);
|
||||
@@ -31,6 +32,94 @@ public static class CalculateHelper
|
||||
private const string RadToDeg = "(180 / pi) * ";
|
||||
private const string RadToGrad = "(200 / pi) * ";
|
||||
|
||||
// replacements from the user input to displayed query
|
||||
private static readonly Dictionary<string, string> QueryReplacements = new()
|
||||
{
|
||||
{ "%", "%" }, { "﹪", "%" },
|
||||
{ "−", "-" }, { "–", "-" }, { "—", "-" },
|
||||
{ "!", "!" },
|
||||
{ "*", "×" }, { "∗", "×" }, { "·", "×" }, { "⊗", "×" }, { "⋅", "×" }, { "✕", "×" }, { "✖", "×" }, { "\u2062", "×" },
|
||||
{ "/", "÷" }, { "∕", "÷" }, { "➗", "÷" }, { ":", "÷" },
|
||||
};
|
||||
|
||||
// replacements from a query to engine input
|
||||
private static readonly Dictionary<string, string> EngineReplacements = new()
|
||||
{
|
||||
{ "×", "*" },
|
||||
{ "÷", "/" },
|
||||
};
|
||||
|
||||
private static readonly Dictionary<string, string> SuperscriptReplacements = new()
|
||||
{
|
||||
{ "²", "^2" }, { "³", "^3" },
|
||||
};
|
||||
|
||||
private static readonly HashSet<char> StandardOperators = [
|
||||
|
||||
// binary operators; doesn't make sense for them to be at the end of a query
|
||||
'+', '-', '*', '/', '%', '^', '=', '&', '|', '\\',
|
||||
|
||||
// parentheses
|
||||
'(', '[',
|
||||
];
|
||||
|
||||
private static readonly HashSet<char> SuffixOperators = [
|
||||
|
||||
// unary operators; can appear at the end of a query
|
||||
')', ']', '!',
|
||||
];
|
||||
|
||||
private static readonly Regex ReplaceScientificNotationRegex = CreateReplaceScientificNotationRegex();
|
||||
|
||||
public static char[] GetQueryOperators()
|
||||
{
|
||||
var ops = new HashSet<char>(StandardOperators);
|
||||
ops.ExceptWith(SuffixOperators);
|
||||
return [.. ops];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Normalizes the query for display
|
||||
/// This replaces standard operators with more visually appealing ones (e.g., '*' -> '×') if enabled.
|
||||
/// Always applies safe normalizations (standardizing variants like minus, percent, etc.).
|
||||
/// </summary>
|
||||
/// <param name="input">The query string to normalize.</param>
|
||||
public static string NormalizeCharsForDisplayQuery(string input)
|
||||
{
|
||||
// 1. Safe/Trivial replacements (Variant -> Standard)
|
||||
// These are always applied to ensure consistent behavior for non-math symbols (spaces) and
|
||||
// operator variants like minus, percent, and exclamation mark.
|
||||
foreach (var (key, value) in QueryReplacements)
|
||||
{
|
||||
input = input.Replace(key, value);
|
||||
}
|
||||
|
||||
return input;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Normalizes the query for the calculation engine.
|
||||
/// This replaces all supported operator variants (visual or standard) with the specific
|
||||
/// ASCII operators required by the engine (e.g., '×' -> '*').
|
||||
/// It duplicates and expands upon replacements in NormalizeQuery to ensure the engine
|
||||
/// receives valid input regardless of whether NormalizeQuery was executed.
|
||||
/// </summary>
|
||||
public static string NormalizeCharsToEngine(string input)
|
||||
{
|
||||
foreach (var (key, value) in EngineReplacements)
|
||||
{
|
||||
input = input.Replace(key, value);
|
||||
}
|
||||
|
||||
// Replace superscript characters with their engine equivalents (e.g., '²' -> '^2')
|
||||
foreach (var (key, value) in SuperscriptReplacements)
|
||||
{
|
||||
input = input.Replace(key, value);
|
||||
}
|
||||
|
||||
return input;
|
||||
}
|
||||
|
||||
public static bool InputValid(string input)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(input))
|
||||
@@ -50,7 +139,7 @@ public static class CalculateHelper
|
||||
|
||||
// If the input ends with a binary operator then it is not a valid input to mages and the Interpret function would throw an exception. Because we expect here that the user has not finished typing we block those inputs.
|
||||
var trimmedInput = input.TrimEnd();
|
||||
if (trimmedInput.EndsWith('+') || trimmedInput.EndsWith('-') || trimmedInput.EndsWith('*') || trimmedInput.EndsWith('|') || trimmedInput.EndsWith('\\') || trimmedInput.EndsWith('^') || trimmedInput.EndsWith('=') || trimmedInput.EndsWith('&') || trimmedInput.EndsWith('/') || trimmedInput.EndsWith('%'))
|
||||
if (EndsWithBinaryOperator(trimmedInput))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
@@ -58,6 +147,18 @@ public static class CalculateHelper
|
||||
return true;
|
||||
}
|
||||
|
||||
private static bool EndsWithBinaryOperator(string input)
|
||||
{
|
||||
var operators = GetQueryOperators();
|
||||
if (string.IsNullOrEmpty(input))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
var lastChar = input[^1];
|
||||
return Array.Exists(operators, op => op == lastChar);
|
||||
}
|
||||
|
||||
public static string FixHumanMultiplicationExpressions(string input)
|
||||
{
|
||||
var output = CheckScientificNotation(input);
|
||||
@@ -72,18 +173,7 @@ public static class CalculateHelper
|
||||
|
||||
private static string CheckScientificNotation(string input)
|
||||
{
|
||||
/**
|
||||
* NOTE: By the time that the expression gets to us, it's already in English format.
|
||||
*
|
||||
* Regex explanation:
|
||||
* (-?(\d+({0}\d*)?)|-?({0}\d+)): Used to capture one of two types:
|
||||
* -?(\d+({0}\d*)?): Captures a decimal number starting with a number (e.g. "-1.23")
|
||||
* -?({0}\d+): Captures a decimal number without leading number (e.g. ".23")
|
||||
* e: Captures 'e' or 'E'
|
||||
* (-?\d+): Captures an integer number (e.g. "-1" or "23")
|
||||
*/
|
||||
var p = @"(-?(\d+(\.\d*)?)|-?(\.\d+))e(-?\d+)";
|
||||
return Regex.Replace(input, p, "($1 * 10^($5))", RegexOptions.IgnoreCase);
|
||||
return ReplaceScientificNotationRegex.Replace(input, "($1 * 10^($2))");
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -292,6 +382,86 @@ public static class CalculateHelper
|
||||
return modifiedInput;
|
||||
}
|
||||
|
||||
public static string UpdateFactorialFunctions(string input)
|
||||
{
|
||||
// Handle n! -> factorial(n)
|
||||
int startSearch = 0;
|
||||
while (true)
|
||||
{
|
||||
var index = input.IndexOf('!', startSearch);
|
||||
if (index == -1)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
// Ignore !=
|
||||
if (index + 1 < input.Length && input[index + 1] == '=')
|
||||
{
|
||||
startSearch = index + 2;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (index == 0)
|
||||
{
|
||||
startSearch = index + 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
// Scan backwards
|
||||
var endArg = index - 1;
|
||||
while (endArg >= 0 && char.IsWhiteSpace(input[endArg]))
|
||||
{
|
||||
endArg--;
|
||||
}
|
||||
|
||||
if (endArg < 0)
|
||||
{
|
||||
startSearch = index + 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
var startArg = endArg;
|
||||
if (input[endArg] == ')')
|
||||
{
|
||||
// Find matching '('
|
||||
startArg = FindOpeningBracketIndexInFrontOfIndex(input, endArg);
|
||||
if (startArg == -1)
|
||||
{
|
||||
startSearch = index + 1;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// Scan back for number or word
|
||||
while (startArg >= 0 && (char.IsLetterOrDigit(input[startArg]) || input[startArg] == '.'))
|
||||
{
|
||||
startArg--;
|
||||
}
|
||||
|
||||
startArg++; // Move back to first valid char
|
||||
}
|
||||
|
||||
if (startArg > endArg)
|
||||
{
|
||||
// No argument found
|
||||
startSearch = index + 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
// Extract argument
|
||||
var arg = input.Substring(startArg, endArg - startArg + 1);
|
||||
|
||||
// Replace <arg><whitespace>! with factorial(<arg>)
|
||||
input = input.Remove(startArg, index - startArg + 1);
|
||||
input = input.Insert(startArg, $"factorial({arg})");
|
||||
|
||||
startSearch = 0; // Reset search because string changed
|
||||
}
|
||||
|
||||
return input;
|
||||
}
|
||||
|
||||
private static string ModifyMathFunction(string input, string function, string modification)
|
||||
{
|
||||
// Create the pattern to match the function, opening bracket, and any spaces in between
|
||||
@@ -325,4 +495,43 @@ public static class CalculateHelper
|
||||
|
||||
return modifiedInput;
|
||||
}
|
||||
|
||||
private static int FindOpeningBracketIndexInFrontOfIndex(string input, int end)
|
||||
{
|
||||
var bracketCount = 0;
|
||||
for (var i = end; i >= 0; i--)
|
||||
{
|
||||
switch (input[i])
|
||||
{
|
||||
case ')':
|
||||
bracketCount++;
|
||||
break;
|
||||
case '(':
|
||||
{
|
||||
bracketCount--;
|
||||
if (bracketCount == 0)
|
||||
{
|
||||
return i;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
/*
|
||||
* NOTE: By the time that the expression gets to us, it's already in English format.
|
||||
*
|
||||
* Regex explanation:
|
||||
* (-?(\d+({0}\d*)?)|-?({0}\d+)): Used to capture one of two types:
|
||||
* -?(\d+({0}\d*)?): Captures a decimal number starting with a number (e.g. "-1.23")
|
||||
* -?({0}\d+): Captures a decimal number without leading number (e.g. ".23")
|
||||
* e: Captures 'e' or 'E'
|
||||
* (?\d+): Captures an integer number (e.g. "-1" or "23")
|
||||
*/
|
||||
[GeneratedRegex(@"(\d+(?:\.\d*)?|\.\d+)e(-?\d+)", RegexOptions.IgnoreCase, "en-US")]
|
||||
private static partial Regex CreateReplaceScientificNotationRegex();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user