(inputPath: string)
| 15 | * - Hidden directories are skipped unless user typed a dot |
| 16 | */ |
| 17 | export function getPathCompletion(inputPath: string): string | null { |
| 18 | if (!inputPath) return null |
| 19 | |
| 20 | // Expand ~ to home directory |
| 21 | let expandedPath = inputPath |
| 22 | const homeDir = os.homedir() |
| 23 | if (expandedPath.startsWith('~')) { |
| 24 | expandedPath = path.join(homeDir, expandedPath.slice(1)) |
| 25 | } |
| 26 | |
| 27 | // If the path ends with /, we're completing inside that directory |
| 28 | // Otherwise, we're completing the last component |
| 29 | let parentDir: string |
| 30 | let partial: string |
| 31 | |
| 32 | if (expandedPath.endsWith(path.sep)) { |
| 33 | parentDir = expandedPath |
| 34 | partial = '' |
| 35 | } else { |
| 36 | parentDir = path.dirname(expandedPath) |
| 37 | partial = path.basename(expandedPath).toLowerCase() |
| 38 | } |
| 39 | |
| 40 | // Check if parent directory exists |
| 41 | try { |
| 42 | if (!existsSync(parentDir) || !statSync(parentDir).isDirectory()) { |
| 43 | return null |
| 44 | } |
| 45 | } catch { |
| 46 | return null |
| 47 | } |
| 48 | |
| 49 | // List directories in parent that match the partial |
| 50 | try { |
| 51 | const items = readdirSync(parentDir) |
| 52 | const matches: string[] = [] |
| 53 | |
| 54 | for (const item of items) { |
| 55 | // Skip hidden files unless user typed a dot |
| 56 | if (item.startsWith('.') && !partial.startsWith('.')) continue |
| 57 | |
| 58 | const fullPath = path.join(parentDir, item) |
| 59 | try { |
| 60 | if (!statSync(fullPath).isDirectory()) continue |
| 61 | } catch { |
| 62 | continue |
| 63 | } |
| 64 | |
| 65 | if (item.toLowerCase().startsWith(partial)) { |
| 66 | matches.push(item) |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | if (matches.length === 0) return null |
| 71 | |
| 72 | // If exactly one match, complete to it with trailing slash |
| 73 | if (matches.length === 1) { |
| 74 | let completed = path.join(parentDir, matches[0]) + path.sep |
no outgoing calls
no test coverage detected