Simple live moves
This commit is contained in:
+15
-1
@@ -1,4 +1,5 @@
|
|||||||
import axios from "axios";
|
import axios from "axios";
|
||||||
|
import { LiveRunMove } from "./types";
|
||||||
|
|
||||||
var cookies: string | undefined;
|
var cookies: string | undefined;
|
||||||
|
|
||||||
@@ -40,4 +41,17 @@ export async function submitRun(runId: string) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function addRunMoves() {}
|
export async function addRunMoves(runId: string, moves: LiveRunMove[]) {
|
||||||
|
await axios.post(
|
||||||
|
"http://localhost:3000/api/run/move",
|
||||||
|
{
|
||||||
|
runId,
|
||||||
|
moves,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
headers: {
|
||||||
|
Cookie: cookies,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|||||||
+65
@@ -0,0 +1,65 @@
|
|||||||
|
import * as vscode from "vscode";
|
||||||
|
import { LiveRunMove } from "./types";
|
||||||
|
import { addRunMoves } from "./client";
|
||||||
|
import { randomInt } from "crypto";
|
||||||
|
|
||||||
|
let lastText = "";
|
||||||
|
let lastCursor = 0;
|
||||||
|
let moveId = 0;
|
||||||
|
|
||||||
|
export function startMonitoring(runId: string) {
|
||||||
|
lastText = "";
|
||||||
|
|
||||||
|
return setInterval(() => {
|
||||||
|
const editor = vscode.window.activeTextEditor;
|
||||||
|
if (!editor) return;
|
||||||
|
|
||||||
|
const currentText = editor.document.getText();
|
||||||
|
const cursorOffset = editor.document.offsetAt(editor.selection.active);
|
||||||
|
|
||||||
|
let change: { from: number; to: number; insert: string } | undefined;
|
||||||
|
|
||||||
|
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,
|
||||||
|
changes: change,
|
||||||
|
moveId,
|
||||||
|
};
|
||||||
|
|
||||||
|
if (move.changes || move.cursor !== lastCursor) {
|
||||||
|
addRunMoves(runId, [move]);
|
||||||
|
}
|
||||||
|
|
||||||
|
lastCursor = move.cursor;
|
||||||
|
}, 1000);
|
||||||
|
}
|
||||||
+7
-1
@@ -1,8 +1,10 @@
|
|||||||
import * as vscode from "vscode";
|
import * as vscode from "vscode";
|
||||||
import { addRun, submitRun } from "./client";
|
import { addRun, submitRun } from "./client";
|
||||||
|
import { startMonitoring } from "./move";
|
||||||
|
|
||||||
export var currentRun: {
|
export var currentRun: {
|
||||||
interval: NodeJS.Timeout;
|
interval: NodeJS.Timeout;
|
||||||
|
monitorInterval: NodeJS.Timeout;
|
||||||
problemId: string;
|
problemId: string;
|
||||||
start: Date;
|
start: Date;
|
||||||
id: string;
|
id: string;
|
||||||
@@ -57,6 +59,7 @@ 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);
|
||||||
@@ -64,8 +67,11 @@ export async function startRun(
|
|||||||
|
|
||||||
export async function stopRun() {
|
export async function stopRun() {
|
||||||
if (currentRun) {
|
if (currentRun) {
|
||||||
clearTimeout(currentRun.interval);
|
clearInterval(currentRun.interval);
|
||||||
|
clearInterval(currentRun.monitorInterval);
|
||||||
|
|
||||||
submitRun(currentRun.id);
|
submitRun(currentRun.id);
|
||||||
|
|
||||||
currentRun = null;
|
currentRun = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,4 +6,5 @@ export interface LiveRunMove {
|
|||||||
to: number;
|
to: number;
|
||||||
insert: string;
|
insert: string;
|
||||||
};
|
};
|
||||||
|
moveId: number;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user