From 666b7247b7b88af93b2638d9ea6007ed012d4225 Mon Sep 17 00:00:00 2001 From: Leo dev Date: Mon, 9 Feb 2026 15:29:51 +0100 Subject: [PATCH] Tab change --- src/client.ts | 9 ++++++++- src/move.ts | 55 +++++++++++++++++++++++++++++++++++++++++---------- 2 files changed, 53 insertions(+), 11 deletions(-) diff --git a/src/client.ts b/src/client.ts index 219a67c..4264aff 100644 --- a/src/client.ts +++ b/src/client.ts @@ -41,12 +41,19 @@ export async function submitRun(runId: string) { ); } -export async function addRunMoves(runId: string, moves: LiveRunMove[]) { +export async function addRunMoves( + runId: string, + file: string | null, + language: string | null, + moves: LiveRunMove[], +) { await axios.post( "http://localhost:3000/api/run/move", { runId, + file, moves, + language, }, { headers: { diff --git a/src/move.ts b/src/move.ts index cd0ca43..1f8250e 100644 --- a/src/move.ts +++ b/src/move.ts @@ -2,6 +2,7 @@ import * as vscode from "vscode"; import { diff_match_patch, Diff } from "diff-match-patch"; import { LiveRunMove } from "./types"; import { addRunMoves } from "./client"; +import path from "path"; export function startMonitoring(runId: string) { const dmp = new diff_match_patch(); @@ -16,12 +17,11 @@ export function startMonitoring(runId: string) { const IDLE_MS = 350; - const editor = vscode.window.activeTextEditor; - if (editor) { - lastText = editor.document.getText(); + if (vscode.window.activeTextEditor) { + lastText = vscode.window.activeTextEditor.document.getText(); } - async function flush() { + async function flush(file: string | null, language: string | null) { if (!moves.length || isSending) return; isSending = true; @@ -29,12 +29,19 @@ export function startMonitoring(runId: string) { moves = []; try { - await addRunMoves(runId, batch); + await addRunMoves(runId, file, language, batch); } finally { isSending = false; } } + function getLatency(): number { + const now = Date.now(); + const latency = capLatency(now - lastEventTime, IDLE_MS); + lastEventTime = now; + return latency; + } + const readInterval = setInterval(() => { const editor = vscode.window.activeTextEditor; if (!editor) return; @@ -42,9 +49,7 @@ export function startMonitoring(runId: string) { const newText = editor.document.getText(); if (newText === lastText) return; - const now = Date.now(); - const latency = capLatency(now - lastEventTime, IDLE_MS); - lastEventTime = now; + let latency = getLatency(); const diffs = dmp.diff_main(lastText, newText); dmp.diff_cleanupEfficiency(diffs); @@ -63,13 +68,43 @@ export function startMonitoring(runId: string) { } if (idleTimeout) clearTimeout(idleTimeout); - idleTimeout = setTimeout(flush, IDLE_MS); + idleTimeout = setTimeout( + () => + flush( + path.basename(editor?.document.fileName || "") || null, + editor?.document.languageId || null, + ), + IDLE_MS, + ); }, 500); + vscode.window.onDidChangeActiveTextEditor((editor) => { + let latency = getLatency(); + + if (idleTimeout) clearTimeout(idleTimeout); + + moves = []; + + moves.push({ + moveId: moveId++, + latency, + cursor: editor?.document.offsetAt(editor.selection.active) || 0, + }); + + flush( + path.basename(editor?.document.fileName || "") || null, + editor?.document.languageId || null, + ); + }); + return () => { clearInterval(readInterval); if (idleTimeout) clearTimeout(idleTimeout); - flush(); + flush( + path.basename(vscode.window.activeTextEditor?.document.fileName || "") || + null, + vscode.window.activeTextEditor?.document.languageId || null, + ); }; }