-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
37b58ff
commit f2eb4e7
Showing
8 changed files
with
230 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// src/editor-utils.ts | ||
import * as vscode from 'vscode'; | ||
|
||
export interface EditorRetryOptions { | ||
maxAttempts?: number; | ||
delayMs?: number; | ||
timeout?: number; | ||
} | ||
|
||
const DEFAULT_OPTIONS: EditorRetryOptions = { | ||
maxAttempts: 3, | ||
delayMs: 100, | ||
timeout: 5000 | ||
}; | ||
|
||
export class EditorTimeoutError extends Error { | ||
constructor(message: string) { | ||
super(message); | ||
this.name = 'EditorTimeoutError'; | ||
} | ||
} | ||
|
||
/** | ||
* Waits for the active editor with retries | ||
*/ | ||
export async function waitForActiveEditor(options: EditorRetryOptions = {}): Promise<vscode.TextEditor> { | ||
const opts = { ...DEFAULT_OPTIONS, ...options }; | ||
const startTime = Date.now(); | ||
|
||
for (let attempt = 1; attempt <= opts.maxAttempts!; attempt++) { | ||
const editor = vscode.window.activeTextEditor; | ||
if (editor) { | ||
return editor; | ||
} | ||
|
||
if (Date.now() - startTime > opts.timeout!) { | ||
throw new EditorTimeoutError('Timed out waiting for active editor'); | ||
} | ||
|
||
// Log retry attempts in debug mode | ||
console.log(`Waiting for active editor... Attempt ${attempt}/${opts.maxAttempts}`); | ||
|
||
await new Promise(resolve => setTimeout(resolve, opts.delayMs)); | ||
} | ||
|
||
throw new Error('No active editor found after retries'); | ||
} | ||
|
||
/** | ||
* Ensures response panel remains visible and active | ||
*/ | ||
export async function ensureResponsePanelActive(panel: vscode.TextEditor, options: EditorRetryOptions = {}): Promise<void> { | ||
const opts = { ...DEFAULT_OPTIONS, ...options }; | ||
const startTime = Date.now(); | ||
|
||
while (Date.now() - startTime < opts.timeout!) { | ||
if (!panel.document.isClosed && vscode.window.visibleTextEditors.includes(panel)) { | ||
return; | ||
} | ||
|
||
try { | ||
await vscode.window.showTextDocument(panel.document, panel.viewColumn); | ||
return; | ||
} catch (error) { | ||
console.log('Retrying to ensure response panel visibility...', error); | ||
await new Promise(resolve => setTimeout(resolve, opts.delayMs)); | ||
} | ||
} | ||
|
||
throw new EditorTimeoutError('Failed to keep response panel active'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// src/test-utils.ts | ||
import * as vscode from 'vscode'; | ||
import { waitForExtensionReady } from './utils'; | ||
|
||
export interface CleanupOptions { | ||
timeout?: number; | ||
retryDelay?: number; | ||
maxRetries?: number; | ||
} | ||
|
||
const DEFAULT_CLEANUP_OPTIONS: CleanupOptions = { | ||
timeout: 1000, | ||
retryDelay: 100, | ||
maxRetries: 3 | ||
}; | ||
|
||
/** | ||
* Thorough cleanup of VS Code resources | ||
*/ | ||
export async function thoroughCleanup(options: CleanupOptions = {}): Promise<void> { | ||
const opts = { ...DEFAULT_CLEANUP_OPTIONS, ...options }; | ||
const startTime = Date.now(); | ||
|
||
// First attempt normal cleanup | ||
await vscode.commands.executeCommand('workbench.action.closeAllEditors'); | ||
await waitForExtensionReady(opts.retryDelay); | ||
|
||
for (let attempt = 0; attempt < opts.maxRetries!; attempt++) { | ||
if (Date.now() - startTime > opts.timeout!) { | ||
console.warn('Cleanup timeout reached'); | ||
break; | ||
} | ||
|
||
// Force close any remaining editors | ||
if (vscode.window.visibleTextEditors.length > 0) { | ||
await vscode.commands.executeCommand('workbench.action.closeAllEditors'); | ||
await waitForExtensionReady(opts.retryDelay); | ||
} | ||
|
||
// Explicit disposal of tab resources | ||
vscode.window.tabGroups.all.forEach(group => { | ||
group.tabs.forEach(tab => { | ||
try { | ||
if (tab.input && typeof tab.input === 'object' && 'dispose' in tab.input) { | ||
(tab.input as { dispose: () => void }).dispose(); | ||
} | ||
} catch (error) { | ||
console.warn('Tab disposal error:', error); | ||
} | ||
}); | ||
}); | ||
|
||
// Force garbage collection if available | ||
if (global.gc) { | ||
global.gc(); | ||
} | ||
|
||
// Break if everything is cleaned up | ||
if (vscode.window.visibleTextEditors.length === 0 && | ||
vscode.window.tabGroups.all.every(group => group.tabs.length === 0)) { | ||
break; | ||
} | ||
|
||
await waitForExtensionReady(opts.retryDelay); | ||
} | ||
} |
Oops, something went wrong.