chore: cleaned up a bit, refactored

This commit is contained in:
əlemi 2024-09-06 14:15:34 +02:00
parent fc6e01b892
commit 12d30b7e6d
Signed by: alemi
GPG key ID: A4895B84D311642C
3 changed files with 24 additions and 44 deletions

View file

@ -1,5 +1,5 @@
import * as vscode from 'vscode'; import * as vscode from 'vscode';
import * as codemp from '@codemp/codemp'; import * as codemp from 'codemp';
import * as mapping from "./mapping"; import * as mapping from "./mapping";
import { LOGGER } from './extension'; import { LOGGER } from './extension';
@ -7,7 +7,6 @@ import { LOGGER } from './extension';
let CACHE = new codemp.OpCache(); let CACHE = new codemp.OpCache();
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 mine : boolean; let mine : boolean;
export async function connect() { export async function connect() {
@ -15,9 +14,7 @@ export async function connect() {
let server : string = config.get("server", "http://codemp.dev:50053"); let server : string = config.get("server", "http://codemp.dev:50053");
let username : string = config.get("username")!; let username : string = config.get("username")!;
let password : string = config.get("password")!; let password : string = config.get("password")!;
console.log(server,username,password);
client = await codemp.connect(server, username, password); client = await codemp.connect(server, username, password);
console.log(client);
} }
@ -33,7 +30,7 @@ export async function join() {
let event = await controller.try_recv(); let event = await controller.try_recv();
if (event === null) break; if (event === null) break;
if (event.user === undefined) { if (event.user === undefined) {
console.log("Skipping cursor without user not found", event) LOGGER.warn(`Skipping cursor event without user: ${event}`)
continue; continue;
} }
let mapp = mapping.colors_cache.get(event.user) let mapp = mapping.colors_cache.get(event.user)
@ -53,8 +50,6 @@ export async function join() {
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] let selection: vscode.Selection = event.selections[0]
let anchor: [number, number] = [selection.anchor.line, selection.anchor.character];
let position: [number, number] = [selection.active.line, selection.active.character + 1];
let buffer = mapping.bufferMapper.by_editor(buf) let buffer = mapping.bufferMapper.by_editor(buf)
if (buffer === undefined) return; if (buffer === undefined) return;
let cursor: codemp.Cursor = { let cursor: codemp.Cursor = {
@ -75,19 +70,15 @@ 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"); vscode.window.showInformationMessage(`new buffer created :${bufferName}`);
} }
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); LOGGER.info(`attached to buffer ${buffer_name}`);
console.log("buffer", buffer);
let editor = vscode.window.activeTextEditor; let editor = vscode.window.activeTextEditor;
if (editor === undefined) { if (editor === undefined) {
let fileUri = buffer_name; let fileUri = buffer_name;
@ -119,22 +110,19 @@ export async function attach() {
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}])`); LOGGER.debug(`onDidChangeTextDocument(event: [${change.rangeOffset}, ${change.text}, ${change.rangeOffset + change.rangeLength}])`);
console.log("Sending buffer event");
await buffer.send({ await 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("Buffer event sent");
} }
}); });
buffer.callback(async (controller: codemp.BufferController) => { buffer.callback(async (controller: codemp.BufferController) => {
while (true) { while (true) {
let event = await controller.try_recv(); let event = await controller.try_recv();
if (event === null) break; if (event === null) break;
LOGGER.info(`buffer.callback(event: [${event.start}, ${event.content}, ${event.end}])`) LOGGER.debug(`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);
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(
@ -176,7 +164,7 @@ export async function sync() {
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 vscode.window.showInformationMessage(buffers.join("\n"))
} }
@ -199,12 +187,10 @@ export async function listWorkspaces(){
return; return;
} }
let result = await client.list_workspaces(true, true); let result = await client.list_workspaces(true, true);
console.log(result); vscode.window.showInformationMessage(result.join("\n"))
} }
// 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

@ -1,40 +1,34 @@
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';
import * as codemplogic from './codemp'; import * as commands from './commands';
export let LOGGER = vscode.window.createOutputChannel("codemp", { log: true }); 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)
console.log('Congratulations, your extension "codemp" is now active!');
// start codemp log poller // start codemp log poller
log_poller_task(new codemp.JsLogger()); // 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('codemp.connect', codemplogic.connect), vscode.commands.registerCommand('codemp.connect', commands.connect),
vscode.commands.registerCommand('codemp.join', codemplogic.join), vscode.commands.registerCommand('codemp.join', commands.join),
vscode.commands.registerCommand('codemp.attach', codemplogic.attach), vscode.commands.registerCommand('codemp.attach', commands.attach),
vscode.commands.registerCommand('codemp.createWorkspace', codemplogic.createWorkspace), vscode.commands.registerCommand('codemp.createWorkspace', commands.createWorkspace),
vscode.commands.registerCommand('codemp.listWorkspaces', codemplogic.listWorkspaces), vscode.commands.registerCommand('codemp.listWorkspaces', commands.listWorkspaces),
vscode.commands.registerCommand('codemp.createBuffer', codemplogic.createBuffer), vscode.commands.registerCommand('codemp.createBuffer', commands.createBuffer),
vscode.commands.registerCommand('codemp.listBuffers', codemplogic.listBuffers), vscode.commands.registerCommand('codemp.listBuffers', commands.listBuffers),
vscode.commands.registerCommand('codemp.sync', codemplogic.sync), vscode.commands.registerCommand('codemp.sync', commands.sync),
]) { ]) {
context.subscriptions.push(cmd); context.subscriptions.push(cmd);
console.log("registered all commands and pushed them");
} }
} }
async function log_poller_task(logger: codemp.JsLogger) { async function log_poller_task(logger: codemp.JsLogger) {
console.log("starting logger task");
while (true) { while (true) {
let message = await logger.message(); let message = await logger.message();
if (message === null) break; if (message === null) break;
console.log(message);
LOGGER.info(message); LOGGER.info(message);
console.log(message);
} }
console.log("stopping logger task");
} }

View file

@ -1,5 +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';
class BufferMapper { class BufferMapper {
bufferToEditorMapping: Map<string, vscode.TextEditor> = new Map(); bufferToEditorMapping: Map<string, vscode.TextEditor> = new Map();