Skip to content

Commit 5966ddc

Browse files
committed
Refactor Code
1. Moving Property match to Settings.cs 2. Use Binding to avoid explicit Function Assign 3. Simplify ActionKeywordMatch
1 parent 995cf56 commit 5966ddc

File tree

7 files changed

+178
-164
lines changed

7 files changed

+178
-164
lines changed

Plugins/Flow.Launcher.Plugin.Explorer/Search/SearchManager.cs

Lines changed: 45 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ private class PathEqualityComparator : IEqualityComparer<Result>
2626
{
2727
private static PathEqualityComparator instance;
2828
public static PathEqualityComparator Instance => instance ??= new PathEqualityComparator();
29+
2930
public bool Equals(Result x, Result y)
3031
{
3132
return x.SubTitle == y.SubTitle;
@@ -46,36 +47,39 @@ internal async Task<List<Result>> SearchAsync(Query query, CancellationToken tok
4647

4748
var result = new HashSet<Result>(PathEqualityComparator.Instance);
4849

49-
if (ActionKeywordMatch(query, Settings.ActionKeyword.PathSearchActionKeyword) || ActionKeywordMatch(query, Settings.ActionKeyword.SearchActionKeyword))
50+
if (ActionKeywordMatch(query, Settings.ActionKeyword.PathSearchActionKeyword) ||
51+
ActionKeywordMatch(query, Settings.ActionKeyword.SearchActionKeyword))
5052
{
5153
result.UnionWith(await PathSearchAsync(query, token).ConfigureAwait(false));
5254
}
5355

54-
if ((ActionKeywordMatch(query, Settings.ActionKeyword.IndexOnlySearchActionKeyword) || ActionKeywordMatch(query, Settings.ActionKeyword.SearchActionKeyword)) &&
55-
querySearch.Length > 0 &&
56-
!querySearch.IsLocationPathString())
56+
if ((ActionKeywordMatch(query, Settings.ActionKeyword.IndexOnlySearchActionKeyword) ||
57+
ActionKeywordMatch(query, Settings.ActionKeyword.SearchActionKeyword)) &&
58+
querySearch.Length > 0 &&
59+
!querySearch.IsLocationPathString())
5760
{
58-
result.UnionWith(await WindowsIndexFilesAndFoldersSearchAsync(query, querySearch, token).ConfigureAwait(false));
61+
result.UnionWith(await WindowsIndexFilesAndFoldersSearchAsync(query, querySearch, token)
62+
.ConfigureAwait(false));
5963
}
6064

6165
return result.ToList();
6266
}
6367

6468
private bool ActionKeywordMatch(Query query, Settings.ActionKeyword allowedActionKeyword)
6569
{
66-
if (query.ActionKeyword == settings.IndexOnlySearchActionKeyword)
67-
return Settings.ActionKeyword.IndexOnlySearchActionKeyword == allowedActionKeyword && settings.EnabledIndexOnlySearchKeyword;
68-
69-
if (query.ActionKeyword == settings.PathSearchActionKeyword)
70-
return Settings.ActionKeyword.PathSearchActionKeyword == allowedActionKeyword && settings.EnabledPathSearchKeyword;
70+
var keyword = query.ActionKeyword.Length == 0 ? "*" : query.ActionKeyword;
7171

72-
if (query.ActionKeyword == settings.SearchActionKeyword)
73-
return Settings.ActionKeyword.SearchActionKeyword == allowedActionKeyword;
74-
75-
76-
return (Settings.ActionKeyword.IndexOnlySearchActionKeyword == allowedActionKeyword && settings.EnabledIndexOnlySearchKeyword)
77-
|| (Settings.ActionKeyword.PathSearchActionKeyword == allowedActionKeyword && settings.EnabledPathSearchKeyword)
78-
|| settings.SearchActionKeyword == Query.GlobalPluginWildcardSign;
72+
return allowedActionKeyword switch
73+
{
74+
Settings.ActionKeyword.SearchActionKeyword => settings.EnableSearchActionKeyword &&
75+
keyword == settings.SearchActionKeyword,
76+
Settings.ActionKeyword.PathSearchActionKeyword => settings.EnabledPathSearchKeyword &&
77+
keyword == settings.PathSearchActionKeyword,
78+
Settings.ActionKeyword.FileContentSearchActionKeyword => keyword ==
79+
settings.FileContentSearchActionKeyword,
80+
Settings.ActionKeyword.IndexOnlySearchActionKeyword => settings.EnabledIndexOnlySearchKeyword &&
81+
keyword == settings.IndexOnlySearchActionKeyword
82+
};
7983
}
8084

8185
public async Task<List<Result>> PathSearchAsync(Query query, CancellationToken token = default)
@@ -129,19 +133,20 @@ public async Task<List<Result>> PathSearchAsync(Query query, CancellationToken t
129133
return results.ToList();
130134
}
131135

132-
private async Task<List<Result>> WindowsIndexFileContentSearchAsync(Query query, string querySearchString, CancellationToken token)
136+
private async Task<List<Result>> WindowsIndexFileContentSearchAsync(Query query, string querySearchString,
137+
CancellationToken token)
133138
{
134139
var queryConstructor = new QueryConstructor(settings);
135140

136141
if (string.IsNullOrEmpty(querySearchString))
137142
return new List<Result>();
138143

139144
return await IndexSearch.WindowsIndexSearchAsync(querySearchString,
140-
queryConstructor.CreateQueryHelper().ConnectionString,
141-
queryConstructor.QueryForFileContentSearch,
142-
settings.IndexSearchExcludedSubdirectoryPaths,
143-
query,
144-
token).ConfigureAwait(false);
145+
queryConstructor.CreateQueryHelper().ConnectionString,
146+
queryConstructor.QueryForFileContentSearch,
147+
settings.IndexSearchExcludedSubdirectoryPaths,
148+
query,
149+
token).ConfigureAwait(false);
145150
}
146151

147152
public bool IsFileContentSearch(string actionKeyword)
@@ -168,28 +173,30 @@ public async Task<List<Result>> TopLevelDirectorySearchBehaviourAsync(
168173
return await windowsIndexSearch(query, querySearchString, token);
169174
}
170175

171-
private async Task<List<Result>> WindowsIndexFilesAndFoldersSearchAsync(Query query, string querySearchString, CancellationToken token)
176+
private async Task<List<Result>> WindowsIndexFilesAndFoldersSearchAsync(Query query, string querySearchString,
177+
CancellationToken token)
172178
{
173179
var queryConstructor = new QueryConstructor(settings);
174180

175181
return await IndexSearch.WindowsIndexSearchAsync(querySearchString,
176-
queryConstructor.CreateQueryHelper().ConnectionString,
177-
queryConstructor.QueryForAllFilesAndFolders,
178-
settings.IndexSearchExcludedSubdirectoryPaths,
179-
query,
180-
token).ConfigureAwait(false);
182+
queryConstructor.CreateQueryHelper().ConnectionString,
183+
queryConstructor.QueryForAllFilesAndFolders,
184+
settings.IndexSearchExcludedSubdirectoryPaths,
185+
query,
186+
token).ConfigureAwait(false);
181187
}
182188

183-
private async Task<List<Result>> WindowsIndexTopLevelFolderSearchAsync(Query query, string path, CancellationToken token)
189+
private async Task<List<Result>> WindowsIndexTopLevelFolderSearchAsync(Query query, string path,
190+
CancellationToken token)
184191
{
185192
var queryConstructor = new QueryConstructor(settings);
186193

187194
return await IndexSearch.WindowsIndexSearchAsync(path,
188-
queryConstructor.CreateQueryHelper().ConnectionString,
189-
queryConstructor.QueryForTopLevelDirectorySearch,
190-
settings.IndexSearchExcludedSubdirectoryPaths,
191-
query,
192-
token).ConfigureAwait(false);
195+
queryConstructor.CreateQueryHelper().ConnectionString,
196+
queryConstructor.QueryForTopLevelDirectorySearch,
197+
settings.IndexSearchExcludedSubdirectoryPaths,
198+
query,
199+
token).ConfigureAwait(false);
193200
}
194201

195202
private bool UseWindowsIndexForDirectorySearch(string locationPath)
@@ -200,11 +207,11 @@ private bool UseWindowsIndexForDirectorySearch(string locationPath)
200207
return false;
201208

202209
if (settings.IndexSearchExcludedSubdirectoryPaths
203-
.Any(x => FilesFolders.ReturnPreviousDirectoryIfIncompleteString(pathToDirectory)
204-
.StartsWith(x.Path, StringComparison.OrdinalIgnoreCase)))
210+
.Any(x => FilesFolders.ReturnPreviousDirectoryIfIncompleteString(pathToDirectory)
211+
.StartsWith(x.Path, StringComparison.OrdinalIgnoreCase)))
205212
return false;
206213

207214
return IndexSearch.PathIsIndexed(pathToDirectory);
208215
}
209216
}
210-
}
217+
}

Plugins/Flow.Launcher.Plugin.Explorer/Settings.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
using Flow.Launcher.Plugin.Explorer.Search;
22
using Flow.Launcher.Plugin.Explorer.Search.QuickAccessLinks;
3+
using Microsoft.AspNetCore.DataProtection.AuthenticatedEncryption;
4+
using System;
35
using System.Collections.Generic;
6+
using System.IO;
47

58
namespace Flow.Launcher.Plugin.Explorer
69
{
@@ -18,6 +21,7 @@ public class Settings
1821
public List<AccessLink> IndexSearchExcludedSubdirectoryPaths { get; set; } = new List<AccessLink>();
1922

2023
public string SearchActionKeyword { get; set; } = Query.GlobalPluginWildcardSign;
24+
public bool EnableSearchActionKeyword { get; set; } = true;
2125

2226
public string FileContentSearchActionKeyword { get; set; } = Constants.DefaultContentSearchActionKeyword;
2327

@@ -36,5 +40,38 @@ internal enum ActionKeyword
3640
FileContentSearchActionKeyword,
3741
IndexOnlySearchActionKeyword
3842
}
43+
44+
internal string GetActionKeyword(ActionKeyword actionKeyword) => actionKeyword switch
45+
{
46+
ActionKeyword.SearchActionKeyword => SearchActionKeyword,
47+
ActionKeyword.PathSearchActionKeyword => PathSearchActionKeyword,
48+
ActionKeyword.FileContentSearchActionKeyword => FileContentSearchActionKeyword,
49+
ActionKeyword.IndexOnlySearchActionKeyword => IndexOnlySearchActionKeyword
50+
};
51+
52+
internal void SetActionKeyword(ActionKeyword actionKeyword, string keyword) => _ = actionKeyword switch
53+
{
54+
ActionKeyword.SearchActionKeyword => SearchActionKeyword = keyword,
55+
ActionKeyword.PathSearchActionKeyword => PathSearchActionKeyword = keyword,
56+
ActionKeyword.FileContentSearchActionKeyword => FileContentSearchActionKeyword = keyword,
57+
ActionKeyword.IndexOnlySearchActionKeyword => IndexOnlySearchActionKeyword = keyword,
58+
_ => throw new ArgumentOutOfRangeException(nameof(actionKeyword), actionKeyword, "Unexpected property")
59+
};
60+
61+
internal bool? GetActionKeywordEnable(ActionKeyword actionKeyword) => actionKeyword switch
62+
{
63+
ActionKeyword.SearchActionKeyword => EnableSearchActionKeyword,
64+
ActionKeyword.PathSearchActionKeyword => EnabledPathSearchKeyword,
65+
ActionKeyword.IndexOnlySearchActionKeyword => EnabledIndexOnlySearchKeyword,
66+
_ => null
67+
};
68+
69+
internal void SetActionKeywordEnable(ActionKeyword actionKeyword, bool enable) => _ = actionKeyword switch
70+
{
71+
ActionKeyword.SearchActionKeyword => EnableSearchActionKeyword = enable,
72+
ActionKeyword.PathSearchActionKeyword => EnabledPathSearchKeyword = enable,
73+
ActionKeyword.IndexOnlySearchActionKeyword => EnabledIndexOnlySearchKeyword = enable,
74+
_ => throw new ArgumentOutOfRangeException(nameof(actionKeyword), actionKeyword, "Unexpected property")
75+
};
3976
}
4077
}

Plugins/Flow.Launcher.Plugin.Explorer/ViewModels/SettingsViewModel.cs

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,22 +41,22 @@ internal void OpenWindowsIndexingOptions()
4141
Process.Start(psi);
4242
}
4343

44-
internal void UpdateActionKeyword(ActionKeywordProperty modifiedActionKeyword, string newActionKeyword, string oldActionKeyword)
44+
internal void UpdateActionKeyword(Settings.ActionKeyword modifiedActionKeyword, string newActionKeyword, string oldActionKeyword)
4545
{
4646
PluginManager.ReplaceActionKeyword(Context.CurrentPluginMetadata.ID, oldActionKeyword, newActionKeyword);
4747

4848
switch (modifiedActionKeyword)
4949
{
50-
case ActionKeywordProperty.SearchActionKeyword:
50+
case Settings.ActionKeyword.SearchActionKeyword:
5151
Settings.SearchActionKeyword = newActionKeyword;
5252
break;
53-
case ActionKeywordProperty.PathSearchActionKeyword:
53+
case Settings.ActionKeyword.PathSearchActionKeyword:
5454
Settings.PathSearchActionKeyword = newActionKeyword;
5555
break;
56-
case ActionKeywordProperty.FileContentSearchActionKeyword:
56+
case Settings.ActionKeyword.FileContentSearchActionKeyword:
5757
Settings.FileContentSearchActionKeyword = newActionKeyword;
5858
break;
59-
case ActionKeywordProperty.IndexOnlySearchActionKeyword:
59+
case Settings.ActionKeyword.IndexOnlySearchActionKeyword:
6060
Settings.IndexOnlySearchActionKeyword = newActionKeyword;
6161
break;
6262
}
@@ -66,12 +66,4 @@ internal void UpdateActionKeyword(ActionKeywordProperty modifiedActionKeyword, s
6666

6767
internal bool IsNewActionKeywordGlobal(string newActionKeyword) => newActionKeyword == Query.GlobalPluginWildcardSign;
6868
}
69-
70-
public enum ActionKeywordProperty
71-
{
72-
SearchActionKeyword,
73-
PathSearchActionKeyword,
74-
FileContentSearchActionKeyword,
75-
IndexOnlySearchActionKeyword
76-
}
7769
}

Plugins/Flow.Launcher.Plugin.Explorer/Views/ActionKeywordSetting.xaml

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
mc:Ignorable="d"
88
ResizeMode="NoResize"
99
WindowStartupLocation="CenterScreen"
10+
DataContext="{Binding RelativeSource={RelativeSource Self}}"
1011
Title="Action Keyword Setting" Height="200" Width="500">
1112
<Grid>
1213
<Grid.RowDefinitions>
@@ -20,12 +21,15 @@
2021
</Grid.ColumnDefinitions>
2122
<TextBlock Margin="20 10 10 10" FontSize="14" Grid.Row="0" Grid.Column="0" VerticalAlignment="Center"
2223
HorizontalAlignment="Left" Text="Current Action Keyword:" />
23-
<TextBox Name="txtCurrentActionKeyword"
24+
<TextBox Name="TxtCurrentActionKeyword"
2425
Margin="10" Grid.Row="0" Width="105" Grid.Column="1"
2526
VerticalAlignment="Center"
26-
HorizontalAlignment="Left" />
27-
<CheckBox Name="chkActionKeywordEnabled" Margin="10" Grid.Row="0" Grid.Column="2" Content="Enabled" Width="auto"
28-
VerticalAlignment="Center" Checked="OnActionKeywordEnabledChecked" Unchecked="OnActionKeywordEnabledUnChecked"/>
27+
HorizontalAlignment="Left"
28+
Text="{Binding CurrentActionKeyword.Keyword}" />
29+
<CheckBox Name="ChkActionKeywordEnabled" Margin="10" Grid.Row="0" Grid.Column="2" Content="Enabled"
30+
Width="auto"
31+
VerticalAlignment="Center" IsChecked="{Binding CurrentActionKeyword.Enabled}"
32+
Visibility="{Binding Visible}"/>
2933
<StackPanel Orientation="Horizontal" Grid.Column="1" Grid.ColumnSpan="2" Margin="100 92 0 0" Grid.RowSpan="2">
3034
<Button Click="OnConfirmButtonClick"
3135
Margin="10 0 10 0" Width="80" Height="35"
@@ -35,4 +39,4 @@
3539
Content="Cancel" />
3640
</StackPanel>
3741
</Grid>
38-
</Window>
42+
</Window>

0 commit comments

Comments
 (0)