onboarding stylecop (#5622)

This commit is contained in:
Clint Rutkas 2020-08-04 16:39:25 -07:00 committed by GitHub
parent 010732108c
commit a793cf4ac2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 413 additions and 385 deletions

View File

@ -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.Drawing;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
using System.Threading;
@ -22,22 +25,29 @@ namespace Microsoft.Plugin.Calculator
@"==|~=|&&|\|\||" +
@"[ei]|[0-9]|[\+\-\*\/\^\., ""]|[\(\)\|\!\[\]]" +
@")+$", RegexOptions.Compiled);
private static readonly Regex RegBrackets = new Regex(@"[\(\)\[\]]", RegexOptions.Compiled);
private static readonly Engine MagesEngine = new Engine();
private PluginInitContext Context { get; set; }
private string IconPath { get; set; }
private bool _disposed = false;
public List<Result> Query(Query query)
{
if(query == null)
if (query == null)
{
throw new ArgumentNullException(paramName: nameof(query));
}
if (query.Search.Length <= 2 // don't affect when user only input "e" or "i" keyword
|| !RegValidExpressChar.IsMatch(query.Search)
|| !IsBracketComplete(query.Search)) return new List<Result>();
|| !IsBracketComplete(query.Search))
{
return new List<Result>();
}
try
{
@ -50,11 +60,14 @@ namespace Microsoft.Plugin.Calculator
}
if (result.ToString() == "NaN")
{
result = Context.API.GetTranslation("wox_plugin_calculator_not_a_number");
}
if (result is Function)
{
result = Context.API.GetTranslation("wox_plugin_calculator_expression_not_complete");
}
if (!string.IsNullOrEmpty(result?.ToString()))
{
@ -85,14 +98,13 @@ namespace Microsoft.Plugin.Calculator
thread.Start();
thread.Join();
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
catch(Exception e)
catch (Exception e)
#pragma warning restore CA1031 // Do not catch general exception types
{
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)
{
if(context == null)
if (context == null)
{
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);
}

View File

@ -116,5 +116,19 @@
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</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>

View File

@ -1,10 +1,11 @@
using System;
using System.Collections.Generic;
// 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.Globalization;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace Microsoft.Plugin.Calculator
{
@ -23,8 +24,8 @@ namespace Microsoft.Plugin.Calculator
this.sourceCulture = sourceCulture;
this.targetCulture = targetCulture;
this.splitRegexForSource = GetSplitRegex(this.sourceCulture);
this.splitRegexForTarget = GetSplitRegex(this.targetCulture);
splitRegexForSource = GetSplitRegex(this.sourceCulture);
splitRegexForTarget = GetSplitRegex(this.targetCulture);
}
/// <summary>
@ -33,12 +34,12 @@ namespace Microsoft.Plugin.Calculator
/// </summary>
/// <param name="sourceCulture">source 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)
{
if (sourceCulture == null)
{
throw new ArgumentNullException(paramName:nameof(sourceCulture));
throw new ArgumentNullException(paramName: nameof(sourceCulture));
}
if (targetCulture == null)
@ -57,21 +58,21 @@ namespace Microsoft.Plugin.Calculator
/// <summary>
/// Translate from source to target culture.
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
/// <param name="input">input string to translate</param>
/// <returns>translated string</returns>
public string Translate(string input)
{
return Translate(input, this.sourceCulture, this.targetCulture, this.splitRegexForSource);
return Translate(input, sourceCulture, targetCulture, splitRegexForSource);
}
/// <summary>
/// Translate from target to source culture.
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
/// <param name="input">input string to translate back to source culture</param>
/// <returns>source culture string</returns>
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)
@ -98,6 +99,7 @@ namespace Microsoft.Plugin.Calculator
{
splitPattern += $"|{Regex.Escape(culture.NumberFormat.NumberGroupSeparator)}";
}
splitPattern += ")+)";
return new Regex(splitPattern);
}