@singapore-editor/find 0.2.12 → 0.2.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,4 @@
1
- import { FIND_MATCHES_LIMIT, FIND_REPLACE_ALL_LIMIT, countMatches, escapeRegExpCharacters, findLineRange, findMatchIndex, findMatches, findNextMatchFrom, findPreviousMatchFrom, findTextSourceFromSnapshot } from "./search.js";
1
+ import { FIND_MATCHES_LIMIT, FIND_REPLACE_ALL_LIMIT, FIND_SELECT_ALL_LIMIT, createFindRangeScanner, escapeRegExpCharacters, findLineRange, findMatchIndex, findMatches, findNextMatchFrom, findPreviousMatchFrom, findQueryPlan, findTextSourceFromSnapshot, partitionViewportRanges, searchedRangesLength } from "./search.js";
2
2
  import { ReplacePattern, parseReplaceString } from "./replacePattern.js";
3
3
  import { wordRangeAtOffset } from "@singapore-editor/core/document";
4
4
  import { EditorSecondaryViewScheduler } from "@singapore-editor/core/secondary-views";
@@ -33,8 +33,17 @@ function findReveal(match, committed = true) {
33
33
  }
34
34
  var SEARCH_STRING_MAX_LENGTH = 524288;
35
35
  var FIND_RESEARCH_KEY = "find.research";
36
+ var FIND_SCAN_SLICE_KEY = "find.scanSlice";
36
37
  var FIND_RESEARCH_DELAY_MS = 100;
37
38
  var FIND_RESEARCH_MAX_DELAY_MS = 400;
39
+ var SCHEDULER_SCOPE_COUNTER_KEY = Symbol.for("@singapore-editor/core/work-scheduler-scope-id");
40
+ function nextSchedulerScopeId(scheduler) {
41
+ const holder = scheduler;
42
+ const current = holder[SCHEDULER_SCOPE_COUNTER_KEY];
43
+ const next = (typeof current === "number" ? current : 0) + 1;
44
+ holder[SCHEDULER_SCOPE_COUNTER_KEY] = next;
45
+ return next;
46
+ }
38
47
  var EditorFindController = class {
39
48
  options;
40
49
  listeners = /* @__PURE__ */ new Set();
@@ -54,21 +63,40 @@ var EditorFindController = class {
54
63
  replaceRevealed: false,
55
64
  inSelection: false
56
65
  };
57
- scheduler = new EditorSecondaryViewScheduler();
66
+ ownScheduler = null;
67
+ researchKey = FIND_RESEARCH_KEY;
68
+ scanSliceKey = FIND_SCAN_SLICE_KEY;
58
69
  matches = [];
59
70
  highlightsTruncated = false;
60
- totalMatches = 0;
71
+ matchesIncomplete = false;
72
+ exactPrefixMatchCount = 0;
61
73
  scope = null;
62
74
  trackedMatches = null;
63
75
  currentMatch = null;
64
76
  replacing = false;
65
77
  pendingResearch = false;
78
+ searchDirty = false;
79
+ scanGeneration = 0;
80
+ activeScan = null;
81
+ activeCursorScan = null;
66
82
  constructor(options = {}) {
67
83
  this.options = resolveFindOptions(options);
68
84
  }
69
85
  attachHost(host, highlightPrefix) {
70
- if (this.host && this.host !== host) this.clearHighlights();
86
+ if (this.host && this.host !== host) {
87
+ this.cancelActiveScan();
88
+ this.activeScheduler()?.cancel(this.researchKey);
89
+ this.clearHighlights();
90
+ }
71
91
  this.host = host;
92
+ if (host.workScheduler) {
93
+ const scopeId = nextSchedulerScopeId(host.workScheduler);
94
+ this.researchKey = `${FIND_RESEARCH_KEY}:${scopeId}`;
95
+ this.scanSliceKey = `${FIND_SCAN_SLICE_KEY}:${scopeId}`;
96
+ } else {
97
+ this.researchKey = FIND_RESEARCH_KEY;
98
+ this.scanSliceKey = FIND_SCAN_SLICE_KEY;
99
+ }
72
100
  this.matchHighlightName = `${highlightPrefix}-find-match`;
73
101
  this.currentHighlightName = `${highlightPrefix}-find-current`;
74
102
  this.scopeHighlightName = `${highlightPrefix}-find-scope`;
@@ -84,7 +112,12 @@ var EditorFindController = class {
84
112
  }
85
113
  dispose() {
86
114
  this.pendingResearch = false;
87
- this.scheduler.dispose();
115
+ this.searchDirty = false;
116
+ this.matchesIncomplete = false;
117
+ this.cancelActiveScan();
118
+ this.activeScheduler()?.cancel(this.researchKey);
119
+ this.ownScheduler?.dispose();
120
+ this.ownScheduler = null;
88
121
  this.clearHighlights();
89
122
  this.listeners.clear();
90
123
  this.host = null;
@@ -113,6 +146,9 @@ var EditorFindController = class {
113
146
  const host = this.host;
114
147
  if (!this.state.revealed || !host) return false;
115
148
  this.pendingResearch = false;
149
+ this.searchDirty = false;
150
+ this.matchesIncomplete = false;
151
+ this.cancelActiveScan();
116
152
  this.state = {
117
153
  ...this.state,
118
154
  revealed: false,
@@ -121,7 +157,7 @@ var EditorFindController = class {
121
157
  this.scope = null;
122
158
  this.trackedMatches = null;
123
159
  this.currentMatch = null;
124
- this.scheduler.cancel(FIND_RESEARCH_KEY);
160
+ this.activeScheduler()?.cancel(this.researchKey);
125
161
  this.clearHighlights();
126
162
  this.emit({ type: "hide" });
127
163
  host.focusEditor();
@@ -190,10 +226,20 @@ var EditorFindController = class {
190
226
  if (!this.ensureFindReady("none")) return false;
191
227
  const search = this.commandSearchTarget();
192
228
  if (!search) return false;
193
- const matches = findMatches(search.source, this.state, search.scopes, false, FIND_REPLACE_ALL_LIMIT);
194
- if (matches.length === 0) return false;
229
+ const found = findMatches(search.source, this.state, search.scopes, false, FIND_SELECT_ALL_LIMIT + 1);
230
+ if (found.length === 0) return false;
231
+ const truncated = found.length > FIND_SELECT_ALL_LIMIT;
232
+ const matches = truncated ? found.slice(0, FIND_SELECT_ALL_LIMIT) : found;
233
+ this.cancelActiveCursorScan();
234
+ if (this.activeScan) this.activeScan.moveCursor = false;
195
235
  const selections = orderedMatchSelections(matches, this.primarySelection());
196
236
  host.setSelections(selections, "input.findSelectAll", selections[0]?.head);
237
+ if (this.canPaintCurrentDocument()) {
238
+ this.currentMatch = matchAtSelection(matches, this.primarySelection());
239
+ this.updateHighlights();
240
+ this.updateWidget();
241
+ }
242
+ if (truncated) host.announce(`Selected the first ${FIND_SELECT_ALL_LIMIT} matches`);
197
243
  return true;
198
244
  }
199
245
  toggleMatchCase() {
@@ -263,7 +309,22 @@ var EditorFindController = class {
263
309
  ...this.state,
264
310
  searchString: value
265
311
  };
266
- if (this.options.findOnType) this.research(this.options.cursorMoveOnType);
312
+ if (this.options.findOnType) {
313
+ this.research(this.options.cursorMoveOnType);
314
+ return;
315
+ }
316
+ const hadIncompleteScan = this.activeScan !== null || this.matchesIncomplete;
317
+ this.cancelActiveScan();
318
+ this.matchesIncomplete = false;
319
+ this.searchDirty = true;
320
+ if (hadIncompleteScan) {
321
+ this.highlightsTruncated = false;
322
+ this.exactPrefixMatchCount = 0;
323
+ this.adoptMatches([]);
324
+ this.currentMatch = null;
325
+ this.updateHighlights();
326
+ this.updateWidget();
327
+ }
267
328
  }
268
329
  setReplaceString(value) {
269
330
  this.state = {
@@ -295,11 +356,17 @@ var EditorFindController = class {
295
356
  this.research(false);
296
357
  return;
297
358
  }
359
+ if (this.activeScan) this.matchesIncomplete = true;
360
+ this.cancelActiveScan();
298
361
  this.scheduleResearch();
299
362
  }
300
363
  detachHost(host) {
301
364
  if (this.host !== host) return;
302
365
  this.pendingResearch = false;
366
+ this.searchDirty = false;
367
+ this.matchesIncomplete = false;
368
+ this.cancelActiveScan();
369
+ this.activeScheduler()?.cancel(this.researchKey);
303
370
  this.clearHighlights();
304
371
  this.host = null;
305
372
  this.matchHighlightName = "";
@@ -312,11 +379,10 @@ var EditorFindController = class {
312
379
  };
313
380
  this.matches = [];
314
381
  this.highlightsTruncated = false;
315
- this.totalMatches = 0;
382
+ this.exactPrefixMatchCount = 0;
316
383
  this.scope = null;
317
384
  this.trackedMatches = null;
318
385
  this.currentMatch = null;
319
- this.scheduler.cancel(FIND_RESEARCH_KEY);
320
386
  }
321
387
  detachEditHost(host) {
322
388
  if (this.editHost !== host) return;
@@ -347,37 +413,421 @@ var EditorFindController = class {
347
413
  replace: focus === "replace",
348
414
  focus
349
415
  });
350
- if (this.matches.length === 0) this.research(false);
416
+ if (this.searchDirty || this.pendingResearch && !this.activeScan) this.research(false);
351
417
  return this.state.searchString.length > 0;
352
418
  }
353
419
  research(moveCursor) {
354
420
  if (!this.host) return;
355
421
  if (!this.canPaintCurrentDocument()) {
422
+ this.cancelActiveScan();
356
423
  this.pendingResearch = true;
357
424
  if (moveCursor) this.selectFirstMatchFromSelection();
358
425
  return;
359
426
  }
360
427
  this.pendingResearch = false;
361
- this.scheduler.cancel(FIND_RESEARCH_KEY);
362
- const found = this.findAll(false, FIND_MATCHES_LIMIT + 1);
428
+ this.searchDirty = false;
429
+ this.activeScheduler()?.cancel(this.researchKey);
430
+ this.cancelActiveScan();
431
+ const search = this.searchTarget();
432
+ const plan = search ? findQueryPlan(this.state) : null;
433
+ if (!search || !plan) {
434
+ this.highlightsTruncated = false;
435
+ this.matchesIncomplete = false;
436
+ this.exactPrefixMatchCount = 0;
437
+ this.adoptMatches([]);
438
+ this.currentMatch = null;
439
+ this.updateHighlights();
440
+ this.updateWidget();
441
+ return;
442
+ }
443
+ if (searchedRangesLength(search.source, search.scopes) <= 262144) {
444
+ this.runSyncSearch(search, moveCursor);
445
+ return;
446
+ }
447
+ this.startChunkedSearch(search, plan.kind, moveCursor);
448
+ }
449
+ runSyncSearch(search, moveCursor) {
450
+ const found = findMatches(search.source, this.state, search.scopes, false, FIND_MATCHES_LIMIT + 1);
363
451
  this.highlightsTruncated = found.length > FIND_MATCHES_LIMIT;
364
- this.totalMatches = this.highlightsTruncated ? this.countAll() : found.length;
365
- this.adoptMatches(this.highlightsTruncated ? found.slice(0, FIND_MATCHES_LIMIT) : found);
366
- this.currentMatch = matchAtSelection(this.matches, this.primarySelection());
452
+ this.matchesIncomplete = false;
453
+ const adopted = this.highlightsTruncated ? found.slice(0, FIND_MATCHES_LIMIT) : found;
454
+ this.exactPrefixMatchCount = adopted.length;
455
+ this.adoptMatches(adopted);
456
+ const selection = this.primarySelection();
457
+ this.currentMatch = matchAtSelection(this.matches, selection);
458
+ this.updateHighlights();
459
+ this.updateWidget();
460
+ if (moveCursor && adopted.length > 0) {
461
+ const offset = selection?.endOffset ?? 0;
462
+ const inList = firstMatchAtOrAfterInList(adopted, offset, !this.highlightsTruncated && this.options.loop);
463
+ if (inList) {
464
+ this.selectMatch(inList, false);
465
+ return;
466
+ }
467
+ if (this.highlightsTruncated) {
468
+ const ahead = this.searchFrom(findNextMatchFrom, offset, {
469
+ escapeEmptyMatchAtOffset: false,
470
+ loop: false
471
+ });
472
+ this.selectMatch(ahead ?? (this.options.loop ? adopted[0] ?? null : null), false);
473
+ }
474
+ }
475
+ }
476
+ startChunkedSearch(search, planKind, moveCursor) {
477
+ const host = this.host;
478
+ if (!host) return;
479
+ const query = {
480
+ searchString: this.state.searchString,
481
+ isRegex: this.state.isRegex,
482
+ matchCase: this.state.matchCase,
483
+ wholeWord: this.state.wholeWord
484
+ };
485
+ const selection = this.primarySelection();
486
+ const selectionSnapshot = snapshotSelection(selection);
487
+ const anchorOffset = selection?.endOffset ?? 0;
488
+ const snapshot = this.editHost?.textSnapshot() ?? null;
489
+ if (planKind === "lines" || planKind === "anchored-lines") {
490
+ const frame0Start = nowMs();
491
+ const partition = partitionViewportRanges(search.source, search.scopes, host.visibleRange?.() ?? null, anchorOffset);
492
+ const vpFound = partition.viewportRanges.length > 0 ? findMatches(search.source, query, partition.viewportRanges, false, FIND_MATCHES_LIMIT + 1) : [];
493
+ const vpTruncated = vpFound.length > FIND_MATCHES_LIMIT;
494
+ const vpMatches = vpTruncated ? vpFound.slice(0, FIND_MATCHES_LIMIT) : vpFound;
495
+ if (!(!vpTruncated && (partition.prefixRanges.length > 0 || partition.suffixRanges.length > 0))) {
496
+ this.highlightsTruncated = vpTruncated;
497
+ this.matchesIncomplete = false;
498
+ this.exactPrefixMatchCount = partition.prefixRanges.length === 0 ? vpMatches.length : 0;
499
+ this.adoptMatches(vpMatches);
500
+ this.currentMatch = matchAtSelection(this.matches, selection);
501
+ this.updateHighlights();
502
+ this.updateWidget();
503
+ if (moveCursor && vpMatches.length > 0) this.moveCursorForTruncatedLargeFile(search, query, snapshot, vpMatches, partition.viewportSpan, selectionSnapshot, anchorOffset);
504
+ return;
505
+ }
506
+ const remainingLimit = FIND_MATCHES_LIMIT - vpMatches.length;
507
+ const prefixScanner = partition.prefixRanges.length > 0 ? createFindRangeScanner(search.source, query, partition.prefixRanges, remainingLimit) : null;
508
+ const scan = {
509
+ generation: ++this.scanGeneration,
510
+ snapshot,
511
+ source: search.source,
512
+ scopes: search.scopes,
513
+ query,
514
+ viewportSpan: partition.viewportSpan,
515
+ viewportMatches: vpMatches,
516
+ prefixScanner,
517
+ suffixRanges: partition.suffixRanges,
518
+ suffixScanner: null,
519
+ prefixMatches: [],
520
+ suffixMatches: [],
521
+ remainingLimit,
522
+ prefixDone: prefixScanner === null,
523
+ moveCursor,
524
+ moveCursorSelection: selectionSnapshot,
525
+ channel: null
526
+ };
527
+ this.activeScan = scan;
528
+ const remainingFrame0Ms = 4 - (nowMs() - frame0Start);
529
+ if (!query.isRegex && partition.viewportSpan !== null && remainingFrame0Ms > 0) {
530
+ const outcome = this.advanceScanSlice(scan, remainingFrame0Ms);
531
+ if (outcome.finished) {
532
+ this.finishActiveScan(scan, outcome.truncated, outcome.prefixComplete);
533
+ return;
534
+ }
535
+ }
536
+ const immediateMatch = moveCursor ? firstMatchInViewport(vpMatches, partition.viewportSpan, partition.prefixRanges.length === 0, anchorOffset) : null;
537
+ if (immediateMatch) scan.moveCursor = false;
538
+ this.highlightsTruncated = false;
539
+ this.matchesIncomplete = true;
540
+ this.exactPrefixMatchCount = partition.prefixRanges.length === 0 ? vpMatches.length : 0;
541
+ this.adoptMatches(vpMatches);
542
+ this.currentMatch = immediateMatch ?? matchAtSelection(vpMatches, selection);
543
+ if (immediateMatch) host.setSelection(immediateMatch.start, immediateMatch.end, "input.findNavigate", findReveal(immediateMatch, false));
544
+ if (this.activeScan !== scan) return;
545
+ this.updateHighlights();
546
+ this.updateWidget();
547
+ this.scheduleNextScanSlice(scan);
548
+ return;
549
+ }
550
+ const prefixScanner = createFindRangeScanner(search.source, query, search.scopes, FIND_MATCHES_LIMIT);
551
+ this.highlightsTruncated = false;
552
+ this.matchesIncomplete = true;
553
+ this.exactPrefixMatchCount = 0;
554
+ this.adoptMatches([]);
555
+ this.currentMatch = null;
556
+ const scan = {
557
+ generation: ++this.scanGeneration,
558
+ snapshot,
559
+ source: search.source,
560
+ scopes: search.scopes,
561
+ query,
562
+ viewportSpan: null,
563
+ viewportMatches: [],
564
+ prefixScanner,
565
+ suffixRanges: [],
566
+ suffixScanner: null,
567
+ prefixMatches: [],
568
+ suffixMatches: [],
569
+ remainingLimit: FIND_MATCHES_LIMIT,
570
+ prefixDone: false,
571
+ moveCursor,
572
+ moveCursorSelection: selectionSnapshot,
573
+ channel: null
574
+ };
575
+ this.activeScan = scan;
576
+ this.updateHighlights();
577
+ this.updateWidget();
578
+ this.scheduleNextScanSlice(scan);
579
+ }
580
+ scheduleNextScanSlice(scan) {
581
+ if (typeof MessageChannel !== "undefined") {
582
+ if (!scan.channel) {
583
+ const channel = new MessageChannel();
584
+ channel.port1.onmessage = () => this.runScanSlice(scan);
585
+ scan.channel = channel;
586
+ }
587
+ scan.channel.port2.postMessage(void 0);
588
+ return;
589
+ }
590
+ this.ensureScheduler().schedule({
591
+ key: this.scanSliceKey,
592
+ taskClass: "background-derived",
593
+ defer: true,
594
+ run: () => this.runScanSlice(scan)
595
+ });
596
+ }
597
+ runScanSlice(scan) {
598
+ if (this.activeScan !== scan || scan.generation !== this.scanGeneration) return;
599
+ if (!this.host || !this.state.revealed) {
600
+ this.cancelActiveScan();
601
+ return;
602
+ }
603
+ if (scan.snapshot && !this.host.hasTextSnapshot(scan.snapshot)) {
604
+ this.cancelActiveScan();
605
+ return;
606
+ }
607
+ const outcome = this.advanceScanSlice(scan, 4);
608
+ if (outcome.finished) {
609
+ this.finishActiveScan(scan, outcome.truncated, outcome.prefixComplete);
610
+ return;
611
+ }
612
+ this.scheduleNextScanSlice(scan);
613
+ }
614
+ advanceScanSlice(scan, budgetMs) {
615
+ const sliceStart = nowMs();
616
+ let remainingWindows = 32;
617
+ if (!scan.prefixDone) {
618
+ if (scan.prefixScanner) {
619
+ const step = scan.prefixScanner.step(budgetMs, remainingWindows);
620
+ remainingWindows -= step.windowsScanned;
621
+ for (const match of step.matches) scan.prefixMatches.push(match);
622
+ if (step.truncated) return {
623
+ finished: true,
624
+ truncated: true,
625
+ prefixComplete: false
626
+ };
627
+ if (step.done) scan.prefixDone = true;
628
+ } else scan.prefixDone = true;
629
+ }
630
+ if (scan.prefixDone) {
631
+ if (scan.suffixRanges.length === 0) return {
632
+ finished: true,
633
+ truncated: false,
634
+ prefixComplete: true
635
+ };
636
+ const remainingMs = budgetMs - (nowMs() - sliceStart);
637
+ if ((remainingMs <= 0 || remainingWindows <= 0) && scan.prefixScanner !== null) return { finished: false };
638
+ scan.suffixScanner ??= createFindRangeScanner(scan.source, scan.query, scan.suffixRanges, scan.remainingLimit - scan.prefixMatches.length);
639
+ const step = scan.suffixScanner.step(Math.max(.5, remainingMs), Math.max(1, remainingWindows));
640
+ for (const match of step.matches) scan.suffixMatches.push(match);
641
+ if (step.truncated) return {
642
+ finished: true,
643
+ truncated: true,
644
+ prefixComplete: true
645
+ };
646
+ if (step.done) return {
647
+ finished: true,
648
+ truncated: false,
649
+ prefixComplete: true
650
+ };
651
+ }
652
+ return { finished: false };
653
+ }
654
+ finishActiveScan(scan, truncated, prefixComplete) {
655
+ if (this.activeScan !== scan || scan.generation !== this.scanGeneration) return;
656
+ this.closeScanChannel(scan);
657
+ this.activeScheduler()?.cancel(this.scanSliceKey);
658
+ this.activeScan = null;
659
+ const finalMatches = scan.prefixMatches.concat(scan.viewportMatches, scan.suffixMatches);
660
+ this.highlightsTruncated = truncated;
661
+ this.matchesIncomplete = false;
662
+ this.exactPrefixMatchCount = prefixComplete ? finalMatches.length : scan.prefixMatches.length;
663
+ this.adoptMatches(finalMatches);
664
+ const selection = this.primarySelection();
665
+ const preservedCurrent = this.currentMatch && selection && this.currentMatch.start === selection.startOffset && this.currentMatch.end === selection.endOffset ? this.currentMatch : null;
666
+ this.currentMatch = matchAtSelection(finalMatches, selection) ?? preservedCurrent;
367
667
  this.updateHighlights();
368
668
  this.updateWidget();
369
- if (moveCursor && found.length > 0) this.selectFirstMatchFromSelection();
669
+ if (scan.moveCursor && finalMatches.length > 0 && sameSelectionSnapshot(selection, scan.moveCursorSelection)) {
670
+ const offset = selection?.endOffset ?? 0;
671
+ if (!truncated) {
672
+ const target = firstMatchAtOrAfterInList(finalMatches, offset, this.options.loop);
673
+ this.selectMatch(target, false);
674
+ } else this.moveCursorForTruncatedLargeFile({
675
+ source: scan.source,
676
+ scopes: scan.scopes
677
+ }, scan.query, scan.snapshot, finalMatches, scan.viewportSpan, scan.moveCursorSelection, offset);
678
+ }
679
+ }
680
+ moveCursorForTruncatedLargeFile(search, query, snapshot, listedMatches, viewportSpan, expectedSelection, offset) {
681
+ const inExactPrefix = firstMatchAtOrAfterInList(listedMatches.slice(0, this.exactPrefixMatchCount), offset, false);
682
+ if (inExactPrefix) {
683
+ this.selectMatch(inExactPrefix, false);
684
+ return;
685
+ }
686
+ const inViewport = firstMatchInViewport(listedMatches, viewportSpan, false, offset);
687
+ if (inViewport) {
688
+ this.selectMatch(inViewport, false);
689
+ return;
690
+ }
691
+ const plan = findQueryPlan(query);
692
+ const tailRanges = plan?.kind === "lines" || plan?.kind === "anchored-lines" ? tailRangesFromOffset(search.source, search.scopes, offset) : search.scopes;
693
+ const fallbackMatch = this.options.loop && this.exactPrefixMatchCount > 0 ? listedMatches[0] ?? null : null;
694
+ const wrapTarget = this.options.loop && fallbackMatch === null ? {
695
+ source: search.source,
696
+ query,
697
+ scopes: search.scopes
698
+ } : null;
699
+ if (tailRanges !== null && tailRanges.length === 0) {
700
+ if (fallbackMatch) {
701
+ this.selectMatch(fallbackMatch, false);
702
+ return;
703
+ }
704
+ if (!wrapTarget) return;
705
+ const wrapScan = {
706
+ generation: ++this.scanGeneration,
707
+ snapshot,
708
+ scanner: createFindRangeScanner(wrapTarget.source, wrapTarget.query, wrapTarget.scopes, 1, 0),
709
+ fallbackMatch: null,
710
+ wrapTarget: null,
711
+ expectedSelection,
712
+ channel: null
713
+ };
714
+ this.activeCursorScan = wrapScan;
715
+ this.scheduleNextCursorScanSlice(wrapScan);
716
+ return;
717
+ }
718
+ const cursorScan = {
719
+ generation: ++this.scanGeneration,
720
+ snapshot,
721
+ scanner: createFindRangeScanner(search.source, query, tailRanges, 1, offset),
722
+ fallbackMatch,
723
+ wrapTarget,
724
+ expectedSelection,
725
+ channel: null
726
+ };
727
+ this.activeCursorScan = cursorScan;
728
+ this.scheduleNextCursorScanSlice(cursorScan);
729
+ }
730
+ scheduleNextCursorScanSlice(cursorScan) {
731
+ if (typeof MessageChannel !== "undefined") {
732
+ if (!cursorScan.channel) {
733
+ const channel = new MessageChannel();
734
+ channel.port1.onmessage = () => this.runCursorScanSlice(cursorScan);
735
+ cursorScan.channel = channel;
736
+ }
737
+ cursorScan.channel.port2.postMessage(void 0);
738
+ return;
739
+ }
740
+ this.ensureScheduler().schedule({
741
+ key: this.scanSliceKey,
742
+ taskClass: "background-derived",
743
+ defer: true,
744
+ run: () => this.runCursorScanSlice(cursorScan)
745
+ });
746
+ }
747
+ runCursorScanSlice(cursorScan) {
748
+ if (this.activeCursorScan !== cursorScan || cursorScan.generation !== this.scanGeneration) return;
749
+ if (!this.host || !this.state.revealed) {
750
+ this.cancelActiveCursorScan();
751
+ return;
752
+ }
753
+ if (cursorScan.snapshot && !this.host.hasTextSnapshot(cursorScan.snapshot)) {
754
+ this.cancelActiveCursorScan();
755
+ return;
756
+ }
757
+ if (!sameSelectionSnapshot(this.primarySelection(), cursorScan.expectedSelection)) {
758
+ this.cancelActiveCursorScan();
759
+ return;
760
+ }
761
+ const step = cursorScan.scanner.step(4);
762
+ const found = step.matches[0] ?? null;
763
+ if (found) {
764
+ this.cancelActiveCursorScan();
765
+ this.selectMatch(found, false);
766
+ return;
767
+ }
768
+ if (step.done) {
769
+ if (cursorScan.fallbackMatch) {
770
+ this.cancelActiveCursorScan();
771
+ this.selectMatch(cursorScan.fallbackMatch, false);
772
+ return;
773
+ }
774
+ if (cursorScan.wrapTarget) {
775
+ const wrap = cursorScan.wrapTarget;
776
+ cursorScan.wrapTarget = null;
777
+ cursorScan.scanner = createFindRangeScanner(wrap.source, wrap.query, wrap.scopes, 1, 0);
778
+ this.scheduleNextCursorScanSlice(cursorScan);
779
+ return;
780
+ }
781
+ this.cancelActiveCursorScan();
782
+ return;
783
+ }
784
+ this.scheduleNextCursorScanSlice(cursorScan);
785
+ }
786
+ cancelActiveScan() {
787
+ const scan = this.activeScan;
788
+ const cursorScan = this.activeCursorScan;
789
+ this.scanGeneration += 1;
790
+ this.activeScheduler()?.cancel(this.scanSliceKey);
791
+ if (scan) {
792
+ this.activeScan = null;
793
+ this.closeScanChannel(scan);
794
+ }
795
+ if (cursorScan) {
796
+ this.activeCursorScan = null;
797
+ this.closeScanChannel(cursorScan);
798
+ }
799
+ }
800
+ cancelActiveCursorScan() {
801
+ const cursorScan = this.activeCursorScan;
802
+ if (!cursorScan) return;
803
+ this.activeCursorScan = null;
804
+ this.closeScanChannel(cursorScan);
805
+ if (!this.activeScan) this.activeScheduler()?.cancel(this.scanSliceKey);
806
+ }
807
+ closeScanChannel(scan) {
808
+ const channel = scan.channel;
809
+ if (!channel) return;
810
+ scan.channel = null;
811
+ channel.port1.onmessage = null;
812
+ channel.port1.close();
813
+ channel.port2.close();
370
814
  }
371
815
  scheduleResearch() {
372
816
  this.followPendingMatches();
373
- this.scheduler.schedule({
374
- key: FIND_RESEARCH_KEY,
817
+ this.ensureScheduler().schedule({
818
+ key: this.researchKey,
375
819
  taskClass: "background-derived",
376
820
  delayMs: FIND_RESEARCH_DELAY_MS,
377
821
  maxDelayMs: FIND_RESEARCH_MAX_DELAY_MS,
378
822
  run: () => this.research(false)
379
823
  });
380
824
  }
825
+ activeScheduler() {
826
+ return this.host?.workScheduler ?? this.ownScheduler;
827
+ }
828
+ ensureScheduler() {
829
+ return this.host?.workScheduler ?? (this.ownScheduler ??= new EditorSecondaryViewScheduler());
830
+ }
381
831
  /**
382
832
  * Adopts a match set and hands it to the host to follow as far as it is painted.
383
833
  *
@@ -409,10 +859,12 @@ var EditorFindController = class {
409
859
  if (!this.canPaintCurrentDocument()) return;
410
860
  const tracked = this.trackedMatches;
411
861
  if (!tracked || this.matches.length === 0) return;
862
+ const previousLength = this.matches.length;
412
863
  this.matches = tracked.resolve().map((range) => ({
413
864
  ...range,
414
865
  matches: null
415
866
  }));
867
+ this.exactPrefixMatchCount = this.exactPrefixMatchCount === previousLength ? this.matches.length : 0;
416
868
  this.currentMatch = matchAtSelection(this.matches, this.primarySelection());
417
869
  this.updateHighlights();
418
870
  this.updateWidget();
@@ -420,6 +872,7 @@ var EditorFindController = class {
420
872
  applyReplacement(edits, timingName, selection) {
421
873
  const editHost = this.editHost;
422
874
  if (!editHost) return;
875
+ this.cancelActiveScan();
423
876
  this.replacing = true;
424
877
  try {
425
878
  editHost.applyEdits(edits, timingName, selection);
@@ -450,15 +903,6 @@ var EditorFindController = class {
450
903
  const source = this.commandTextSource();
451
904
  return source ? this.searchTarget(source) : null;
452
905
  }
453
- findAll(captureMatches, limit = FIND_MATCHES_LIMIT) {
454
- const search = this.searchTarget();
455
- if (!search) return [];
456
- return findMatches(search.source, this.state, search.scopes, captureMatches, limit);
457
- }
458
- countAll(before) {
459
- const search = this.searchTarget();
460
- return search ? countMatches(search.source, this.state, search.scopes, before) : 0;
461
- }
462
906
  searchFrom(find, offset, options) {
463
907
  const search = this.commandSearchTarget();
464
908
  if (!search) return null;
@@ -489,6 +933,10 @@ var EditorFindController = class {
489
933
  selectMatch(match, committed = true) {
490
934
  const host = this.host;
491
935
  if (!match || !host) return false;
936
+ if (committed) {
937
+ this.cancelActiveCursorScan();
938
+ if (this.activeScan) this.activeScan.moveCursor = false;
939
+ }
492
940
  if (this.canPaintCurrentDocument()) this.currentMatch = match;
493
941
  host.setSelection(match.start, match.end, "input.findNavigate", findReveal(match, committed));
494
942
  if (!this.canPaintCurrentDocument()) return true;
@@ -528,16 +976,17 @@ var EditorFindController = class {
528
976
  widgetState() {
529
977
  return {
530
978
  ...this.state,
531
- matchesCount: this.highlightsTruncated ? this.totalMatches : this.matches.length,
979
+ matchesCount: this.matches.length,
532
980
  matchesPosition: this.currentMatchPosition(),
533
- highlightsTruncated: this.highlightsTruncated
981
+ highlightsTruncated: this.highlightsTruncated,
982
+ searching: this.activeScan !== null || this.matchesIncomplete
534
983
  };
535
984
  }
536
985
  currentMatchPosition() {
537
986
  if (!this.currentMatch) return 0;
538
987
  const painted = findMatchIndex(this.matches, this.currentMatch);
539
- if (painted >= 0 || !this.highlightsTruncated) return painted + 1;
540
- return this.countAll(this.currentMatch.start) + 1;
988
+ if (painted < 0 || painted >= this.exactPrefixMatchCount) return 0;
989
+ return painted + 1;
541
990
  }
542
991
  focusWidget(focus) {
543
992
  if (focus === "find") this.emit({
@@ -671,6 +1120,49 @@ function mergeReplaceEdit(merged, edit) {
671
1120
  text: previous.text + edit.text
672
1121
  };
673
1122
  }
1123
+ function firstMatchInViewport(viewportMatches, viewportSpan, noPrefixBeforeViewport, offset) {
1124
+ if (!viewportSpan || viewportMatches.length === 0) return null;
1125
+ if (!(offset >= viewportSpan.start && offset <= viewportSpan.end || noPrefixBeforeViewport && offset <= viewportSpan.end)) return null;
1126
+ return viewportMatches.find((match) => match.start >= offset) ?? null;
1127
+ }
1128
+ function firstMatchAtOrAfterInList(matches, offset, loop) {
1129
+ const ahead = matches.find((match) => match.start >= offset);
1130
+ if (ahead) return ahead;
1131
+ return loop ? matches[0] ?? null : null;
1132
+ }
1133
+ function snapshotSelection(selection) {
1134
+ if (!selection) return null;
1135
+ return {
1136
+ anchorOffset: selection.anchorOffset,
1137
+ headOffset: selection.headOffset,
1138
+ affinity: selection.affinity
1139
+ };
1140
+ }
1141
+ function sameSelectionSnapshot(current, expected) {
1142
+ if (!current || !expected) return current === expected;
1143
+ return current.anchorOffset === expected.anchorOffset && current.headOffset === expected.headOffset && current.affinity === expected.affinity;
1144
+ }
1145
+ function tailRangesFromOffset(source, scopes, offset) {
1146
+ const clamped = Math.min(Math.max(0, offset), source.length);
1147
+ const line = source.lineStartsView.indexForOffset(clamped);
1148
+ const lineStart = source.lineStartsView.at(line) ?? 0;
1149
+ if (!scopes) return [{
1150
+ start: lineStart,
1151
+ end: source.length
1152
+ }];
1153
+ const tail = [];
1154
+ for (const scope of scopes) {
1155
+ if (scope.end < clamped) continue;
1156
+ tail.push({
1157
+ start: Math.max(scope.start, lineStart),
1158
+ end: scope.end
1159
+ });
1160
+ }
1161
+ return tail;
1162
+ }
1163
+ function nowMs() {
1164
+ return globalThis.performance?.now() ?? Date.now();
1165
+ }
674
1166
  export { EditorFindController, FIND_HIGHLIGHT_Z_INDEX };
675
1167
 
676
1168
  //# sourceMappingURL=findController.js.map