({
searchQuery,
setSearchQuery,
currentPath,
setCurrentPath,
expandPath,
}: UsePathTabCompletionOptions)
| 29 | * Always navigates to completed directories when completion ends with /. |
| 30 | */ |
| 31 | export function usePathTabCompletion({ |
| 32 | searchQuery, |
| 33 | setSearchQuery, |
| 34 | currentPath, |
| 35 | setCurrentPath, |
| 36 | expandPath, |
| 37 | }: UsePathTabCompletionOptions): UsePathTabCompletionReturn { |
| 38 | const handleTabCompletion = useCallback((): boolean => { |
| 39 | if (searchQuery.startsWith('/') || searchQuery.startsWith('~')) { |
| 40 | // Absolute path completion |
| 41 | const completed = getPathCompletion(searchQuery) |
| 42 | if (completed) { |
| 43 | // If completion is a full directory (ends with /), navigate there and keep the path in input |
| 44 | if (completed.endsWith('/')) { |
| 45 | const dirPath = expandPath(completed.slice(0, -1)) |
| 46 | try { |
| 47 | if (existsSync(dirPath) && statSync(dirPath).isDirectory()) { |
| 48 | setCurrentPath(dirPath) |
| 49 | setSearchQuery(completed) |
| 50 | return true |
| 51 | } |
| 52 | } catch { |
| 53 | // Fall through to just set the query |
| 54 | } |
| 55 | } |
| 56 | setSearchQuery(completed) |
| 57 | } |
| 58 | } else if (searchQuery.length > 0) { |
| 59 | // Relative path completion - try from current directory |
| 60 | const relativePath = path.join(currentPath, searchQuery) |
| 61 | const completed = getPathCompletion(relativePath) |
| 62 | if (completed) { |
| 63 | // If completion is a full directory (ends with /), navigate there and keep the path in input |
| 64 | if (completed.endsWith('/')) { |
| 65 | try { |
| 66 | const dirPath = completed.slice(0, -1) |
| 67 | if (existsSync(dirPath) && statSync(dirPath).isDirectory()) { |
| 68 | setCurrentPath(dirPath) |
| 69 | setSearchQuery(completed) |
| 70 | return true |
| 71 | } |
| 72 | } catch { |
| 73 | // Fall through to just set the query |
| 74 | } |
| 75 | } |
| 76 | // Convert back to relative path for display |
| 77 | if (completed.startsWith(currentPath + path.sep)) { |
| 78 | setSearchQuery(completed.slice(currentPath.length + 1)) |
| 79 | } else { |
| 80 | setSearchQuery(completed) |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | return true |
| 85 | }, [searchQuery, setSearchQuery, currentPath, setCurrentPath, expandPath]) |
| 86 | |
| 87 | return { handleTabCompletion } |
| 88 | } |
no test coverage detected