Skip to content

Commit

Permalink
[fxcop] Fixes for Wox.Plugin (1of3) (#7457)
Browse files Browse the repository at this point in the history
* Added CultureInfo (CA1307: Specify StringComparison for clarity / CA1304: Specify CultureInfo)

* Check arguments and throw ArgumentNullException (CA1062: Validate arguments of public methods)

* Changed url parameter from string to System.Uri and added null checks (CA1054: URI parameters should not be strings)

* Rethrow exception without specifying the exception explicitly (CA2200: Rethrow to preserve stack details)

* Changed from Collection property to methods for PluginMetadata::ActionKeywords (CA2227: Collection properties should be read only)

* Changed from Collection property to methods for Result::GetTitleHighlightData (CA2227: Collection properties should be read only)

* Made Collection property read-only and added parameter in constructor for Result::SubTitleHighlightData (CA2227: Collection properties should be read only)

* Made Collection property read only and added parameter in constructor for ResultUpdatedEventArgs::Results (CA2227: Collection properties should be read only)

* CA1507: Use nameof in place of string

* Removed initialization for ThemeManager::_disposed (CA1805: Do not initialize unnecessarily)

* Changed Query::Terms array property to ReadOnlyCollection and added private set (CA1819: Properties should not return arrays)

* CA1060: Move P/Invokes to NativeMethods class

* CA1806: Do not ignore method results

* CA2101: Specify marshaling for P/Invoke string arguments

* Removed unnecessary empty interface IFeatures (CA1040: Avoid empty interfaces)

- Removed IFeatures interface and references
- Renamed IFeatures.cs to IContextMenu.cs according to guidelines

* Added comments for CultureInfo (CA1307: Specify StringComparison for clarity / CA1304: Specify CultureInfo)

* Added localization for Wox.Plugin and localized strings in FilesFolders.cs
  • Loading branch information
avneet-kr authored Oct 26, 2020
1 parent 3906896 commit ca1e5d1
Show file tree
Hide file tree
Showing 31 changed files with 430 additions and 95 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,11 @@ public class FileItemResult : IItemResult

public Wox.Plugin.Result Create(IPublicAPI contextApi)
{
var result = new Wox.Plugin.Result
var result = new Wox.Plugin.Result(StringMatcher.FuzzySearch(Search, Path.GetFileName(FilePath)).MatchData)
{
Title = Title,
SubTitle = string.Format(CultureInfo.CurrentCulture, Properties.Resources.wox_plugin_folder_select_file_result_subtitle, FilePath),
IcoPath = FilePath,
TitleHighlightData = StringMatcher.FuzzySearch(Search, Path.GetFileName(FilePath)).MatchData,
Action = c => ShellAction.Execute(FilePath, contextApi),
ContextData = new SearchResult { Type = ResultType.File, FullPath = FilePath },
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,12 @@ public FolderItemResult(DisplayFileInfo fileSystemInfo)

public Wox.Plugin.Result Create(IPublicAPI contextApi)
{
return new Wox.Plugin.Result
return new Wox.Plugin.Result(StringMatcher.FuzzySearch(Search, Title).MatchData)
{
Title = Title,
IcoPath = Path,
SubTitle = string.Format(CultureInfo.CurrentCulture, Properties.Resources.wox_plugin_folder_select_folder_result_subtitle, Subtitle),
QueryTextDisplay = Path,
TitleHighlightData = StringMatcher.FuzzySearch(Search, Title).MatchData,
ContextData = new SearchResult { Type = ResultType.Folder, FullPath = Path },
Action = c => ShellAction.Execute(Path, contextApi),
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,12 @@ public class UserFolderResult : IItemResult

public Result Create(IPublicAPI contextApi)
{
return new Result
return new Result(StringMatcher.FuzzySearch(Search, Title).MatchData)
{
Title = Title,
IcoPath = Path,
SubTitle = string.Format(CultureInfo.CurrentCulture, Properties.Resources.wox_plugin_folder_select_folder_result_subtitle, Subtitle),
QueryTextDisplay = Path,
TitleHighlightData = StringMatcher.FuzzySearch(Search, Title).MatchData,
ContextData = new SearchResult { Type = ResultType.Folder, FullPath = Path },
Action = c => _shellAction.Execute(Path, contextApi),
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// See the LICENSE file in the project root for more information.

using Microsoft.Plugin.Program.ProgramArgumentParser;
using Mono.Collections.Generic;
using NUnit.Framework;
using Wox.Plugin;

Expand Down Expand Up @@ -34,7 +35,7 @@ public void ProgramArgumentParserTestsCanParseQuery(string inputQuery, string ex
// basic version of the Quey parser which can be found at Wox.Core.Plugin.QueryBuilder but did not want to create a project reference
var splittedSearchString = inputQuery?.Split(Query.TermSeparator, System.StringSplitOptions.RemoveEmptyEntries);
var cleanQuery = string.Join(Query.TermSeparator, splittedSearchString);
var query = new Query(cleanQuery, cleanQuery, splittedSearchString, string.Empty);
var query = new Query(cleanQuery, cleanQuery, new ReadOnlyCollection<string>(splittedSearchString), string.Empty);

// Act
string program = null, programArguments = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ public bool TryParse(Query query, out string program, out string programArgument
if (!string.IsNullOrEmpty(query?.Search))
{
// First Argument is always (part of) the program, 2nd term is possibly a Program Argument
if (query.Terms.Length > 1)
if (query.Terms.Count > 1)
{
for (var i = 1; i < query.Terms.Length; i++)
for (var i = 1; i < query.Terms.Count; i++)
{
if (!string.Equals(query.Terms[i], DoubleDash, StringComparison.Ordinal))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ public bool TryParse(Query query, out string program, out string programArgument
if (!string.IsNullOrEmpty(query?.Search))
{
// First Argument is always (part of) the program, 2nd term is possibly a Program Argument
if (query.Terms.Length > 1)
if (query.Terms.Count > 1)
{
for (var i = 1; i < query.Terms.Length; i++)
for (var i = 1; i < query.Terms.Count; i++)
{
if (!ArgumentPrefixRegex.IsMatch(query.Terms[i]))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public Result Result(string query, string queryArguments, IPublicAPI api)

// To set the title to always be the displayname of the packaged application
result.Title = DisplayName;
result.TitleHighlightData = StringMatcher.FuzzySearch(query, Name).MatchData;
result.SetTitleHighlightData(StringMatcher.FuzzySearch(query, Name).MatchData);

var toolTipTitle = string.Format(CultureInfo.CurrentCulture, "{0}: {1}", Properties.Resources.powertoys_run_plugin_program_file_name, result.Title);
var toolTipText = string.Format(CultureInfo.CurrentCulture, "{0}: {1}", Properties.Resources.powertoys_run_plugin_program_file_path, Package.Location);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ public Result Result(string query, string queryArguments, IPublicAPI api)

// To set the title for the result to always be the name of the application
result.Title = Name;
result.TitleHighlightData = StringMatcher.FuzzySearch(query, Name).MatchData;
result.SetTitleHighlightData(StringMatcher.FuzzySearch(query, Name).MatchData);

var toolTipTitle = string.Format(CultureInfo.CurrentCulture, "{0}: {1}", Properties.Resources.powertoys_run_plugin_program_file_name, result.Title);
var toolTipText = string.Format(CultureInfo.CurrentCulture, "{0}: {1}", Properties.Resources.powertoys_run_plugin_program_file_path, FullPath);
Expand Down
4 changes: 2 additions & 2 deletions src/modules/launcher/Wox.Core/Plugin/PluginConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@ private static PluginMetadata GetPluginMetadata(string pluginDirectory)
metadata.PluginDirectory = pluginDirectory;

// for plugins which doesn't has ActionKeywords key
metadata.ActionKeywords = metadata.ActionKeywords ?? new List<string> { metadata.ActionKeyword };
metadata.SetActionKeywords(metadata.GetActionKeywords() ?? new List<string> { metadata.ActionKeyword });

// for plugin still use old ActionKeyword
metadata.ActionKeyword = metadata.ActionKeywords?[0];
metadata.ActionKeyword = metadata.GetActionKeywords()?[0];
}
catch (Exception e)
{
Expand Down
11 changes: 5 additions & 6 deletions src/modules/launcher/Wox.Core/Plugin/PluginManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public static void InitializePlugins(IPublicAPI api)
}

// Plugins may have multiple ActionKeywords, eg. WebSearch
plugin.Metadata.ActionKeywords.Where(x => x != Query.GlobalPluginWildcardSign)
plugin.Metadata.GetActionKeywords().Where(x => x != Query.GlobalPluginWildcardSign)
.ToList()
.ForEach(x => NonGlobalPlugins[x] = plugin);
}
Expand Down Expand Up @@ -244,7 +244,7 @@ public static void UpdatePluginMetadata(List<Result> results, PluginMetadata met

private static bool IsGlobalPlugin(PluginMetadata metadata)
{
return metadata.ActionKeywords.Contains(Query.GlobalPluginWildcardSign);
return metadata.GetActionKeywords().Contains(Query.GlobalPluginWildcardSign);
}

/// <summary>
Expand All @@ -258,7 +258,6 @@ public static PluginPair GetPluginForId(string id)
}

public static IEnumerable<PluginPair> GetPluginsForInterface<T>()
where T : IFeatures
{
return AllPlugins.Where(p => p.Plugin is T);
}
Expand Down Expand Up @@ -320,7 +319,7 @@ public static void AddActionKeyword(string id, string newActionKeyword)
NonGlobalPlugins[newActionKeyword] = plugin;
}

plugin.Metadata.ActionKeywords.Add(newActionKeyword);
plugin.Metadata.GetActionKeywords().Add(newActionKeyword);
}

/// <summary>
Expand All @@ -332,7 +331,7 @@ public static void RemoveActionKeyword(string id, string oldActionkeyword)
var plugin = GetPluginForId(id);
if (oldActionkeyword == Query.GlobalPluginWildcardSign
&& // Plugins may have multiple ActionKeywords that are global, eg. WebSearch
plugin.Metadata.ActionKeywords
plugin.Metadata.GetActionKeywords()
.Where(x => x == Query.GlobalPluginWildcardSign)
.ToList()
.Count == 1)
Expand All @@ -345,7 +344,7 @@ public static void RemoveActionKeyword(string id, string oldActionkeyword)
NonGlobalPlugins.Remove(oldActionkeyword);
}

plugin.Metadata.ActionKeywords.Remove(oldActionkeyword);
plugin.Metadata.GetActionKeywords().Remove(oldActionkeyword);
}

public static void ReplaceActionKeyword(string id, string oldActionKeyword, string newActionKeyword)
Expand Down
5 changes: 3 additions & 2 deletions src/modules/launcher/Wox.Core/Plugin/QueryBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Mono.Collections.Generic;
using Wox.Plugin;

namespace Wox.Core.Plugin
Expand Down Expand Up @@ -63,7 +64,7 @@ public static Dictionary<PluginPair, Query> Build(ref string text, Dictionary<st
}

// A new query is constructed for each plugin as they have different action keywords
var query = new Query(rawQuery, search, terms, pluginActionKeyword);
var query = new Query(rawQuery, search, new ReadOnlyCollection<string>(terms), pluginActionKeyword);

pluginQueryPair.TryAdd(pluginPair, query);
}
Expand All @@ -80,7 +81,7 @@ public static Dictionary<PluginPair, Query> Build(ref string text, Dictionary<st
{
if (!pluginQueryPair.ContainsKey(globalPlugin))
{
var query = new Query(rawQuery, rawQuery, terms, string.Empty);
var query = new Query(rawQuery, rawQuery, new ReadOnlyCollection<string>(terms), string.Empty);
pluginQueryPair.Add(globalPlugin, query);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public void UpdatePluginSettings(List<PluginMetadata> metadatas)
var settings = Plugins[metadata.ID];
if (settings.ActionKeywords?.Count > 0)
{
metadata.ActionKeywords = settings.ActionKeywords;
metadata.SetActionKeywords(settings.ActionKeywords);
metadata.ActionKeyword = settings.ActionKeywords[0];
}

Expand All @@ -32,7 +32,7 @@ public void UpdatePluginSettings(List<PluginMetadata> metadatas)
{
ID = metadata.ID,
Name = metadata.Name,
ActionKeywords = metadata.ActionKeywords,
ActionKeywords = metadata.GetActionKeywords(),
Disabled = metadata.Disabled,
};
}
Expand Down
13 changes: 11 additions & 2 deletions src/modules/launcher/Wox.Plugin/AllowedLanguage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
// 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;

namespace Wox.Plugin
{
public static class AllowedLanguage
Expand All @@ -18,8 +21,14 @@ public static string Executable

public static bool IsAllowed(string language)
{
return language.ToUpper() == CSharp.ToUpper()
|| language.ToUpper() == Executable.ToUpper();
if (language == null)
{
throw new ArgumentNullException(nameof(language));
}

// Using InvariantCulture since this is a command line arg
return language.ToUpper(CultureInfo.InvariantCulture) == CSharp.ToUpper(CultureInfo.InvariantCulture)
|| language.ToUpper(CultureInfo.InvariantCulture) == Executable.ToUpper(CultureInfo.InvariantCulture);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,22 @@

namespace Wox.Plugin
{
public interface IFeatures
{
}

public interface IContextMenu : IFeatures
public interface IContextMenu
{
List<ContextMenuResult> LoadContextMenus(Result selectedResult);
}

/// <summary>
/// Represent plugins that support internationalization
/// </summary>
public interface IPluginI18n : IFeatures
public interface IPluginI18n
{
string GetTranslatedPluginTitle();

string GetTranslatedPluginDescription();
}

public interface IResultUpdated : IFeatures
public interface IResultUpdated
{
event ResultUpdatedEventHandler ResultsUpdated;
}
Expand Down
2 changes: 1 addition & 1 deletion src/modules/launcher/Wox.Plugin/IDelayedExecutionPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace Wox.Plugin
{
public interface IDelayedExecutionPlugin : IFeatures
public interface IDelayedExecutionPlugin
{
List<Result> Query(Query query, bool delayedExecution);
}
Expand Down
14 changes: 14 additions & 0 deletions src/modules/launcher/Wox.Plugin/LocProject.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"Projects": [
{
"LanguageSet": "Azure_Languages",
"LocItems": [
{
"SourceFile": "src\\modules\\launcher\\Wox.Plugin\\Properties\\Resources.resx",
"CopyOption": "LangIDOnName",
"OutputPath": "src\\modules\\launcher\\Wox.Plugin\\Properties"
}
]
}
]
}
17 changes: 16 additions & 1 deletion src/modules/launcher/Wox.Plugin/PluginMetadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ public class PluginMetadata : BaseModel
{
private string _pluginDirectory;

private List<string> _actionKeywords;

public PluginMetadata(List<string> actionKeywords = null)
{
_actionKeywords = actionKeywords;
}

public string ID { get; set; }

public string Name { get; set; }
Expand Down Expand Up @@ -51,7 +58,15 @@ internal set

public string ActionKeyword { get; set; }

public List<string> ActionKeywords { get; set; }
public List<string> GetActionKeywords()
{
return _actionKeywords;
}

public void SetActionKeywords(List<string> value)
{
_actionKeywords = value;
}

public string IcoPath { get; set; }

Expand Down
8 changes: 6 additions & 2 deletions src/modules/launcher/Wox.Plugin/PluginPair.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// 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;

namespace Wox.Plugin
{
public class PluginPair
Expand All @@ -19,7 +21,8 @@ public override bool Equals(object obj)
{
if (obj is PluginPair r)
{
return string.Equals(r.Metadata.ID, Metadata.ID);
// Using Ordinal since this is used internally
return string.Equals(r.Metadata.ID, Metadata.ID, StringComparison.Ordinal);
}
else
{
Expand All @@ -29,7 +32,8 @@ public override bool Equals(object obj)

public override int GetHashCode()
{
var hashcode = Metadata.ID?.GetHashCode() ?? 0;
// Using Ordinal since this is used internally
var hashcode = Metadata.ID?.GetHashCode(StringComparison.Ordinal) ?? 0;
return hashcode;
}
}
Expand Down
Loading

0 comments on commit ca1e5d1

Please sign in to comment.