mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-09 20:57:22 +02:00
onboarding stylecop (#5622)
This commit is contained in:
@@ -1,6 +1,9 @@
|
|||||||
using System;
|
// Copyright (c) Microsoft Corporation
|
||||||
|
// The Microsoft Corporation licenses this file to you under the MIT license.
|
||||||
|
// See the LICENSE file in the project root for more information.
|
||||||
|
|
||||||
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Drawing;
|
|
||||||
using System.Runtime.InteropServices;
|
using System.Runtime.InteropServices;
|
||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
@@ -22,22 +25,29 @@ namespace Microsoft.Plugin.Calculator
|
|||||||
@"==|~=|&&|\|\||" +
|
@"==|~=|&&|\|\||" +
|
||||||
@"[ei]|[0-9]|[\+\-\*\/\^\., ""]|[\(\)\|\!\[\]]" +
|
@"[ei]|[0-9]|[\+\-\*\/\^\., ""]|[\(\)\|\!\[\]]" +
|
||||||
@")+$", RegexOptions.Compiled);
|
@")+$", RegexOptions.Compiled);
|
||||||
|
|
||||||
private static readonly Regex RegBrackets = new Regex(@"[\(\)\[\]]", RegexOptions.Compiled);
|
private static readonly Regex RegBrackets = new Regex(@"[\(\)\[\]]", RegexOptions.Compiled);
|
||||||
private static readonly Engine MagesEngine = new Engine();
|
private static readonly Engine MagesEngine = new Engine();
|
||||||
|
|
||||||
private PluginInitContext Context { get; set; }
|
private PluginInitContext Context { get; set; }
|
||||||
|
|
||||||
private string IconPath { get; set; }
|
private string IconPath { get; set; }
|
||||||
|
|
||||||
private bool _disposed = false;
|
private bool _disposed = false;
|
||||||
|
|
||||||
public List<Result> Query(Query query)
|
public List<Result> Query(Query query)
|
||||||
{
|
{
|
||||||
if(query == null)
|
if (query == null)
|
||||||
{
|
{
|
||||||
throw new ArgumentNullException(paramName: nameof(query));
|
throw new ArgumentNullException(paramName: nameof(query));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (query.Search.Length <= 2 // don't affect when user only input "e" or "i" keyword
|
if (query.Search.Length <= 2 // don't affect when user only input "e" or "i" keyword
|
||||||
|| !RegValidExpressChar.IsMatch(query.Search)
|
|| !RegValidExpressChar.IsMatch(query.Search)
|
||||||
|| !IsBracketComplete(query.Search)) return new List<Result>();
|
|| !IsBracketComplete(query.Search))
|
||||||
|
{
|
||||||
|
return new List<Result>();
|
||||||
|
}
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
@@ -50,11 +60,14 @@ namespace Microsoft.Plugin.Calculator
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (result.ToString() == "NaN")
|
if (result.ToString() == "NaN")
|
||||||
|
{
|
||||||
result = Context.API.GetTranslation("wox_plugin_calculator_not_a_number");
|
result = Context.API.GetTranslation("wox_plugin_calculator_not_a_number");
|
||||||
|
}
|
||||||
|
|
||||||
if (result is Function)
|
if (result is Function)
|
||||||
|
{
|
||||||
result = Context.API.GetTranslation("wox_plugin_calculator_expression_not_complete");
|
result = Context.API.GetTranslation("wox_plugin_calculator_expression_not_complete");
|
||||||
|
}
|
||||||
|
|
||||||
if (!string.IsNullOrEmpty(result?.ToString()))
|
if (!string.IsNullOrEmpty(result?.ToString()))
|
||||||
{
|
{
|
||||||
@@ -85,14 +98,13 @@ namespace Microsoft.Plugin.Calculator
|
|||||||
thread.Start();
|
thread.Start();
|
||||||
thread.Join();
|
thread.Join();
|
||||||
return ret;
|
return ret;
|
||||||
}
|
},
|
||||||
}
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
} // We want to keep the process alive if any the mages library throws any exceptions.
|
||||||
//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
|
#pragma warning disable CA1031 // Do not catch general exception types
|
||||||
catch(Exception e)
|
catch (Exception e)
|
||||||
#pragma warning restore CA1031 // Do not catch general exception types
|
#pragma warning restore CA1031 // Do not catch general exception types
|
||||||
{
|
{
|
||||||
Log.Exception($"|Microsoft.Plugin.Calculator.Main.Query|Exception when query for <{query}>", e);
|
Log.Exception($"|Microsoft.Plugin.Calculator.Main.Query|Exception when query for <{query}>", e);
|
||||||
@@ -122,7 +134,7 @@ namespace Microsoft.Plugin.Calculator
|
|||||||
|
|
||||||
public void Init(PluginInitContext context)
|
public void Init(PluginInitContext context)
|
||||||
{
|
{
|
||||||
if(context == null)
|
if (context == null)
|
||||||
{
|
{
|
||||||
throw new ArgumentNullException(paramName: nameof(context));
|
throw new ArgumentNullException(paramName: nameof(context));
|
||||||
}
|
}
|
||||||
@@ -145,7 +157,7 @@ namespace Microsoft.Plugin.Calculator
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnThemeChanged(Theme _, Theme newTheme)
|
private void OnThemeChanged(Theme currentTheme, Theme newTheme)
|
||||||
{
|
{
|
||||||
UpdateIconPath(newTheme);
|
UpdateIconPath(newTheme);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -116,5 +116,19 @@
|
|||||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||||
</None>
|
</None>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<Compile Include="..\..\..\..\codeAnalysis\GlobalSuppressions.cs">
|
||||||
|
<Link>GlobalSuppressions.cs</Link>
|
||||||
|
</Compile>
|
||||||
|
<AdditionalFiles Include="..\..\..\..\codeAnalysis\StyleCop.json">
|
||||||
|
<Link>StyleCop.json</Link>
|
||||||
|
</AdditionalFiles>
|
||||||
|
</ItemGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="StyleCop.Analyzers">
|
||||||
|
<Version>1.1.118</Version>
|
||||||
|
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||||
|
<PrivateAssets>all</PrivateAssets>
|
||||||
|
</PackageReference>
|
||||||
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
||||||
@@ -1,10 +1,11 @@
|
|||||||
using System;
|
// Copyright (c) Microsoft Corporation
|
||||||
using System.Collections.Generic;
|
// The Microsoft Corporation licenses this file to you under the MIT license.
|
||||||
|
// See the LICENSE file in the project root for more information.
|
||||||
|
|
||||||
|
using System;
|
||||||
using System.Globalization;
|
using System.Globalization;
|
||||||
using System.Linq;
|
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
namespace Microsoft.Plugin.Calculator
|
namespace Microsoft.Plugin.Calculator
|
||||||
{
|
{
|
||||||
@@ -23,8 +24,8 @@ namespace Microsoft.Plugin.Calculator
|
|||||||
this.sourceCulture = sourceCulture;
|
this.sourceCulture = sourceCulture;
|
||||||
this.targetCulture = targetCulture;
|
this.targetCulture = targetCulture;
|
||||||
|
|
||||||
this.splitRegexForSource = GetSplitRegex(this.sourceCulture);
|
splitRegexForSource = GetSplitRegex(this.sourceCulture);
|
||||||
this.splitRegexForTarget = GetSplitRegex(this.targetCulture);
|
splitRegexForTarget = GetSplitRegex(this.targetCulture);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -33,12 +34,12 @@ namespace Microsoft.Plugin.Calculator
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="sourceCulture">source culture</param>
|
/// <param name="sourceCulture">source culture</param>
|
||||||
/// <param name="targetCulture">target culture</param>
|
/// <param name="targetCulture">target culture</param>
|
||||||
/// <returns></returns>
|
/// <returns>Number translator for target culture</returns>
|
||||||
public static NumberTranslator Create(CultureInfo sourceCulture, CultureInfo targetCulture)
|
public static NumberTranslator Create(CultureInfo sourceCulture, CultureInfo targetCulture)
|
||||||
{
|
{
|
||||||
if (sourceCulture == null)
|
if (sourceCulture == null)
|
||||||
{
|
{
|
||||||
throw new ArgumentNullException(paramName:nameof(sourceCulture));
|
throw new ArgumentNullException(paramName: nameof(sourceCulture));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (targetCulture == null)
|
if (targetCulture == null)
|
||||||
@@ -57,21 +58,21 @@ namespace Microsoft.Plugin.Calculator
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Translate from source to target culture.
|
/// Translate from source to target culture.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="input"></param>
|
/// <param name="input">input string to translate</param>
|
||||||
/// <returns></returns>
|
/// <returns>translated string</returns>
|
||||||
public string Translate(string input)
|
public string Translate(string input)
|
||||||
{
|
{
|
||||||
return Translate(input, this.sourceCulture, this.targetCulture, this.splitRegexForSource);
|
return Translate(input, sourceCulture, targetCulture, splitRegexForSource);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Translate from target to source culture.
|
/// Translate from target to source culture.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="input"></param>
|
/// <param name="input">input string to translate back to source culture</param>
|
||||||
/// <returns></returns>
|
/// <returns>source culture string</returns>
|
||||||
public string TranslateBack(string input)
|
public string TranslateBack(string input)
|
||||||
{
|
{
|
||||||
return Translate(input, this.targetCulture, this.sourceCulture, this.splitRegexForTarget);
|
return Translate(input, targetCulture, sourceCulture, splitRegexForTarget);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static string Translate(string input, CultureInfo cultureFrom, CultureInfo cultureTo, Regex splitRegex)
|
private static string Translate(string input, CultureInfo cultureFrom, CultureInfo cultureTo, Regex splitRegex)
|
||||||
@@ -98,6 +99,7 @@ namespace Microsoft.Plugin.Calculator
|
|||||||
{
|
{
|
||||||
splitPattern += $"|{Regex.Escape(culture.NumberFormat.NumberGroupSeparator)}";
|
splitPattern += $"|{Regex.Escape(culture.NumberFormat.NumberGroupSeparator)}";
|
||||||
}
|
}
|
||||||
|
|
||||||
splitPattern += ")+)";
|
splitPattern += ")+)";
|
||||||
return new Regex(splitPattern);
|
return new Regex(splitPattern);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user