@codemirror-search-npm-6.5.8.patch 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. diff --git a/dist/index.cjs b/dist/index.cjs
  2. index 46231ae..fb0f9aa 100644
  3. --- a/dist/index.cjs
  4. +++ b/dist/index.cjs
  5. @@ -592,6 +592,7 @@ class SearchQuery {
  6. this.valid = !!this.search && (!this.regexp || validRegExp(this.search));
  7. this.unquoted = this.unquote(this.search);
  8. this.wholeWord = !!config.wholeWord;
  9. + this.scope = config.scope;
  10. }
  11. /**
  12. @internal
  13. @@ -606,7 +607,7 @@ class SearchQuery {
  14. eq(other) {
  15. return this.search == other.search && this.replace == other.replace &&
  16. this.caseSensitive == other.caseSensitive && this.regexp == other.regexp &&
  17. - this.wholeWord == other.wholeWord;
  18. + this.wholeWord == other.wholeWord && this.scope == other.scope;
  19. }
  20. /**
  21. @internal
  22. @@ -631,7 +632,12 @@ class QueryType {
  23. }
  24. }
  25. function stringCursor(spec, state, from, to) {
  26. - return new SearchCursor(state.doc, spec.unquoted, from, to, spec.caseSensitive ? undefined : x => x.toLowerCase(), spec.wholeWord ? stringWordTest(state.doc, state.charCategorizer(state.selection.main.head)) : undefined);
  27. + const test = spec.wholeWord ? stringWordTest(state.doc, state.charCategorizer(state.selection.main.head)) : undefined;
  28. + const testWithinScope = (from, to, buffer, bufferPos) => {
  29. + return (!test || test(from, to, buffer, bufferPos))
  30. + && (!spec.scope || spec.scope.some(range => from >= range.from && from <= range.to && to >= range.from && to <= range.to));
  31. + };
  32. + return new SearchCursor(state.doc, spec.unquoted, from, to, spec.caseSensitive ? undefined : x => x.toLowerCase(), testWithinScope);
  33. }
  34. function stringWordTest(doc, categorizer) {
  35. return (from, to, buf, bufPos) => {
  36. @@ -695,9 +701,14 @@ class StringQuery extends QueryType {
  37. }
  38. }
  39. function regexpCursor(spec, state, from, to) {
  40. + const test = spec.wholeWord ? regexpWordTest(state.charCategorizer(state.selection.main.head)) : undefined;
  41. + const testWithinScope = (from, to, match) => {
  42. + return (!test || test(from, to, match))
  43. + && (!spec.scope || spec.scope.some(range => from >= range.from && from <= range.to && to >= range.from && to <= range.to));
  44. + };
  45. return new RegExpCursor(state.doc, spec.search, {
  46. ignoreCase: !spec.caseSensitive,
  47. - test: spec.wholeWord ? regexpWordTest(state.charCategorizer(state.selection.main.head)) : undefined
  48. + test: testWithinScope,
  49. }, from, to);
  50. }
  51. function charBefore(str, index) {
  52. @@ -737,10 +748,18 @@ class RegExpQuery extends QueryType {
  53. this.prevMatchInRange(state, curTo, state.doc.length);
  54. }
  55. getReplacement(result) {
  56. - return this.spec.unquote(this.spec.replace).replace(/\$([$&\d+])/g, (m, i) => i == "$" ? "$"
  57. - : i == "&" ? result.match[0]
  58. - : i != "0" && +i < result.match.length ? result.match[i]
  59. - : m);
  60. + return this.spec.unquote(this.spec.replace).replace(/\$([$&]|\d+)/g, (m, i) => {
  61. + if (i == "&")
  62. + return result.match[0];
  63. + if (i == "$")
  64. + return "$";
  65. + for (let l = i.length; l > 0; l--) {
  66. + let n = +i.slice(0, l);
  67. + if (n > 0 && n < result.match.length)
  68. + return result.match[n] + i.slice(l);
  69. + }
  70. + return m;
  71. + });
  72. }
  73. matchAll(state, limit) {
  74. let cursor = regexpCursor(this.spec, state, 0, state.doc.length), ranges = [];
  75. @@ -1227,7 +1246,9 @@ const searchExtensions = [
  76. exports.RegExpCursor = RegExpCursor;
  77. exports.SearchCursor = SearchCursor;
  78. exports.SearchQuery = SearchQuery;
  79. +exports.StringQuery = StringQuery;
  80. exports.closeSearchPanel = closeSearchPanel;
  81. +exports.createSearchPanel = createSearchPanel;
  82. exports.findNext = findNext;
  83. exports.findPrevious = findPrevious;
  84. exports.getSearchQuery = getSearchQuery;
  85. @@ -1242,4 +1263,6 @@ exports.searchPanelOpen = searchPanelOpen;
  86. exports.selectMatches = selectMatches;
  87. exports.selectNextOccurrence = selectNextOccurrence;
  88. exports.selectSelectionMatches = selectSelectionMatches;
  89. +exports.selectWord = selectWord;
  90. exports.setSearchQuery = setSearchQuery;
  91. +exports.togglePanel = togglePanel;
  92. diff --git a/dist/index.d.cts b/dist/index.d.cts
  93. index 08f5696..663d192 100644
  94. --- a/dist/index.d.cts
  95. +++ b/dist/index.d.cts
  96. @@ -1,6 +1,6 @@
  97. import * as _codemirror_state from '@codemirror/state';
  98. import { Text, Extension, StateCommand, EditorState, SelectionRange, StateEffect } from '@codemirror/state';
  99. -import { Command, KeyBinding, EditorView, Panel } from '@codemirror/view';
  100. +import { Command, EditorView, Panel, KeyBinding } from '@codemirror/view';
  101. /**
  102. A search cursor provides an iterator over text matches in a
  103. @@ -161,6 +161,7 @@ the `"cm-selectionMatch"` class for the highlighting. When
  104. itself will be highlighted with `"cm-selectionMatch-main"`.
  105. */
  106. declare function highlightSelectionMatches(options?: HighlightOptions): Extension;
  107. +declare const selectWord: StateCommand;
  108. /**
  109. Select next occurrence of the current selection. Expand selection
  110. to the surrounding word when the selection is empty.
  111. @@ -264,6 +265,13 @@ declare class SearchQuery {
  112. */
  113. readonly wholeWord: boolean;
  114. /**
  115. + When set, only include search matches within these ranges
  116. + */
  117. + readonly scope?: Readonly<{
  118. + from: number;
  119. + to: number;
  120. + }[]>;
  121. + /**
  122. Create a query object.
  123. */
  124. constructor(config: {
  125. @@ -293,6 +301,13 @@ declare class SearchQuery {
  126. Enable whole-word matching.
  127. */
  128. wholeWord?: boolean;
  129. + /**
  130. + The ranges to match within
  131. + */
  132. + scope?: Readonly<{
  133. + from: number;
  134. + to: number;
  135. + }[]>;
  136. });
  137. /**
  138. Compare this query to another query.
  139. @@ -307,6 +322,34 @@ declare class SearchQuery {
  140. to: number;
  141. }>;
  142. }
  143. +type SearchResult = typeof SearchCursor.prototype.value;
  144. +declare abstract class QueryType<Result extends SearchResult = SearchResult> {
  145. + readonly spec: SearchQuery;
  146. + constructor(spec: SearchQuery);
  147. + abstract nextMatch(state: EditorState, curFrom: number, curTo: number): Result | null;
  148. + abstract prevMatch(state: EditorState, curFrom: number, curTo: number): Result | null;
  149. + abstract getReplacement(result: Result): string;
  150. + abstract matchAll(state: EditorState, limit: number): readonly Result[] | null;
  151. + abstract highlight(state: EditorState, from: number, to: number, add: (from: number, to: number) => void): void;
  152. +}
  153. +declare class StringQuery extends QueryType<SearchResult> {
  154. + constructor(spec: SearchQuery);
  155. + nextMatch(state: EditorState, curFrom: number, curTo: number): {
  156. + from: number;
  157. + to: number;
  158. + } | null;
  159. + private prevMatchInRange;
  160. + prevMatch(state: EditorState, curFrom: number, curTo: number): {
  161. + from: number;
  162. + to: number;
  163. + } | null;
  164. + getReplacement(_result: SearchResult): string;
  165. + matchAll(state: EditorState, limit: number): {
  166. + from: number;
  167. + to: number;
  168. + }[] | null;
  169. + highlight(state: EditorState, from: number, to: number, add: (from: number, to: number) => void): void;
  170. +}
  171. /**
  172. A state effect that updates the current search query. Note that
  173. this only has an effect if the search state has been initialized
  174. @@ -315,6 +358,7 @@ by running [`openSearchPanel`](https://codemirror.net/6/docs/ref/#search.openSea
  175. once).
  176. */
  177. declare const setSearchQuery: _codemirror_state.StateEffectType<SearchQuery>;
  178. +declare const togglePanel: _codemirror_state.StateEffectType<boolean>;
  179. /**
  180. Get the current search query from an editor state.
  181. */
  182. @@ -353,6 +397,7 @@ Replace all instances of the search query with the given
  183. replacement.
  184. */
  185. declare const replaceAll: Command;
  186. +declare function createSearchPanel(view: EditorView): Panel;
  187. /**
  188. Make sure the search panel is open and focused.
  189. */
  190. @@ -372,4 +417,4 @@ Default search-related key bindings.
  191. */
  192. declare const searchKeymap: readonly KeyBinding[];
  193. -export { RegExpCursor, SearchCursor, SearchQuery, closeSearchPanel, findNext, findPrevious, getSearchQuery, gotoLine, highlightSelectionMatches, openSearchPanel, replaceAll, replaceNext, search, searchKeymap, searchPanelOpen, selectMatches, selectNextOccurrence, selectSelectionMatches, setSearchQuery };
  194. +export { RegExpCursor, SearchCursor, SearchQuery, StringQuery, closeSearchPanel, createSearchPanel, findNext, findPrevious, getSearchQuery, gotoLine, highlightSelectionMatches, openSearchPanel, replaceAll, replaceNext, search, searchKeymap, searchPanelOpen, selectMatches, selectNextOccurrence, selectSelectionMatches, selectWord, setSearchQuery, togglePanel };
  195. diff --git a/dist/index.d.ts b/dist/index.d.ts
  196. index 08f5696..663d192 100644
  197. --- a/dist/index.d.ts
  198. +++ b/dist/index.d.ts
  199. @@ -1,6 +1,6 @@
  200. import * as _codemirror_state from '@codemirror/state';
  201. import { Text, Extension, StateCommand, EditorState, SelectionRange, StateEffect } from '@codemirror/state';
  202. -import { Command, KeyBinding, EditorView, Panel } from '@codemirror/view';
  203. +import { Command, EditorView, Panel, KeyBinding } from '@codemirror/view';
  204. /**
  205. A search cursor provides an iterator over text matches in a
  206. @@ -161,6 +161,7 @@ the `"cm-selectionMatch"` class for the highlighting. When
  207. itself will be highlighted with `"cm-selectionMatch-main"`.
  208. */
  209. declare function highlightSelectionMatches(options?: HighlightOptions): Extension;
  210. +declare const selectWord: StateCommand;
  211. /**
  212. Select next occurrence of the current selection. Expand selection
  213. to the surrounding word when the selection is empty.
  214. @@ -264,6 +265,13 @@ declare class SearchQuery {
  215. */
  216. readonly wholeWord: boolean;
  217. /**
  218. + When set, only include search matches within these ranges
  219. + */
  220. + readonly scope?: Readonly<{
  221. + from: number;
  222. + to: number;
  223. + }[]>;
  224. + /**
  225. Create a query object.
  226. */
  227. constructor(config: {
  228. @@ -293,6 +301,13 @@ declare class SearchQuery {
  229. Enable whole-word matching.
  230. */
  231. wholeWord?: boolean;
  232. + /**
  233. + The ranges to match within
  234. + */
  235. + scope?: Readonly<{
  236. + from: number;
  237. + to: number;
  238. + }[]>;
  239. });
  240. /**
  241. Compare this query to another query.
  242. @@ -307,6 +322,34 @@ declare class SearchQuery {
  243. to: number;
  244. }>;
  245. }
  246. +type SearchResult = typeof SearchCursor.prototype.value;
  247. +declare abstract class QueryType<Result extends SearchResult = SearchResult> {
  248. + readonly spec: SearchQuery;
  249. + constructor(spec: SearchQuery);
  250. + abstract nextMatch(state: EditorState, curFrom: number, curTo: number): Result | null;
  251. + abstract prevMatch(state: EditorState, curFrom: number, curTo: number): Result | null;
  252. + abstract getReplacement(result: Result): string;
  253. + abstract matchAll(state: EditorState, limit: number): readonly Result[] | null;
  254. + abstract highlight(state: EditorState, from: number, to: number, add: (from: number, to: number) => void): void;
  255. +}
  256. +declare class StringQuery extends QueryType<SearchResult> {
  257. + constructor(spec: SearchQuery);
  258. + nextMatch(state: EditorState, curFrom: number, curTo: number): {
  259. + from: number;
  260. + to: number;
  261. + } | null;
  262. + private prevMatchInRange;
  263. + prevMatch(state: EditorState, curFrom: number, curTo: number): {
  264. + from: number;
  265. + to: number;
  266. + } | null;
  267. + getReplacement(_result: SearchResult): string;
  268. + matchAll(state: EditorState, limit: number): {
  269. + from: number;
  270. + to: number;
  271. + }[] | null;
  272. + highlight(state: EditorState, from: number, to: number, add: (from: number, to: number) => void): void;
  273. +}
  274. /**
  275. A state effect that updates the current search query. Note that
  276. this only has an effect if the search state has been initialized
  277. @@ -315,6 +358,7 @@ by running [`openSearchPanel`](https://codemirror.net/6/docs/ref/#search.openSea
  278. once).
  279. */
  280. declare const setSearchQuery: _codemirror_state.StateEffectType<SearchQuery>;
  281. +declare const togglePanel: _codemirror_state.StateEffectType<boolean>;
  282. /**
  283. Get the current search query from an editor state.
  284. */
  285. @@ -353,6 +397,7 @@ Replace all instances of the search query with the given
  286. replacement.
  287. */
  288. declare const replaceAll: Command;
  289. +declare function createSearchPanel(view: EditorView): Panel;
  290. /**
  291. Make sure the search panel is open and focused.
  292. */
  293. @@ -372,4 +417,4 @@ Default search-related key bindings.
  294. */
  295. declare const searchKeymap: readonly KeyBinding[];
  296. -export { RegExpCursor, SearchCursor, SearchQuery, closeSearchPanel, findNext, findPrevious, getSearchQuery, gotoLine, highlightSelectionMatches, openSearchPanel, replaceAll, replaceNext, search, searchKeymap, searchPanelOpen, selectMatches, selectNextOccurrence, selectSelectionMatches, setSearchQuery };
  297. +export { RegExpCursor, SearchCursor, SearchQuery, StringQuery, closeSearchPanel, createSearchPanel, findNext, findPrevious, getSearchQuery, gotoLine, highlightSelectionMatches, openSearchPanel, replaceAll, replaceNext, search, searchKeymap, searchPanelOpen, selectMatches, selectNextOccurrence, selectSelectionMatches, selectWord, setSearchQuery, togglePanel };
  298. diff --git a/dist/index.js b/dist/index.js
  299. index 22172ef..08a9974 100644
  300. --- a/dist/index.js
  301. +++ b/dist/index.js
  302. @@ -590,6 +590,7 @@ class SearchQuery {
  303. this.valid = !!this.search && (!this.regexp || validRegExp(this.search));
  304. this.unquoted = this.unquote(this.search);
  305. this.wholeWord = !!config.wholeWord;
  306. + this.scope = config.scope;
  307. }
  308. /**
  309. @internal
  310. @@ -604,7 +605,7 @@ class SearchQuery {
  311. eq(other) {
  312. return this.search == other.search && this.replace == other.replace &&
  313. this.caseSensitive == other.caseSensitive && this.regexp == other.regexp &&
  314. - this.wholeWord == other.wholeWord;
  315. + this.wholeWord == other.wholeWord && this.scope == other.scope;
  316. }
  317. /**
  318. @internal
  319. @@ -629,7 +630,12 @@ class QueryType {
  320. }
  321. }
  322. function stringCursor(spec, state, from, to) {
  323. - return new SearchCursor(state.doc, spec.unquoted, from, to, spec.caseSensitive ? undefined : x => x.toLowerCase(), spec.wholeWord ? stringWordTest(state.doc, state.charCategorizer(state.selection.main.head)) : undefined);
  324. + const test = spec.wholeWord ? stringWordTest(state.doc, state.charCategorizer(state.selection.main.head)) : undefined;
  325. + const testWithinScope = (from, to, buffer, bufferPos) => {
  326. + return (!test || test(from, to, buffer, bufferPos))
  327. + && (!spec.scope || spec.scope.some(range => from >= range.from && from <= range.to && to >= range.from && to <= range.to));
  328. + };
  329. + return new SearchCursor(state.doc, spec.unquoted, from, to, spec.caseSensitive ? undefined : x => x.toLowerCase(), testWithinScope);
  330. }
  331. function stringWordTest(doc, categorizer) {
  332. return (from, to, buf, bufPos) => {
  333. @@ -693,9 +699,14 @@ class StringQuery extends QueryType {
  334. }
  335. }
  336. function regexpCursor(spec, state, from, to) {
  337. + const test = spec.wholeWord ? regexpWordTest(state.charCategorizer(state.selection.main.head)) : undefined;
  338. + const testWithinScope = (from, to, match) => {
  339. + return (!test || test(from, to, match))
  340. + && (!spec.scope || spec.scope.some(range => from >= range.from && from <= range.to && to >= range.from && to <= range.to));
  341. + };
  342. return new RegExpCursor(state.doc, spec.search, {
  343. ignoreCase: !spec.caseSensitive,
  344. - test: spec.wholeWord ? regexpWordTest(state.charCategorizer(state.selection.main.head)) : undefined
  345. + test: testWithinScope,
  346. }, from, to);
  347. }
  348. function charBefore(str, index) {
  349. @@ -735,10 +746,18 @@ class RegExpQuery extends QueryType {
  350. this.prevMatchInRange(state, curTo, state.doc.length);
  351. }
  352. getReplacement(result) {
  353. - return this.spec.unquote(this.spec.replace).replace(/\$([$&\d+])/g, (m, i) => i == "$" ? "$"
  354. - : i == "&" ? result.match[0]
  355. - : i != "0" && +i < result.match.length ? result.match[i]
  356. - : m);
  357. + return this.spec.unquote(this.spec.replace).replace(/\$([$&]|\d+)/g, (m, i) => {
  358. + if (i == "&")
  359. + return result.match[0];
  360. + if (i == "$")
  361. + return "$";
  362. + for (let l = i.length; l > 0; l--) {
  363. + let n = +i.slice(0, l);
  364. + if (n > 0 && n < result.match.length)
  365. + return result.match[n] + i.slice(l);
  366. + }
  367. + return m;
  368. + });
  369. }
  370. matchAll(state, limit) {
  371. let cursor = regexpCursor(this.spec, state, 0, state.doc.length), ranges = [];
  372. @@ -1222,4 +1241,4 @@ const searchExtensions = [
  373. baseTheme
  374. ];
  375. -export { RegExpCursor, SearchCursor, SearchQuery, closeSearchPanel, findNext, findPrevious, getSearchQuery, gotoLine, highlightSelectionMatches, openSearchPanel, replaceAll, replaceNext, search, searchKeymap, searchPanelOpen, selectMatches, selectNextOccurrence, selectSelectionMatches, setSearchQuery };
  376. +export { RegExpCursor, SearchCursor, SearchQuery, StringQuery, closeSearchPanel, createSearchPanel, findNext, findPrevious, getSearchQuery, gotoLine, highlightSelectionMatches, openSearchPanel, replaceAll, replaceNext, search, searchKeymap, searchPanelOpen, selectMatches, selectNextOccurrence, selectSelectionMatches, selectWord, setSearchQuery, togglePanel };