index.d.ts 10 KB

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