(filePath: string, options: OpenFileOptions = {})
| 11 | } |
| 12 | |
| 13 | export async function openFile(filePath: string, options: OpenFileOptions = {}) { |
| 14 | try { |
| 15 | // Store the original path for error messages before any modifications |
| 16 | const originalFilePathForError = filePath |
| 17 | |
| 18 | // Try to decode the URI component, but if it fails, use the original path |
| 19 | try { |
| 20 | filePath = decodeURIComponent(filePath) |
| 21 | } catch (decodeError) { |
| 22 | // If decoding fails (e.g., invalid escape sequences), continue with the original path |
| 23 | console.warn(`Failed to decode file path: ${decodeError}. Using original path.`) |
| 24 | } |
| 25 | |
| 26 | const workspaceRoot = getWorkspacePath() |
| 27 | const homeDir = os.homedir() |
| 28 | |
| 29 | const attemptPaths: string[] = [] |
| 30 | |
| 31 | if (filePath.startsWith("./")) { |
| 32 | const relativePart = filePath.slice(2) |
| 33 | if (workspaceRoot) { |
| 34 | attemptPaths.push(path.join(workspaceRoot, relativePart)) |
| 35 | } |
| 36 | if (homeDir) { |
| 37 | const homePath = path.join(homeDir, relativePart) |
| 38 | // Add home path if it's different from what might have been added via workspaceRoot |
| 39 | // (e.g. if workspaceRoot itself is the home directory) |
| 40 | if (!attemptPaths.includes(homePath)) { |
| 41 | attemptPaths.push(homePath) |
| 42 | } |
| 43 | } |
| 44 | // If no workspace and no home, or if paths were identical. |
| 45 | if (attemptPaths.length === 0) { |
| 46 | attemptPaths.push(filePath) // Try the original relative path as a last resort |
| 47 | } |
| 48 | } else { |
| 49 | attemptPaths.push(filePath) // Assumed absolute or directly resolvable |
| 50 | } |
| 51 | |
| 52 | let fileStat: vscode.FileStat | undefined |
| 53 | let successfulUri: vscode.Uri | undefined |
| 54 | |
| 55 | for (const p of attemptPaths) { |
| 56 | try { |
| 57 | const tempUri = vscode.Uri.file(p) |
| 58 | fileStat = await vscode.workspace.fs.stat(tempUri) |
| 59 | successfulUri = tempUri // Path found |
| 60 | break // Exit loop once a path is successfully stated |
| 61 | } catch (e) { |
| 62 | // Stat failed for this path, continue to the next one |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | let uriToProcess: vscode.Uri |
| 67 | |
| 68 | if (fileStat && successfulUri) { |
| 69 | // Path was found |
| 70 | if (fileStat.type === vscode.FileType.Directory) { |
no test coverage detected