|
| 1 | +import { getSearchQuery, searchPanelOpen } from '@codemirror/search'; |
| 2 | +import type { Extension } from '@codemirror/state'; |
| 3 | +import { type EditorView, ViewPlugin, type ViewUpdate } from '@codemirror/view'; |
| 4 | + |
| 5 | +/** |
| 6 | + * A CodeMirror extension that displays the total number of search matches |
| 7 | + * inside the built-in search panel. |
| 8 | + */ |
| 9 | +export function searchMatchCount(): Extension { |
| 10 | + return ViewPlugin.fromClass( |
| 11 | + class { |
| 12 | + private countEl: HTMLElement | null = null; |
| 13 | + |
| 14 | + constructor(private view: EditorView) { |
| 15 | + this.updateCount(); |
| 16 | + } |
| 17 | + |
| 18 | + update(update: ViewUpdate) { |
| 19 | + // Recompute when doc changes, search state changes, or selection moves |
| 20 | + const query = getSearchQuery(update.state); |
| 21 | + const prevQuery = getSearchQuery(update.startState); |
| 22 | + const open = searchPanelOpen(update.state); |
| 23 | + const prevOpen = searchPanelOpen(update.startState); |
| 24 | + |
| 25 | + if (update.docChanged || update.selectionSet || !query.eq(prevQuery) || open !== prevOpen) { |
| 26 | + this.updateCount(); |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + private updateCount() { |
| 31 | + const state = this.view.state; |
| 32 | + const open = searchPanelOpen(state); |
| 33 | + const query = getSearchQuery(state); |
| 34 | + |
| 35 | + if (!open) { |
| 36 | + this.removeCountEl(); |
| 37 | + return; |
| 38 | + } |
| 39 | + |
| 40 | + this.ensureCountEl(); |
| 41 | + |
| 42 | + if (!query.search) { |
| 43 | + if (this.countEl) { |
| 44 | + this.countEl.textContent = '0/0'; |
| 45 | + } |
| 46 | + return; |
| 47 | + } |
| 48 | + |
| 49 | + const selection = state.selection.main; |
| 50 | + let count = 0; |
| 51 | + let currentIndex = 0; |
| 52 | + const MAX_COUNT = 9999; |
| 53 | + const cursor = query.getCursor(state); |
| 54 | + while (!cursor.next().done) { |
| 55 | + count++; |
| 56 | + if (cursor.value.from <= selection.from && cursor.value.to >= selection.to) { |
| 57 | + currentIndex = count; |
| 58 | + } |
| 59 | + if (count > MAX_COUNT) break; |
| 60 | + } |
| 61 | + |
| 62 | + if (this.countEl) { |
| 63 | + if (count > MAX_COUNT) { |
| 64 | + this.countEl.textContent = `${MAX_COUNT}+`; |
| 65 | + } else if (count === 0) { |
| 66 | + this.countEl.textContent = '0/0'; |
| 67 | + } else if (currentIndex > 0) { |
| 68 | + this.countEl.textContent = `${currentIndex}/${count}`; |
| 69 | + } else { |
| 70 | + this.countEl.textContent = `0/${count}`; |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + private ensureCountEl() { |
| 76 | + // Find the search panel in the editor DOM |
| 77 | + const panel = this.view.dom.querySelector('.cm-search'); |
| 78 | + if (!panel) { |
| 79 | + this.countEl = null; |
| 80 | + return; |
| 81 | + } |
| 82 | + |
| 83 | + if (this.countEl && this.countEl.parentElement === panel) { |
| 84 | + return; // Already attached |
| 85 | + } |
| 86 | + |
| 87 | + this.countEl = document.createElement('span'); |
| 88 | + this.countEl.className = 'cm-search-match-count'; |
| 89 | + |
| 90 | + // Reorder: insert prev button, then next button, then count after the search input |
| 91 | + const searchInput = panel.querySelector('input'); |
| 92 | + const prevBtn = panel.querySelector('button[name="prev"]'); |
| 93 | + const nextBtn = panel.querySelector('button[name="next"]'); |
| 94 | + if (searchInput && searchInput.parentElement === panel) { |
| 95 | + searchInput.after(this.countEl); |
| 96 | + if (prevBtn) this.countEl.after(prevBtn); |
| 97 | + if (nextBtn && prevBtn) prevBtn.after(nextBtn); |
| 98 | + } else { |
| 99 | + panel.prepend(this.countEl); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + private removeCountEl() { |
| 104 | + if (this.countEl) { |
| 105 | + this.countEl.remove(); |
| 106 | + this.countEl = null; |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + destroy() { |
| 111 | + this.removeCountEl(); |
| 112 | + } |
| 113 | + }, |
| 114 | + ); |
| 115 | +} |
0 commit comments