diff --git a/Plugins/Flow.Launcher.Plugin.WebSearch/Main.cs b/Plugins/Flow.Launcher.Plugin.WebSearch/Main.cs index 31d56c108..9848d3c0a 100644 --- a/Plugins/Flow.Launcher.Plugin.WebSearch/Main.cs +++ b/Plugins/Flow.Launcher.Plugin.WebSearch/Main.cs @@ -41,8 +41,8 @@ namespace Flow.Launcher.Plugin.WebSearch var results = new List(); foreach (SearchSource searchSource in _settings.SearchSources.Where(o => (o.ActionKeyword == query.ActionKeyword || - o.ActionKeyword == SearchSourceGlobalPluginWildCardSign) - && o.Enabled)) + o.ActionKeyword == SearchSourceGlobalPluginWildCardSign) + && o.Enabled)) { string keyword = string.Empty; keyword = searchSource.ActionKeyword == SearchSourceGlobalPluginWildCardSign ? query.ToString() : query.Search; @@ -85,8 +85,7 @@ namespace Flow.Launcher.Plugin.WebSearch ResultsUpdated?.Invoke(this, new ResultUpdatedEventArgs { - Results = results, - Query = query + Results = results, Query = query }); await UpdateResultsFromSuggestionAsync(results, keyword, subtitle, searchSource, query, token).ConfigureAwait(false); @@ -105,11 +104,11 @@ namespace Flow.Launcher.Plugin.WebSearch if (_settings.EnableSuggestion) { var suggestions = await SuggestionsAsync(keyword, subtitle, searchSource, token).ConfigureAwait(false); - if (token.IsCancellationRequested || !suggestions.Any()) + var enumerable = suggestions?.ToList(); + if (token.IsCancellationRequested || enumerable is not { Count: > 0 }) return; - - - results.AddRange(suggestions); + + results.AddRange(enumerable); token.ThrowIfCancellationRequested(); } @@ -118,32 +117,31 @@ namespace Flow.Launcher.Plugin.WebSearch private async Task> SuggestionsAsync(string keyword, string subtitle, SearchSource searchSource, CancellationToken token) { var source = _settings.SelectedSuggestion; - if (source != null) + if (source == null) { - //Suggestions appear below actual result, and appear above global action keyword match if non-global; - var score = searchSource.ActionKeyword == SearchSourceGlobalPluginWildCardSign ? scoreSuggestions : scoreSuggestions + 1; - - var suggestions = await source.Suggestions(keyword, token).ConfigureAwait(false); - - token.ThrowIfCancellationRequested(); - - var resultsFromSuggestion = suggestions?.Select(o => new Result - { - Title = o, - SubTitle = subtitle, - Score = score, - IcoPath = searchSource.IconPath, - ActionKeywordAssigned = searchSource.ActionKeyword == SearchSourceGlobalPluginWildCardSign ? string.Empty : searchSource.ActionKeyword, - Action = c => - { - _context.API.OpenUrl(searchSource.Url.Replace("{q}", Uri.EscapeDataString(o))); - - return true; - } - }); - return resultsFromSuggestion; + return new List(); } - return new List(); + //Suggestions appear below actual result, and appear above global action keyword match if non-global; + var score = searchSource.ActionKeyword == SearchSourceGlobalPluginWildCardSign ? scoreSuggestions : scoreSuggestions + 1; + + var suggestions = await source.SuggestionsAsync(keyword, token).ConfigureAwait(false); + + token.ThrowIfCancellationRequested(); + + var resultsFromSuggestion = suggestions?.Select(o => new Result + { + Title = o, + SubTitle = subtitle, + Score = score, + IcoPath = searchSource.IconPath, + Action = c => + { + _context.API.OpenUrl(searchSource.Url.Replace("{q}", Uri.EscapeDataString(o))); + + return true; + } + }); + return resultsFromSuggestion; } public Task InitAsync(PluginInitContext context) @@ -167,7 +165,9 @@ namespace Flow.Launcher.Plugin.WebSearch // Custom images directory is in the WebSearch's data location folder var name = Path.GetFileNameWithoutExtension(_context.CurrentPluginMetadata.ExecuteFileName); CustomImagesDirectory = Path.Combine(DataLocation.PluginSettingsDirectory, name, "CustomIcons"); - }; + } + + ; } #region ISettingProvider Members @@ -191,4 +191,4 @@ namespace Flow.Launcher.Plugin.WebSearch public event ResultUpdatedEventHandler ResultsUpdated; } -} +} \ No newline at end of file diff --git a/Plugins/Flow.Launcher.Plugin.WebSearch/SuggestionSources/Baidu.cs b/Plugins/Flow.Launcher.Plugin.WebSearch/SuggestionSources/Baidu.cs index ccb5b20d7..d1fbd4ac5 100644 --- a/Plugins/Flow.Launcher.Plugin.WebSearch/SuggestionSources/Baidu.cs +++ b/Plugins/Flow.Launcher.Plugin.WebSearch/SuggestionSources/Baidu.cs @@ -16,7 +16,7 @@ namespace Flow.Launcher.Plugin.WebSearch.SuggestionSources { private readonly Regex _reg = new Regex("window.baidu.sug\\((.*)\\)"); - public override async Task> Suggestions(string query, CancellationToken token) + public override async Task> SuggestionsAsync(string query, CancellationToken token) { string result; @@ -25,7 +25,7 @@ namespace Flow.Launcher.Plugin.WebSearch.SuggestionSources const string api = "http://suggestion.baidu.com/su?json=1&wd="; result = await Http.GetAsync(api + Uri.EscapeUriString(query), token).ConfigureAwait(false); } - catch (Exception e) when (e is HttpRequestException || e.InnerException is TimeoutException) + catch (Exception e) when (e is HttpRequestException or {InnerException: TimeoutException}) { Log.Exception("|Baidu.Suggestions|Can't get suggestion from baidu", e); return null; diff --git a/Plugins/Flow.Launcher.Plugin.WebSearch/SuggestionSources/Bing.cs b/Plugins/Flow.Launcher.Plugin.WebSearch/SuggestionSources/Bing.cs index 38c5fb4a0..971fc079c 100644 --- a/Plugins/Flow.Launcher.Plugin.WebSearch/SuggestionSources/Bing.cs +++ b/Plugins/Flow.Launcher.Plugin.WebSearch/SuggestionSources/Bing.cs @@ -15,7 +15,7 @@ namespace Flow.Launcher.Plugin.WebSearch.SuggestionSources { class Bing : SuggestionSource { - public override async Task> Suggestions(string query, CancellationToken token) + public override async Task> SuggestionsAsync(string query, CancellationToken token) { try @@ -40,7 +40,7 @@ namespace Flow.Launcher.Plugin.WebSearch.SuggestionSources } - catch (Exception e) when (e is HttpRequestException || e.InnerException is TimeoutException) + catch (Exception e) when (e is HttpRequestException or {InnerException: TimeoutException}) { Log.Exception("|Baidu.Suggestions|Can't get suggestion from baidu", e); return null; diff --git a/Plugins/Flow.Launcher.Plugin.WebSearch/SuggestionSources/Google.cs b/Plugins/Flow.Launcher.Plugin.WebSearch/SuggestionSources/Google.cs index c5f43d081..a6c18b1ef 100644 --- a/Plugins/Flow.Launcher.Plugin.WebSearch/SuggestionSources/Google.cs +++ b/Plugins/Flow.Launcher.Plugin.WebSearch/SuggestionSources/Google.cs @@ -14,7 +14,7 @@ namespace Flow.Launcher.Plugin.WebSearch.SuggestionSources { public class Google : SuggestionSource { - public override async Task> Suggestions(string query, CancellationToken token) + public override async Task> SuggestionsAsync(string query, CancellationToken token) { try { @@ -32,7 +32,7 @@ namespace Flow.Launcher.Plugin.WebSearch.SuggestionSources return results.EnumerateArray().Select(o => o.GetString()).ToList(); } - catch (Exception e) when (e is HttpRequestException || e.InnerException is TimeoutException) + catch (Exception e) when (e is HttpRequestException or {InnerException: TimeoutException}) { Log.Exception("|Baidu.Suggestions|Can't get suggestion from baidu", e); return null; diff --git a/Plugins/Flow.Launcher.Plugin.WebSearch/SuggestionSources/SuggestionSource.cs b/Plugins/Flow.Launcher.Plugin.WebSearch/SuggestionSources/SuggestionSource.cs index c58e61141..e89addee9 100644 --- a/Plugins/Flow.Launcher.Plugin.WebSearch/SuggestionSources/SuggestionSource.cs +++ b/Plugins/Flow.Launcher.Plugin.WebSearch/SuggestionSources/SuggestionSource.cs @@ -6,6 +6,6 @@ namespace Flow.Launcher.Plugin.WebSearch.SuggestionSources { public abstract class SuggestionSource { - public abstract Task> Suggestions(string query, CancellationToken token); + public abstract Task> SuggestionsAsync(string query, CancellationToken token); } }