From 4f16b462235e6be4ad28961a0c29aa850019126d Mon Sep 17 00:00:00 2001 From: frelodev Date: Tue, 1 Oct 2024 18:35:57 +0200 Subject: [PATCH] feat: resync with action on warning --- src/commands/buffers.ts | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/src/commands/buffers.ts b/src/commands/buffers.ts index 3196fa2..594d4c4 100644 --- a/src/commands/buffers.ts +++ b/src/commands/buffers.ts @@ -8,7 +8,6 @@ import { LOGGER, provider } from '../extension'; let locks: Map = new Map(); let autoResync = vscode.workspace.getConfiguration('codemp').get("autoResync"); - export async function apply_changes_to_buffer(path: string, controller: codemp.BufferController | undefined | null, force?: boolean) { if (workspace === null) throw "can't apply changes while not in a workspace"; if (!controller) controller = workspace.buffer_by_name(path); @@ -34,12 +33,19 @@ export async function apply_changes_to_buffer(path: string, controller: codemp.B }); if (event.hash !== undefined) { - if (codemp.hash(editor.document.getText()) !== event.hash) - if (autoResync) await resync(path, workspace, editor); - else vscode.window.showErrorMessage("Client out of sync"); + if (codemp.hash(editor.document.getText()) !== event.hash) { + if (autoResync) { + vscode.window.showWarningMessage("Out of Sync, resynching..."); + await resync(path, workspace, editor); + } else { + controller.clear_callback(); + const selection = await vscode.window.showWarningMessage('Out of Sync', 'Resync'); + if (selection !== undefined && workspace) await resync(path, workspace, editor); + } + } } + locks.set(path, false); } - locks.set(path, false); } export async function attach_to_remote_buffer(buffer_name: string, set_content?: boolean): Promise { @@ -89,7 +95,7 @@ export async function attach_to_remote_buffer(buffer_name: string, set_content?: if (set_content) { // make remote document match local content let doc_len = remoteContent.length; - await buffer.send({ start: 0, end: doc_len, content: localContent}); + await buffer.send({ start: 0, end: doc_len, content: localContent }); } else { // make local document match remote content let doc_len = localContent.length; @@ -177,7 +183,10 @@ export async function sync(selected: vscode.TreeItem | undefined) { buffer_name = mapping.bufferMapper.by_editor(editor.document.uri); if (buffer_name === undefined) throw "No such buffer managed by codemp" } + + locks.set(buffer_name, true); resync(buffer_name, workspace, editor); + locks.set(buffer_name, false); } export async function resync(buffer_name: string, workspace: codemp.Workspace, editor: vscode.TextEditor) { @@ -192,8 +201,6 @@ export async function resync(buffer_name: string, workspace: codemp.Workspace, e editor.document.positionAt(doc_len) ); - locks.set(buffer_name, true); await editor.edit(editBuilder => editBuilder.replace(range, content)); - locks.set(buffer_name, false); }