|
| 1 | +import { Controller } from ".." |
| 2 | +import { RelativePathsRequest, RelativePaths } from "@shared/proto/file" |
| 3 | +import { FileMethodHandler } from "./index" |
| 4 | +import * as vscode from "vscode" |
| 5 | +import * as path from "path" |
| 6 | + |
| 7 | +/** |
| 8 | + * Converts a list of URIs to workspace-relative paths |
| 9 | + * @param controller The controller instance |
| 10 | + * @param request The request containing URIs to convert |
| 11 | + * @returns Response with resolved relative paths |
| 12 | + */ |
| 13 | +export const getRelativePaths: FileMethodHandler = async ( |
| 14 | + controller: Controller, |
| 15 | + request: RelativePathsRequest, |
| 16 | +): Promise<RelativePaths> => { |
| 17 | + const resolvedPaths = await Promise.all( |
| 18 | + request.uris.map(async (uriString) => { |
| 19 | + try { |
| 20 | + const fileUri = vscode.Uri.parse(uriString, true) |
| 21 | + const relativePathToGet = vscode.workspace.asRelativePath(fileUri, false) |
| 22 | + |
| 23 | + // If the path is still absolute, it's outside the workspace |
| 24 | + if (path.isAbsolute(relativePathToGet)) { |
| 25 | + console.warn(`Dropped file ${relativePathToGet} is outside the workspace. Sending original path.`) |
| 26 | + return fileUri.fsPath.replace(/\\/g, "/") |
| 27 | + } else { |
| 28 | + let finalPath = "/" + relativePathToGet.replace(/\\/g, "/") |
| 29 | + try { |
| 30 | + const stat = await vscode.workspace.fs.stat(fileUri) |
| 31 | + if (stat.type === vscode.FileType.Directory) { |
| 32 | + finalPath += "/" |
| 33 | + } |
| 34 | + } catch (statError) { |
| 35 | + console.error(`Error stating file ${fileUri.fsPath}:`, statError) |
| 36 | + } |
| 37 | + return finalPath |
| 38 | + } |
| 39 | + } catch (error) { |
| 40 | + console.error(`Error calculating relative path for ${uriString}:`, error) |
| 41 | + return null |
| 42 | + } |
| 43 | + }), |
| 44 | + ) |
| 45 | + |
| 46 | + // Filter out any null values from errors |
| 47 | + const validPaths = resolvedPaths.filter((path): path is string => path !== null) |
| 48 | + |
| 49 | + return RelativePaths.create({ paths: validPaths }) |
| 50 | +} |
0 commit comments