|
| 1 | +# Path-Specific Instructions for SkiaSharp |
| 2 | + |
| 3 | +This directory contains path-specific instruction files that provide targeted guidance for AI coding agents working on different parts of the SkiaSharp codebase. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +Path-specific instructions automatically apply based on the files being edited, ensuring AI assistants use appropriate patterns, rules, and best practices for each layer or component. |
| 8 | + |
| 9 | +## Instruction Files |
| 10 | + |
| 11 | +| File | Applies To | Key Focus | |
| 12 | +|------|-----------|-----------| |
| 13 | +| **[c-api-layer.instructions.md](c-api-layer.instructions.md)** | `externals/skia/include/c/`, `externals/skia/src/c/` | C API bridging layer - no exceptions, C types, error codes | |
| 14 | +| **[csharp-bindings.instructions.md](csharp-bindings.instructions.md)** | `binding/SkiaSharp/` | C# wrappers - IDisposable, P/Invoke, validation, exceptions | |
| 15 | +| **[generated-code.instructions.md](generated-code.instructions.md)** | `*.generated.cs` files | Generated code - don't edit manually, modify templates | |
| 16 | +| **[native-skia.instructions.md](native-skia.instructions.md)** | `externals/skia/` (excluding C API) | Upstream Skia C++ - understanding only, pointer types | |
| 17 | +| **[tests.instructions.md](tests.instructions.md)** | `tests/`, `*Tests.cs` | Test code - memory management, error cases, lifecycle | |
| 18 | +| **[documentation.instructions.md](documentation.instructions.md)** | `design/`, `*.md` | Documentation - clear examples, architecture focus | |
| 19 | +| **[samples.instructions.md](samples.instructions.md)** | `samples/` | Sample code - best practices, complete examples | |
| 20 | + |
| 21 | +## How It Works |
| 22 | + |
| 23 | +AI coding agents that support path-specific instructions (like GitHub Copilot, Cursor, etc.) will automatically load and apply the relevant instruction file based on the file paths you're working with. |
| 24 | + |
| 25 | +For example: |
| 26 | +- Editing `externals/skia/src/c/sk_canvas.cpp` → Loads **c-api-layer.instructions.md** |
| 27 | +- Editing `binding/SkiaSharp/SKCanvas.cs` → Loads **csharp-bindings.instructions.md** |
| 28 | +- Editing `tests/SKCanvasTests.cs` → Loads **tests.instructions.md** |
| 29 | + |
| 30 | +## Key Benefits |
| 31 | + |
| 32 | +### 1. Layer-Specific Guidance |
| 33 | +Each layer has unique requirements: |
| 34 | +- **C API:** Never throw exceptions, use C types, handle errors with return codes |
| 35 | +- **C# Bindings:** Always dispose, validate parameters, convert to C# exceptions |
| 36 | +- **Tests:** Focus on memory management, error cases, lifecycle |
| 37 | + |
| 38 | +### 2. Automatic Context |
| 39 | +AI assistants automatically understand: |
| 40 | +- Which patterns to follow |
| 41 | +- What mistakes to avoid |
| 42 | +- How to handle special cases |
| 43 | + |
| 44 | +### 3. Consistency |
| 45 | +Ensures all AI-generated code follows the same patterns across the codebase. |
| 46 | + |
| 47 | +## Critical Concepts Covered |
| 48 | + |
| 49 | +### Memory Management (All Layers) |
| 50 | +- **Raw pointers** (non-owning) - No cleanup needed |
| 51 | +- **Owned pointers** - One owner, explicit delete/dispose |
| 52 | +- **Reference-counted** - Shared ownership, ref/unref |
| 53 | + |
| 54 | +### Error Handling (Per Layer) |
| 55 | +- **C API:** Catch all exceptions, return bool/null, defensive null checks |
| 56 | +- **C#:** Validate parameters, check returns, throw typed exceptions |
| 57 | +- **Tests:** Verify proper exception handling |
| 58 | + |
| 59 | +### Best Practices |
| 60 | +- Proper disposal in C# (`using` statements) |
| 61 | +- Complete, self-contained examples in samples |
| 62 | +- Memory leak testing in test code |
| 63 | +- Clear documentation with examples |
| 64 | + |
| 65 | +## Usage Examples |
| 66 | + |
| 67 | +### For AI Assistants |
| 68 | + |
| 69 | +When working on different files: |
| 70 | + |
| 71 | +``` |
| 72 | +# Editing C API layer |
| 73 | +externals/skia/src/c/sk_canvas.cpp |
| 74 | +→ Applies: Never throw exceptions, use SK_C_API, handle errors |
| 75 | +
|
| 76 | +# Editing C# wrapper |
| 77 | +binding/SkiaSharp/SKCanvas.cs |
| 78 | +→ Applies: Validate parameters, use IDisposable, throw exceptions |
| 79 | +
|
| 80 | +# Writing tests |
| 81 | +tests/SKCanvasTests.cs |
| 82 | +→ Applies: Use using statements, test disposal, verify no leaks |
| 83 | +``` |
| 84 | + |
| 85 | +### For Contributors |
| 86 | + |
| 87 | +These files serve as quick reference guides for: |
| 88 | +- Understanding layer-specific requirements |
| 89 | +- Following established patterns |
| 90 | +- Avoiding common mistakes |
| 91 | + |
| 92 | +## Maintaining Instructions |
| 93 | + |
| 94 | +### When to Update |
| 95 | + |
| 96 | +Update instruction files when: |
| 97 | +- Patterns or best practices change |
| 98 | +- New common mistakes are discovered |
| 99 | +- Layer responsibilities change |
| 100 | +- New tooling or generators are added |
| 101 | + |
| 102 | +### What to Include |
| 103 | + |
| 104 | +Each instruction file should cover: |
| 105 | +- ✅ Critical rules and requirements |
| 106 | +- ✅ Common patterns with code examples |
| 107 | +- ✅ What NOT to do (anti-patterns) |
| 108 | +- ✅ Error handling specifics |
| 109 | +- ✅ Memory management patterns |
| 110 | + |
| 111 | +### What to Avoid |
| 112 | + |
| 113 | +Don't include in instruction files: |
| 114 | +- ❌ Exhaustive API documentation |
| 115 | +- ❌ Build/setup instructions (use main docs) |
| 116 | +- ❌ Temporary workarounds |
| 117 | +- ❌ Implementation details |
| 118 | + |
| 119 | +## Related Documentation |
| 120 | + |
| 121 | +For comprehensive guidance, see: |
| 122 | +- **[AGENTS.md](../../AGENTS.md)** - High-level project overview for AI agents |
| 123 | +- **[design/](../../design/)** - Detailed architecture documentation |
| 124 | +- **[.github/copilot-instructions.md](../copilot-instructions.md)** - General AI assistant context |
| 125 | + |
| 126 | +## Integration with AI Tools |
| 127 | + |
| 128 | +These instructions integrate with: |
| 129 | +- **GitHub Copilot** - Workspace instructions |
| 130 | +- **Cursor** - .cursorrules and workspace context |
| 131 | +- **Other AI assistants** - Supporting path-specific patterns |
| 132 | + |
| 133 | +## Summary |
| 134 | + |
| 135 | +Path-specific instructions ensure AI coding agents apply the right patterns in the right places, maintaining code quality and consistency across SkiaSharp's three-layer architecture. |
| 136 | + |
| 137 | +**Key Principle:** Different layers require different approaches - these instructions ensure AI assistants understand and apply the correct patterns for each context. |
0 commit comments