feat: display the users current cursor position in the tree view

This commit is contained in:
frelodev 2024-09-24 19:15:28 +02:00
parent c6c2ac7558
commit f5d2091891
3 changed files with 20 additions and 8 deletions

View file

@ -67,15 +67,15 @@ export async function join(selected: vscode.TreeItem | undefined) {
LOGGER.warn(`Skipping cursor event without user: ${event}`)
continue;
}
let mapp = mapping.colors_cache.get(event.user)
let mapp = mapping.colors_cache.get(event.user);
if (mapp === undefined) { // first time we see this user
mapp = new mapping.UserDecoration(event);
mapping.colors_cache.set(event.user, mapp);
provider.refresh();
}
let editor = mapping.bufferMapper.by_buffer(event.buffer);
if (editor !== undefined) {
mapp.apply(editor, event);
}
mapp.update(event,editor);
/*if(event.buffer!=mapp.buffer) */provider.refresh();
}
});
@ -166,6 +166,7 @@ export async function share(selected: vscode.TreeItem | undefined) {
if (locks.get(buffer_name)) { return }
if (event.document.uri !== file_uri) return; // ?
for (let change of event.contentChanges) {
console.log(event.contentChanges);
LOGGER.debug(`onDidChangeTextDocument(event: [${change.rangeOffset}, ${change.text}, ${change.rangeOffset + change.rangeLength}])`);
await buffer.send({
start: change.rangeOffset,

View file

@ -24,34 +24,40 @@ class BufferMapper {
public static instance = new BufferMapper();
}
// TODO rename maybe? mapper.bufferMapper is a little bit overkill
export let bufferMapper = BufferMapper.instance;
export class UserDecoration {
decoration: vscode.TextEditorDecorationType | null;
color: string;
buffer: string;
public constructor(event: codemp.Cursor) {
let hash = codemp.hash(event.user || "anon");
this.color = colors[hash % colors.length];
this.decoration = null;
this.buffer = event.buffer;
}
// TODO can we avoid disposing and recreating the decoration type every time?
public apply(editor: vscode.TextEditor, event: codemp.Cursor) {
public update(event: codemp.Cursor, editor?: vscode.TextEditor) {
this.buffer=event.buffer;
if (this.decoration == null) {
this.decoration = vscode.window.createTextEditorDecorationType({
borderWidth: '1px',
borderStyle: 'solid',
borderColor: this.color,
backgroundColor: this.color + '44', // add alpha
});
}
const range_start: vscode.Position = new vscode.Position(event.startRow, event.startCol); // -1?
const range_end: vscode.Position = new vscode.Position(event.endRow, event.endCol); // -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);
editor.setDecorations(this.decoration, [decorationRange]);
if(editor !== undefined) editor.setDecorations(this.decoration, [decorationRange]);
}
public clear() {

View file

@ -41,8 +41,13 @@ export class CodempTreeProvider implements vscode.TreeDataProvider<CodempTreeIte
case Type.UserList:
let out = [];
for (let x of colors_cache.keys()){
out.push(new CodempTreeItem(x, Type.User, false));
/*colors_cache.forEach(function(x){
out.push(new CodempTreeItem(x.color, Type.User, false));
});*/
for (let x of colors_cache){
out.push(new CodempTreeItem(`${x[0]}(${x[1].buffer})`, Type.User, false));
};
return out;
case Type.Buffer: