import * as vscode from 'vscode'; import { canActivate, tabUri } from './tabs'; /** * What the list can do to a tab. * * Everything here takes a live `vscode.Tab`. The rows the view draws are snapshots, and * acting on stale state silently does the wrong thing (or nothing at all) — an earlier * version compared a snapshot's `isPinned` and so never unpinned anything. */ /** * Brings a tab to the front by re-opening its resource in its own group. There is no API * to activate a tab directly, which is also why webview tabs cannot be focused: they * have no resource to re-open. */ export async function openTab(tab: vscode.Tab): Promise { const uri = tabUri(tab); if (!uri) { return; } const column = tab.group.viewColumn ?? vscode.ViewColumn.Active; const input = tab.input; if (input instanceof vscode.TabInputNotebook) { const notebook = await vscode.workspace.openNotebookDocument(uri); await vscode.window.showNotebookDocument(notebook, { viewColumn: column, preview: false }); return; } if (input instanceof vscode.TabInputTextDiff) { await vscode.commands.executeCommand('vscode.diff', input.original, input.modified, tab.label, { viewColumn: column, preview: false }); return; } if (input instanceof vscode.TabInputCustom) { await vscode.commands.executeCommand('vscode.openWith', uri, input.viewType, { viewColumn: column, preview: false }); return; } const doc = await vscode.workspace.openTextDocument(uri); await vscode.window.showTextDocument(doc, { viewColumn: column, preview: false }); } export async function closeTab(tab: vscode.Tab): Promise { await vscode.window.tabGroups.close(tab, true); } /** * VS Code can only pin the *active* editor, so pinning another tab means focusing it * first. The focus lands where the user clicked anyway, which is what they expect. */ export async function togglePin(tab: vscode.Tab): Promise { const pinned = !tab.isPinned; if (!tab.isActive) { if (!canActivate(tab)) { void vscode.window.showInformationMessage( `Cannot pin “${tab.label}”: this kind of tab has to be focused first.`); return; } await openTab(tab); } await vscode.commands.executeCommand( pinned ? 'workbench.action.pinEditor' : 'workbench.action.unpinEditor'); } /** Closes everything except `tab`, keeping pinned tabs the way a tab bar does. */ export async function closeOthers(tab: vscode.Tab): Promise { const others = vscode.window.tabGroups.all .flatMap(group => group.tabs) .filter(other => other !== tab && !other.isPinned); if (others.length > 0) { await vscode.window.tabGroups.close(others, true); } } export async function copyPath(tab: vscode.Tab): Promise { const uri = tabUri(tab); if (uri) { await vscode.env.clipboard.writeText(uri.fsPath); } } export async function revealInExplorer(tab: vscode.Tab): Promise { const uri = tabUri(tab); if (uri) { await vscode.commands.executeCommand('revealInExplorer', uri); } }