feat: resync with action on warning

This commit is contained in:
frelodev 2024-10-01 18:35:57 +02:00
parent 21b93f9d14
commit 4f16b46223

View file

@ -8,7 +8,6 @@ import { LOGGER, provider } from '../extension';
let locks: Map<string, boolean> = new Map(); let locks: Map<string, boolean> = new Map();
let autoResync = vscode.workspace.getConfiguration('codemp').get<string>("autoResync"); let autoResync = vscode.workspace.getConfiguration('codemp').get<string>("autoResync");
export async function apply_changes_to_buffer(path: string, controller: codemp.BufferController | undefined | null, force?: boolean) { 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 (workspace === null) throw "can't apply changes while not in a workspace";
if (!controller) controller = workspace.buffer_by_name(path); 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 (event.hash !== undefined) {
if (codemp.hash(editor.document.getText()) !== event.hash) if (codemp.hash(editor.document.getText()) !== event.hash) {
if (autoResync) await resync(path, workspace, editor); if (autoResync) {
else vscode.window.showErrorMessage("Client out of sync"); 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<codemp.BufferController | undefined> { export async function attach_to_remote_buffer(buffer_name: string, set_content?: boolean): Promise<codemp.BufferController | undefined> {
@ -89,7 +95,7 @@ export async function attach_to_remote_buffer(buffer_name: string, set_content?:
if (set_content) { if (set_content) {
// make remote document match local content // make remote document match local content
let doc_len = remoteContent.length; 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 { } else {
// make local document match remote content // make local document match remote content
let doc_len = localContent.length; 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); buffer_name = mapping.bufferMapper.by_editor(editor.document.uri);
if (buffer_name === undefined) throw "No such buffer managed by codemp" if (buffer_name === undefined) throw "No such buffer managed by codemp"
} }
locks.set(buffer_name, true);
resync(buffer_name, workspace, editor); resync(buffer_name, workspace, editor);
locks.set(buffer_name, false);
} }
export async function resync(buffer_name: string, workspace: codemp.Workspace, editor: vscode.TextEditor) { 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) editor.document.positionAt(doc_len)
); );
locks.set(buffer_name, true);
await editor.edit(editBuilder => editBuilder.replace(range, content)); await editor.edit(editBuilder => editBuilder.replace(range, content));
locks.set(buffer_name, false);
} }