|
| 1 | +# OpenSwiftUI Copilot Instructions |
| 2 | + |
| 3 | +This file contains coding guidelines and conventions for AI assistants working on the OpenSwiftUI project. |
| 4 | + |
| 5 | +## Quick Reference |
| 6 | + |
| 7 | +### Key Principles |
| 8 | +- Use `swift-testing` framework with `#expect` macro (not XCTest) |
| 9 | +- Use `package` access level for cross-module APIs |
| 10 | +- Follow SwiftUI API compatibility patterns |
| 11 | +- Trim trailing whitespaces automatically |
| 12 | +- Use 4-space indentation consistently |
| 13 | + |
| 14 | +## Testing Guidelines |
| 15 | + |
| 16 | +### Testing Framework |
| 17 | + |
| 18 | +- Use the `swift-testing` framework with the `#expect` macro for all test assertions |
| 19 | +- Import testing framework with: `import Testing` |
| 20 | +- Do NOT use XCTest framework unless specifically required for compatibility |
| 21 | + |
| 22 | +### Test Structure |
| 23 | + |
| 24 | +```swift |
| 25 | +import Testing |
| 26 | + |
| 27 | +struct DemoTests { |
| 28 | + @Test |
| 29 | + func functionality() { |
| 30 | + let value = SomeType() |
| 31 | + |
| 32 | + #expect(value.property == expectedValue) |
| 33 | + #expect(value.method() == expectedResult) |
| 34 | + } |
| 35 | + |
| 36 | + @Test |
| 37 | + func errorConditions() { |
| 38 | + #expect(throws: SomeError.self) { |
| 39 | + try riskyOperation() |
| 40 | + } |
| 41 | + } |
| 42 | +} |
| 43 | +``` |
| 44 | + |
| 45 | +### Test Conventions |
| 46 | + |
| 47 | +- **Do NOT write any comments in test case body** - keep test code clean and self-explanatory |
| 48 | +- Use descriptive test function names that clearly indicate what is being tested |
| 49 | +- Group related tests using `// MARK: -` sections |
| 50 | +- Use `#expect` for all assertions instead of XCTest assertions |
| 51 | +- Prefer multiple focused tests over one large test |
| 52 | +- Do not add test prefix to test function names (e.g., `testFunctionality` should be `functionality`) |
| 53 | +- Use `@Test` attribute for test functions |
| 54 | + |
| 55 | +## Code Organization |
| 56 | + |
| 57 | +### File Structure |
| 58 | +- Use `// MARK: -` to separate different topics and sections within files |
| 59 | +- Organize code logically with clear separation of concerns |
| 60 | +- Place imports at the top, followed by type definitions, then implementations |
| 61 | + |
| 62 | +### Example MARK Usage |
| 63 | +```swift |
| 64 | +// MARK: - A |
| 65 | + |
| 66 | +... |
| 67 | + |
| 68 | +// MARK: - B |
| 69 | + |
| 70 | +... |
| 71 | + |
| 72 | +// MARK: - C |
| 73 | + |
| 74 | +... |
| 75 | +``` |
| 76 | + |
| 77 | +## Swift Coding Style |
| 78 | + |
| 79 | +### Access Control Hierarchy |
| 80 | + |
| 81 | +1. `public` - External API surface |
| 82 | +2. `package` - Cross-module internal APIs |
| 83 | +3. `internal` - Module-internal APIs (default) |
| 84 | +4. `private` - Implementation details |
| 85 | + |
| 86 | +### Naming Conventions |
| 87 | + |
| 88 | +- Follow Swift API Design Guidelines |
| 89 | +- Use descriptive names that clearly indicate purpose |
| 90 | +- Prefer full words over abbreviations |
| 91 | +- Use camelCase for variables, functions, and properties |
| 92 | +- Use PascalCase for types, protocols, and cases |
| 93 | +- Prefix internal types with `_` when they mirror SwiftUI internals |
| 94 | + |
| 95 | +### Code Formatting Rules |
| 96 | + |
| 97 | +- **Automatically trim trailing whitespaces including whitespace-only lines** |
| 98 | +- Use consistent indentation (4 spaces, not tabs) |
| 99 | +- Place opening braces on the same line as the declaration |
| 100 | +- Use proper spacing around operators and after commas |
| 101 | +- Align code vertically when it improves readability |
| 102 | +- Maximum line length: 120 characters (soft limit) |
| 103 | + |
| 104 | +### Type Definitions |
| 105 | + |
| 106 | +```swift |
| 107 | +struct SomeType { |
| 108 | + let property: String |
| 109 | + |
| 110 | + private let internalProperty: Int |
| 111 | + |
| 112 | + func method() -> String { |
| 113 | + // Implementation |
| 114 | + } |
| 115 | +} |
| 116 | +``` |
| 117 | + |
| 118 | +## Architecture Patterns |
| 119 | + |
| 120 | +### SwiftUI Compatibility |
| 121 | +- Maintain API compatibility with SwiftUI when implementing equivalent functionality |
| 122 | +- Use similar naming conventions and parameter patterns as SwiftUI |
| 123 | +- Implement protocols and extensions that mirror SwiftUI's design |
| 124 | + |
| 125 | +### Module Organization |
| 126 | +- Keep related functionality in appropriate modules |
| 127 | +- Use clear module boundaries |
| 128 | +- Avoid circular dependencies between modules |
| 129 | + |
| 130 | +### Error Handling |
| 131 | +- Use Swift's error handling mechanisms (`throws`, `Result`, etc.) |
| 132 | +- Provide meaningful error messages |
| 133 | +- Handle errors gracefully at appropriate levels |
| 134 | + |
| 135 | +## Documentation |
| 136 | + |
| 137 | +### Code Comments |
| 138 | +- Write clear, concise comments for complex logic |
| 139 | +- Use documentation comments (`///`) for APIs documentation |
| 140 | +- Avoid obvious comments that don't add value |
| 141 | +- Keep comments up-to-date with code changes |
| 142 | + |
| 143 | +### API Documentation |
| 144 | +```swift |
| 145 | +/// A brief description of what this function does. |
| 146 | +/// |
| 147 | +/// - Parameter value: Description of the parameter |
| 148 | +/// - Returns: Description of the return value |
| 149 | +/// - Throws: Description of potential errors |
| 150 | +func someFunction(value: String) throws -> Int { |
| 151 | + // Implementation |
| 152 | +} |
| 153 | +``` |
| 154 | + |
| 155 | +## Performance Considerations |
| 156 | + |
| 157 | +- Prefer value types (structs) over reference types (classes) when appropriate |
| 158 | +- Use lazy initialization for expensive computations |
| 159 | +- Consider memory management and avoid retain cycles |
| 160 | +- Optimize for common use cases while maintaining flexibility |
| 161 | + |
| 162 | +## Dependencies and Imports |
| 163 | + |
| 164 | +- Minimize external dependencies |
| 165 | +- Use conditional compilation for platform-specific code |
| 166 | +- Import only what is needed (avoid importing entire modules when specific types suffice) |
| 167 | +- Organize imports alphabetically within groups (system frameworks first, then project modules) |
| 168 | + |
| 169 | +## Version Control |
| 170 | + |
| 171 | +- Make atomic commits with clear, descriptive messages |
| 172 | +- Keep changes focused and reviewable |
| 173 | +- Follow the project's branching strategy |
| 174 | +- Ensure all tests pass before committing |
| 175 | + |
| 176 | +## Platform Compatibility |
| 177 | + |
| 178 | +- Support multiple Apple platforms (iOS, macOS, watchOS, tvOS, visionOS) |
| 179 | +- Use availability annotations when using platform-specific APIs |
| 180 | +- Test on supported platform versions |
| 181 | +- Use feature detection rather than version checking when possible |
| 182 | + |
| 183 | +--- |
| 184 | + |
| 185 | +## Troubleshooting |
| 186 | + |
| 187 | +### Common Issues |
| 188 | +- **Build errors**: Check `package` vs `internal` access levels |
| 189 | +- **Test failures**: Ensure using `#expect` not XCTest assertions |
| 190 | +- **SwiftUI compatibility**: Verify API signatures match exactly |
| 191 | + |
| 192 | +Remember: This project aims to provide a compatible alternative to SwiftUI, so maintain consistency with SwiftUI's patterns and conventions while following these OpenSwiftUI-specific guidelines. |
0 commit comments