(commandId: string, options?: { keepOpen?: boolean; exactLabelMatch?: boolean })
| 180 | } |
| 181 | |
| 182 | async runCommand(commandId: string, options?: { keepOpen?: boolean; exactLabelMatch?: boolean }): Promise<void> { |
| 183 | const keepOpen = options?.keepOpen; |
| 184 | const exactLabelMatch = options?.exactLabelMatch; |
| 185 | |
| 186 | const openCommandPalletteAndTypeCommand = async (): Promise<boolean> => { |
| 187 | // open commands picker |
| 188 | await this.openQuickAccessWithRetry(QuickAccessKind.Commands, `>${commandId}`); |
| 189 | |
| 190 | // wait for best choice to be focused |
| 191 | await this.quickInput.waitForQuickInputElementFocused(); |
| 192 | |
| 193 | // Retry for as long as the command not found |
| 194 | const text = await this.quickInput.waitForQuickInputElementText(); |
| 195 | |
| 196 | if (text === 'No matching commands' || (exactLabelMatch && text !== commandId)) { |
| 197 | return false; |
| 198 | } |
| 199 | |
| 200 | return true; |
| 201 | }; |
| 202 | |
| 203 | let hasCommandFound = await openCommandPalletteAndTypeCommand(); |
| 204 | |
| 205 | if (!hasCommandFound) { |
| 206 | |
| 207 | this.code.logger.log(`QuickAccess: No matching commands, will retry...`); |
| 208 | await this.quickInput.closeQuickInput(); |
| 209 | |
| 210 | let retries = 0; |
| 211 | while (++retries < 5) { |
| 212 | hasCommandFound = await openCommandPalletteAndTypeCommand(); |
| 213 | if (hasCommandFound) { |
| 214 | break; |
| 215 | } else { |
| 216 | this.code.logger.log(`QuickAccess: No matching commands, will retry...`); |
| 217 | await this.quickInput.closeQuickInput(); |
| 218 | await this.code.wait(1000); |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | if (!hasCommandFound) { |
| 223 | throw new Error(`QuickAccess.runCommand(commandId: ${commandId}) failed to find command.`); |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | // Wait and click on best choice. Focus can be stolen away from the |
| 228 | // quick input between opening the palette and now (e.g. by an async |
| 229 | // UI event from a previously opened editor), which causes the |
| 230 | // `waitForQuickInputOpened` inside `selectQuickInputElement` to time |
| 231 | // out. Retry the open+type+select sequence in that case. |
| 232 | let selectRetries = 0; |
| 233 | while (true) { |
| 234 | try { |
| 235 | await this.quickInput.selectQuickInputElement(0, keepOpen); |
| 236 | break; |
| 237 | } catch (err) { |
| 238 | if (++selectRetries > 3) { |
| 239 | throw err; |
no test coverage detected