Add null check before enumerating the enumerable

This commit is contained in:
Hongtao Zhang 2022-01-12 15:21:21 -06:00
parent de97cd4e52
commit 77f3788539
5 changed files with 41 additions and 41 deletions

View file

@ -41,8 +41,8 @@ namespace Flow.Launcher.Plugin.WebSearch
var results = new List<Result>();
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<IEnumerable<Result>> 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<Result>();
}
return new List<Result>();
//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;
}
}
}

View file

@ -16,7 +16,7 @@ namespace Flow.Launcher.Plugin.WebSearch.SuggestionSources
{
private readonly Regex _reg = new Regex("window.baidu.sug\\((.*)\\)");
public override async Task<List<string>> Suggestions(string query, CancellationToken token)
public override async Task<List<string>> 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;

View file

@ -15,7 +15,7 @@ namespace Flow.Launcher.Plugin.WebSearch.SuggestionSources
{
class Bing : SuggestionSource
{
public override async Task<List<string>> Suggestions(string query, CancellationToken token)
public override async Task<List<string>> 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;

View file

@ -14,7 +14,7 @@ namespace Flow.Launcher.Plugin.WebSearch.SuggestionSources
{
public class Google : SuggestionSource
{
public override async Task<List<string>> Suggestions(string query, CancellationToken token)
public override async Task<List<string>> 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;

View file

@ -6,6 +6,6 @@ namespace Flow.Launcher.Plugin.WebSearch.SuggestionSources
{
public abstract class SuggestionSource
{
public abstract Task<List<string>> Suggestions(string query, CancellationToken token);
public abstract Task<List<string>> SuggestionsAsync(string query, CancellationToken token);
}
}