$npx -y skills add haoxiang-xu/PuPu --skill gitnexus-refactoringUse when the user wants to rename, extract, split, move, or restructure code safely. Examples: \"Rename this function\", \"Extract this into a module\", \"Refactor this class\", \"Move this to a separate file\"
| 1 | # Refactoring with GitNexus |
| 2 | |
| 3 | ## When to Use |
| 4 | |
| 5 | - "Rename this function safely" |
| 6 | - "Extract this into a module" |
| 7 | - "Split this service" |
| 8 | - "Move this to a new file" |
| 9 | - Any task involving renaming, extracting, splitting, or restructuring code |
| 10 | |
| 11 | ## Workflow |
| 12 | |
| 13 | ``` |
| 14 | 1. impact({target: "X", direction: "upstream"}) → Map all dependents |
| 15 | 2. query({search_query: "X"}) → Find execution flows involving X |
| 16 | 3. context({name: "X"}) → See all incoming/outgoing refs |
| 17 | 4. Plan update order: interfaces → implementations → callers → tests |
| 18 | ``` |
| 19 | |
| 20 | > If "Index is stale" → run `node .gitnexus/run.cjs analyze` in terminal. |
| 21 | |
| 22 | ## Checklists |
| 23 | |
| 24 | ### Rename Symbol |
| 25 | |
| 26 | ``` |
| 27 | - [ ] rename({symbol_name: "oldName", new_name: "newName", dry_run: true}) — preview all edits |
| 28 | - [ ] Review graph edits (high confidence) and text_search edits (review carefully) |
| 29 | - [ ] If satisfied: rename({..., dry_run: false}) — apply edits |
| 30 | - [ ] detect_changes() — verify only expected files changed |
| 31 | - [ ] Run tests for affected processes |
| 32 | ``` |
| 33 | |
| 34 | ### Extract Module |
| 35 | |
| 36 | ``` |
| 37 | - [ ] context({name: target}) — see all incoming/outgoing refs |
| 38 | - [ ] impact({target, direction: "upstream"}) — find all external callers |
| 39 | - [ ] Define new module interface |
| 40 | - [ ] Extract code, update imports |
| 41 | - [ ] detect_changes() — verify affected scope |
| 42 | - [ ] Run tests for affected processes |
| 43 | ``` |
| 44 | |
| 45 | ### Split Function/Service |
| 46 | |
| 47 | ``` |
| 48 | - [ ] context({name: target}) — understand all callees |
| 49 | - [ ] Group callees by responsibility |
| 50 | - [ ] impact({target, direction: "upstream"}) — map callers to update |
| 51 | - [ ] Create new functions/services |
| 52 | - [ ] Update callers |
| 53 | - [ ] detect_changes() — verify affected scope |
| 54 | - [ ] Run tests for affected processes |
| 55 | ``` |
| 56 | |
| 57 | ## Tools |
| 58 | |
| 59 | **rename** — automated multi-file rename: |
| 60 | |
| 61 | ``` |
| 62 | rename({symbol_name: "validateUser", new_name: "authenticateUser", dry_run: true}) |
| 63 | → 12 edits across 8 files |
| 64 | → 10 graph edits (high confidence), 2 text_search edits (review) |
| 65 | → Changes: [{file_path, edits: [{line, old_text, new_text, confidence}]}] |
| 66 | ``` |
| 67 | |
| 68 | **impact** — map all dependents first: |
| 69 | |
| 70 | ``` |
| 71 | impact({target: "validateUser", direction: "upstream"}) |
| 72 | → d=1: loginHandler, apiMiddleware, testUtils |
| 73 | → Affected Processes: LoginFlow, TokenRefresh |
| 74 | ``` |
| 75 | |
| 76 | **detect_changes** — verify your changes after refactoring: |
| 77 | |
| 78 | ``` |
| 79 | detect_changes({scope: "all"}) |
| 80 | → Changed: 8 files, 12 symbols |
| 81 | → Affected processes: LoginFlow, TokenRefresh |
| 82 | → Risk: MEDIUM |
| 83 | ``` |
| 84 | |
| 85 | **cypher** — custom reference queries: |
| 86 | |
| 87 | ```cypher |
| 88 | MATCH (caller)-[:CodeRelation {type: 'CALLS'}]->(f:Function {name: "validateUser"}) |
| 89 | RETURN caller.name, caller.filePath ORDER BY caller.filePath |
| 90 | ``` |
| 91 | |
| 92 | ## Risk Rules |
| 93 | |
| 94 | | Risk Factor | Mitigation | |
| 95 | | ------------------- | ----------------------------------------- | |
| 96 | | Many callers (>5) | Use rename for automated updates | |
| 97 | | Cross-area refs | Use detect_changes after to verify scope | |
| 98 | | String/dynamic refs | query to find them | |
| 99 | | External/public API | Version and deprecate properly | |
| 100 | |
| 101 | ## Example: Rename `validateUser` to `authenticateUser` |
| 102 | |
| 103 | ``` |
| 104 | 1. rename({symbol_name: "validateUser", new_name: "authenticateUser", dry_run: true}) |
| 105 | → 12 edits: 10 graph (safe), 2 text_search (review) |
| 106 | → Files: validator.ts, login.ts, middleware.ts, config.json... |
| 107 | |
| 108 | 2. Review text_search edits (config.json: dynamic reference!) |
| 109 | |
| 110 | 3. rename({symbol_name: "validateUser", new_name: "authenticateUser", dry_run: false}) |
| 111 | → Applied 12 edits across 8 files |
| 112 | |
| 113 | 4. detect_changes({scope: "all"}) |
| 114 | → Affected: LoginFlow, TokenRefresh |
| 115 | → Risk: MEDIUM — run tests for these flows |
| 116 | ``` |