Skip to content

Commit f3aa978

Browse files
committed
Refactor SearchManager.cs
1 parent b9f58a7 commit f3aa978

File tree

3 files changed

+179
-167
lines changed

3 files changed

+179
-167
lines changed

Flow.Launcher.Plugin.OneNote/Flow.Launcher.Plugin.OneNote.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
1212
<UseWPF>true</UseWPF>
1313
<UseWindowsForms>true</UseWindowsForms>
14-
<AllowUnsafeBlocks>true</AllowUnsafeBlocks> <!-- Required for bringing OneNote to front -->
14+
<AllowUnsafeBlocks>true</AllowUnsafeBlocks> <!-- Required for bringing OneNote to front -->
15+
<SatelliteResourceLanguages>en</SatelliteResourceLanguages>
1516
</PropertyGroup>
1617

1718
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using Odotocodot.OneNote.Linq;
5+
6+
namespace Flow.Launcher.Plugin.OneNote
7+
{
8+
public partial class SearchManager
9+
{
10+
private sealed class NotebookExplorer
11+
{
12+
private readonly SearchManager searchManager;
13+
private readonly ResultCreator resultCreator;
14+
15+
private Keywords Keywords => searchManager.settings.Keywords;
16+
internal NotebookExplorer(SearchManager searchManager, ResultCreator resultCreator)
17+
{
18+
this.searchManager = searchManager;
19+
this.resultCreator = resultCreator;
20+
}
21+
22+
internal List<Result> Query(Query query)
23+
{
24+
var results = new List<Result>();
25+
26+
string fullSearch = query.Search[(query.Search.IndexOf(Keywords.NotebookExplorer, StringComparison.Ordinal) + Keywords.NotebookExplorer.Length)..];
27+
28+
IOneNoteItem parent = null;
29+
IEnumerable<IOneNoteItem> collection = OneNoteApplication.GetNotebooks();
30+
31+
string[] searches = fullSearch.Split(Keywords.NotebookExplorerSeparator, StringSplitOptions.None);
32+
33+
for (int i = -1; i < searches.Length - 1; i++)
34+
{
35+
if (i < 0)
36+
{
37+
continue;
38+
}
39+
40+
parent = collection.FirstOrDefault(item => item.Name.Equals(searches[i]));
41+
if (parent == null)
42+
{
43+
return results;
44+
}
45+
46+
collection = parent.Children;
47+
}
48+
49+
string lastSearch = searches[^1];
50+
51+
results = lastSearch switch
52+
{
53+
// Empty search so show all in collection
54+
string search when string.IsNullOrWhiteSpace(search)
55+
=> EmptySearch(parent, collection),
56+
57+
// Search by title
58+
string search when search.StartsWith(Keywords.TitleSearch) && parent is not OneNotePage
59+
=> searchManager.TitleSearch(search, parent, collection),
60+
61+
// Scoped search
62+
string search when search.StartsWith(Keywords.ScopedSearch) && parent is OneNoteNotebook or OneNoteSectionGroup
63+
=> ScopedSearch(search, parent),
64+
65+
// Default search
66+
_ => Explorer(lastSearch, parent, collection),
67+
};
68+
69+
if (parent != null)
70+
{
71+
var result = resultCreator.CreateOneNoteItemResult(parent, false, score: 4000);
72+
result.Title = $"Open \"{parent.Name}\" in OneNote";
73+
result.SubTitle = lastSearch switch
74+
{
75+
string search when search.StartsWith(Keywords.TitleSearch)
76+
=> $"Now searching by title in \"{parent.Name}\"",
77+
78+
string search when search.StartsWith(Keywords.ScopedSearch)
79+
=> $"Now searching all pages in \"{parent.Name}\"",
80+
81+
_ => $"Use \'{Keywords.ScopedSearch}\' to search this item. Use \'{Keywords.TitleSearch}\' to search by title in this item",
82+
};
83+
84+
results.Add(result);
85+
}
86+
87+
return results;
88+
}
89+
90+
private List<Result> EmptySearch(IOneNoteItem parent, IEnumerable<IOneNoteItem> collection)
91+
{
92+
List<Result> results = collection.Where(searchManager.SettingsCheck)
93+
.Select(item => resultCreator.CreateOneNoteItemResult(item, true))
94+
.ToList();
95+
if (results.Any())
96+
return results;
97+
return resultCreator.NoItemsInCollection(results, parent);
98+
}
99+
100+
private List<Result> ScopedSearch(string query, IOneNoteItem parent)
101+
{
102+
if (query.Length == Keywords.ScopedSearch.Length)
103+
{
104+
return ResultCreator.NoMatchesFound();
105+
}
106+
107+
if (!char.IsLetterOrDigit(query[Keywords.ScopedSearch.Length]))
108+
{
109+
return resultCreator.InvalidQuery();
110+
}
111+
112+
string currentSearch = query[Keywords.TitleSearch.Length..];
113+
114+
var results = OneNoteApplication.FindPages(currentSearch, parent)
115+
.Select(pg => resultCreator.CreatePageResult(pg, currentSearch))
116+
.ToList();
117+
118+
if (!results.Any())
119+
{
120+
results = ResultCreator.NoMatchesFound();
121+
}
122+
123+
return results;
124+
}
125+
#nullable enable
126+
private List<Result> Explorer(string search, IOneNoteItem? parent, IEnumerable<IOneNoteItem> collection)
127+
{
128+
List<int>? highlightData = null;
129+
int score = 0;
130+
131+
var results = collection.Where(searchManager.SettingsCheck)
132+
.Where(item => searchManager.FuzzySearch(item.Name, search, out highlightData, out score))
133+
.Select(item => resultCreator.CreateOneNoteItemResult(item, true, highlightData, score))
134+
.ToList();
135+
136+
AddCreateNewOneNoteItemResults(search, parent, results);
137+
return results;
138+
}
139+
140+
private void AddCreateNewOneNoteItemResults(string newItemName, IOneNoteItem? parent, List<Result> results)
141+
{
142+
if (results.Any(result => string.Equals(newItemName.Trim(), result.Title, StringComparison.OrdinalIgnoreCase)))
143+
{
144+
return;
145+
}
146+
147+
if (parent?.IsInRecycleBin() == true)
148+
{
149+
return;
150+
}
151+
152+
switch (parent)
153+
{
154+
case null:
155+
results.Add(resultCreator.CreateNewNotebookResult(newItemName));
156+
break;
157+
case OneNoteNotebook:
158+
case OneNoteSectionGroup:
159+
results.Add(resultCreator.CreateNewSectionResult(newItemName, parent));
160+
results.Add(resultCreator.CreateNewSectionGroupResult(newItemName, parent));
161+
break;
162+
case OneNoteSection section:
163+
if (!section.Locked)
164+
{
165+
results.Add(resultCreator.CreateNewPageResult(newItemName, section));
166+
}
167+
168+
break;
169+
}
170+
}
171+
}
172+
}
173+
}
Lines changed: 4 additions & 166 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
using System;
2-
using System.Collections.Generic;
1+
using System.Collections.Generic;
32
using System.Linq;
43
using Odotocodot.OneNote.Linq;
54

65
namespace Flow.Launcher.Plugin.OneNote
76
{
8-
public class SearchManager
7+
public partial class SearchManager
98
{
109
private readonly PluginInitContext context;
1110
private readonly Settings settings;
@@ -73,10 +72,10 @@ private List<Result> TitleSearch(string query, IOneNoteItem parent, IEnumerable<
7372
private List<Result> RecentPages(string query)
7473
{
7574
int count = settings.DefaultRecentsCount;
76-
75+
7776
if (query.Length > settings.Keywords.RecentPages.Length && int.TryParse(query[settings.Keywords.RecentPages.Length..], out int userChosenCount))
7877
count = userChosenCount;
79-
78+
8079
return OneNoteApplication.GetNotebooks()
8180
.GetPages()
8281
.Where(SettingsCheck)
@@ -102,166 +101,5 @@ private bool SettingsCheck(IOneNoteItem item)
102101
success = false;
103102
return success;
104103
}
105-
private sealed class NotebookExplorer
106-
{
107-
private readonly SearchManager searchManager;
108-
private readonly ResultCreator resultCreator;
109-
110-
private Keywords Keywords => searchManager.settings.Keywords;
111-
internal NotebookExplorer(SearchManager searchManager, ResultCreator resultCreator)
112-
{
113-
this.searchManager = searchManager;
114-
this.resultCreator = resultCreator;
115-
}
116-
117-
internal List<Result> Query(Query query)
118-
{
119-
var results = new List<Result>();
120-
121-
string fullSearch = query.Search[(query.Search.IndexOf(Keywords.NotebookExplorer, StringComparison.Ordinal) + Keywords.NotebookExplorer.Length)..];
122-
123-
IOneNoteItem parent = null;
124-
IEnumerable<IOneNoteItem> collection = OneNoteApplication.GetNotebooks();
125-
126-
string[] searches = fullSearch.Split(Keywords.NotebookExplorerSeparator, StringSplitOptions.None);
127-
128-
for (int i = -1; i < searches.Length - 1; i++)
129-
{
130-
if (i < 0)
131-
{
132-
continue;
133-
}
134-
135-
parent = collection.FirstOrDefault(item => item.Name.Equals(searches[i]));
136-
if (parent == null)
137-
{
138-
return results;
139-
}
140-
141-
collection = parent.Children;
142-
}
143-
144-
string lastSearch = searches[^1];
145-
146-
results = lastSearch switch
147-
{
148-
// Empty search so show all in collection
149-
string search when string.IsNullOrWhiteSpace(search)
150-
=> EmptySearch(parent, collection),
151-
152-
// Search by title
153-
string search when search.StartsWith(Keywords.TitleSearch) && parent is not OneNotePage
154-
=> searchManager.TitleSearch(search, parent, collection),
155-
156-
// Scoped search
157-
string search when search.StartsWith(Keywords.ScopedSearch) && parent is OneNoteNotebook or OneNoteSectionGroup
158-
=> ScopedSearch(search, parent),
159-
160-
// Default search
161-
_ => Explorer(lastSearch, parent, collection),
162-
};
163-
164-
if (parent != null)
165-
{
166-
var result = resultCreator.CreateOneNoteItemResult(parent, false, score: 4000);
167-
result.Title = $"Open \"{parent.Name}\" in OneNote";
168-
result.SubTitle = lastSearch switch
169-
{
170-
string search when search.StartsWith(Keywords.TitleSearch)
171-
=> $"Now searching by title in \"{parent.Name}\"",
172-
173-
string search when search.StartsWith(Keywords.ScopedSearch)
174-
=> $"Now searching all pages in \"{parent.Name}\"",
175-
176-
_ => $"Use \'{Keywords.ScopedSearch}\' to search this item. Use \'{Keywords.TitleSearch}\' to search by title in this item",
177-
};
178-
179-
results.Add(result);
180-
}
181-
182-
return results;
183-
}
184-
185-
private List<Result> EmptySearch(IOneNoteItem parent, IEnumerable<IOneNoteItem> collection)
186-
{
187-
List<Result> results = collection.Where(searchManager.SettingsCheck)
188-
.Select(item => resultCreator.CreateOneNoteItemResult(item, true))
189-
.ToList();
190-
if (results.Any())
191-
return results;
192-
return resultCreator.NoItemsInCollection(results, parent);
193-
}
194-
195-
private List<Result> ScopedSearch(string query, IOneNoteItem parent)
196-
{
197-
if (query.Length == Keywords.ScopedSearch.Length)
198-
{
199-
return ResultCreator.NoMatchesFound();
200-
}
201-
202-
if (!char.IsLetterOrDigit(query[Keywords.ScopedSearch.Length]))
203-
{
204-
return resultCreator.InvalidQuery();
205-
}
206-
207-
string currentSearch = query[Keywords.TitleSearch.Length..];
208-
var results = new List<Result>();
209-
210-
results = OneNoteApplication.FindPages(currentSearch, parent)
211-
.Select(pg => resultCreator.CreatePageResult(pg, currentSearch))
212-
.ToList();
213-
214-
if (!results.Any())
215-
{
216-
results = ResultCreator.NoMatchesFound();
217-
}
218-
219-
return results;
220-
}
221-
#nullable enable
222-
private List<Result> Explorer(string search, IOneNoteItem? parent, IEnumerable<IOneNoteItem> collection)
223-
{
224-
List<int>? highlightData = null;
225-
int score = 0;
226-
227-
var results = collection.Where(searchManager.SettingsCheck)
228-
.Where(item => searchManager.FuzzySearch(item.Name, search, out highlightData, out score))
229-
.Select(item => resultCreator.CreateOneNoteItemResult(item, true, highlightData, score))
230-
.ToList();
231-
232-
AddCreateNewOneNoteItemResults(search, parent, results);
233-
return results;
234-
}
235-
236-
private void AddCreateNewOneNoteItemResults(string newItemName, IOneNoteItem? parent, List<Result> results)
237-
{
238-
if (!results.Any(result => string.Equals(newItemName.Trim(), result.Title, StringComparison.OrdinalIgnoreCase)))
239-
{
240-
if (parent?.IsInRecycleBin() == true)
241-
{
242-
return;
243-
}
244-
245-
switch (parent)
246-
{
247-
case null:
248-
results.Add(resultCreator.CreateNewNotebookResult(newItemName));
249-
break;
250-
case OneNoteNotebook:
251-
case OneNoteSectionGroup:
252-
results.Add(resultCreator.CreateNewSectionResult(newItemName, parent));
253-
results.Add(resultCreator.CreateNewSectionGroupResult(newItemName, parent));
254-
break;
255-
case OneNoteSection section:
256-
if (!section.Locked)
257-
{
258-
results.Add(resultCreator.CreateNewPageResult(newItemName, section));
259-
}
260-
261-
break;
262-
}
263-
}
264-
}
265-
}
266104
}
267105
}

0 commit comments

Comments
 (0)