Fixed issues
This commit is contained in:
+35
-50
@@ -1,65 +1,50 @@
|
|||||||
import * as vscode from "vscode";
|
import * as vscode from "vscode";
|
||||||
import { LiveRunMove } from "./types";
|
import { LiveRunMove } from "./types";
|
||||||
import { addRunMoves } from "./client";
|
import { addRunMoves } from "./client";
|
||||||
import { randomInt } from "crypto";
|
|
||||||
|
|
||||||
let lastText = "";
|
|
||||||
let lastCursor = 0;
|
|
||||||
let moveId = 0;
|
|
||||||
|
|
||||||
export function startMonitoring(runId: string) {
|
export function startMonitoring(runId: string) {
|
||||||
lastText = "";
|
let moveId = 0;
|
||||||
|
let moves: LiveRunMove[] = [];
|
||||||
|
let lastLatency = Date.now();
|
||||||
|
|
||||||
return setInterval(() => {
|
const docSub = vscode.workspace.onDidChangeTextDocument((e) => {
|
||||||
const editor = vscode.window.activeTextEditor;
|
const editor = vscode.window.activeTextEditor;
|
||||||
if (!editor) return;
|
if (!editor || e.document !== editor.document) return;
|
||||||
|
|
||||||
|
const edit = e.contentChanges[0];
|
||||||
|
if (!edit) return;
|
||||||
|
|
||||||
|
const now = Date.now();
|
||||||
|
const latency = capToZeroAbove(now - lastLatency, 350);
|
||||||
|
lastLatency = now;
|
||||||
|
|
||||||
const currentText = editor.document.getText();
|
|
||||||
const cursorOffset = editor.document.offsetAt(editor.selection.active);
|
const cursorOffset = editor.document.offsetAt(editor.selection.active);
|
||||||
|
|
||||||
let change: { from: number; to: number; insert: string } | undefined;
|
moves.push({
|
||||||
|
latency,
|
||||||
if (currentText !== lastText) {
|
|
||||||
let start = 0;
|
|
||||||
while (
|
|
||||||
start < lastText.length &&
|
|
||||||
start < currentText.length &&
|
|
||||||
lastText[start] === currentText[start]
|
|
||||||
) {
|
|
||||||
start++;
|
|
||||||
}
|
|
||||||
|
|
||||||
let endOld = lastText.length - 1;
|
|
||||||
let endNew = currentText.length - 1;
|
|
||||||
while (
|
|
||||||
endOld >= start &&
|
|
||||||
endNew >= start &&
|
|
||||||
lastText[endOld] === currentText[endNew]
|
|
||||||
) {
|
|
||||||
endOld--;
|
|
||||||
endNew--;
|
|
||||||
}
|
|
||||||
|
|
||||||
change = {
|
|
||||||
from: start,
|
|
||||||
to: endOld + 1, // ⚡ correct range in old text
|
|
||||||
insert: currentText.slice(start, endNew + 1),
|
|
||||||
};
|
|
||||||
|
|
||||||
lastText = currentText;
|
|
||||||
}
|
|
||||||
|
|
||||||
const move: LiveRunMove = {
|
|
||||||
latency: 300,
|
|
||||||
cursor: cursorOffset,
|
cursor: cursorOffset,
|
||||||
changes: change,
|
changes: {
|
||||||
moveId,
|
from: edit.rangeOffset,
|
||||||
};
|
to: edit.rangeOffset + edit.rangeLength,
|
||||||
|
insert: edit.text,
|
||||||
|
},
|
||||||
|
moveId: moveId++,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
if (move.changes || move.cursor !== lastCursor) {
|
const sendInterval = setInterval(() => {
|
||||||
addRunMoves(runId, [move]);
|
if (moves.length) {
|
||||||
|
addRunMoves(runId, moves);
|
||||||
|
moves = [];
|
||||||
}
|
}
|
||||||
|
}, 3000);
|
||||||
|
|
||||||
lastCursor = move.cursor;
|
return () => {
|
||||||
}, 1000);
|
docSub.dispose();
|
||||||
|
clearInterval(sendInterval);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function capToZeroAbove(value: number, max: number) {
|
||||||
|
return value > max ? 0 : value;
|
||||||
}
|
}
|
||||||
|
|||||||
+2
-4
@@ -4,7 +4,7 @@ import { startMonitoring } from "./move";
|
|||||||
|
|
||||||
export var currentRun: {
|
export var currentRun: {
|
||||||
interval: NodeJS.Timeout;
|
interval: NodeJS.Timeout;
|
||||||
monitorInterval: NodeJS.Timeout;
|
stopMonitoring: () => void;
|
||||||
problemId: string;
|
problemId: string;
|
||||||
start: Date;
|
start: Date;
|
||||||
id: string;
|
id: string;
|
||||||
@@ -48,6 +48,7 @@ export async function startRun(
|
|||||||
id: runId,
|
id: runId,
|
||||||
problemId,
|
problemId,
|
||||||
start: new Date(),
|
start: new Date(),
|
||||||
|
stopMonitoring: startMonitoring(runId),
|
||||||
interval: setInterval(() => {
|
interval: setInterval(() => {
|
||||||
const elapsed = Date.now() - start;
|
const elapsed = Date.now() - start;
|
||||||
|
|
||||||
@@ -59,7 +60,6 @@ export async function startRun(
|
|||||||
.toString()
|
.toString()
|
||||||
.padStart(2, "0")}`;
|
.padStart(2, "0")}`;
|
||||||
}, 50),
|
}, 50),
|
||||||
monitorInterval: startMonitoring(runId),
|
|
||||||
};
|
};
|
||||||
|
|
||||||
await vscode.commands.executeCommand("setContext", "devrun.runActive", true);
|
await vscode.commands.executeCommand("setContext", "devrun.runActive", true);
|
||||||
@@ -68,8 +68,6 @@ export async function startRun(
|
|||||||
export async function stopRun() {
|
export async function stopRun() {
|
||||||
if (currentRun) {
|
if (currentRun) {
|
||||||
clearInterval(currentRun.interval);
|
clearInterval(currentRun.interval);
|
||||||
clearInterval(currentRun.monitorInterval);
|
|
||||||
|
|
||||||
submitRun(currentRun.id);
|
submitRun(currentRun.id);
|
||||||
|
|
||||||
currentRun = null;
|
currentRun = null;
|
||||||
|
|||||||
Reference in New Issue
Block a user