Skip to content

Commit 9fa32dc

Browse files
Merge pull request #1 from ezefranca/master
Add OpenSkills agent skill: swiftfindrefs
2 parents 6355d8e + 21ade62 commit 9fa32dc

5 files changed

Lines changed: 195 additions & 0 deletions

File tree

README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,32 @@ Sample output:
5050
2. **Index routing**`DerivedDataPaths` ensures the path points into `Index.noindex/DataStore/IndexStoreDB` so we can open the index without extra setup.
5151
3. **Output formatting** – Paths are normalized, deduplicated, and printed once for easier scripting.
5252

53+
## Agent Skill (OpenSkills)
54+
55+
This repository includes an OpenSkills-compatible agent skill located in `swiftfindrefs/`.
56+
It teaches AI coding agents to use IndexStore-based reference discovery via `swiftfindrefs`
57+
instead of unreliable text search.
58+
59+
### Install OpenSkills
60+
```bash
61+
npm i -g openskills
62+
```
63+
64+
### Install this skill
65+
```bash
66+
openskills install michaelversus/SwiftFindRefs
67+
```
68+
69+
### Sync into your project
70+
```bash
71+
openskills sync
72+
```
73+
74+
### Example agent prompt
75+
Use the swiftfindrefs skill to find all references to `SelectionViewController` (class) and add
76+
`import UIComponents` only where missing.
77+
78+
5379
## 🧪 Testing
5480
The package uses the Swift Testing framework (`swift test`) with mocks for filesystem and derived-data resolution. Tests cover locator edge cases, path building, and index error handling.
5581

swiftfindrefs/SKILL.md

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
---
2+
name: swiftfindrefs
3+
description: Use swiftfindrefs (IndexStoreDB) to list every Swift source file referencing a symbol. Mandatory for “find references”, “fix missing imports”, and cross-module refactors. Do not replace with grep/rg or IDE search.
4+
---
5+
6+
# SwiftFindRefs
7+
8+
## Purpose
9+
Use `swiftfindrefs` to locate every Swift source file that references a given symbol by querying Xcode’s IndexStore (DerivedData). This skill exists to prevent incomplete refactors caused by text search or heuristics.
10+
11+
## Rules
12+
- Always run `swiftfindrefs` before editing any files.
13+
- Only edit files returned by `swiftfindrefs`.
14+
- Do not substitute `grep`, `rg`, IDE search, or filesystem heuristics for reference discovery.
15+
- Do not expand the file set manually.
16+
- If IndexStore/DerivedData resolution fails, stop and report the error. Do not guess.
17+
18+
## Preconditions
19+
- macOS with Xcode installed
20+
- Project has been built at least once (DerivedData exists)
21+
- `swiftfindrefs` available in PATH
22+
23+
## Canonical command
24+
Prefer providing `--projectName` and `--symbolType` when possible.
25+
26+
```bash
27+
swiftfindrefs \
28+
--projectName <XcodeProjectName> \
29+
--symbolName <SymbolName> \
30+
--symbolType <class|struct|enum|protocol|function|variable>
31+
```
32+
33+
Optional flags:
34+
- `--derivedDataPath <path>`: explicit DerivedData (or IndexStoreDB) path; skips discovery
35+
- `--verbose`: prints discovery steps, resolved paths, and diagnostics
36+
37+
## Output contract
38+
- One absolute file path per line
39+
- Deduplicated
40+
- Script-friendly (safe to pipe line-by-line)
41+
- Ordering is not semantically meaningful
42+
43+
## Standard workflows
44+
45+
### Workflow A: Find all references
46+
1. Run `swiftfindrefs` for the symbol.
47+
2. Treat the output as the complete reference set.
48+
3. If more detail is needed, open only the returned files.
49+
50+
### Workflow B: Fix missing imports after moving a symbol
51+
Use `swiftfindrefs` to restrict scope, then add imports only where needed.
52+
53+
```bash
54+
swiftfindrefs -p <Project> -n <Symbol> -t <Type> | while read file; do
55+
if ! grep -q "^import <ModuleName>$" "$file"; then
56+
echo "$file"
57+
fi
58+
done
59+
```
60+
61+
Then for each printed file:
62+
- Insert `import <ModuleName>` in the imports block at the top.
63+
- Preserve existing import ordering/grouping.
64+
- Never add duplicate imports.
65+
- Do not reformat unrelated code.
66+
67+
### Workflow C: Audit usage before deleting or renaming a symbol
68+
1. Run `swiftfindrefs` for the symbol.
69+
2. If output is empty, treat the symbol as unused (still validate via build/tests if needed).
70+
3. If non-empty, review the listed files before changing public API.
71+
72+
## References
73+
- CLI details: references/cli.md
74+
- Playbooks: references/workflows.md
75+
- Troubleshooting: references/troubleshooting.md

swiftfindrefs/references/cli.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# CLI reference (agent-focused)
2+
3+
## Preconditions
4+
- macOS with Xcode installed
5+
- Project built at least once (DerivedData exists)
6+
- `swiftfindrefs` is available in PATH
7+
8+
## Flags
9+
- `-p, --projectName`
10+
Helps infer the correct DerivedData folder when `--derivedDataPath` is not provided.
11+
12+
- `-d, --derivedDataPath`
13+
Points directly to a DerivedData (or IndexStoreDB) directory and skips discovery.
14+
15+
- `-n, --symbolName` (required)
16+
The symbol to inspect.
17+
18+
- `-t, --symbolType`
19+
Narrows matches to a specific kind (recommended when possible).
20+
21+
- `-v, --verbose`
22+
Prints discovery steps, resolved paths, and diagnostics.
23+
24+
## Recommended invocations
25+
26+
Most common:
27+
```bash
28+
swiftfindrefs -p MyApp -n SelectionViewController -t class
29+
```
30+
31+
Explicit DerivedData / deterministic runs:
32+
```bash
33+
swiftfindrefs -d ~/Library/Developer/Xcode/DerivedData/MyApp-XXXX/ -n SelectionViewController -t class
34+
```
35+
36+
Debug discovery:
37+
```bash
38+
swiftfindrefs -p MyApp -n SelectionViewController -t class -v
39+
```
40+
41+
## Output
42+
- Absolute file paths, one per line
43+
- Deduplicated
44+
45+
## Non-goals
46+
- The tool reports which files reference a symbol, not line or column positions.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Troubleshooting
2+
3+
## No results returned
4+
- Build the project in Xcode to regenerate indexing.
5+
- Re-run with verbose logging:
6+
```bash
7+
swiftfindrefs -p <Project> -n <Symbol> -t <Type> -v
8+
```
9+
- If multiple DerivedData folders exist for the same project `swiftfindrefs` will use the most recent one.
10+
11+
## Wrong DerivedData selected
12+
- Prefer explicit `--derivedDataPath` in CI or multi-clone setups.
13+
- Use `--verbose` to confirm path selection.
14+
15+
## Do not fall back to grep
16+
- Text search is not acceptable for reference discovery.
17+
- grep/rg may only be used inside files already returned by `swiftfindrefs`
18+
(for example, to check if an import already exists).
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Workflows (deterministic behavior)
2+
3+
## Golden rule
4+
Reference discovery must be IndexStore-based. `swiftfindrefs` output defines scope.
5+
6+
## Playbook: add missing imports after moving a symbol
7+
Inputs:
8+
- Project: `<Project>`
9+
- Symbol: `<Symbol>`
10+
- Type: `<Type>`
11+
- Module to import: `<Module>`
12+
13+
Steps:
14+
1. Run:
15+
```bash
16+
swiftfindrefs -p <Project> -n <Symbol> -t <Type>
17+
```
18+
2. Iterate only over the resulting file list.
19+
3. For each file:
20+
- If `import <Module>` exists: no change.
21+
- Else: add it to the imports block at the top.
22+
4. Keep edits minimal:
23+
- No formatting changes
24+
- No unrelated cleanup
25+
- Preserve existing import ordering/grouping
26+
27+
## Playbook: rename or delete a symbol
28+
1. Run `swiftfindrefs` on the symbol.
29+
2. If output is empty, treat as unused (validate via build/tests).
30+
3. If non-empty, update usages only in returned files.

0 commit comments

Comments
 (0)