in HuggingChat-Mac/Models/AccessibilityHelpers.swift [37:67]
func getActiveEditorContent() async throws -> VSCodeContent {
// Try each port until we find an active window
for port in portRange {
do {
let window = try await getWindowContent(port: port)
if window.isFocused {
return VSCodeContent(
content: window.content,
selectedText: window.selectedText,
language: window.language,
fileName: window.fileName
)
}
} catch {
continue
}
}
// If no focused window found, get the most recently focused one
let windows = try await getAllWindows()
guard let mostRecent = windows.max(by: { $0.lastFocusTime < $1.lastFocusTime }) else {
throw AccessibilityError.noActiveWindow
}
return VSCodeContent(
content: mostRecent.content,
selectedText: mostRecent.selectedText,
language: mostRecent.language,
fileName: mostRecent.fileName
)
}