MCPcopy Create free account
hub / github.com/Zoo-Code-Org/Zoo-Code / openFile

Function openFile

src/integrations/misc/open-file.ts:13–153  ·  view source on GitHub ↗
(filePath: string, options: OpenFileOptions = {})

Source from the content-addressed store, hash-verified

11}
12
13export 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) {

Callers 8

webviewMessageHandlerFunction · 0.90
handleCreateRuleFunction · 0.90
handleOpenRuleFileFunction · 0.90
handleOpenRulesDirectoryFunction · 0.90
handleCreateSkillFunction · 0.90
handleOpenSkillFileFunction · 0.90
openMentionFunction · 0.90
open-file.spec.tsFile · 0.90

Calls 13

getWorkspacePathFunction · 0.90
arePathsEqualFunction · 0.90
tFunction · 0.90
fileMethod · 0.80
statMethod · 0.80
executeCommandMethod · 0.80
writeFileMethod · 0.80
fromMethod · 0.80
openTextDocumentMethod · 0.80
showTextDocumentMethod · 0.80
warnMethod · 0.65
closeMethod · 0.65

Tested by

no test coverage detected