LoadMatchingRules returns all rules that match the given file paths. If contextPaths is empty, returns only global rules (no paths: filter). Rules from workspace override global rules with the same name.
(contextPaths []string)
| 43 | // If contextPaths is empty, returns only global rules (no paths: filter). |
| 44 | // Rules from workspace override global rules with the same name. |
| 45 | func (rl *RulesLoader) LoadMatchingRules(contextPaths []string) string { |
| 46 | allRules := rl.loadAllRules() |
| 47 | if len(allRules) == 0 { |
| 48 | return "" |
| 49 | } |
| 50 | |
| 51 | var matched []*Rule |
| 52 | for _, rule := range allRules { |
| 53 | if len(rule.Paths) == 0 { |
| 54 | // Global rule — always include |
| 55 | matched = append(matched, rule) |
| 56 | continue |
| 57 | } |
| 58 | |
| 59 | // Path-specific rule — check if any context path matches any rule glob |
| 60 | for _, ruleGlob := range rule.Paths { |
| 61 | if matchesAnyPath(ruleGlob, contextPaths) { |
| 62 | matched = append(matched, rule) |
| 63 | break |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | if len(matched) == 0 { |
| 69 | return "" |
| 70 | } |
| 71 | |
| 72 | parts := make([]string, 0, len(matched)) |
| 73 | for _, rule := range matched { |
| 74 | header := fmt.Sprintf("### Rule: %s", rule.Name) |
| 75 | if len(rule.Paths) > 0 { |
| 76 | header += fmt.Sprintf(" (paths: %s)", strings.Join(rule.Paths, ", ")) |
| 77 | } |
| 78 | parts = append(parts, header+"\n\n"+rule.Content) |
| 79 | } |
| 80 | |
| 81 | return "## Path-Specific Rules\n\n" + strings.Join(parts, "\n\n---\n\n") |
| 82 | } |
| 83 | |
| 84 | // loadAllRules scans both global and workspace rules directories. |
| 85 | // Workspace rules override global rules with the same filename. |