fix: changed follow impl

Co-authored-by: alemi <me@alemi.dev>
This commit is contained in:
frelodev 2024-10-12 23:07:16 +02:00
parent dfe0650516
commit 4bc9d27a32
4 changed files with 19 additions and 28 deletions

View file

@ -91,11 +91,6 @@
"command": "codemp.jump", "command": "codemp.jump",
"when": "view == codemp-tree-view && viewItem == user", "when": "view == codemp-tree-view && viewItem == user",
"group": "inline" "group": "inline"
},
{
"command": "codemp.follow",
"when": "view == codemp-tree-view && viewItem == user",
"group": "inline"
} }
] ]
}, },

View file

@ -1,7 +1,7 @@
import * as vscode from 'vscode'; import * as vscode from 'vscode';
import * as codemp from 'codemp'; import * as codemp from 'codemp';
import * as mapping from "../mapping"; import * as mapping from "../mapping";
import { workspace, setWorkspace } from "./workspaces"; import { workspace, setWorkspace, follow, setFollow, executeJump } from "./workspaces";
import { LOGGER, provider } from '../extension'; import { LOGGER, provider } from '../extension';
@ -9,6 +9,7 @@ import { LOGGER, provider } from '../extension';
export let client: codemp.Client | null = null; export let client: codemp.Client | null = null;
export let workspace_list: string[] = []; export let workspace_list: string[] = [];
export let cursor_disposable: vscode.Disposable | null; export let cursor_disposable: vscode.Disposable | null;
let movedByFollow = false;
export async function connect() { export async function connect() {
let config = vscode.workspace.getConfiguration('codemp'); let config = vscode.workspace.getConfiguration('codemp');
@ -78,6 +79,11 @@ export async function join(selected: vscode.TreeItem | undefined) {
mapping.colors_cache.set(event.user, mapp); mapping.colors_cache.set(event.user, mapp);
provider.refresh(); provider.refresh();
} }
if (follow === event.user) {
movedByFollow = true;
executeJump(event.user);
}
let editor = mapping.bufferMapper.visible_by_buffer(event.buffer); let editor = mapping.bufferMapper.visible_by_buffer(event.buffer);
let refresh = event.buffer != mapp.buffer; let refresh = event.buffer != mapp.buffer;
mapp.update(event, editor); mapp.update(event, editor);
@ -88,9 +94,11 @@ export async function join(selected: vscode.TreeItem | undefined) {
let once = true; let once = true;
cursor_disposable = vscode.window.onDidChangeTextEditorSelection(async (event: vscode.TextEditorSelectionChangeEvent) => { cursor_disposable = vscode.window.onDidChangeTextEditorSelection(async (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
if (!movedByFollow) setFollow(null);
else movedByFollow = false;
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 buffer = mapping.bufferMapper.by_editor(buf) let buffer = mapping.bufferMapper.by_editor(buf);
if (buffer === undefined) { if (buffer === undefined) {
if (once) { if (once) {
await controller.send({ await controller.send({
@ -103,6 +111,7 @@ export async function join(selected: vscode.TreeItem | undefined) {
} }
once = false; once = false;
} else { } else {
await controller.send({ await controller.send({
startRow: selection.anchor.line, startRow: selection.anchor.line,
startCol: selection.anchor.character, startCol: selection.anchor.character,

View file

@ -6,13 +6,17 @@ import { LOGGER, provider } from '../extension';
export let workspace: codemp.Workspace | null = null; export let workspace: codemp.Workspace | null = null;
export let doFollow: boolean = false; export let follow: string | null = null;
export function setWorkspace(ws: codemp.Workspace | null) { export function setWorkspace(ws: codemp.Workspace | null) {
workspace = ws; workspace = ws;
} }
export function setFollow(doFollow: string | null) {
follow = doFollow;
}
export async function jump(selected: vscode.TreeItem | undefined) { export async function jump(selected: vscode.TreeItem | undefined) {
if (client === null) return vscode.window.showWarningMessage("Connect first"); if (client === null) return vscode.window.showWarningMessage("Connect first");
@ -26,6 +30,7 @@ export async function jump(selected: vscode.TreeItem | undefined) {
} }
if (!user) user = await vscode.window.showInputBox({ prompt: "username" }); if (!user) user = await vscode.window.showInputBox({ prompt: "username" });
if (!user) return; // user cancelled with ESC if (!user) return; // user cancelled with ESC
setFollow(user);
executeJump(user); executeJump(user);
} }
@ -43,23 +48,6 @@ export async function executeJump(user: string) {
editor.revealRange(cursor_range, vscode.TextEditorRevealType.InCenter); editor.revealRange(cursor_range, vscode.TextEditorRevealType.InCenter);
} }
export async function follow(selected: vscode.TreeItem | undefined) {
doFollow=!doFollow
if (client === null) return vscode.window.showWarningMessage("Connect first");
let user;
if (selected !== undefined && selected.label !== undefined) {
if (typeof (selected.label) === 'string') {
user = selected.label;
} else {
user = selected.label.label;
}
}
if (!user) user = await vscode.window.showInputBox({ prompt: "username" });
if (!user) return; // user cancelled with ESC
while(doFollow){
executeJump(user);
}
}
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" }));

View file

@ -3,7 +3,7 @@ import * as codemp from 'codemp';
import { client, connect, join, refresh, createWorkspace, inviteToWorkspace, listWorkspaces, leave } from './commands/client'; import { client, connect, join, refresh, createWorkspace, inviteToWorkspace, listWorkspaces, leave } from './commands/client';
import { CodempTreeProvider } from './tree'; import { CodempTreeProvider } from './tree';
import * as mapping from './mapping'; import * as mapping from './mapping';
import { workspace, jump, listBuffers, createBuffer, deleteBuffer, follow } from './commands/workspaces' import { workspace, jump, listBuffers, createBuffer, deleteBuffer } from './commands/workspaces'
import { attach, share, sync, apply_changes_to_buffer, detach } from './commands/buffers' import { attach, share, sync, apply_changes_to_buffer, detach } from './commands/buffers'
export let provider = new CodempTreeProvider(); export let provider = new CodempTreeProvider();
@ -46,7 +46,6 @@ export function activate(context: vscode.ExtensionContext) {
vscode.commands.registerCommand('codemp.sync', sync), vscode.commands.registerCommand('codemp.sync', sync),
vscode.commands.registerCommand('codemp.refresh', refresh), vscode.commands.registerCommand('codemp.refresh', refresh),
vscode.commands.registerCommand('codemp.jump', jump), vscode.commands.registerCommand('codemp.jump', jump),
vscode.commands.registerCommand('codemp.follow', follow),
]) { ]) {
context.subscriptions.push(cmd); context.subscriptions.push(cmd);
} }