chore(code/find): update current found

This commit is contained in:
Elian Doran 2025-05-12 20:43:30 +03:00
parent e08011b3d6
commit e5417827f4
No known key found for this signature in database
3 changed files with 21 additions and 22 deletions

View File

@ -31,17 +31,12 @@ export default class FindInCode {
} }
async performFind(searchTerm: string, matchCase: boolean, wholeWord: boolean) { async performFind(searchTerm: string, matchCase: boolean, wholeWord: boolean) {
let totalFound = 0;
const currentFound = 0;
const codeEditor = await this.getCodeEditor(); const codeEditor = await this.getCodeEditor();
if (!codeEditor) { if (!codeEditor) {
return { totalFound, currentFound }; return { totalFound: 0, currentFound: 0 };
} }
const result = await codeEditor.performFind(searchTerm, matchCase, wholeWord); const { totalFound, currentFound } = await codeEditor.performFind(searchTerm, matchCase, wholeWord);
totalFound = result.totalFound;
return { totalFound, currentFound }; return { totalFound, currentFound };
} }

View File

@ -22,11 +22,13 @@ export function createSearchHighlighter(view: EditorView, searchTerm: string, ma
return ViewPlugin.fromClass(class SearchHighlighter { return ViewPlugin.fromClass(class SearchHighlighter {
matches!: RangeSet<Decoration>; matches!: RangeSet<Decoration>;
currentFound: number;
totalFound: number; totalFound: number;
private parsedMatches: Match[]; private parsedMatches: Match[];
constructor(public view: EditorView) { constructor(public view: EditorView) {
this.parsedMatches = []; this.parsedMatches = [];
this.currentFound = 0;
this.totalFound = 0; this.totalFound = 0;
this.updateSearchData(view); this.updateSearchData(view);
} }
@ -57,26 +59,27 @@ export function createSearchHighlighter(view: EditorView, searchTerm: string, ma
return; return;
} }
this.scrollTo(this.parsedMatches[matchIndex]); const match = this.parsedMatches[matchIndex];
} this.currentFound = matchIndex + 1;
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) {
this.view.dispatch({ this.view.dispatch({
effects: EditorView.scrollIntoView(match.from, { y: "center" }), effects: EditorView.scrollIntoView(match.from, { y: "center" }),
scrollIntoView: true 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() { destroy() {
// Do nothing. // Do nothing.
} }

View File

@ -185,7 +185,8 @@ export default class CodeMirror extends EditorView {
} }
return { return {
totalFound: instance?.totalFound ?? 0 totalFound: instance?.totalFound ?? 0,
currentFound: instance?.currentFound ?? 0
} }
} }