feat: updated callback according to new API and added debug console logs

This commit is contained in:
frelodev 2024-08-24 20:38:07 +02:00
parent f5261b6a71
commit 3dca7476a7
3 changed files with 144 additions and 96 deletions

View file

@ -1,8 +1,8 @@
{ {
"name": "codempvscode", "name": "codemp",
"displayName": "codempvscode", "displayName": "codemp",
"description": "", "description": "",
"repository": "https://github.com/codewithotherpeopleandchangenamelater/codemp-vscode", "repository": "",
"version": "0.0.1", "version": "0.0.1",
"napi": { "napi": {
"name": "index" "name": "index"
@ -20,35 +20,39 @@
"contributes": { "contributes": {
"commands": [ "commands": [
{ {
"command": "codempvscode.connect", "command": "codemp.connect",
"title": "Connect to a codemp host" "title": "Connect to a codemp host"
}, },
{ {
"command": "codempvscode.createBuffer", "command": "codemp.join",
"title": "Join a codemp Workspace"
},
{
"command": "codemp.createBuffer",
"title": "Create a codemp buffer" "title": "Create a codemp buffer"
}, },
{ {
"command": "codempvscode.listBuffers", "command": "codemp.listBuffers",
"title": "List all buffers of joined Workspace" "title": "List all buffers of joined Workspace"
}, },
{ {
"command": "codempvscode.attach", "command": "codemp.attach",
"title": "attach to a codemp workspace" "title": "attach to a codemp workspace"
}, },
{ {
"command": "codempvscode.disconnectBuffer", "command": "codemp.disconnectBuffer",
"title": "disconnect from a codemp Buffer (unused)" "title": "disconnect from a codemp Buffer (unused)"
}, },
{ {
"command": "codempvscode.sync", "command": "codemp.sync",
"title": "Sync the current buffer" "title": "Sync the current buffer"
}, },
{ {
"command": "codempvscode.printOpCache", "command": "codemp.printOpCache",
"title": "Print current OpCache" "title": "Print current OpCache"
}, },
{ {
"command": "codempvscode.helloWorld", "command": "codemp.helloWorld",
"title": "Hello World (debug)" "title": "Hello World (debug)"
} }
] ]
@ -76,8 +80,8 @@
"typescript": "^5.1.6" "typescript": "^5.1.6"
}, },
"dependencies": { "dependencies": {
"@codemp/codemp" : "0.0.4", "@codemp/codemp": "0.0.8-debug",
"@vscode/vsce": "^2.22.0", "@vscode/vsce": "^2.32.0",
"npx": "^10.2.2" "npx": "^10.2.2"
} }
} }

View file

@ -1,3 +1,5 @@
import * as vscode from 'vscode'; import * as vscode from 'vscode';
import * as codemp from '@codemp/codemp'; // TODO why won't it work with a custom name??? import * as codemp from '@codemp/codemp'; // TODO why won't it work with a custom name???
import * as mapping from "./mapping"; import * as mapping from "./mapping";
@ -7,32 +9,35 @@ import { LOGGER } from './extension';
let CACHE = new codemp.OpCache(); let CACHE = new codemp.OpCache();
let MAPPINGS = new mapping.BufferMappingContainer(); let MAPPINGS = new mapping.BufferMappingContainer();
let smallNumberDecorationType = vscode.window.createTextEditorDecorationType({}); let smallNumberDecorationType = vscode.window.createTextEditorDecorationType({});
let client : codemp.Client | null = null; let client: codemp.Client | null = null;
let workspace : codemp.Workspace | null = null; let workspace: codemp.Workspace | null = null;
let username : string; let username: string = "";
export async function connect() {
let username = await vscode.window.showInputBox({ prompt: "enter username" });
export async function connect(){ if (username === null) throw "choose an username";
let username = await vscode.window.showInputBox({prompt: "enter username"});
if(username===null) throw "choose an username";
client = await codemp.connect("http://codemp.alemi.dev:50053", username!, "lmaodefaultpassword"); client = await codemp.connect("http://codemp.alemi.dev:50053", username!, "lmaodefaultpassword");
} }
export async function join() { export async function join() {
let workspace_id = await vscode.window.showInputBox({prompt: "workspace to attach (default to default)"}); let workspace_id = await vscode.window.showInputBox({ prompt: "workspace to attach (default to default)" });
//let editor = vscode.window.activeTextEditor; //let editor = vscode.window.activeTextEditor;
if (workspace_id === undefined) return // user cancelled with ESC if (workspace_id === undefined) return // user cancelled with ESC
if (workspace_id.length == 0) workspace_id = "asd" if (workspace_id.length == 0) workspace_id = "diamond"
if(client===null) throw "connect first"; if (client === null) throw "connect first";
workspace = await client.join_workspace(workspace_id) workspace = await client.join_workspace(workspace_id)
let controller = workspace.cursor(); let controller = workspace.cursor();
controller.callback((event: codemp.Cursor) => { controller.callback(async function (controller: codemp.CursorController) {
let range_start : vscode.Position = new vscode.Position(event.startRow , event.startCol); // -1? while (true) {
let range_end : vscode.Position = new vscode.Position(event.endRow, event.endCol); // -1? idk if this works it's kinda funny, should test with someone with a working version of codemp console.log("debug 12");
let event = await controller.try_recv();
console.log("debug 13");
if (event === null) break;
let range_start: vscode.Position = new vscode.Position(event.startRow, event.startCol); // -1?
let range_end: vscode.Position = new vscode.Position(event.endRow, event.endCol); // -1? idk if this works it's kinda funny, should test with someone with a working version of codemp
const decorationRange = new vscode.Range(range_start, range_end); const decorationRange = new vscode.Range(range_start, range_end);
smallNumberDecorationType.dispose(); smallNumberDecorationType.dispose();
smallNumberDecorationType = vscode.window.createTextEditorDecorationType({ smallNumberDecorationType = vscode.window.createTextEditorDecorationType({
@ -49,29 +54,32 @@ export async function join() {
borderColor: 'lightblue' //should create this color based on event.user (uuid) borderColor: 'lightblue' //should create this color based on event.user (uuid)
} }
}); });
console.log("debug 14");
let m = MAPPINGS.get_by_buffer(event.buffer); let m = MAPPINGS.get_by_buffer(event.buffer);
if (m===null) return; if (m === null) return;
m.editor.setDecorations(smallNumberDecorationType, [decorationRange]); m.editor.setDecorations(smallNumberDecorationType, [decorationRange]);
}
}); });
vscode.window.onDidChangeTextEditorSelection((event: vscode.TextEditorSelectionChangeEvent) => { vscode.window.onDidChangeTextEditorSelection((event: vscode.TextEditorSelectionChangeEvent) => {
if (event.kind == vscode.TextEditorSelectionChangeKind.Command) return; // TODO commands might move cursor too if (event.kind == vscode.TextEditorSelectionChangeKind.Command) return; // TODO commands might move cursor too
let buf = event.textEditor.document.uri; let buf = event.textEditor.document.uri;
let selection : vscode.Selection = event.selections[0] // TODO there may be more than one cursor!! let selection: vscode.Selection = event.selections[0] // TODO there may be more than one cursor!!
let anchor : [number, number] = [selection.anchor.line, selection.anchor.character]; let anchor: [number, number] = [selection.anchor.line, selection.anchor.character];
let position : [number, number] = [selection.active.line, selection.active.character+1]; let position: [number, number] = [selection.active.line, selection.active.character + 1];
let n = MAPPINGS.get_by_editor(buf) let n = MAPPINGS.get_by_editor(buf)
if (n===null) return; if (n === null) return;
let cursor : codemp.Cursor = { let cursor: codemp.Cursor = {
startRow: selection.anchor.line, startRow: selection.anchor.line,
startCol: selection.anchor.character, startCol: selection.anchor.character,
endRow: selection.active.line, endRow: selection.active.line,
endCol: selection.active.character+1, endCol: selection.active.character + 1,
buffer: n.buffer.get_name(), buffer: n.buffer.get_name(),
user: username user: username
} }
console.log("Crashing?")
controller.send(cursor); controller.send(cursor);
}); });
console.log("workspace id \n"); console.log("workspace id \n");
@ -81,8 +89,8 @@ export async function join() {
export async function createBuffer() { export async function createBuffer() {
let bufferName : any = (await vscode.window.showInputBox({prompt: "path of the buffer to create"}))!; let bufferName: any = (await vscode.window.showInputBox({ prompt: "path of the buffer to create" }))!;
if(workspace===null) throw "join a workspace first" if (workspace === null) throw "join a workspace first"
workspace.create(bufferName); workspace.create(bufferName);
console.log("new buffer created ", bufferName, "\n"); console.log("new buffer created ", bufferName, "\n");
} }
@ -92,17 +100,18 @@ export async function createBuffer() {
export async function attach() { export async function attach() {
let buffer_name : any = (await vscode.window.showInputBox({prompt: "buffer to attach to"}))!; let buffer_name: any = (await vscode.window.showInputBox({ prompt: "buffer to attach to" }))!;
if(workspace===null) throw "join a workspace first" if (workspace === null) throw "join a workspace first"
let buffer : codemp.BufferController = await workspace.attach(buffer_name); let buffer: codemp.BufferController = await workspace.attach(buffer_name);
console.log("attached to buffer", buffer_name); console.log("attached to buffer", buffer_name);
console.log("buffer", buffer); console.log("buffer", buffer);
let editor = vscode.window.activeTextEditor; let editor = vscode.window.activeTextEditor;
console.log("debug 1");
if (editor === undefined) { if (editor === undefined) {
console.log("debug 2");
let fileUri = buffer_name; let fileUri = buffer_name;
let random = (Math.random() + 1).toString(36).substring(2); let random = (Math.random() + 1).toString(36).substring(2);
const fileName = ''+ random ; const fileName = '' + random;
//const newFileUri = vscode.Uri.file(fileName).with({ scheme: 'untitled', path: fileName }); //const newFileUri = vscode.Uri.file(fileName).with({ scheme: 'untitled', path: fileName });
//Create a document not a file so it's temp and it doesn't get saved //Create a document not a file so it's temp and it doesn't get saved
@ -113,30 +122,56 @@ export async function attach() {
//vscode.window.showInformationMessage(`Open a file first`); //vscode.window.showInformationMessage(`Open a file first`);
//return; //return;
} }
console.log("debug 3");
editor = vscode.window.activeTextEditor!; editor = vscode.window.activeTextEditor!;
console.log("debug 4");
//console.log("Buffer = ", buffer, "\n"); //console.log("Buffer = ", buffer, "\n");
vscode.window.showInformationMessage(`Connected to codemp workspace buffer @[${buffer_name}]`); vscode.window.showInformationMessage(`Connected to codemp workspace buffer @[${buffer_name}]`);
let file_uri : vscode.Uri = editor.document.uri; let file_uri: vscode.Uri = editor.document.uri;
MAPPINGS.put(new mapping.BufferMapping(buffer, editor)); MAPPINGS.put(new mapping.BufferMapping(buffer, editor));
console.log("debug 5");
let bufferContent = await buffer.content(); //Temp fix for content not being applied when attached
console.log("Crashed after content ")
vscode.workspace.onDidChangeTextDocument((event:vscode.TextDocumentChangeEvent) => {
let range = new vscode.Range(
editor.document.positionAt(0),
editor.document.positionAt(0)
);
CACHE.put(buffer_name, 0, bufferContent, 0)
editor.edit(editBuilder => {
editBuilder
.replace(range, bufferContent)
});
vscode.workspace.onDidChangeTextDocument((event: vscode.TextDocumentChangeEvent) => {
console.log("debug 6");
if (event.document.uri != file_uri) return; // ? if (event.document.uri != file_uri) return; // ?
for (let change of event.contentChanges) { for (let change of event.contentChanges) {
if (CACHE.get(buffer_name, change.rangeOffset, change.text, change.rangeOffset + change.rangeLength)) continue; let tmp = CACHE.get(buffer_name, change.rangeOffset, change.text, change.rangeOffset + change.rangeLength)
console.log("CACHE DUMPP", tmp);
if (tmp) continue; // Remove TMP is for debug
console.log("debug 7");
LOGGER.info(`onDidChangeTextDocument(event: [${change.rangeOffset}, ${change.text}, ${change.rangeOffset + change.rangeLength}])`); LOGGER.info(`onDidChangeTextDocument(event: [${change.rangeOffset}, ${change.text}, ${change.rangeOffset + change.rangeLength}])`);
buffer.send({ buffer.send({
start: change.rangeOffset, start: change.rangeOffset,
end: change.rangeOffset+change.rangeLength, end: change.rangeOffset + change.rangeLength,
content: change.text content: change.text
}); });
console.log("debug 8");
} }
}); });
buffer.callback((event: codemp.TextChange) => { buffer.callback(async function (controller: codemp.BufferController) {
while (true) {
console.log("debug 9");
let event = await controller.try_recv();
if (event === null) break;
LOGGER.info(`buffer.callback(event: [${event.start}, ${event.content}, ${event.end}])`) LOGGER.info(`buffer.callback(event: [${event.start}, ${event.content}, ${event.end}])`)
console.log(`console log buffer.callback(event: [${event.start}, ${event.content}, ${event.end}])`)
CACHE.put(buffer_name, event.start, event.content, event.end); CACHE.put(buffer_name, event.start, event.content, event.end);
console.log("debug 10");
if (editor === undefined) { throw "Open an editor first" } if (editor === undefined) { throw "Open an editor first" }
let range = new vscode.Range( let range = new vscode.Range(
editor.document.positionAt(event.start), editor.document.positionAt(event.start),
@ -146,7 +181,10 @@ export async function attach() {
editBuilder editBuilder
.replace(range, event.content) .replace(range, event.content)
}); });
}
}); });
} }
/*export async function disconnectBuffer() { TODO i should just set buffer=null /*export async function disconnectBuffer() { TODO i should just set buffer=null
@ -156,13 +194,13 @@ export async function attach() {
}*/ }*/
export async function sync() { export async function sync() {
if(workspace===null) throw "join a workspace first"; if (workspace === null) throw "join a workspace first";
let editor = vscode.window.activeTextEditor; let editor = vscode.window.activeTextEditor;
if (editor === undefined) throw "no active editor to sync"; if (editor === undefined) throw "no active editor to sync";
let k = MAPPINGS.get_by_editor(editor.document.uri); let k = MAPPINGS.get_by_editor(editor.document.uri);
if(k === null) throw "No such buffer managed by codemp" if (k === null) throw "No such buffer managed by codemp"
let buffer = workspace.buffer_by_name(k.buffer.get_name()); let buffer = workspace.buffer_by_name(k.buffer.get_name());
if (buffer==null) throw "This buffer does not exist anymore"; if (buffer == null) throw "This buffer does not exist anymore";
let content = await buffer.content(); let content = await buffer.content();
let doc_len = editor.document.getText().length; let doc_len = editor.document.getText().length;
@ -175,15 +213,19 @@ export async function sync() {
editor.edit(editBuilder => editBuilder.replace(range, content)); editor.edit(editBuilder => editBuilder.replace(range, content));
} }
export async function listBuffers(){ export async function listBuffers() {
if(workspace===null) throw "join a workspace first" if (workspace === null) throw "join a workspace first"
let buffers = workspace.filetree(); let buffers = workspace.filetree();
console.log(buffers); // improve UX console.log(buffers); // improve UX
} }
export async function helloWorld() {
vscode.window.showInformationMessage("Hello World");
}
// This method is called when your extension is deactivated // This method is called when your extension is deactivated
export function deactivate() { export function deactivate() {
//Maybe i should disconnect from every workspace and buffer ??? // TODO //Maybe i should disconnect from every workspace and buffer ??? // TODO
} }

View file

@ -7,23 +7,25 @@ export let LOGGER = vscode.window.createOutputChannel("codemp", {log: true});
// extension is activated the very first time the command is executed // extension is activated the very first time the command is executed
export function activate(context: vscode.ExtensionContext) { export function activate(context: vscode.ExtensionContext) {
// Use the console to output diagnostic information (console.log) and errors (console.error) // Use the console to output diagnostic information (console.log) and errors (console.error)
console.log('Congratulations, your extension "codempvscode" is now active!'); console.log('Congratulations, your extension "codemp" is now active!');
// start codemp log poller // start codemp log poller
log_poller_task(new codemp.JsLogger(true)); // don't await it! run it in background forever log_poller_task(new codemp.JsLogger()); // don't await it! run it in background forever
// register commands: the commandId parameter must match the command field in package.json // register commands: the commandId parameter must match the command field in package.json
for (let cmd of [ for (let cmd of [
vscode.commands.registerCommand('codempvscode.connect', codemplogic.connect), vscode.commands.registerCommand('codemp.helloWorld', codemplogic.helloWorld),
vscode.commands.registerCommand('codempvscode.join', codemplogic.join), vscode.commands.registerCommand('codemp.connect', codemplogic.connect),
vscode.commands.registerCommand('codempvscode.attach', codemplogic.attach), vscode.commands.registerCommand('codemp.join', codemplogic.join),
vscode.commands.registerCommand('codempvscode.createBuffer', codemplogic.createBuffer), vscode.commands.registerCommand('codemp.attach', codemplogic.attach),
vscode.commands.registerCommand('codempvscode.listBuffers', codemplogic.listBuffers), vscode.commands.registerCommand('codemp.createBuffer', codemplogic.createBuffer),
// vscode.commands.registerCommand('codempvscode.disconnectBuffer', codemplogic.disconnectBuffer), vscode.commands.registerCommand('codemp.listBuffers', codemplogic.listBuffers),
vscode.commands.registerCommand('codempvscode.sync', codemplogic.sync), // vscode.commands.registerCommand('codemp.disconnectBuffer', codemplogic.disconnectBuffer),
vscode.commands.registerCommand('codempvscode.printOpCache', codemplogic.printOpCache) vscode.commands.registerCommand('codemp.sync', codemplogic.sync),
vscode.commands.registerCommand('codemp.printOpCache', codemplogic.printOpCache)
]) { ]) {
context.subscriptions.push(cmd); context.subscriptions.push(cmd);
console.log("registered all commands and pushed them");
} }
} }