-
Notifications
You must be signed in to change notification settings - Fork 2
/
SearchManager.cs
267 lines (219 loc) · 10.8 KB
/
SearchManager.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
using System;
using System.Collections.Generic;
using System.Linq;
using Odotocodot.OneNote.Linq;
namespace Flow.Launcher.Plugin.OneNote
{
public class SearchManager
{
private readonly PluginInitContext context;
private readonly Settings settings;
private readonly ResultCreator resultCreator;
private readonly NotebookExplorer notebookExplorer;
public SearchManager(PluginInitContext context, Settings settings, ResultCreator resultCreator)
{
this.context = context;
this.settings = settings;
this.resultCreator = resultCreator;
notebookExplorer = new NotebookExplorer(this, resultCreator);
}
internal List<Result> Query(Query query)
{
return query.Search switch
{
string s when s.StartsWith(settings.Keywords.RecentPages)
=> RecentPages(s),
string s when s.StartsWith(settings.Keywords.NotebookExplorer)
=> notebookExplorer.Query(query),
string s when s.StartsWith(settings.Keywords.TitleSearch)
=> TitleSearch(s, null, OneNoteApplication.GetNotebooks()),
_ => DefaultSearch(query.Search),
};
}
private List<Result> DefaultSearch(string query)
{
// Check for invalid start of query i.e. symbols
if (!char.IsLetterOrDigit(query[0]))
{
return resultCreator.InvalidQuery();
}
var results = OneNoteApplication.FindPages(query)
.Select(pg => resultCreator.CreatePageResult(pg, query));
return results.Any() ? results.ToList() : ResultCreator.NoMatchesFound();
}
private List<Result> TitleSearch(string query, IOneNoteItem parent, IEnumerable<IOneNoteItem> currentCollection)
{
if (query.Length == settings.Keywords.TitleSearch.Length && parent == null)
{
return resultCreator.SearchingByTitle();
}
List<int> highlightData = null;
int score = 0;
var currentSearch = query[settings.Keywords.TitleSearch.Length..];
var results = currentCollection.Traverse(item => SettingsCheck(item) && FuzzySearch(item.Name, currentSearch, out highlightData, out score))
.Select(item => resultCreator.CreateOneNoteItemResult(item, false, highlightData, score))
.ToList();
return results.Any() ? results : ResultCreator.NoMatchesFound();
}
private List<Result> RecentPages(string query)
{
int count = settings.DefaultRecentsCount;
if (query.Length > settings.Keywords.RecentPages.Length && int.TryParse(query[settings.Keywords.RecentPages.Length..], out int userChosenCount))
count = userChosenCount;
return OneNoteApplication.GetNotebooks()
.GetPages()
.Where(SettingsCheck)
.OrderByDescending(pg => pg.LastModified)
.Take(count)
.Select(resultCreator.CreateRecentPageResult)
.ToList();
}
private bool FuzzySearch(string itemName, string search, out List<int> highlightData, out int score)
{
var matchResult = context.API.FuzzySearch(search, itemName);
highlightData = matchResult.MatchData;
score = matchResult.Score;
return matchResult.IsSearchPrecisionScoreMet();
}
private bool SettingsCheck(IOneNoteItem item)
{
bool success = true;
if (!settings.ShowEncrypted && item is OneNoteSection section)
success = !section.Encrypted;
if (!settings.ShowRecycleBin && item.IsInRecycleBin())
success = false;
return success;
}
private sealed class NotebookExplorer
{
private readonly SearchManager searchManager;
private readonly ResultCreator resultCreator;
private Keywords Keywords => searchManager.settings.Keywords;
internal NotebookExplorer(SearchManager searchManager, ResultCreator resultCreator)
{
this.searchManager = searchManager;
this.resultCreator = resultCreator;
}
internal List<Result> Query(Query query)
{
var results = new List<Result>();
string fullSearch = query.Search[(query.Search.IndexOf(Keywords.NotebookExplorer, StringComparison.Ordinal) + Keywords.NotebookExplorer.Length)..];
IOneNoteItem parent = null;
IEnumerable<IOneNoteItem> collection = OneNoteApplication.GetNotebooks();
string[] searches = fullSearch.Split(Keywords.NotebookExplorerSeparator, StringSplitOptions.None);
for (int i = -1; i < searches.Length - 1; i++)
{
if (i < 0)
{
continue;
}
parent = collection.FirstOrDefault(item => item.Name.Equals(searches[i]));
if (parent == null)
{
return results;
}
collection = parent.Children;
}
string lastSearch = searches[^1];
results = lastSearch switch
{
// Empty search so show all in collection
string search when string.IsNullOrWhiteSpace(search)
=> EmptySearch(parent, collection),
// Search by title
string search when search.StartsWith(Keywords.TitleSearch) && parent is not OneNotePage
=> searchManager.TitleSearch(search, parent, collection),
// Scoped search
string search when search.StartsWith(Keywords.ScopedSearch) && parent is OneNoteNotebook or OneNoteSectionGroup
=> ScopedSearch(search, parent),
// Default search
_ => Explorer(lastSearch, parent, collection),
};
if (parent != null)
{
var result = resultCreator.CreateOneNoteItemResult(parent, false, score: 4000);
result.Title = $"Open \"{parent.Name}\" in OneNote";
result.SubTitle = lastSearch switch
{
string search when search.StartsWith(Keywords.TitleSearch)
=> $"Now search by title in \"{parent.Name}\"",
string search when search.StartsWith(Keywords.ScopedSearch)
=> $"Now searching all pages in \"{parent.Name}\"",
_ => $"Use \'{Keywords.ScopedSearch}\' to search this item. Use \'{Keywords.TitleSearch}\' to search by title in this item",
};
results.Add(result);
}
return results;
}
private List<Result> EmptySearch(IOneNoteItem parent, IEnumerable<IOneNoteItem> collection)
{
List<Result> results = collection.Where(searchManager.SettingsCheck)
.Select(item => resultCreator.CreateOneNoteItemResult(item, true))
.ToList();
if (results.Any())
return results;
return resultCreator.NoItemsInCollection(results, parent);
}
private List<Result> ScopedSearch(string query, IOneNoteItem parent)
{
if (query.Length == Keywords.ScopedSearch.Length)
{
return ResultCreator.NoMatchesFound();
}
if (!char.IsLetterOrDigit(query[Keywords.ScopedSearch.Length]))
{
return resultCreator.InvalidQuery();
}
string currentSearch = query[Keywords.TitleSearch.Length..];
var results = new List<Result>();
results = OneNoteApplication.FindPages(currentSearch, parent)
.Select(pg => resultCreator.CreatePageResult(pg, currentSearch))
.ToList();
if (!results.Any())
{
results = ResultCreator.NoMatchesFound();
}
return results;
}
#nullable enable
private List<Result> Explorer(string search, IOneNoteItem? parent, IEnumerable<IOneNoteItem> collection)
{
List<int>? highlightData = null;
int score = 0;
var results = collection.Where(searchManager.SettingsCheck)
.Where(item => searchManager.FuzzySearch(item.Name, search, out highlightData, out score))
.Select(item => resultCreator.CreateOneNoteItemResult(item, true, highlightData, score))
.ToList();
AddCreateNewOneNoteItemResults(search, parent, results);
return results;
}
private void AddCreateNewOneNoteItemResults(string newItemName, IOneNoteItem? parent, List<Result> results)
{
if (!results.Any(result => string.Equals(newItemName.Trim(), result.Title, StringComparison.OrdinalIgnoreCase)))
{
if (parent?.IsInRecycleBin() == true)
{
return;
}
switch (parent)
{
case null:
results.Add(resultCreator.CreateNewNotebookResult(newItemName));
break;
case OneNoteNotebook:
case OneNoteSectionGroup:
results.Add(resultCreator.CreateNewSectionResult(newItemName, parent));
results.Add(resultCreator.CreateNewSectionGroupResult(newItemName, parent));
break;
case OneNoteSection section:
if (!section.Locked)
{
results.Add(resultCreator.CreateNewPageResult(newItemName, section));
}
break;
}
}
}
}
}
}