From 9dd60a56deb2354d2753b9773c9401b54c5525ba Mon Sep 17 00:00:00 2001 From: Klesti Selimaj Date: Mon, 16 Feb 2026 01:38:47 +0100 Subject: [PATCH] Text stability sync --- src/client.ts | 10 ++++++++++ src/move.ts | 32 +++++++++++++++++++++++++++++++- 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/src/client.ts b/src/client.ts index 41db992..8591d9a 100644 --- a/src/client.ts +++ b/src/client.ts @@ -212,3 +212,13 @@ export async function addRunMoves( moves, }); } + +export async function getText( + useLocalhost: boolean, + runId: string, +): Promise { + return await send(useLocalhost, { + type: "getText", + runId, + }); +} diff --git a/src/move.ts b/src/move.ts index dbedef9..8b9a1a9 100644 --- a/src/move.ts +++ b/src/move.ts @@ -1,7 +1,7 @@ import * as vscode from "vscode"; import { diff_match_patch, Diff } from "diff-match-patch"; import { LiveRunMove } from "./types"; -import { addRunMoves } from "./client"; +import { addRunMoves, getText } from "./client"; import path from "path"; export function startMonitoring(useLocalhost: boolean, runId: string) { @@ -112,6 +112,35 @@ export function startMonitoring(useLocalhost: boolean, runId: string) { } }, 1000); // every second + // Sync text every 5 seconds to prevent desync + const textStabilityInterval = setInterval(async () => { + const editor = vscode.window.activeTextEditor; + if (!editor) return; + + const serverText = await getText(useLocalhost, runId); + + const editorText = editor.document.getText(); + if (editorText !== serverText) { + clearTimeout(idleTimeout); + moves = [ + { + latency: 0, + cursor: editor.document.offsetAt(editor.selection.active), + moveId: moveId++, + changes: { + from: 0, + to: serverText.length, + insert: editorText, + }, + }, + ]; + flush( + path.basename(editor?.document.fileName || "") || null, + editor?.document.languageId || null, + ); + } + }, 5000); // every 5 seconds + vscode.window.onDidChangeActiveTextEditor((editor) => { let latency = getLatency(); @@ -132,6 +161,7 @@ export function startMonitoring(useLocalhost: boolean, runId: string) { }); return () => { + clearInterval(textStabilityInterval); clearInterval(readInterval); clearInterval(cursorInterval); if (idleTimeout) clearTimeout(idleTimeout);