|
| 1 | +# Xcode Integration Components Guide |
| 2 | + |
| 3 | +This guide breaks down which components in the CopilotForXcode project are needed for Xcode interaction versus Copilot-specific features. Use this when building a parallel app that needs to monitor and communicate with Xcode without Copilot functionality. |
| 4 | + |
| 5 | +## **Essential Xcode Monitoring Components** |
| 6 | + |
| 7 | +### Core Xcode State Monitoring |
| 8 | +These components provide the fundamental ability to monitor Xcode's state in real-time: |
| 9 | + |
| 10 | +#### **`Tool/Sources/XcodeInspector/`** - Central Coordinator |
| 11 | +- **`XcodeInspector.swift`** - Main coordinator that publishes Xcode state |
| 12 | +- **`Apps/XcodeAppInstanceInspector.swift`** - Xcode-specific window and document tracking |
| 13 | +- **`XcodeWindowInspector.swift`** - Window-level inspection |
| 14 | +- **`SourceEditor.swift`** - Editor content and cursor management |
| 15 | + |
| 16 | +**Published State Properties:** |
| 17 | +```swift |
| 18 | +@Published public var activeDocumentURL: URL? |
| 19 | +@Published public var activeWorkspaceURL: URL? |
| 20 | +@Published public var focusedWindow: XcodeWindowInspector? |
| 21 | +@Published public var focusedEditor: SourceEditor? |
| 22 | +@Published public var focusedElement: AXUIElement? |
| 23 | +``` |
| 24 | + |
| 25 | +#### **`Tool/Sources/ActiveApplicationMonitor/`** - App State Tracking |
| 26 | +- **`ActiveApplicationMonitor.swift`** - Tracks which applications are active/running |
| 27 | +- Uses `NSWorkspace.didActivateApplicationNotification` for real-time updates |
| 28 | +- Provides: `isActive`, `isXcode`, process identifiers, lifecycle events |
| 29 | + |
| 30 | +#### **`Tool/Sources/AXExtension/`** - Accessibility API Interface |
| 31 | +- **`AXUIElement.swift`** - Core accessibility API extensions |
| 32 | +- Provides UI element access: `selectedTextRange`, `value`, `isFocused`, `isSourceEditor` |
| 33 | +- Element navigation: `parent`, `children`, `focusedElement`, `window` |
| 34 | +- Xcode detection: `isXcodeWorkspaceWindow`, `isEditorArea` |
| 35 | + |
| 36 | +#### **`Tool/Sources/AXNotificationStream/`** - Real-time Events |
| 37 | +- **`AXNotificationStream.swift`** - Real-time accessibility event monitoring |
| 38 | +- Monitors: Focus changes, title changes, window moves/resizes, element destruction |
| 39 | +- Features: Configurable run loops, retry with backoff, permission detection |
| 40 | + |
| 41 | +#### **`Tool/Sources/AXHelper/`** - Accessibility Utilities |
| 42 | +- **`AXHelper.swift`** - Higher-level accessibility operations |
| 43 | +- Code injection, cursor management, scroll position handling |
| 44 | + |
| 45 | +### Communication Infrastructure (if needed) |
| 46 | + |
| 47 | +#### **`Tool/Sources/XPCShared/`** - XPC Communication |
| 48 | +- **`XPCServiceProtocol.swift`** - Service protocols |
| 49 | +- **`XcodeInspectorData.swift`** - Xcode state data types |
| 50 | +- **`Models.swift`** - Shared data models |
| 51 | + |
| 52 | +#### **`ExtensionService/XPCController.swift`** - Service Coordination |
| 53 | +- Anonymous XPC listener for cross-process communication |
| 54 | +- Bridge management and connection lifecycle |
| 55 | +- Data exposure API: `getXcodeInspectorData()` |
| 56 | + |
| 57 | +#### **`EditorExtension/SourceEditorExtension.swift`** - Xcode Menu Integration |
| 58 | +- Official Xcode source editor extension |
| 59 | +- Command registration for custom menu items |
| 60 | +- XPC service wake-up and communication |
| 61 | + |
| 62 | +### Supporting Infrastructure |
| 63 | + |
| 64 | +#### **`Tool/Sources/Workspace/`** - File System Management |
| 65 | +- **`Workspace.swift`** - Project and workspace management |
| 66 | +- **`WorkspacePool.swift`** - Multiple workspace handling |
| 67 | +- **File watching components** - Monitor file system changes |
| 68 | + |
| 69 | +#### **`Tool/Sources/Preferences/`** - Configuration |
| 70 | +- **`AppStorage.swift`** - UserDefaults with property wrappers |
| 71 | +- **`Keys.swift`** - Preference key definitions |
| 72 | +- **`UserDefaults.swift`** - Type-safe preference access |
| 73 | + |
| 74 | +#### **`Tool/Sources/Logger/`** - Logging |
| 75 | +- **`Logger.swift`** - Centralized logging infrastructure |
| 76 | +- **`FileLogger.swift`** - File-based logging |
| 77 | + |
| 78 | +#### **`Tool/Sources/UserDefaultsObserver/`** - Settings Observation |
| 79 | +- **`UserDefaultsObserver.swift`** - Real-time settings changes |
| 80 | + |
| 81 | +## **Copilot-Specific Components (Not Needed)** |
| 82 | + |
| 83 | +### GitHub Copilot Integration |
| 84 | +- **`Tool/Sources/GitHubCopilotService/`** - All Language Server integration |
| 85 | +- **`Tool/Sources/BuiltinExtension/`** - Copilot extension provider |
| 86 | +- **`Tool/Sources/SuggestionProvider/`** - Code suggestion services |
| 87 | +- **`Tool/Sources/SuggestionBasic/`** - Suggestion data types |
| 88 | +- **`Tool/Sources/ConversationServiceProvider/`** - Chat conversation handling |
| 89 | +- **`Tool/Sources/TelemetryService/`** - Usage telemetry |
| 90 | + |
| 91 | +### UI Components for Copilot Features |
| 92 | +- **`Core/Sources/SuggestionWidget/`** - Code suggestion UI |
| 93 | +- **`Core/Sources/ConversationTab/`** - Chat interface |
| 94 | +- **`Core/Sources/ChatService/`** - Chat functionality |
| 95 | +- **`Core/Sources/SuggestionService/`** - Suggestion management |
| 96 | +- **`Core/Sources/PromptToCodeService/`** - Code generation |
| 97 | +- **`Tool/Sources/ChatAPIService/`** - Chat API integration |
| 98 | +- **`Tool/Sources/ChatTab/`** - Chat tab management |
| 99 | + |
| 100 | +### Server Components |
| 101 | +- **`Server/`** - All Node.js/TypeScript web components |
| 102 | +- Monaco editor integration, terminal integration, webpack build |
| 103 | + |
| 104 | +## **Minimal Architecture for Xcode Monitoring** |
| 105 | + |
| 106 | +### Recommended Component Structure |
| 107 | +``` |
| 108 | +Tool/Sources/ |
| 109 | +├── ActiveApplicationMonitor/ # App state tracking |
| 110 | +├── AXExtension/ # Accessibility API |
| 111 | +├── AXNotificationStream/ # Real-time events |
| 112 | +├── AXHelper/ # AX utilities |
| 113 | +├── XcodeInspector/ # Central coordinator |
| 114 | +├── XPCShared/ # Communication (optional) |
| 115 | +├── Logger/ # Logging |
| 116 | +├── Preferences/ # Configuration |
| 117 | +├── UserDefaultsObserver/ # Settings |
| 118 | +└── Workspace/ # File management |
| 119 | +``` |
| 120 | + |
| 121 | +### Core Data Flow Pattern |
| 122 | +``` |
| 123 | +1. ActiveApplicationMonitor → Detect Xcode launch/focus |
| 124 | +2. AXNotificationStream → Real-time UI change events |
| 125 | +3. XcodeInspector → Aggregate and publish state |
| 126 | +4. XcodeAppInstanceInspector → Extract specific data |
| 127 | +5. AXUIElement extensions → Low-level element access |
| 128 | +6. XPC Service → Expose data to extensions (optional) |
| 129 | +``` |
| 130 | + |
| 131 | +### Key Published Data |
| 132 | +The `XcodeInspector` provides real-time awareness of: |
| 133 | +- **Active File**: `activeDocumentURL: URL?` |
| 134 | +- **Workspace**: `activeWorkspaceURL: URL?` |
| 135 | +- **Editor State**: `focusedEditor: SourceEditor?` |
| 136 | +- **Cursor Position**: Available through `SourceEditor.selectedTextRange` |
| 137 | +- **Editor Content**: Available through `SourceEditor.getContent()` |
| 138 | + |
| 139 | +## **Implementation Notes** |
| 140 | + |
| 141 | +### Accessibility Permissions |
| 142 | +- Requires macOS Accessibility permission |
| 143 | +- Automatic permission detection and retry mechanisms |
| 144 | +- Robust error handling for permission issues |
| 145 | + |
| 146 | +### Performance Optimizations |
| 147 | +- **Debounced Updates**: Prevents excessive state changes |
| 148 | +- **Selective Monitoring**: Only tracks relevant UI elements |
| 149 | +- **Async Processing**: Non-blocking state updates |
| 150 | +- **Memory Management**: Proper cleanup of observers |
| 151 | + |
| 152 | +### Error Recovery |
| 153 | +- **Automatic Restart**: When accessibility API corrupts |
| 154 | +- **Exponential Backoff**: For failed connections |
| 155 | +- **State Validation**: Consistency checking |
| 156 | +- **Graceful Degradation**: Fallback to cached data |
| 157 | + |
| 158 | +### Thread Safety |
| 159 | +- Uses global actors (`@XcodeInspectorActor`) for thread safety |
| 160 | +- All state updates happen on appropriate actors |
| 161 | +- Proper async/await patterns throughout |
| 162 | + |
| 163 | +This architecture provides comprehensive Xcode monitoring without any Copilot dependencies, giving you real-time awareness of files, workspaces, cursor position, and editor content. |
0 commit comments