From 82d631d0ca891b6156f88b73da9c22b241e3df9d Mon Sep 17 00:00:00 2001 From: Saoud Rizwan <7799382+saoudrizwan@users.noreply.github.com> Date: Thu, 12 Sep 2024 04:11:19 -0400 Subject: [PATCH] Open new tab on the right if no files open --- src/extension.ts | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/extension.ts b/src/extension.ts index 0f04812..a73b861 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -2,6 +2,7 @@ // Import the module and reference it with the alias vscode in your code below import * as vscode from "vscode" import { ClaudeDevProvider } from "./providers/ClaudeDevProvider" +import delay from "delay" /* Built using https://github.com/microsoft/vscode-webview-ui-toolkit @@ -53,14 +54,21 @@ export function activate(context: vscode.ExtensionContext) { }) ) - const openClaudeDevInNewTab = () => { + const openClaudeDevInNewTab = async () => { outputChannel.appendLine("Opening Claude Dev in new tab") // (this example uses webviewProvider activation event which is necessary to deserialize cached webview, but since we use retainContextWhenHidden, we don't need to use that event) // https://github.com/microsoft/vscode-extension-samples/blob/main/webview-sample/src/extension.ts const tabProvider = new ClaudeDevProvider(context, outputChannel) //const column = vscode.window.activeTextEditor ? vscode.window.activeTextEditor.viewColumn : undefined const lastCol = Math.max(...vscode.window.visibleTextEditors.map((editor) => editor.viewColumn || 0)) - const targetCol = Math.max(lastCol + 1, 1) + + // Check if there are any visible text editors, otherwise open a new group to the right + const hasVisibleEditors = vscode.window.visibleTextEditors.length > 0 + if (!hasVisibleEditors) { + await vscode.commands.executeCommand("workbench.action.newGroupRight") + } + const targetCol = hasVisibleEditors ? Math.max(lastCol + 1, 1) : vscode.ViewColumn.Two + const panel = vscode.window.createWebviewPanel(ClaudeDevProvider.tabPanelId, "Claude Dev", targetCol, { enableScripts: true, retainContextWhenHidden: true, @@ -75,9 +83,8 @@ export function activate(context: vscode.ExtensionContext) { tabProvider.resolveWebviewView(panel) // Lock the editor group so clicking on files doesn't open them over the panel - new Promise((resolve) => setTimeout(resolve, 100)).then(() => { - vscode.commands.executeCommand("workbench.action.lockEditorGroup") - }) + await delay(100) + await vscode.commands.executeCommand("workbench.action.lockEditorGroup") } context.subscriptions.push(vscode.commands.registerCommand("claude-dev.popoutButtonTapped", openClaudeDevInNewTab))