-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
tokens.ts
396 lines (349 loc) · 9.05 KB
/
tokens.ts
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
import { ITranslator } from '@jupyterlab/translation';
import { Token } from '@lumino/coreutils';
import { IDisposable } from '@lumino/disposable';
import { ISignal } from '@lumino/signaling';
import { Widget } from '@lumino/widgets';
/**
* The search provider registry token.
*/
export const ISearchProviderRegistry = new Token<ISearchProviderRegistry>(
'@jupyterlab/documentsearch:ISearchProviderRegistry',
`A service for a registry of search
providers for the application. Plugins can register their UI elements with this registry
to provide find/replace support.`
);
/**
* Filter interface
*/
export interface IFilter {
/**
* Filter title
*/
title: string;
/**
* Filter description
*/
description: string;
/**
* Filter description to be used when the filter is disabled in replace mode.
*/
disabledDescription?: string;
/**
* Default value
*/
default: boolean;
/**
* Does the filter support replace?
*/
supportReplace: boolean;
}
/**
* Type of filters
*
* TODO support additional filter type
*/
export interface IFilters {
/**
* Filter name: value
*/
[key: string]: boolean;
}
/**
* Options adjusting replacement behavior.
*/
export interface IReplaceOptions {
/**
* Should the letter case be preserved?
*/
preserveCase?: boolean;
/**
* Did user request regular expressions?
*
* This has impact on how `$` is interpreted in replacement text.
*/
regularExpression?: boolean;
}
/**
* Support for options adjusting replacement behavior.
*/
export interface IReplaceOptionsSupport {
/**
* Support for preserving letter case.
*/
preserveCase?: boolean;
}
/**
* How many items are selected?
*/
export type SelectionState = 'multiple' | 'single' | 'none';
/**
* React search component state
*/
export interface IDisplayState {
/**
* The index of the currently selected match
*/
currentIndex: number | null;
/**
* The total number of matches found in the document
*/
totalMatches: number;
/**
* Should the search be case sensitive?
*/
caseSensitive: boolean;
/**
* Should the search string be treated as a RegExp?
*/
useRegex: boolean;
/**
* The text in the search entry
*/
searchText: string;
/**
* The query constructed from the text and the case/regex flags
*/
query: RegExp | null;
/**
* An error message (used for bad regex syntax)
*/
errorMessage: string;
/**
* Should the focus forced into the input on the next render?
*/
forceFocus: boolean;
/**
* Whether or not the search input is currently focused
*/
searchInputFocused: boolean;
/**
* Whether or not the replace input is currently focused
*/
replaceInputFocused: boolean;
/**
* The text in the replace entry
*/
replaceText: string;
/**
* Whether or not the replace entry row is visible
*/
replaceEntryVisible: boolean;
/**
* What should we include when we search?
*/
filters: IFilters;
/**
* Is the filters view open?
*/
filtersOpen: boolean;
}
/**
* Base search match interface
*/
export interface ISearchMatch {
/**
* Text of the exact match itself
*/
readonly text: string;
/**
* Start location of the match (in a text, this is the column)
*/
position: number;
}
/**
* HTML search match interface
*/
export interface IHTMLSearchMatch extends ISearchMatch {
/**
* Node containing the match
*/
readonly node: Text;
}
/**
* Interface for search provider factory
*/
export interface ISearchProviderFactory<T extends Widget = Widget> {
/**
* Instantiate a search provider for the widget.
*
* #### Notes
* The widget provided is always checked using `isApplicable` before calling
* this factory.
*
* @param widget The widget to search on
* @param translator [optional] The translator object
*
* @returns The search provider on the widget
*/
readonly createNew: (widget: T, translator?: ITranslator) => ISearchProvider;
/**
* Report whether or not this provider has the ability to search on the
* given widget. The function is a type guard, meaning that it returns
* a boolean, but has a type predicate (`x is T`) for its return signature.
*
* @param domain Widget to test
*/
readonly isApplicable: (domain: Widget) => domain is T;
}
/**
* Search provider registry interface
*/
export interface ISearchProviderRegistry {
/**
* Add a provider to the registry.
*
* @param key - The provider key.
* @returns A disposable delegate that, when disposed, deregisters the given search provider
*/
add(key: string, provider: ISearchProviderFactory<Widget>): IDisposable;
/**
* Returns a matching provider for the widget.
*
* @param widget - The widget to search over.
* @returns the search provider, or undefined if none exists.
*/
getProvider(widget: Widget): ISearchProvider | undefined;
/**
* Whether the registry as a matching provider for the widget.
*
* @param widget - The widget to search over.
* @returns Provider existence
*/
hasProvider(widget: Widget): boolean;
/**
* Signal that emits when a new search provider has been registered
* or removed.
*/
changed: ISignal<ISearchProviderRegistry, void>;
}
/**
* Base search provider interface
*
* #### Notes
* It is implemented by subprovider like searching on a single cell.
*/
export interface IBaseSearchProvider extends IDisposable {
/**
* Start a search
*
* @param query Regular expression to test for
* @param filters Filters to apply when searching
*/
startQuery(query: RegExp, filters: IFilters): Promise<void>;
/**
* Stop a search and clear any internal state of the provider
*/
endQuery(): Promise<void>;
/**
* Clear currently highlighted match.
*/
clearHighlight(): Promise<void>;
/**
* Highlight the next match
*
* @param loop Whether to loop within the matches list.
*
* @returns The next match if it exists
*/
highlightNext(loop?: boolean): Promise<ISearchMatch | undefined>;
/**
* Highlight the previous match
*
* @param loop Whether to loop within the matches list.
*
* @returns The previous match if it exists.
*/
highlightPrevious(loop?: boolean): Promise<ISearchMatch | undefined>;
/**
* Replace the currently selected match with the provided text
* and highlight the next match.
*
* @param newText The replacement text
* @param loop Whether to loop within the matches list.
*
* @returns A promise that resolves with a boolean indicating whether a replace occurred.
*/
replaceCurrentMatch(
newText: string,
loop?: boolean,
options?: IReplaceOptions
): Promise<boolean>;
/**
* Replace all matches in the widget with the provided text
*
* @param newText The replacement text.
*
* @returns A promise that resolves with a boolean indicating whether a replace occurred.
*/
replaceAllMatches(
newText: string,
options?: IReplaceOptions
): Promise<boolean>;
/**
* Signal indicating that something in the search has changed, so the UI should update
*/
readonly stateChanged: ISignal<IBaseSearchProvider, void>;
/**
* The current index of the selected match.
*/
readonly currentMatchIndex: number | null;
/**
* The number of matches.
*/
readonly matchesCount: number | null;
}
/**
* Search provider interface
*/
export interface ISearchProvider extends IBaseSearchProvider {
/**
* Get an initial query value if applicable so that it can be entered
* into the search box as an initial query
*
* @returns Initial value used to populate the search box.
*/
getInitialQuery(): string;
/**
* Set to true if the widget under search is read-only, false
* if it is editable. Will be used to determine whether to show
* the replace option.
*/
readonly isReadOnly: boolean;
/**
* Specifies which replace options are supported by provider.
*/
readonly replaceOptionsSupport?: IReplaceOptionsSupport;
/**
* Get the filters definition for the given provider.
*
* @returns The filters definition.
*
* ### Notes
* TODO For now it only supports boolean filters (represented with checkboxes)
*/
getFilters?(): { [key: string]: IFilter };
/**
* Validate a new filter value for the widget.
*
* @param name The filter name
* @param value The filter value candidate
*
* @returns The valid filter value
*/
validateFilter?(name: string, value: boolean): Promise<boolean>;
/**
* Signal emitted when filter definition changed.
*/
filtersChanged?: ISignal<ISearchProvider, void>;
/**
* Is there one or more objects selected?
*
* The selection can be made of one or more lines, notebook cells, or other
* objects (e.g. spreadsheet cells). The provider can decide whether it counts
* multiple characters (as opposed to lines) as multiple selection or not,
* which will influence the heuristic auto-enabling "search in selection" mode.
*/
getSelectionState?(): SelectionState;
}