feat: only scroll when selection is not adjacent (#1787)
Some checks failed
tinymist::ci / Duplicate Actions Detection (push) Has been cancelled
tinymist::ci / prepare-build (push) Has been cancelled
tinymist::ci / Check Clippy, Formatting, Completion, Documentation, and Tests (Linux) (push) Has been cancelled
tinymist::ci / Check Minimum Rust version and Tests (Windows) (push) Has been cancelled
tinymist::gh_pages / build-gh-pages (push) Has been cancelled
tinymist::ci / build-binary (push) Has been cancelled
tinymist::ci / build-vsc-assets (push) Has been cancelled
tinymist::ci / build-vscode (push) Has been cancelled
tinymist::ci / build-vscode-others (push) Has been cancelled
tinymist::ci / publish-vscode (push) Has been cancelled
tinymist::ci / E2E Tests (win32-x64 on windows-latest) (push) Has been cancelled
tinymist::ci / E2E Tests (darwin-arm64 on macos-latest) (push) Has been cancelled
tinymist::ci / E2E Tests (linux-x64 on ubuntu-22.04) (push) Has been cancelled
tinymist::ci / E2E Tests (linux-x64 on ubuntu-latest) (push) Has been cancelled
tinymist::ci / E2E Tests (win32-x64 on windows-2019) (push) Has been cancelled

This commit is contained in:
Myriad-Dreamin 2025-05-31 01:10:32 +08:00 committed by GitHub
parent 39368da55f
commit cecb424b1e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -461,6 +461,8 @@ async function launchPreviewLsp(task: LaunchInBrowserTask | LaunchInWebViewTask)
return { message: "ok", taskId };
async function invokeLspCommand() {
let prevSelection: EditorSelection | undefined = undefined;
console.log(`Preview Command ${filePath}`);
const partialRenderingArgs = getPreviewConfCompat<boolean>("partialRendering")
? ["--partial-rendering"]
@ -495,14 +497,10 @@ async function launchPreviewLsp(task: LaunchInBrowserTask | LaunchInWebViewTask)
}
if (scrollSyncMode !== ScrollSyncModeEnum.never) {
// See comment of reportPosition function to get context about multi-file project related logic.
const src2docHandler = (e: vscode.TextEditorSelectionChangeEvent) => {
const editor = e.textEditor;
const kind = e.kind;
// console.log(
// `selection changed, kind: ${kind && vscode.TextEditorSelectionChangeKind[kind]}`
// );
const shouldScrollPanel =
// scroll by mouse
kind === vscode.TextEditorSelectionChangeKind.Mouse ||
@ -511,7 +509,7 @@ async function launchPreviewLsp(task: LaunchInBrowserTask | LaunchInWebViewTask)
kind === vscode.TextEditorSelectionChangeKind.Keyboard);
if (shouldScrollPanel) {
// console.log(`selection changed, sending src2doc jump request`);
reportPosition(editor, "panelScrollTo");
mayReportPosition(editor, "panelScrollTo");
}
if (enableCursor) {
@ -523,6 +521,53 @@ async function launchPreviewLsp(task: LaunchInBrowserTask | LaunchInWebViewTask)
}
return { staticServerPort, dataPlanePort, isPrimary };
/**
* Reports the position of the editor when necessary.
*/
function mayReportPosition(editor: vscode.TextEditor, event: "panelScrollTo") {
// For multiple selections, we don't try to scroll the preview panel.
if (editor.selections.length > 1) {
return;
}
// For adjacent selections, we don't try to scroll the preview panel.
if (adjacentSelection(editor, prevSelection)) {
return;
}
// Updates selection and reports the position.
prevSelection = {
uri: editor.document.uri,
start: editor.selection.start,
end: editor.selection.end,
};
return reportPosition(editor, event);
}
}
interface EditorSelection {
uri: vscode.Uri;
start: vscode.Position;
end: vscode.Position;
}
function adjacentSelection(editor: vscode.TextEditor, prevSelection?: EditorSelection): boolean {
// If there is no previous position, we cannot determine if the current position is adjacent.
// Or if the previous position is not from the same document, we cannot determine either.
// It is intended to compare uri equality by reference, not by value.
if (!prevSelection || prevSelection.uri !== editor.document.uri) {
return false;
}
// Any of the current selection start or end shares the same position with the previous
// selection start or end, we consider it as adjacent.
const currentStart = editor.selection.start;
const currentEnd = editor.selection.end;
return (
currentStart.isEqual(prevSelection.start) ||
currentEnd.isEqual(prevSelection.end) ||
currentStart.isEqual(prevSelection.end) ||
currentEnd.isEqual(prevSelection.start)
);
}
async function reportPosition(