Skip to content

Commit 2a68a41

Browse files
authored
Merge pull request #681 from Flow-Launcher/QueryTermsRefactor
Fixes Typo TermSeparator & remove the actionkeyword in Terms
2 parents 13917d1 + fdc8ae2 commit 2a68a41

File tree

7 files changed

+44
-47
lines changed

7 files changed

+44
-47
lines changed

Flow.Launcher.Core/Plugin/QueryBuilder.cs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,35 +10,31 @@ public static class QueryBuilder
1010
public static Query Build(string text, Dictionary<string, PluginPair> nonGlobalPlugins)
1111
{
1212
// replace multiple white spaces with one white space
13-
var terms = text.Split(new[] { Query.TermSeperater }, StringSplitOptions.RemoveEmptyEntries);
13+
var terms = text.Split(Query.TermSeparator, StringSplitOptions.RemoveEmptyEntries);
1414
if (terms.Length == 0)
1515
{ // nothing was typed
1616
return null;
1717
}
1818

19-
var rawQuery = string.Join(Query.TermSeperater, terms);
19+
var rawQuery = string.Join(Query.TermSeparator, terms);
2020
string actionKeyword, search;
2121
string possibleActionKeyword = terms[0];
22-
List<string> actionParameters;
22+
string[] searchTerms;
23+
2324
if (nonGlobalPlugins.TryGetValue(possibleActionKeyword, out var pluginPair) && !pluginPair.Metadata.Disabled)
2425
{ // use non global plugin for query
2526
actionKeyword = possibleActionKeyword;
26-
actionParameters = terms.Skip(1).ToList();
27-
search = actionParameters.Count > 0 ? rawQuery.Substring(actionKeyword.Length + 1) : string.Empty;
27+
search = terms.Length > 1 ? rawQuery[(actionKeyword.Length + 1)..] : string.Empty;
28+
searchTerms = terms[1..];
2829
}
2930
else
3031
{ // non action keyword
3132
actionKeyword = string.Empty;
3233
search = rawQuery;
34+
searchTerms = terms;
3335
}
3436

35-
var query = new Query
36-
{
37-
Terms = terms,
38-
RawQuery = rawQuery,
39-
ActionKeyword = actionKeyword,
40-
Search = search
41-
};
37+
var query = new Query(rawQuery, search,terms, searchTerms, actionKeyword);
4238

4339
return query;
4440
}

Flow.Launcher.Plugin/Query.cs

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System;
1+
using JetBrains.Annotations;
2+
using System;
23
using System.Collections.Generic;
34
using System.Linq;
45

@@ -11,65 +12,74 @@ public Query() { }
1112
/// <summary>
1213
/// to allow unit tests for plug ins
1314
/// </summary>
14-
public Query(string rawQuery, string search, string[] terms, string actionKeyword = "")
15+
public Query(string rawQuery, string search, string[] terms, string[] searchTerms, string actionKeyword = "")
1516
{
1617
Search = search;
1718
RawQuery = rawQuery;
1819
Terms = terms;
20+
SearchTerms = searchTerms;
1921
ActionKeyword = actionKeyword;
2022
}
2123

2224
/// <summary>
2325
/// Raw query, this includes action keyword if it has
2426
/// We didn't recommend use this property directly. You should always use Search property.
2527
/// </summary>
26-
public string RawQuery { get; internal set; }
28+
public string RawQuery { get; internal init; }
2729

2830
/// <summary>
2931
/// Search part of a query.
3032
/// This will not include action keyword if exclusive plugin gets it, otherwise it should be same as RawQuery.
3133
/// Since we allow user to switch a exclusive plugin to generic plugin,
3234
/// so this property will always give you the "real" query part of the query
3335
/// </summary>
34-
public string Search { get; internal set; }
36+
public string Search { get; internal init; }
3537

3638
/// <summary>
37-
/// The raw query splited into a string array.
39+
/// The search string split into a string array.
3840
/// </summary>
39-
public string[] Terms { get; set; }
41+
public string[] SearchTerms { get; init; }
42+
43+
/// <summary>
44+
/// The raw query split into a string array
45+
/// </summary>
46+
[Obsolete("It may or may not include action keyword, which can be confusing. Use SearchTerms instead")]
47+
public string[] Terms { get; init; }
4048

4149
/// <summary>
4250
/// Query can be splited into multiple terms by whitespace
4351
/// </summary>
44-
public const string TermSeperater = " ";
52+
public const string TermSeparator = " ";
53+
54+
[Obsolete("Typo")]
55+
public const string TermSeperater = TermSeparator;
4556
/// <summary>
4657
/// User can set multiple action keywords seperated by ';'
4758
/// </summary>
48-
public const string ActionKeywordSeperater = ";";
59+
public const string ActionKeywordSeparator = ";";
60+
61+
[Obsolete("Typo")]
62+
public const string ActionKeywordSeperater = ActionKeywordSeparator;
63+
4964

5065
/// <summary>
5166
/// '*' is used for System Plugin
5267
/// </summary>
5368
public const string GlobalPluginWildcardSign = "*";
5469

55-
public string ActionKeyword { get; set; }
70+
public string ActionKeyword { get; init; }
5671

5772
/// <summary>
5873
/// Return first search split by space if it has
5974
/// </summary>
6075
public string FirstSearch => SplitSearch(0);
6176

77+
private string _secondToEndSearch;
78+
6279
/// <summary>
6380
/// strings from second search (including) to last search
6481
/// </summary>
65-
public string SecondToEndSearch
66-
{
67-
get
68-
{
69-
var index = string.IsNullOrEmpty(ActionKeyword) ? 1 : 2;
70-
return string.Join(TermSeperater, Terms.Skip(index).ToArray());
71-
}
72-
}
82+
public string SecondToEndSearch => SearchTerms.Length > 1 ? (_secondToEndSearch ??= string.Join(' ', SearchTerms[1..])) : "";
7383

7484
/// <summary>
7585
/// Return second search split by space if it has
@@ -83,16 +93,9 @@ public string SecondToEndSearch
8393

8494
private string SplitSearch(int index)
8595
{
86-
try
87-
{
88-
return string.IsNullOrEmpty(ActionKeyword) ? Terms[index] : Terms[index + 1];
89-
}
90-
catch (IndexOutOfRangeException)
91-
{
92-
return string.Empty;
93-
}
96+
return index < SearchTerms.Length ? SearchTerms[index] : string.Empty;
9497
}
9598

9699
public override string ToString() => RawQuery;
97100
}
98-
}
101+
}

Flow.Launcher/ActionKeywords.xaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public ActionKeywords(string pluginId, Settings settings, PluginViewModel plugin
3030

3131
private void ActionKeyword_OnLoaded(object sender, RoutedEventArgs e)
3232
{
33-
tbOldActionKeyword.Text = string.Join(Query.ActionKeywordSeperater, plugin.Metadata.ActionKeywords.ToArray());
33+
tbOldActionKeyword.Text = string.Join(Query.ActionKeywordSeparator, plugin.Metadata.ActionKeywords.ToArray());
3434
tbAction.Focus();
3535
}
3636

Flow.Launcher/ViewModel/PluginViewModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public bool PluginState
2222
public Visibility ActionKeywordsVisibility => PluginPair.Metadata.ActionKeywords.Count == 1 ? Visibility.Visible : Visibility.Collapsed;
2323
public string InitilizaTime => PluginPair.Metadata.InitTime.ToString() + "ms";
2424
public string QueryTime => PluginPair.Metadata.AvgQueryTime + "ms";
25-
public string ActionKeywordsText => string.Join(Query.ActionKeywordSeperater, PluginPair.Metadata.ActionKeywords);
25+
public string ActionKeywordsText => string.Join(Query.ActionKeywordSeparator, PluginPair.Metadata.ActionKeywords);
2626
public int Priority => PluginPair.Metadata.Priority;
2727

2828
public void ChangeActionKeyword(string newActionKeyword, string oldActionKeyword)

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ public List<Result> Query(Query query)
1212
{
1313
// if query contains more than one word, eg. github tips
1414
// user has decided to type something else rather than wanting to see the available action keywords
15-
if (query.Terms.Length > 1)
15+
if (query.SearchTerms.Length > 1)
1616
return new List<Result>();
1717

1818
var results = from keyword in PluginManager.NonGlobalPlugins.Keys
19-
where keyword.StartsWith(query.Terms[0])
19+
where keyword.StartsWith(query.SearchTerms[0])
2020
let metadata = PluginManager.NonGlobalPlugins[keyword].Metadata
2121
where !metadata.Disabled
2222
select new Result
@@ -27,7 +27,7 @@ where keyword.StartsWith(query.Terms[0])
2727
IcoPath = metadata.IcoPath,
2828
Action = c =>
2929
{
30-
context.API.ChangeQuery($"{keyword}{Plugin.Query.TermSeperater}");
30+
context.API.ChangeQuery($"{keyword}{Plugin.Query.TermSeparator}");
3131
return false;
3232
}
3333
};

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,7 @@ public void Init(PluginInitContext context)
2323

2424
public List<Result> Query(Query query)
2525
{
26-
var termToSearch = query.Terms.Length <= 1
27-
? null
28-
: string.Join(Plugin.Query.TermSeperater, query.Terms.Skip(1)).ToLower();
26+
var termToSearch = query.Search;
2927

3028
var processlist = processHelper.GetMatchingProcesses(termToSearch);
3129

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ bool API_GlobalKeyboardEvent(int keyevent, int vkcode, SpecialKeyState state)
325325

326326
private void OnWinRPressed()
327327
{
328-
context.API.ChangeQuery($"{context.CurrentPluginMetadata.ActionKeywords[0]}{Plugin.Query.TermSeperater}");
328+
context.API.ChangeQuery($"{context.CurrentPluginMetadata.ActionKeywords[0]}{Plugin.Query.TermSeparator}");
329329

330330
// show the main window and set focus to the query box
331331
Window mainWindow = Application.Current.MainWindow;

0 commit comments

Comments
 (0)