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