Skip to content

Commit 77f3788

Browse files
committed
Add null check before enumerating the enumerable
1 parent de97cd4 commit 77f3788

File tree

5 files changed

+37
-37
lines changed

5 files changed

+37
-37
lines changed

Plugins/Flow.Launcher.Plugin.WebSearch/Main.cs

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ public async Task<List<Result>> QueryAsync(Query query, CancellationToken token)
4141
var results = new List<Result>();
4242

4343
foreach (SearchSource searchSource in _settings.SearchSources.Where(o => (o.ActionKeyword == query.ActionKeyword ||
44-
o.ActionKeyword == SearchSourceGlobalPluginWildCardSign)
45-
&& o.Enabled))
44+
o.ActionKeyword == SearchSourceGlobalPluginWildCardSign)
45+
&& o.Enabled))
4646
{
4747
string keyword = string.Empty;
4848
keyword = searchSource.ActionKeyword == SearchSourceGlobalPluginWildCardSign ? query.ToString() : query.Search;
@@ -85,8 +85,7 @@ public async Task<List<Result>> QueryAsync(Query query, CancellationToken token)
8585

8686
ResultsUpdated?.Invoke(this, new ResultUpdatedEventArgs
8787
{
88-
Results = results,
89-
Query = query
88+
Results = results, Query = query
9089
});
9190

9291
await UpdateResultsFromSuggestionAsync(results, keyword, subtitle, searchSource, query, token).ConfigureAwait(false);
@@ -105,11 +104,11 @@ private async Task UpdateResultsFromSuggestionAsync(List<Result> results, string
105104
if (_settings.EnableSuggestion)
106105
{
107106
var suggestions = await SuggestionsAsync(keyword, subtitle, searchSource, token).ConfigureAwait(false);
108-
if (token.IsCancellationRequested || !suggestions.Any())
107+
var enumerable = suggestions?.ToList();
108+
if (token.IsCancellationRequested || enumerable is not { Count: > 0 })
109109
return;
110-
111-
112-
results.AddRange(suggestions);
110+
111+
results.AddRange(enumerable);
113112

114113
token.ThrowIfCancellationRequested();
115114
}
@@ -118,32 +117,31 @@ private async Task UpdateResultsFromSuggestionAsync(List<Result> results, string
118117
private async Task<IEnumerable<Result>> SuggestionsAsync(string keyword, string subtitle, SearchSource searchSource, CancellationToken token)
119118
{
120119
var source = _settings.SelectedSuggestion;
121-
if (source != null)
120+
if (source == null)
122121
{
123-
//Suggestions appear below actual result, and appear above global action keyword match if non-global;
124-
var score = searchSource.ActionKeyword == SearchSourceGlobalPluginWildCardSign ? scoreSuggestions : scoreSuggestions + 1;
122+
return new List<Result>();
123+
}
124+
//Suggestions appear below actual result, and appear above global action keyword match if non-global;
125+
var score = searchSource.ActionKeyword == SearchSourceGlobalPluginWildCardSign ? scoreSuggestions : scoreSuggestions + 1;
125126

126-
var suggestions = await source.Suggestions(keyword, token).ConfigureAwait(false);
127+
var suggestions = await source.SuggestionsAsync(keyword, token).ConfigureAwait(false);
127128

128-
token.ThrowIfCancellationRequested();
129+
token.ThrowIfCancellationRequested();
129130

130-
var resultsFromSuggestion = suggestions?.Select(o => new Result
131+
var resultsFromSuggestion = suggestions?.Select(o => new Result
132+
{
133+
Title = o,
134+
SubTitle = subtitle,
135+
Score = score,
136+
IcoPath = searchSource.IconPath,
137+
Action = c =>
131138
{
132-
Title = o,
133-
SubTitle = subtitle,
134-
Score = score,
135-
IcoPath = searchSource.IconPath,
136-
ActionKeywordAssigned = searchSource.ActionKeyword == SearchSourceGlobalPluginWildCardSign ? string.Empty : searchSource.ActionKeyword,
137-
Action = c =>
138-
{
139-
_context.API.OpenUrl(searchSource.Url.Replace("{q}", Uri.EscapeDataString(o)));
139+
_context.API.OpenUrl(searchSource.Url.Replace("{q}", Uri.EscapeDataString(o)));
140140

141-
return true;
142-
}
143-
});
144-
return resultsFromSuggestion;
145-
}
146-
return new List<Result>();
141+
return true;
142+
}
143+
});
144+
return resultsFromSuggestion;
147145
}
148146

149147
public Task InitAsync(PluginInitContext context)
@@ -167,7 +165,9 @@ void Init()
167165
// Custom images directory is in the WebSearch's data location folder
168166
var name = Path.GetFileNameWithoutExtension(_context.CurrentPluginMetadata.ExecuteFileName);
169167
CustomImagesDirectory = Path.Combine(DataLocation.PluginSettingsDirectory, name, "CustomIcons");
170-
};
168+
}
169+
170+
;
171171
}
172172

173173
#region ISettingProvider Members
@@ -191,4 +191,4 @@ public string GetTranslatedPluginDescription()
191191

192192
public event ResultUpdatedEventHandler ResultsUpdated;
193193
}
194-
}
194+
}

Plugins/Flow.Launcher.Plugin.WebSearch/SuggestionSources/Baidu.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public class Baidu : SuggestionSource
1616
{
1717
private readonly Regex _reg = new Regex("window.baidu.sug\\((.*)\\)");
1818

19-
public override async Task<List<string>> Suggestions(string query, CancellationToken token)
19+
public override async Task<List<string>> SuggestionsAsync(string query, CancellationToken token)
2020
{
2121
string result;
2222

@@ -25,7 +25,7 @@ public override async Task<List<string>> Suggestions(string query, CancellationT
2525
const string api = "http://suggestion.baidu.com/su?json=1&wd=";
2626
result = await Http.GetAsync(api + Uri.EscapeUriString(query), token).ConfigureAwait(false);
2727
}
28-
catch (Exception e) when (e is HttpRequestException || e.InnerException is TimeoutException)
28+
catch (Exception e) when (e is HttpRequestException or {InnerException: TimeoutException})
2929
{
3030
Log.Exception("|Baidu.Suggestions|Can't get suggestion from baidu", e);
3131
return null;

Plugins/Flow.Launcher.Plugin.WebSearch/SuggestionSources/Bing.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ namespace Flow.Launcher.Plugin.WebSearch.SuggestionSources
1515
{
1616
class Bing : SuggestionSource
1717
{
18-
public override async Task<List<string>> Suggestions(string query, CancellationToken token)
18+
public override async Task<List<string>> SuggestionsAsync(string query, CancellationToken token)
1919
{
2020

2121
try
@@ -40,7 +40,7 @@ public override async Task<List<string>> Suggestions(string query, CancellationT
4040

4141

4242
}
43-
catch (Exception e) when (e is HttpRequestException || e.InnerException is TimeoutException)
43+
catch (Exception e) when (e is HttpRequestException or {InnerException: TimeoutException})
4444
{
4545
Log.Exception("|Baidu.Suggestions|Can't get suggestion from baidu", e);
4646
return null;

Plugins/Flow.Launcher.Plugin.WebSearch/SuggestionSources/Google.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ namespace Flow.Launcher.Plugin.WebSearch.SuggestionSources
1414
{
1515
public class Google : SuggestionSource
1616
{
17-
public override async Task<List<string>> Suggestions(string query, CancellationToken token)
17+
public override async Task<List<string>> SuggestionsAsync(string query, CancellationToken token)
1818
{
1919
try
2020
{
@@ -32,7 +32,7 @@ public override async Task<List<string>> Suggestions(string query, CancellationT
3232
return results.EnumerateArray().Select(o => o.GetString()).ToList();
3333

3434
}
35-
catch (Exception e) when (e is HttpRequestException || e.InnerException is TimeoutException)
35+
catch (Exception e) when (e is HttpRequestException or {InnerException: TimeoutException})
3636
{
3737
Log.Exception("|Baidu.Suggestions|Can't get suggestion from baidu", e);
3838
return null;

Plugins/Flow.Launcher.Plugin.WebSearch/SuggestionSources/SuggestionSource.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ namespace Flow.Launcher.Plugin.WebSearch.SuggestionSources
66
{
77
public abstract class SuggestionSource
88
{
9-
public abstract Task<List<string>> Suggestions(string query, CancellationToken token);
9+
public abstract Task<List<string>> SuggestionsAsync(string query, CancellationToken token);
1010
}
1111
}

0 commit comments

Comments
 (0)