Send cursor position every second

This commit is contained in:
2026-02-16 01:09:26 +01:00
parent fde6a4ccb9
commit 62c49404cb
+38 -3
View File
@@ -11,6 +11,7 @@ export function startMonitoring(useLocalhost: boolean, runId: string) {
let moves: LiveRunMove[] = []; let moves: LiveRunMove[] = [];
let lastText = ""; let lastText = "";
let lastCursorOffset: number | undefined;
let lastEventTime = Date.now(); let lastEventTime = Date.now();
let idleTimeout: NodeJS.Timeout | undefined; let idleTimeout: NodeJS.Timeout | undefined;
let isSending = false; let isSending = false;
@@ -18,7 +19,9 @@ export function startMonitoring(useLocalhost: boolean, runId: string) {
const IDLE_MS = 350; const IDLE_MS = 350;
if (vscode.window.activeTextEditor) { if (vscode.window.activeTextEditor) {
lastText = vscode.window.activeTextEditor.document.getText(); const editor = vscode.window.activeTextEditor;
lastText = editor.document.getText();
lastCursorOffset = editor.document.offsetAt(editor.selection.active);
} }
async function flush(file: string | null, language: string | null) { async function flush(file: string | null, language: string | null) {
@@ -42,13 +45,13 @@ export function startMonitoring(useLocalhost: boolean, runId: string) {
return latency; return latency;
} }
// --- Existing text change interval ---
const readInterval = setInterval(() => { const readInterval = setInterval(() => {
const editor = vscode.window.activeTextEditor; const editor = vscode.window.activeTextEditor;
if (!editor) return; if (!editor) return;
const newText = editor.document.getText(); const newText = editor.document.getText();
if (newText === lastText) return; if (newText !== lastText) {
let latency = getLatency(); let latency = getLatency();
const diffs = dmp.diff_main(lastText, newText); const diffs = dmp.diff_main(lastText, newText);
@@ -76,8 +79,39 @@ export function startMonitoring(useLocalhost: boolean, runId: string) {
), ),
IDLE_MS, IDLE_MS,
); );
}
}, 500); }, 500);
// --- New cursor tracking interval ---
const cursorInterval = setInterval(() => {
const editor = vscode.window.activeTextEditor;
if (!editor) return;
const cursorOffset = editor.document.offsetAt(editor.selection.active);
if (cursorOffset !== lastCursorOffset) {
const latency = getLatency();
moves.push({
moveId: moveId++,
latency,
cursor: cursorOffset,
});
lastCursorOffset = cursorOffset;
if (idleTimeout) clearTimeout(idleTimeout);
idleTimeout = setTimeout(
() =>
flush(
path.basename(editor.document.fileName || "") || null,
editor.document.languageId || null,
),
IDLE_MS,
);
}
}, 1000); // every second
vscode.window.onDidChangeActiveTextEditor((editor) => { vscode.window.onDidChangeActiveTextEditor((editor) => {
let latency = getLatency(); let latency = getLatency();
@@ -99,6 +133,7 @@ export function startMonitoring(useLocalhost: boolean, runId: string) {
return () => { return () => {
clearInterval(readInterval); clearInterval(readInterval);
clearInterval(cursorInterval);
if (idleTimeout) clearTimeout(idleTimeout); if (idleTimeout) clearTimeout(idleTimeout);
flush( flush(
path.basename(vscode.window.activeTextEditor?.document.fileName || "") || path.basename(vscode.window.activeTextEditor?.document.fileName || "") ||