Diffdown is a real-time collaborative Markdown editor/previewer built on the AT Protocol
diffdown.com
1import * as _codemirror_state from '@codemirror/state';
2import { Text, Extension, StateCommand, EditorState, SelectionRange, StateEffect } from '@codemirror/state';
3import { Command, KeyBinding, EditorView, Panel } from '@codemirror/view';
4
5/**
6A search cursor provides an iterator over text matches in a
7document.
8*/
9declare class SearchCursor implements Iterator<{
10 from: number;
11 to: number;
12}> {
13 private test?;
14 private iter;
15 /**
16 The current match (only holds a meaningful value after
17 [`next`](https://codemirror.net/6/docs/ref/#search.SearchCursor.next) has been called and when
18 `done` is false).
19 */
20 value: {
21 from: number;
22 to: number;
23 };
24 /**
25 Whether the end of the iterated region has been reached.
26 */
27 done: boolean;
28 private matches;
29 private buffer;
30 private bufferPos;
31 private bufferStart;
32 private normalize;
33 private query;
34 /**
35 Create a text cursor. The query is the search string, `from` to
36 `to` provides the region to search.
37
38 When `normalize` is given, it will be called, on both the query
39 string and the content it is matched against, before comparing.
40 You can, for example, create a case-insensitive search by
41 passing `s => s.toLowerCase()`.
42
43 Text is always normalized with
44 [`.normalize("NFKD")`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/normalize)
45 (when supported).
46 */
47 constructor(text: Text, query: string, from?: number, to?: number, normalize?: (string: string) => string, test?: ((from: number, to: number, buffer: string, bufferPos: number) => boolean) | undefined);
48 private peek;
49 /**
50 Look for the next match. Updates the iterator's
51 [`value`](https://codemirror.net/6/docs/ref/#search.SearchCursor.value) and
52 [`done`](https://codemirror.net/6/docs/ref/#search.SearchCursor.done) properties. Should be called
53 at least once before using the cursor.
54 */
55 next(): this;
56 /**
57 The `next` method will ignore matches that partially overlap a
58 previous match. This method behaves like `next`, but includes
59 such matches.
60 */
61 nextOverlapping(): this;
62 private match;
63 [Symbol.iterator]: () => Iterator<{
64 from: number;
65 to: number;
66 }>;
67}
68
69interface RegExpCursorOptions {
70 ignoreCase?: boolean;
71 test?: (from: number, to: number, match: RegExpExecArray) => boolean;
72}
73/**
74This class is similar to [`SearchCursor`](https://codemirror.net/6/docs/ref/#search.SearchCursor)
75but searches for a regular expression pattern instead of a plain
76string.
77*/
78declare class RegExpCursor implements Iterator<{
79 from: number;
80 to: number;
81 match: RegExpExecArray;
82}> {
83 private text;
84 private to;
85 private iter;
86 private re;
87 private test?;
88 private curLine;
89 private curLineStart;
90 private matchPos;
91 /**
92 Set to `true` when the cursor has reached the end of the search
93 range.
94 */
95 done: boolean;
96 /**
97 Will contain an object with the extent of the match and the
98 match object when [`next`](https://codemirror.net/6/docs/ref/#search.RegExpCursor.next)
99 sucessfully finds a match.
100 */
101 value: {
102 from: number;
103 to: number;
104 match: RegExpExecArray;
105 };
106 /**
107 Create a cursor that will search the given range in the given
108 document. `query` should be the raw pattern (as you'd pass it to
109 `new RegExp`).
110 */
111 constructor(text: Text, query: string, options?: RegExpCursorOptions, from?: number, to?: number);
112 private getLine;
113 private nextLine;
114 /**
115 Move to the next match, if there is one.
116 */
117 next(): this;
118 [Symbol.iterator]: () => Iterator<{
119 from: number;
120 to: number;
121 match: RegExpExecArray;
122 }>;
123}
124
125/**
126Command that shows a dialog asking the user for a line number, and
127when a valid position is provided, moves the cursor to that line.
128
129Supports line numbers, relative line offsets prefixed with `+` or
130`-`, document percentages suffixed with `%`, and an optional
131column position by adding `:` and a second number after the line
132number.
133*/
134declare const gotoLine: Command;
135
136type HighlightOptions = {
137 /**
138 Determines whether, when nothing is selected, the word around
139 the cursor is matched instead. Defaults to false.
140 */
141 highlightWordAroundCursor?: boolean;
142 /**
143 The minimum length of the selection before it is highlighted.
144 Defaults to 1 (always highlight non-cursor selections).
145 */
146 minSelectionLength?: number;
147 /**
148 The amount of matches (in the viewport) at which to disable
149 highlighting. Defaults to 100.
150 */
151 maxMatches?: number;
152 /**
153 Whether to only highlight whole words.
154 */
155 wholeWords?: boolean;
156};
157/**
158This extension highlights text that matches the selection. It uses
159the `"cm-selectionMatch"` class for the highlighting. When
160`highlightWordAroundCursor` is enabled, the word at the cursor
161itself will be highlighted with `"cm-selectionMatch-main"`.
162*/
163declare function highlightSelectionMatches(options?: HighlightOptions): Extension;
164/**
165Select next occurrence of the current selection. Expand selection
166to the surrounding word when the selection is empty.
167*/
168declare const selectNextOccurrence: StateCommand;
169
170interface SearchConfig {
171 /**
172 Whether to position the search panel at the top of the editor
173 (the default is at the bottom).
174 */
175 top?: boolean;
176 /**
177 Whether to enable case sensitivity by default when the search
178 panel is activated (defaults to false).
179 */
180 caseSensitive?: boolean;
181 /**
182 Whether to treat string searches literally by default (defaults to false).
183 */
184 literal?: boolean;
185 /**
186 Controls whether the default query has by-word matching enabled.
187 Defaults to false.
188 */
189 wholeWord?: boolean;
190 /**
191 Used to turn on regular expression search in the default query.
192 Defaults to false.
193 */
194 regexp?: boolean;
195 /**
196 Can be used to override the way the search panel is implemented.
197 Should create a [Panel](https://codemirror.net/6/docs/ref/#view.Panel) that contains a form
198 which lets the user:
199
200 - See the [current](https://codemirror.net/6/docs/ref/#search.getSearchQuery) search query.
201 - Manipulate the [query](https://codemirror.net/6/docs/ref/#search.SearchQuery) and
202 [update](https://codemirror.net/6/docs/ref/#search.setSearchQuery) the search state with a new
203 query.
204 - Notice external changes to the query by reacting to the
205 appropriate [state effect](https://codemirror.net/6/docs/ref/#search.setSearchQuery).
206 - Run some of the search commands.
207
208 The field that should be focused when opening the panel must be
209 tagged with a `main-field=true` DOM attribute.
210 */
211 createPanel?: (view: EditorView) => Panel;
212 /**
213 By default, matches are scrolled into view using the default
214 behavior of
215 [`EditorView.scrollIntoView`](https://codemirror.net/6/docs/ref/#view.EditorView^scrollIntoView).
216 This option allows you to pass a custom function to produce the
217 scroll effect.
218 */
219 scrollToMatch?: (range: SelectionRange, view: EditorView) => StateEffect<unknown>;
220}
221/**
222Add search state to the editor configuration, and optionally
223configure the search extension.
224([`openSearchPanel`](https://codemirror.net/6/docs/ref/#search.openSearchPanel) will automatically
225enable this if it isn't already on).
226*/
227declare function search(config?: SearchConfig): Extension;
228/**
229A search query. Part of the editor's search state.
230*/
231declare class SearchQuery {
232 /**
233 The search string (or regular expression).
234 */
235 readonly search: string;
236 /**
237 Indicates whether the search is case-sensitive.
238 */
239 readonly caseSensitive: boolean;
240 /**
241 By default, string search will replace `\n`, `\r`, and `\t` in
242 the query with newline, return, and tab characters. When this
243 is set to true, that behavior is disabled.
244 */
245 readonly literal: boolean;
246 /**
247 When true, the search string is interpreted as a regular
248 expression.
249 */
250 readonly regexp: boolean;
251 /**
252 The replace text, or the empty string if no replace text has
253 been given.
254 */
255 readonly replace: string;
256 /**
257 Whether this query is non-empty and, in case of a regular
258 expression search, syntactically valid.
259 */
260 readonly valid: boolean;
261 /**
262 When true, matches that contain words are ignored when there are
263 further word characters around them.
264 */
265 readonly wholeWord: boolean;
266 /**
267 Optional test function used to filter matches.
268 */
269 readonly test: ((match: string, state: EditorState, from: number, to: number) => boolean) | undefined;
270 /**
271 Create a query object.
272 */
273 constructor(config: {
274 /**
275 The search string.
276 */
277 search: string;
278 /**
279 Controls whether the search should be case-sensitive.
280 */
281 caseSensitive?: boolean;
282 /**
283 By default, string search will replace `\n`, `\r`, and `\t` in
284 the query with newline, return, and tab characters. When this
285 is set to true, that behavior is disabled.
286 */
287 literal?: boolean;
288 /**
289 When true, interpret the search string as a regular expression.
290 */
291 regexp?: boolean;
292 /**
293 The replace text.
294 */
295 replace?: string;
296 /**
297 Enable whole-word matching.
298 */
299 wholeWord?: boolean;
300 /**
301 Optional custom filter. It is passed the matched string, the
302 editor state, and the match range. Matches for which it
303 returns false will be ignored.
304 */
305 test?: (match: string, state: EditorState, from: number, to: number) => boolean;
306 });
307 /**
308 Compare this query to another query.
309 */
310 eq(other: SearchQuery): boolean;
311 /**
312 Get a search cursor for this query, searching through the given
313 range in the given state.
314 */
315 getCursor(state: EditorState | Text, from?: number, to?: number): Iterator<{
316 from: number;
317 to: number;
318 }>;
319}
320/**
321A state effect that updates the current search query. Note that
322this only has an effect if the search state has been initialized
323(by including [`search`](https://codemirror.net/6/docs/ref/#search.search) in your configuration or
324by running [`openSearchPanel`](https://codemirror.net/6/docs/ref/#search.openSearchPanel) at least
325once).
326*/
327declare const setSearchQuery: _codemirror_state.StateEffectType<SearchQuery>;
328/**
329Get the current search query from an editor state.
330*/
331declare function getSearchQuery(state: EditorState): SearchQuery;
332/**
333Query whether the search panel is open in the given editor state.
334*/
335declare function searchPanelOpen(state: EditorState): boolean;
336/**
337Open the search panel if it isn't already open, and move the
338selection to the first match after the current main selection.
339Will wrap around to the start of the document when it reaches the
340end.
341*/
342declare const findNext: Command;
343/**
344Move the selection to the previous instance of the search query,
345before the current main selection. Will wrap past the start
346of the document to start searching at the end again.
347*/
348declare const findPrevious: Command;
349/**
350Select all instances of the search query.
351*/
352declare const selectMatches: Command;
353/**
354Select all instances of the currently selected text.
355*/
356declare const selectSelectionMatches: StateCommand;
357/**
358Replace the current match of the search query.
359*/
360declare const replaceNext: Command;
361/**
362Replace all instances of the search query with the given
363replacement.
364*/
365declare const replaceAll: Command;
366/**
367Make sure the search panel is open and focused.
368*/
369declare const openSearchPanel: Command;
370/**
371Close the search panel.
372*/
373declare const closeSearchPanel: Command;
374/**
375Default search-related key bindings.
376
377 - Mod-f: [`openSearchPanel`](https://codemirror.net/6/docs/ref/#search.openSearchPanel)
378 - F3, Mod-g: [`findNext`](https://codemirror.net/6/docs/ref/#search.findNext)
379 - Shift-F3, Shift-Mod-g: [`findPrevious`](https://codemirror.net/6/docs/ref/#search.findPrevious)
380 - Mod-Alt-g: [`gotoLine`](https://codemirror.net/6/docs/ref/#search.gotoLine)
381 - Mod-d: [`selectNextOccurrence`](https://codemirror.net/6/docs/ref/#search.selectNextOccurrence)
382*/
383declare const searchKeymap: readonly KeyBinding[];
384
385export { RegExpCursor, SearchCursor, SearchQuery, closeSearchPanel, findNext, findPrevious, getSearchQuery, gotoLine, highlightSelectionMatches, openSearchPanel, replaceAll, replaceNext, search, searchKeymap, searchPanelOpen, selectMatches, selectNextOccurrence, selectSelectionMatches, setSearchQuery };