stable new version with logger, debug and fixed opcache

This commit is contained in:
frelodev 2024-02-14 15:42:40 +01:00
parent 03eda74d78
commit a45f5fbaa0

View file

@ -1,18 +1,11 @@
import * as vscode from 'vscode'; import * as vscode from 'vscode';
import * as codemp from '../index'; // TODO why won't it work with a custom name??? import * as codemp from '../index'; // TODO why won't it work with a custom name???
import * as mapping from "./mapping";
import { LOGGER } from './extension';
class BufferMapping {
codemp: string;
vscode: vscode.TextEditor;
constructor(codemp_path: string, editor: vscode.TextEditor) { let CACHE = new codemp.OpCache();
this.codemp = codemp_path; let MAPPINGS = new mapping.BufferMappingContainer();
this.vscode = editor;
}
}
var CACHE = new codemp.OpCache();
var BUFFERS : BufferMapping[] = [];
let smallNumberDecorationType = vscode.window.createTextEditorDecorationType({}); let smallNumberDecorationType = vscode.window.createTextEditorDecorationType({});
let client : codemp.JsCodempClient | null = null; let client : codemp.JsCodempClient | null = null;
let workspace : codemp.JsWorkspace | null = null; let workspace : codemp.JsWorkspace | null = null;
@ -39,19 +32,15 @@ export async function login(){
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 buffer : string = (await vscode.window.showInputBox({prompt: "buffer name for the file needed to update other clients cursors"}))!;
//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 = "asd"
if (buffer === undefined) return // user cancelled with ESC
if (buffer.length == 0) {workspace_id = "asd"; buffer="fucl"; }
if(client===null) throw "connect first"; if(client===null) throw "connect first";
workspace = await client.joinWorkspace(workspace_id) workspace = await client.joinWorkspace(workspace_id)
let controller = workspace.cursor(); let controller = workspace.cursor();
controller.callback((event: codemp.JsCursorEvent) => { controller.callback((event: codemp.JsCursorEvent) => {
console.log(`received cursor event, im on ${event.buffer}`)
let range_start : vscode.Position = new vscode.Position(event.start.row , event.start.col); // -1? let range_start : vscode.Position = new vscode.Position(event.start.row , event.start.col); // -1?
let range_end : vscode.Position = new vscode.Position(event.end.row, event.end.col); // -1? idk if this works it's kinda funny, should test with someone with a working version of codemp let range_end : vscode.Position = new vscode.Position(event.end.row, event.end.col); // -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);
@ -70,14 +59,10 @@ 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)
} }
}); });
for (let mapping of BUFFERS) {
console.log(`checking tuple ${mapping}`); let m = MAPPINGS.get_by_buffer(event.buffer);
if (mapping.codemp === event.buffer) { if (m===null) return;
mapping.vscode.setDecorations(smallNumberDecorationType, [decorationRange]); m.editor.setDecorations(smallNumberDecorationType, [decorationRange]);
return
}
}
console.log(`wtf buffers didn't contain it???? ${BUFFERS}`)
}); });
@ -87,11 +72,9 @@ export async function join() {
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];
for (let mapping of BUFFERS) { let n = MAPPINGS.get_by_editor(buf)
if (mapping.vscode.document.uri === buf) { if (n===null) return;
controller.send(mapping.codemp, anchor, position); controller.send(n.buffer.getName(),anchor,position);
}
}
}); });
console.log("workspace id \n"); console.log("workspace id \n");
console.log(workspace.id()); console.log(workspace.id());
@ -128,7 +111,7 @@ export async function attach() {
const newFileUri = vscode.Uri.file(fileName).with({ scheme: 'untitled', path: "" }); const newFileUri = vscode.Uri.file(fileName).with({ scheme: 'untitled', path: "" });
//vscode.workspace.openTextDocument() //vscode.workspace.openTextDocument()
await vscode.workspace.openTextDocument(newFileUri); await vscode.workspace.openTextDocument(newFileUri);
vscode.commands.executeCommand('vscode.open', newFileUri); vscode.commands.executeCommand('vscode.open', newFileUri); //It should already be opened with the api command above idk why i do this?
//vscode.window.showInformationMessage(`Open a file first`); //vscode.window.showInformationMessage(`Open a file first`);
//return; //return;
} }
@ -137,13 +120,13 @@ export async function attach() {
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;
BUFFERS.push(new BufferMapping(buffer_name, editor)); MAPPINGS.put(new mapping.BufferMapping(buffer, editor));
vscode.workspace.onDidChangeTextDocument((event:vscode.TextDocumentChangeEvent) => { vscode.workspace.onDidChangeTextDocument((event:vscode.TextDocumentChangeEvent) => {
//console.log(event.reason);
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; if (CACHE.get(buffer_name, change.rangeOffset, change.text, change.rangeOffset + change.rangeLength)) continue;
LOGGER.info(`onDidChangeTextDocument(event: [${change.rangeOffset}, ${change.text}, ${change.rangeOffset + change.rangeLength}])`);
buffer.send({ buffer.send({
span: { span: {
start: change.rangeOffset, start: change.rangeOffset,
@ -154,13 +137,11 @@ export async function attach() {
} }
}); });
//await new Promise((resolve) => setTimeout(resolve, 200)); // tonioware buffer.callback((event: codemp.JsTextChange) => {
//console.log("test"); LOGGER.info(`buffer.callback(event: [${event.span.start}, ${event.content}, ${event.span.end}])`)
buffer.callback((event: any) => {
CACHE.put(buffer_name, event.span.start, event.content, event.span.end); CACHE.put(buffer_name, event.span.start, event.content, event.span.end);
if (editor === undefined) { return } // TODO say something!!!!!! if (editor === undefined) { throw "Open an editor first" }
let range = new vscode.Range( let range = new vscode.Range(
editor.document.positionAt(event.span.start), editor.document.positionAt(event.span.start),
editor.document.positionAt(event.span.end) editor.document.positionAt(event.span.end)
@ -168,7 +149,7 @@ export async function attach() {
editor.edit(editBuilder => { editor.edit(editBuilder => {
editBuilder editBuilder
.replace(range, event.content) .replace(range, event.content)
}) });
}); });
} }
@ -179,35 +160,23 @@ export async function attach() {
}*/ }*/
export async function sync() { export async function sync() {
if(workspace===null) throw "join a workspace first";
let editor = vscode.window.activeTextEditor; let editor = vscode.window.activeTextEditor;
if (editor === undefined) { return } if (editor === undefined) throw "no active editor to sync";
for (let mapping of BUFFERS) { let k = MAPPINGS.get_by_editor(editor.document.uri);
console.log(mapping.vscode.document.uri); if(k === null) throw "No such buffer managed by codemp"
//console.log(tuple[1]); let buffer = workspace.bufferByName(k.buffer.getName());
console.log("\n"); if (buffer==null) throw "This buffer does not exist anymore";
console.log(editor?.document.uri.toString());
//console.log(BUFFERS[0]);
if (mapping.vscode.document.uri === editor?.document.uri) {
if(workspace===null) throw "join a workspace first"
let buffer = await workspace.bufferByName(mapping.codemp);
if (buffer==null) {
vscode.window.showErrorMessage("This buffer does not exist anymore");
return;
}
let content = buffer.content(); let content = buffer.content();
let doc_len = editor.document.getText().length;
let range = new vscode.Range( let range = new vscode.Range(
editor.document.positionAt(0), editor.document.positionAt(0),
editor.document.positionAt(editor.document.getText().length) editor.document.positionAt(doc_len)
); );
CACHE.put(mapping.codemp, 0, content, editor.document.getText().length); CACHE.put(k.buffer.getName(), 0, content, doc_len);
editor.edit(editBuilder => editBuilder.replace(range, content)); editor.edit(editBuilder => editBuilder.replace(range, content));
return;
}
else{
vscode.window.showErrorMessage("This buffer is not managed by codemp");
}
}
} }
export async function listBuffers(){ export async function listBuffers(){
@ -216,12 +185,13 @@ export async function listBuffers(){
console.log(buffers); // improve UX console.log(buffers); // improve UX
} }
// 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
} }
export function printOpCache() {
console.log("CACHE\n");
console.log(CACHE.toString());
}