Beispiel #1
0
        public List <Result> Query(Query query)
        {
            bool        isGlobalQuery = string.IsNullOrEmpty(query.ActionKeyword);
            CultureInfo inputCulture  = _inputUseEnglishFormat ? new CultureInfo("en-us") : CultureInfo.CurrentCulture;
            CultureInfo outputCulture = _outputUseEnglishFormat ? new CultureInfo("en-us") : CultureInfo.CurrentCulture;

            if (query == null)
            {
                throw new ArgumentNullException(paramName: nameof(query));
            }

            // Happens if the user has only typed the action key so far
            if (string.IsNullOrEmpty(query.Search))
            {
                return(new List <Result>());
            }

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

            if (!CalculateHelper.InputValid(input))
            {
                return(new List <Result>());
            }

            try
            {
                // Using CurrentUICulture since this is user facing
                var result = CalculateEngine.Interpret(input, outputCulture, out string errorMessage);

                // This could happen for some incorrect queries, like pi(2)
                if (result.Equals(default(CalculateResult)))
                {
                    // If errorMessage is not default then do error handling
                    return(errorMessage == default ? new List <Result>() : ErrorHandler.OnError(IconPath, isGlobalQuery, query.RawQuery, errorMessage));
                }

                return(new List <Result>
                {
                    ResultHelper.CreateResult(result.RoundedResult, IconPath, outputCulture),
                });
            }
            catch (Mages.Core.ParseException)
            {
                // Invalid input
                return(ErrorHandler.OnError(IconPath, isGlobalQuery, query.RawQuery, Properties.Resources.wox_plugin_calculator_expression_not_complete));
            }
            catch (OverflowException)
            {
                // Result to big to convert to decimal
                return(ErrorHandler.OnError(IconPath, isGlobalQuery, query.RawQuery, Properties.Resources.wox_plugin_calculator_not_covert_to_decimal));
            }
#pragma warning disable CA1031 // Do not catch general exception types
            catch (Exception e)
#pragma warning restore CA1031 // Do not catch general exception types
            {
                // Any other crash occurred
                // We want to keep the process alive if any the mages library throws any exceptions.
                return(ErrorHandler.OnError(IconPath, isGlobalQuery, query.RawQuery, default, e));
Beispiel #2
0
        public List <Result> Query(Query query)
        {
            if (query == null)
            {
                throw new ArgumentNullException(paramName: nameof(query));
            }

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

            if (!CalculateHelper.InputValid(input))
            {
                return(new List <Result>());
            }

            try
            {
                // Using CurrentUICulture since this is user facing
                var result = CalculateEngine.Interpret(input, CultureInfo.CurrentUICulture);

                // This could happen for some incorrect queries, like pi(2)
                if (result.Equals(default(CalculateResult)))
                {
                    return(new List <Result>());
                }

                return(new List <Result>
                {
                    ResultHelper.CreateResult(result.RoundedResult, IconPath),
                });
            } // 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
            {
                Log.Exception("Exception when query for <{query}>", e, GetType());
            }

            return(new List <Result>());
        }