fix: when attaching open local files if exist

This commit is contained in:
əlemi 2024-09-15 03:11:32 +02:00
parent 366f5aec82
commit f600465290
Signed by: alemi
GPG key ID: A4895B84D311642C

View file

@ -122,16 +122,19 @@ export async function attach(selected: vscode.TreeItem | undefined) {
let buffer: codemp.BufferController = await workspace.attach(buffer_name); let buffer: codemp.BufferController = await workspace.attach(buffer_name);
await buffer.poll(); // wait for server changes await buffer.poll(); // wait for server changes
LOGGER.info(`attached to buffer ${buffer_name}`); LOGGER.info(`attached to buffer ${buffer_name}`);
let editor = vscode.window.activeTextEditor; if (vscode.workspace.workspaceFolders === undefined) {
if (editor === undefined) { throw "no active vscode workspace";
let random = (Math.random() + 1).toString(36).substring(2);
const fileName = '' + random;
const newFileUri = vscode.Uri.file(fileName).with({ scheme: 'untitled', path: "" });
await vscode.workspace.openTextDocument(newFileUri);
vscode.commands.executeCommand('vscode.open', newFileUri);
editor = vscode.window.activeTextEditor!;
} }
vscode.window.showInformationMessage(`Connected to codemp workspace buffer @[${buffer_name}]`); let cwd = vscode.workspace.workspaceFolders[0].uri; // TODO picking the first one is a bit arbitrary
let path = vscode.Uri.file(cwd.path + '/' + buffer_name);
try {
await vscode.workspace.fs.stat(path);
} catch {
path = path.with({ scheme: 'untitled' });
}
let doc = await vscode.workspace.openTextDocument(path);
let editor = await vscode.window.showTextDocument(doc, { preserveFocus: false })
vscode.window.showInformationMessage(`Connected to buffer '${buffer_name}'`);
let file_uri: vscode.Uri = editor.document.uri; let file_uri: vscode.Uri = editor.document.uri;
mapping.bufferMapper.register(buffer.get_path(), file_uri); mapping.bufferMapper.register(buffer.get_path(), file_uri);