|
| 1 | +import { loadConfig } from "./config"; |
| 2 | + |
1 | 3 | /**
|
2 |
| - * Pattern to match various "AI!" style trigger comments with possible instructions |
3 |
| - * Supports multiple single-line programming language comment styles: |
4 |
| - * - Double slash comment (C, C++, JavaScript, TypeScript, Java, etc.) |
5 |
| - * - Hash comment (Python, Ruby, Perl, Shell scripts, YAML, etc.) |
6 |
| - * - Double dash comment (SQL, Haskell, Lua) |
7 |
| - * - Semicolon comment (Lisp, Clojure, Assembly) |
8 |
| - * - Single quote comment (VB, VBA) |
9 |
| - * - Percent comment (LaTeX, Matlab, Erlang) |
10 |
| - * - REM comment (Batch files) |
11 |
| - * |
| 4 | + * Custom trigger patterns for watch mode |
| 5 | + * |
| 6 | + * Users can define their own trigger patterns in config.json: |
| 7 | + * ```json |
| 8 | + * { |
| 9 | + * "watchMode": { |
| 10 | + * "triggerPattern": "/(?:\\/\\/|#)\\s*AI:(TODO|FIXME)\\s+(.*)/i" |
| 11 | + * } |
| 12 | + * } |
| 13 | + * ``` |
| 14 | + * |
| 15 | + * The pattern MUST include at least one capture group that will contain the instruction. |
| 16 | + * |
12 | 17 | * Examples:
|
| 18 | + * |
| 19 | + * Default pattern (single-line comments ending with AI! or AI?): |
13 | 20 | * - "// what does this function do, AI?"
|
14 | 21 | * - "# Fix this code, AI!"
|
15 | 22 | * - "-- Optimize this query, AI!"
|
| 23 | + * |
| 24 | + * Custom pattern for task management: |
| 25 | + * - "// AI:TODO fix this bug" |
| 26 | + * - "// AI:FIXME handle error case" |
| 27 | + * - "# AI:BUG this crashes with null input" |
| 28 | + * |
| 29 | + * Custom pattern with different keyword: |
| 30 | + * - "// codex! fix this" |
| 31 | + * - "# codex? what does this function do" |
16 | 32 | */
|
17 | 33 |
|
18 |
| -export const TRIGGER_PATTERN = |
19 |
| - /(?:\/\/|#|--|;|'|%|REM)\s*(.*?)(?:,\s*)?AI[!?]/i; |
| 34 | +// Default trigger pattern |
| 35 | +const DEFAULT_TRIGGER_PATTERN = '/(?:\\/\\/|#|--|;|\'|%|REM)\\s*(.*?)(?:,\\s*)?AI[!?]/i'; |
| 36 | + |
| 37 | +/** |
| 38 | + * Get the configured trigger pattern from config.json or use the default |
| 39 | + * @returns A RegExp object for the trigger pattern |
| 40 | + */ |
| 41 | +export function getTriggerPattern(): RegExp { |
| 42 | + const config = loadConfig(); |
| 43 | + |
| 44 | + // Get the pattern string from config or use default |
| 45 | + const patternString = config.watchMode?.triggerPattern || DEFAULT_TRIGGER_PATTERN; |
| 46 | + |
| 47 | + try { |
| 48 | + // Parse the regex from the string - first remove enclosing slashes and extract flags |
| 49 | + const match = patternString.match(/^\/(.*)\/([gimuy]*)$/); |
| 50 | + |
| 51 | + if (match) { |
| 52 | + const [, pattern, flags] = match; |
| 53 | + return new RegExp(pattern, flags); |
| 54 | + } else { |
| 55 | + // If not in /pattern/flags format, try to use directly as a RegExp |
| 56 | + return new RegExp(patternString, 'i'); |
| 57 | + } |
| 58 | + } catch (error) { |
| 59 | + console.warn(`Invalid trigger pattern in config: ${patternString}. Using default.`); |
| 60 | + // Parse default pattern |
| 61 | + const match = DEFAULT_TRIGGER_PATTERN.match(/^\/(.*)\/([gimuy]*)$/); |
| 62 | + const [, pattern, flags] = match!; |
| 63 | + return new RegExp(pattern, flags); |
| 64 | + } |
| 65 | +} |
20 | 66 |
|
21 | 67 | /**
|
22 |
| - * Function to find all AI trigger matches in a file content |
| 68 | + * Function to find all trigger matches in a file content |
| 69 | + * Uses the configured trigger pattern from config.json |
23 | 70 | */
|
24 | 71 | export function findAllTriggers(content: string): Array<RegExpMatchArray> {
|
25 | 72 | const matches: Array<RegExpMatchArray> = [];
|
26 |
| - const regex = new RegExp(TRIGGER_PATTERN, "g"); |
| 73 | + const pattern = getTriggerPattern(); |
| 74 | + // We need to ensure the global flag is set for .exec() to work properly |
| 75 | + const regex = new RegExp(pattern.source, pattern.flags.includes('g') ? pattern.flags : pattern.flags + 'g'); |
27 | 76 |
|
28 | 77 | let match;
|
29 | 78 | while ((match = regex.exec(content)) != null) {
|
@@ -58,15 +107,22 @@ export function extractContextAroundTrigger(
|
58 | 107 | // Join the context lines back together
|
59 | 108 | const context = contextLines.join("\n");
|
60 | 109 |
|
61 |
| - // Extract the instruction from the capture groups for different comment styles |
62 |
| - // There are multiple capture groups for different comment syntaxes |
63 |
| - // Find the first non-undefined capture group |
64 |
| - let instruction = |
65 |
| - Array.from( |
66 |
| - { length: triggerMatch.length - 1 }, |
67 |
| - (_, i) => triggerMatch[i + 1], |
68 |
| - ).find((group) => group !== undefined) || "fix or improve this code"; |
69 |
| - |
| 110 | + // Get instruction from capture groups |
| 111 | + // For custom patterns, check all capture groups and use the last non-empty one |
| 112 | + // This allows patterns like /AI:(TODO|FIXME)\s+(.*)/ where we want the second group |
| 113 | + // For the default pattern, it will be the first capture group |
| 114 | + const captureGroups = Array.from( |
| 115 | + { length: triggerMatch.length - 1 }, |
| 116 | + (_, i) => triggerMatch[i + 1] |
| 117 | + ).filter(group => group !== undefined); |
| 118 | + |
| 119 | + // Use the last non-empty capture group as the instruction |
| 120 | + // For simple patterns with one capture, this will be that capture |
| 121 | + // For patterns with multiple captures (like task types), this will be the actual instruction |
| 122 | + let instruction = captureGroups.length > 0 |
| 123 | + ? captureGroups[captureGroups.length - 1] |
| 124 | + : "fix or improve this code"; |
| 125 | + |
70 | 126 | // Remove any comment prefixes that might have been captured
|
71 | 127 | instruction = instruction.replace(/^(?:\/\/|#|--|;|'|%|REM)\s*/, "");
|
72 | 128 |
|
|
0 commit comments