File size: 4,304 Bytes
9332ccf | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 | /**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import * as path from 'node:path';
import * as fs from 'node:fs';
import { opendir } from 'node:fs/promises';
import { homedir, type WorkspaceContext } from '@google/gemini-cli-core';
const MAX_SUGGESTIONS = 50;
const MATCH_BUFFER_MULTIPLIER = 3;
export function expandHomeDir(p: string): string {
if (!p) {
return '';
}
let expandedPath = p;
if (p.toLowerCase().startsWith('%userprofile%')) {
expandedPath = homedir() + p.substring('%userprofile%'.length);
} else if (p === '~' || p.startsWith('~/')) {
expandedPath = homedir() + p.substring(1);
}
return path.normalize(expandedPath);
}
interface ParsedPath {
searchDir: string;
filter: string;
isHomeExpansion: boolean;
resultPrefix: string;
}
function parsePartialPath(partialPath: string): ParsedPath {
const isHomeExpansion = partialPath.startsWith('~');
const expandedPath = expandHomeDir(partialPath || '.');
let searchDir: string;
let filter: string;
if (
partialPath === '' ||
partialPath.endsWith('/') ||
partialPath.endsWith(path.sep)
) {
searchDir = expandedPath;
filter = '';
} else {
searchDir = path.dirname(expandedPath);
filter = path.basename(expandedPath);
// Special case for ~ because path.dirname('~') can be '.'
if (
isHomeExpansion &&
!partialPath.includes('/') &&
!partialPath.includes(path.sep)
) {
searchDir = homedir();
filter = partialPath.substring(1);
}
}
// Calculate result prefix
let resultPrefix = '';
if (
partialPath === '' ||
partialPath.endsWith('/') ||
partialPath.endsWith(path.sep)
) {
resultPrefix = partialPath;
} else {
const lastSlashIndex = Math.max(
partialPath.lastIndexOf('/'),
partialPath.lastIndexOf(path.sep),
);
if (lastSlashIndex !== -1) {
resultPrefix = partialPath.substring(0, lastSlashIndex + 1);
} else if (isHomeExpansion) {
resultPrefix = `~${path.sep}`;
}
}
return { searchDir, filter, isHomeExpansion, resultPrefix };
}
/**
* Gets directory suggestions based on a partial path.
* Uses async iteration with fs.opendir for efficient handling of large directories.
*
* @param partialPath The partial path typed by the user.
* @returns A promise resolving to an array of directory path suggestions.
*/
export async function getDirectorySuggestions(
partialPath: string,
): Promise<string[]> {
try {
const { searchDir, filter, resultPrefix } = parsePartialPath(partialPath);
if (!fs.existsSync(searchDir) || !fs.statSync(searchDir).isDirectory()) {
return [];
}
const matches: string[] = [];
const filterLower = filter.toLowerCase();
const showHidden = filter.startsWith('.');
const dir = await opendir(searchDir);
try {
for await (const entry of dir) {
if (!entry.isDirectory()) {
continue;
}
if (entry.name.startsWith('.') && !showHidden) {
continue;
}
if (entry.name.toLowerCase().startsWith(filterLower)) {
matches.push(entry.name);
// Early termination with buffer for sorting
if (matches.length >= MAX_SUGGESTIONS * MATCH_BUFFER_MULTIPLIER) {
break;
}
}
}
} finally {
await dir.close().catch(() => {});
}
// Use the separator style from user's input for consistency
const userSep = resultPrefix.includes('/') ? '/' : path.sep;
return matches
.sort()
.slice(0, MAX_SUGGESTIONS)
.map((name) => resultPrefix + name + userSep);
} catch {
return [];
}
}
export interface BatchAddResult {
added: string[];
errors: string[];
}
/**
* Helper to batch add directories to the workspace context.
* Handles expansion and error formatting.
*/
export function batchAddDirectories(
workspaceContext: WorkspaceContext,
paths: string[],
): BatchAddResult {
const result = workspaceContext.addDirectories(
paths.map((p) => expandHomeDir(p.trim())),
);
const errors: string[] = [];
for (const failure of result.failed) {
errors.push(`Error adding '${failure.path}': ${failure.error.message}`);
}
return { added: result.added, errors };
}
|