chore(code/find): scroll to nearest result

This commit is contained in:
Elian Doran 2025-05-12 20:39:09 +03:00
parent c4863dec50
commit e08011b3d6
No known key found for this signature in database
2 changed files with 16 additions and 3 deletions

View File

@ -57,9 +57,22 @@ export function createSearchHighlighter(view: EditorView, searchTerm: string, ma
return;
}
const pos = this.parsedMatches[matchIndex];
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) {
this.view.dispatch({
effects: EditorView.scrollIntoView(pos.from, { y: "center" }),
effects: EditorView.scrollIntoView(match.from, { y: "center" }),
scrollIntoView: true
});
}

View File

@ -181,7 +181,7 @@ export default class CodeMirror extends EditorView {
await new Promise(requestAnimationFrame);
const instance = this.plugin(plugin);
if (instance) {
instance.scrollToMatch(0);
instance.scrollToMatchNearestSelection();
}
return {