Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 26 additions & 27 deletions Plugins/Flow.Launcher.Plugin.WebSearch/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ public async Task<List<Result>> QueryAsync(Query query, CancellationToken token)
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;
Expand Down Expand Up @@ -105,11 +105,11 @@ private async Task UpdateResultsFromSuggestionAsync(List<Result> results, string
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();
}
Expand All @@ -118,32 +118,31 @@ private async Task UpdateResultsFromSuggestionAsync(List<Result> results, string
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;
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.Suggestions(keyword, token).ConfigureAwait(false);
var suggestions = await source.SuggestionsAsync(keyword, token).ConfigureAwait(false);

token.ThrowIfCancellationRequested();
token.ThrowIfCancellationRequested();

var resultsFromSuggestion = suggestions?.Select(o => new Result
var resultsFromSuggestion = suggestions?.Select(o => new Result
{
Title = o,
SubTitle = subtitle,
Score = score,
IcoPath = searchSource.IconPath,
Action = c =>
{
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)));
_context.API.OpenUrl(searchSource.Url.Replace("{q}", Uri.EscapeDataString(o)));

return true;
}
});
return resultsFromSuggestion;
}
return new List<Result>();
return true;
}
});
return resultsFromSuggestion;
}

public Task InitAsync(PluginInitContext context)
Expand Down Expand Up @@ -191,4 +190,4 @@ public string GetTranslatedPluginDescription()

public event ResultUpdatedEventHandler ResultsUpdated;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class Baidu : SuggestionSource
{
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;

Expand All @@ -25,7 +25,7 @@ public override async Task<List<string>> Suggestions(string query, CancellationT
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -40,7 +40,7 @@ public override async Task<List<string>> Suggestions(string query, CancellationT


}
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -32,7 +32,7 @@ public override async Task<List<string>> Suggestions(string query, CancellationT
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}