refactor(code/find): reimplement find next

This commit is contained in:
Elian Doran
2025-05-12 21:21:46 +03:00
parent 2fbff5328b
commit 22d9091315
3 changed files with 37 additions and 49 deletions

View File

@@ -33,11 +33,34 @@ export class SearchHighlighter {
regexp: regex,
decoration: searchMatchDecoration,
});
this.updateSearchData(this.view);
this.scrollToMatchNearestSelection();
this.#updateSearchData(this.view);
this.#scrollToMatchNearestSelection();
}
updateSearchData(view: EditorView) {
scrollToMatch(matchIndex: number) {
if (this.parsedMatches.length <= matchIndex) {
return;
}
const match = this.parsedMatches[matchIndex];
this.currentFound = matchIndex + 1;
this.view.dispatch({
effects: EditorView.scrollIntoView(match.from, { y: "center" }),
scrollIntoView: true
});
}
update(update: ViewUpdate) {
if (update.docChanged || update.viewportChanged) {
this.#updateSearchData(update.view);
}
}
destroy() {
// Do nothing.
}
#updateSearchData(view: EditorView) {
if (!this.matcher) {
return;
}
@@ -56,26 +79,7 @@ export class SearchHighlighter {
this.totalFound = this.parsedMatches.length;
}
update(update: ViewUpdate) {
if (update.docChanged || update.viewportChanged) {
this.updateSearchData(update.view);
}
}
scrollToMatch(matchIndex: number) {
if (this.parsedMatches.length <= matchIndex) {
return;
}
const match = this.parsedMatches[matchIndex];
this.currentFound = matchIndex + 1;
this.view.dispatch({
effects: EditorView.scrollIntoView(match.from, { y: "center" }),
scrollIntoView: true
});
}
scrollToMatchNearestSelection() {
#scrollToMatchNearestSelection() {
const cursorPos = this.view.state.selection.main.head;
let index = 0;
for (const match of this.parsedMatches) {
@@ -88,10 +92,6 @@ export class SearchHighlighter {
}
}
destroy() {
// Do nothing.
}
static deco = (v: SearchHighlighter) => v.matches;
}