func getActiveEditorContent()

in HuggingChat-Mac/Models/AccessibilityHelpers.swift [142:188]


    func getActiveEditorContent() async -> EditorContent? {
        guard let app = NSWorkspace.shared.frontmostApplication,
              let windowElement = getFocusedWindow(for: app) else {
            return nil
        }
        
        async let icon = getApplicationIcon(for: app)
        
        if let bundleId = app.bundleIdentifier,
           supportedApps.contains(bundleId) {
            do {
                // For VSCode, use the VSCodeReader
                if bundleId == "com.microsoft.VSCode" {
                    let vsContent = try await VSCodeReader.shared.getActiveEditorContent()
                    return EditorContent(
                        fullText: vsContent.content,
                        selectedText: vsContent.selectedText,
                        applicationName: app.localizedName,
                        bundleIdentifier: bundleId,
                        applicationIcon: await icon
                    )
                } else {
                    async let fullText = getFullText(from: windowElement)
                    async let selectedText = getSelectedText(from: windowElement)
                    
                    return EditorContent(
                        fullText: (try await fullText) ?? "",
                        selectedText: await selectedText,
                        applicationName: app.localizedName,
                        bundleIdentifier: app.bundleIdentifier,
                        applicationIcon: await icon
                    )
                }
            } catch {
                print("Error getting editor content: \(error)")
                return nil
            }
        } else {
            return await EditorContent(
                fullText: "",
                selectedText: nil,
                applicationName: app.localizedName,
                bundleIdentifier: app.bundleIdentifier,
                applicationIcon: icon
            )
        }
    }