* Parse input to extract completion context
(input: string, cursorOffset: number)
| 80 | * Parse input to extract completion context |
| 81 | */ |
| 82 | function parseInputContext(input: string, cursorOffset: number): InputContext { |
| 83 | const beforeCursor = input.slice(0, cursorOffset) |
| 84 | |
| 85 | // Check if it's a variable prefix, before expanding with shell-quote |
| 86 | const varMatch = beforeCursor.match(/\$[a-zA-Z_][a-zA-Z0-9_]*$/) |
| 87 | if (varMatch) { |
| 88 | return { prefix: varMatch[0], completionType: 'variable' } |
| 89 | } |
| 90 | |
| 91 | // Parse with shell-quote |
| 92 | const parseResult = tryParseShellCommand(beforeCursor) |
| 93 | if (!parseResult.success) { |
| 94 | // Fallback to simple parsing |
| 95 | const tokens = beforeCursor.split(/\s+/) |
| 96 | const prefix = tokens[tokens.length - 1] || '' |
| 97 | const isFirstToken = tokens.length === 1 && !beforeCursor.includes(' ') |
| 98 | const completionType = isFirstToken |
| 99 | ? 'command' |
| 100 | : getCompletionTypeFromPrefix(prefix) |
| 101 | return { prefix, completionType } |
| 102 | } |
| 103 | |
| 104 | // Extract current token |
| 105 | const lastToken = findLastStringToken(parseResult.tokens) |
| 106 | if (!lastToken) { |
| 107 | // No string token found - check if after operator |
| 108 | const lastParsedToken = parseResult.tokens[parseResult.tokens.length - 1] |
| 109 | const completionType = |
| 110 | lastParsedToken && isCommandOperator(lastParsedToken) |
| 111 | ? 'command' |
| 112 | : 'command' // Default to command at start |
| 113 | return { prefix: '', completionType } |
| 114 | } |
| 115 | |
| 116 | // If there's a trailing space, the user is starting a new argument |
| 117 | if (beforeCursor.endsWith(' ')) { |
| 118 | // After first token (command) with space = file argument expected |
| 119 | return { prefix: '', completionType: 'file' } |
| 120 | } |
| 121 | |
| 122 | // Determine completion type from context |
| 123 | const baseType = getCompletionTypeFromPrefix(lastToken.token) |
| 124 | |
| 125 | // If it's clearly a file or variable based on prefix, use that type |
| 126 | if (baseType === 'variable' || baseType === 'file') { |
| 127 | return { prefix: lastToken.token, completionType: baseType } |
| 128 | } |
| 129 | |
| 130 | // For command-like tokens, check context: are we starting a new command? |
| 131 | const completionType = isNewCommandContext( |
| 132 | parseResult.tokens, |
| 133 | lastToken.index, |
| 134 | ) |
| 135 | ? 'command' |
| 136 | : 'file' // Not after operator = file argument |
| 137 | |
| 138 | return { prefix: lastToken.token, completionType } |
| 139 | } |
no test coverage detected