Text stability sync

This commit is contained in:
2026-02-16 01:38:47 +01:00
parent 10bf1ae31d
commit 9dd60a56de
2 changed files with 41 additions and 1 deletions
+10
View File
@@ -212,3 +212,13 @@ export async function addRunMoves(
moves, moves,
}); });
} }
export async function getText(
useLocalhost: boolean,
runId: string,
): Promise<string> {
return await send(useLocalhost, {
type: "getText",
runId,
});
}
+31 -1
View File
@@ -1,7 +1,7 @@
import * as vscode from "vscode"; import * as vscode from "vscode";
import { diff_match_patch, Diff } from "diff-match-patch"; import { diff_match_patch, Diff } from "diff-match-patch";
import { LiveRunMove } from "./types"; import { LiveRunMove } from "./types";
import { addRunMoves } from "./client"; import { addRunMoves, getText } from "./client";
import path from "path"; import path from "path";
export function startMonitoring(useLocalhost: boolean, runId: string) { export function startMonitoring(useLocalhost: boolean, runId: string) {
@@ -112,6 +112,35 @@ export function startMonitoring(useLocalhost: boolean, runId: string) {
} }
}, 1000); // every second }, 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) => { vscode.window.onDidChangeActiveTextEditor((editor) => {
let latency = getLatency(); let latency = getLatency();
@@ -132,6 +161,7 @@ export function startMonitoring(useLocalhost: boolean, runId: string) {
}); });
return () => { return () => {
clearInterval(textStabilityInterval);
clearInterval(readInterval); clearInterval(readInterval);
clearInterval(cursorInterval); clearInterval(cursorInterval);
if (idleTimeout) clearTimeout(idleTimeout); if (idleTimeout) clearTimeout(idleTimeout);