1
- using System ;
2
- using System . Collections . Generic ;
1
+ using System . Collections . Generic ;
3
2
using System . Linq ;
4
3
using Odotocodot . OneNote . Linq ;
5
4
6
5
namespace Flow . Launcher . Plugin . OneNote
7
6
{
8
- public class SearchManager
7
+ public partial class SearchManager
9
8
{
10
9
private readonly PluginInitContext context ;
11
10
private readonly Settings settings ;
@@ -73,10 +72,10 @@ private List<Result> TitleSearch(string query, IOneNoteItem parent, IEnumerable<
73
72
private List < Result > RecentPages ( string query )
74
73
{
75
74
int count = settings . DefaultRecentsCount ;
76
-
75
+
77
76
if ( query . Length > settings . Keywords . RecentPages . Length && int . TryParse ( query [ settings . Keywords . RecentPages . Length ..] , out int userChosenCount ) )
78
77
count = userChosenCount ;
79
-
78
+
80
79
return OneNoteApplication . GetNotebooks ( )
81
80
. GetPages ( )
82
81
. Where ( SettingsCheck )
@@ -102,166 +101,5 @@ private bool SettingsCheck(IOneNoteItem item)
102
101
success = false ;
103
102
return success ;
104
103
}
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
- }
266
104
}
267
105
}
0 commit comments