| 43 | } |
| 44 | |
| 45 | export function getDisplayPath( |
| 46 | filename: Uri | string | undefined, |
| 47 | workspaceFolders: readonly WorkspaceFolder[] | WorkspaceFolder[] = [], |
| 48 | homePathUri?: Uri |
| 49 | ) { |
| 50 | homePathUri = homePathUri || getHomeDir(); |
| 51 | let fileUri: Uri | undefined = undefined; |
| 52 | if (typeof filename && typeof filename === 'string') { |
| 53 | fileUri = Uri.file(filename); |
| 54 | } |
| 55 | if (typeof filename && typeof filename !== 'string') { |
| 56 | fileUri = filename; |
| 57 | } |
| 58 | const relativeToHome = getDisplayPathImpl(fileUri, undefined, homePathUri); |
| 59 | const workspaceFolderThatOwnsTheFile = workspaceFolders.find( |
| 60 | (w) => fileUri && uriPath.isEqualOrParent(fileUri, w.uri, true) |
| 61 | ); |
| 62 | // If there is more than one workspace folder, then we need to display the workspace folder name. |
| 63 | // This is because we can have two files with the same name in different workspace folders. |
| 64 | // E.g. .venv/bin/python can exist in multiple workspace folders. |
| 65 | if (workspaceFolders.length > 1 && workspaceFolderThatOwnsTheFile) { |
| 66 | return `${workspaceFolderThatOwnsTheFile.name}${path.sep}${getDisplayPathImpl( |
| 67 | fileUri, |
| 68 | workspaceFolderThatOwnsTheFile.uri, |
| 69 | homePathUri |
| 70 | )}`; |
| 71 | } |
| 72 | |
| 73 | const relativeToWorkspaceFolders = workspaceFolderThatOwnsTheFile |
| 74 | ? [getDisplayPathImpl(fileUri, workspaceFolderThatOwnsTheFile.uri, homePathUri)] |
| 75 | : []; |
| 76 | // Pick the shortest path for display purposes. |
| 77 | // As those are most likely relative to some workspace folder. |
| 78 | let bestDisplayPath = relativeToHome; |
| 79 | [relativeToHome, ...relativeToWorkspaceFolders].forEach((relativePath) => { |
| 80 | if (relativePath.length < bestDisplayPath.length) { |
| 81 | bestDisplayPath = relativePath; |
| 82 | } |
| 83 | }); |
| 84 | |
| 85 | return bestDisplayPath; |
| 86 | } |
| 87 | |
| 88 | function getDisplayPathImpl(file: Uri | undefined, cwd: Uri | undefined, homePath: Uri | undefined): string { |
| 89 | const isWindows = getOSType() === OSType.Windows; |