From e5417827f4a0d7008b30c124de7d3497a2030f73 Mon Sep 17 00:00:00 2001 From: Elian Doran Date: Mon, 12 May 2025 20:43:30 +0300 Subject: [PATCH] chore(code/find): update current found --- apps/client/src/widgets/find_in_code.ts | 9 ++----- packages/codemirror/src/find_replace.ts | 31 ++++++++++++++----------- packages/codemirror/src/index.ts | 3 ++- 3 files changed, 21 insertions(+), 22 deletions(-) diff --git a/apps/client/src/widgets/find_in_code.ts b/apps/client/src/widgets/find_in_code.ts index 343ce0737..4e80f789e 100644 --- a/apps/client/src/widgets/find_in_code.ts +++ b/apps/client/src/widgets/find_in_code.ts @@ -31,17 +31,12 @@ export default class FindInCode { } async performFind(searchTerm: string, matchCase: boolean, wholeWord: boolean) { - let totalFound = 0; - const currentFound = 0; - const codeEditor = await this.getCodeEditor(); if (!codeEditor) { - return { totalFound, currentFound }; + return { totalFound: 0, currentFound: 0 }; } - const result = await codeEditor.performFind(searchTerm, matchCase, wholeWord); - totalFound = result.totalFound; - + const { totalFound, currentFound } = await codeEditor.performFind(searchTerm, matchCase, wholeWord); return { totalFound, currentFound }; } diff --git a/packages/codemirror/src/find_replace.ts b/packages/codemirror/src/find_replace.ts index 4adc2d3e2..24bd6da7d 100644 --- a/packages/codemirror/src/find_replace.ts +++ b/packages/codemirror/src/find_replace.ts @@ -22,11 +22,13 @@ export function createSearchHighlighter(view: EditorView, searchTerm: string, ma return ViewPlugin.fromClass(class SearchHighlighter { matches!: RangeSet; + currentFound: number; totalFound: number; private parsedMatches: Match[]; constructor(public view: EditorView) { this.parsedMatches = []; + this.currentFound = 0; this.totalFound = 0; this.updateSearchData(view); } @@ -57,26 +59,27 @@ export function createSearchHighlighter(view: EditorView, searchTerm: string, ma return; } - this.scrollTo(this.parsedMatches[matchIndex]); - } - - scrollToMatchNearestSelection() { - const cursorPos = this.view.state.selection.main.head; - for (const match of this.parsedMatches) { - if (match.from >= cursorPos) { - this.scrollTo(match); - return; - } - } - } - - private scrollTo(match: Match) { + const match = this.parsedMatches[matchIndex]; + this.currentFound = matchIndex + 1; this.view.dispatch({ effects: EditorView.scrollIntoView(match.from, { y: "center" }), scrollIntoView: true }); } + scrollToMatchNearestSelection() { + const cursorPos = this.view.state.selection.main.head; + let index = 0; + for (const match of this.parsedMatches) { + if (match.from >= cursorPos) { + this.scrollToMatch(index); + return; + } + + index++; + } + } + destroy() { // Do nothing. } diff --git a/packages/codemirror/src/index.ts b/packages/codemirror/src/index.ts index 72efa20f2..59c58aaa5 100644 --- a/packages/codemirror/src/index.ts +++ b/packages/codemirror/src/index.ts @@ -185,7 +185,8 @@ export default class CodeMirror extends EditorView { } return { - totalFound: instance?.totalFound ?? 0 + totalFound: instance?.totalFound ?? 0, + currentFound: instance?.currentFound ?? 0 } }