Skip to content

Connect Claude Code CLI to Xcode. Fix build errors, ask for information and context, validate selected lines.

Notifications You must be signed in to change notification settings

leocoout/claude-xcode-context

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

1 Commit
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

claude-xcode-context

Xcode status monitor for terminal - captures your current file, selection, and build errors as context for Claude Code.

Features

  • Xcode Status: Shows whether Xcode is running and which project is active
  • Current File Tracking: Displays the currently open file in Xcode with clickable file links
  • Selection Tracking: Shows number of lines selected and their content
  • Build Error Detection: Monitors Xcode build logs and reports build errors
  • Detailed Error Messages: Extracts and displays Swift compilation errors with file locations
  • Claude Code Integration: Provides context for AI-assisted development

Installation

npm install -g claude-xcode-context

Usage

Basic Usage

Get current Xcode status:

claude-xcode-context

Output example:

🟒 ExampleApp
⧉ In ContentView.swift
13 lines selected (10-22)

--- Context ---
Project Path: /Users/you/ExampleApp/ExampleApp.xcworkspace
File Path: /Users/you/ExampleApp/Sources/ContentView.swift

--- Selected Code ---
10: struct ContentView: View {
11:     var body: some View {
12:         Text("Hello, World!")
...

Command-Line Options

# Show help
claude-xcode-context --help
claude-xcode-context -h

# Show version
claude-xcode-context --version
claude-xcode-context -v

# Use custom Xcode app name (e.g., Xcode-beta)
claude-xcode-context --xcode Xcode-beta
claude-xcode-context -x Xcode-beta

Claude Code Integration

The tool writes Xcode context to:

~/.claude-xcode-build-infra/statusline_context.json

This file contains:

  • Current file and path
  • Selected line range (selection_start, selection_end, selected_lines)
  • Build errors with detailed messages
  • Project information

Use the /xcode-context command in Claude Code to leverage this context for AI-assisted development.

Status Display

Xcode Not Running

πŸ”΄ Xcode closed, open now

Xcode Running - No Project

🟒 Xcode opened but not focused
0 lines selected

Active File with Selection

🟒 ExampleApp
⧉ In ContentView.swift
15 lines selected (42-56)

--- Context ---
Project Path: /Users/you/ExampleApp/ExampleApp.xcworkspace
File Path: /Users/you/ExampleApp/Sources/ContentView.swift

--- Selected Code ---
42: func fetchData() {
43:     // Implementation
...

With Build Errors

🟒 ExampleApp
⧉ In ContentView.swift
3 build errors

--- Build Errors ---
ContentView.swift:23:15: error: Cannot find 'invalidFunction' in scope
ViewModel.swift:45:9: error: Value of type 'String' has no member 'invalidProperty'
...

Note: File names are clickable links in supported terminals (iTerm2, Terminal.app with Cmd+Click)

How It Works

Architecture

The tool is organized into focused modules:

lib/
  cli/
    options.js              - CLI configuration
    handlers.js             - Command handlers
  constants.js              - Configuration constants
  applescript-bridge.js     - AppleScript execution
  xcode-project-finder.js   - Project/file resolution
  build-error-parser.js     - Build log parsing
  status-collector.js       - Main orchestration
  output-formatter.js       - Terminal formatting
  selection-handler.js      - Code selection
  persistence.js            - Log file I/O

Build Error Detection

The tool monitors Xcode's build logs in ~/Library/Developer/Xcode/DerivedData:

  1. Matches the active project with its DerivedData folder
  2. Reads LogStoreManifest.plist to find the latest build
  3. Parses compressed build logs to extract Swift compilation errors
  4. Filters out warnings and focuses on actual errors
  5. Formats error messages with file:line:column locations

File Path Resolution

Multiple strategies are used to resolve the current file path:

  1. Query Xcode's active source document via AppleScript
  2. Parse window title to extract filename
  3. Search project directory using find command
  4. Try common source directory patterns (Sources, src)

Performance

  • AppleScript calls have configurable timeouts (2-5 seconds)
  • File operations are optimized with early exits
  • Compressed log decompression handled efficiently
  • Single execution model (no continuous polling)

Requirements

  • macOS: This package only works on macOS (darwin)
  • Node.js: >= 18.3.0 (for native parseArgs API)
  • Xcode: Any version that uses DerivedData

Context File Format

The JSON file at ~/.claude-xcode-build-infra/statusline_context.json contains:

{
  "timestamp": "2025-01-05T10:30:45.123Z",
  "xcode_running": true,
  "project_name": "ExampleApp",
  "project_path": "/Users/you/ExampleApp/ExampleApp.xcworkspace",
  "current_file": "ContentView.swift",
  "current_file_path": "/Users/you/ExampleApp/Sources/ContentView.swift",
  "selected_lines": 13,
  "selection_start": 10,
  "selection_end": 22,
  "build_errors": 0,
  "detailed_errors": []
}

Development

Project Structure

bin/
  statusline.js           - CLI entry point (61 lines)
lib/
  cli/                    - CLI argument handling
  constants.js            - Configuration
  applescript-bridge.js   - AppleScript utilities
  build-error-parser.js   - Error extraction
  xcode-project-finder.js - Path resolution
  status-collector.js     - Status orchestration
  output-formatter.js     - Terminal output
  selection-handler.js    - Selection handling
  persistence.js          - File I/O

Adding New CLI Flags

  1. Add option to lib/cli/options.js
  2. Add handler to lib/cli/handlers.js
  3. Use in bin/statusline.js

Testing

# Run with your local Xcode
node bin/statusline.js

# Test with custom Xcode
node bin/statusline.js --xcode Xcode-beta

# Check version
node bin/statusline.js --version

Troubleshooting

"Xcode closed" when Xcode is running

  • Ensure Xcode is the foreground app
  • Try using --xcode flag if using Xcode-beta or custom name
  • Check Xcode's Accessibility permissions in System Settings

No file path shown

  • Make sure a file is open and focused in Xcode
  • The file must be part of the active project
  • Try clicking in the editor to focus the file

Build errors not detected

  • Errors appear only after a build (⌘B)
  • Make sure the build failed (not warnings)
  • Check that DerivedData is in the default location

Author

leocoout

Repository

https://github.com/leocoout/claude-xcode-context

License

MIT

About

Connect Claude Code CLI to Xcode. Fix build errors, ask for information and context, validate selected lines.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published