Skip to content

Commit

Permalink
fix issue with missing activeTextEditor
Browse files Browse the repository at this point in the history
  • Loading branch information
effektsvk committed Jan 14, 2022
1 parent 52a5b61 commit 0e1e90b
Showing 1 changed file with 33 additions and 5 deletions.
38 changes: 33 additions & 5 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,23 @@
// Import the module and reference it with the alias vscode in your code below
import * as vscode from 'vscode'

const waitForValue = async <VALUE>(value: () => (VALUE | undefined), timeout: number, startTime?: number): Promise<VALUE> => {
const start = startTime ?? Date.now()
return (
new Promise((resolve, reject) => {
const resolvedValue = value()
if (resolvedValue) {
resolve(resolvedValue)
} else {
setTimeout(() => waitForValue(value, timeout, start).then(resolve), 100)
}
if (start + timeout < Date.now()) {
reject('Failed to get value in time')
}
})
)
}

// this method is called when your extension is activated
// your extension is activated the very first time the command is executed
export function activate(context: vscode.ExtensionContext) {
Expand All @@ -28,12 +45,23 @@ export function activate(context: vscode.ExtensionContext) {
console.log(`Found ${tabs} tabs`)
console.log(`Found ${tabsToClose} tabs to close`)

if (tabsToClose === 0) {
vscode.window.showInformationMessage('No tabs are open')
}

while (tabsToClose > 0) {
if (vscode.window.activeTextEditor?.document.uri.scheme === 'git' || !vscode.window.activeTextEditor?.viewColumn) {
await vscode.commands.executeCommand('workbench.action.closeActiveEditor')
tabsToClose--
} else {
await vscode.commands.executeCommand('workbench.action.nextEditor')
try {
// HACK: activeTextEditor is undefined after tab is closing
const activeTextEditor = await waitForValue(() => vscode.window.activeTextEditor, 5000)

if (vscode.window.activeTextEditor?.document.uri.scheme === 'git' || !activeTextEditor?.viewColumn) {
await vscode.commands.executeCommand('workbench.action.closeActiveEditor')
tabsToClose--
} else {
await vscode.commands.executeCommand('workbench.action.nextEditor')
}
} catch (error) {
vscode.window.showErrorMessage(`Failed to close all tabs: ${error}`)
}
}
})
Expand Down

0 comments on commit 0e1e90b

Please sign in to comment.