| 1 | import { IDE } from ".."; |
| 2 | |
| 3 | import { |
| 4 | joinEncodedUriPathSegmentToUri, |
| 5 | joinPathsToUri, |
| 6 | pathToUriPathSegment, |
| 7 | } from "./uri"; |
| 8 | |
| 9 | /* |
| 10 | This function takes a relative (to workspace) filepath |
| 11 | And checks each workspace for if it exists or not |
| 12 | Only returns fully resolved URI if it exists |
| 13 | */ |
| 14 | export async function resolveRelativePathInDir( |
| 15 | path: string, |
| 16 | ide: IDE, |
| 17 | dirUriCandidates?: string[], |
| 18 | ): Promise<string | undefined> { |
| 19 | const dirs = dirUriCandidates ?? (await ide.getWorkspaceDirs()); |
| 20 | for (const dirUri of dirs) { |
| 21 | const fullUri = joinPathsToUri(dirUri, path); |
| 22 | if (await ide.fileExists(fullUri)) { |
| 23 | return fullUri; |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | return undefined; |
| 28 | } |
| 29 | |
| 30 | /* |
| 31 | Same as above but in this case the relative path does not need to exist (e.g. file to be created, etc) |
| 32 | Checks closes match with the dirs, path segment by segment |
| 33 | and based on which workspace has the closest matching path, returns resolved URI |
| 34 | If no meaninful path match just concatenates to first dir's uri |
| 35 | */ |
| 36 | export async function inferResolvedUriFromRelativePath( |
| 37 | _relativePath: string, |
| 38 | ide: IDE, |
| 39 | dirCandidates?: string[], |
| 40 | ): Promise<string> { |
| 41 | const relativePath = _relativePath.trim().replaceAll("\\", "/"); |
| 42 | const dirs = dirCandidates ?? (await ide.getWorkspaceDirs()); |
| 43 | |
| 44 | if (dirs.length === 0) { |
| 45 | throw new Error("inferResolvedUriFromRelativePath: no dirs provided"); |
| 46 | } |
| 47 | |
| 48 | const segments = pathToUriPathSegment(relativePath).split("/"); |
| 49 | // Generate all possible suffixes from shortest to longest |
| 50 | const suffixes: string[] = []; |
| 51 | for (let i = segments.length - 1; i >= 0; i--) { |
| 52 | suffixes.push(segments.slice(i).join("/")); |
| 53 | } |
| 54 | |
| 55 | // For each suffix, try to find a unique matching dir/file |
| 56 | for (const suffix of suffixes) { |
| 57 | const uris = dirs.map((dir) => ({ |
| 58 | dir, |
| 59 | partialUri: joinEncodedUriPathSegmentToUri(dir, suffix), |
| 60 | })); |
| 61 | const promises = uris.map(async ({ partialUri, dir }) => { |
| 62 | const exists = await ide.fileExists(partialUri); |
| 63 | return { |
| 64 | dir, |
| 65 | partialUri, |
| 66 | exists, |
| 67 | }; |
| 68 | }); |
| 69 | const existenceChecks = await Promise.all(promises); |
| 70 | |
| 71 | const existingUris = existenceChecks.filter(({ exists }) => exists); |
| 72 | |
| 73 | // If exactly one directory matches, use it |
| 74 | if (existingUris.length === 1) { |
| 75 | return joinEncodedUriPathSegmentToUri( |
| 76 | existingUris[0].dir, |
| 77 | segments.join("/"), |
| 78 | ); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | // Sometimes the model will decide to only output the base name or small number of path parts |
| 83 | // in which case we shouldn't create a new file if it matches the current file |
| 84 | const activeFile = await ide.getCurrentFile(); |
| 85 | if (activeFile && activeFile.path.endsWith(relativePath)) { |
| 86 | return activeFile.path; |
| 87 | } |
| 88 | |
| 89 | // If no unique match found, use the first directory |
| 90 | return joinPathsToUri(dirs[0], relativePath); |
| 91 | } |