@@ -71,34 +71,32 @@ def get_commands(self) -> Iterator[Command]:
71
71
)
72
72
}
73
73
)
74
- async def open_file (self , file_path : Path ) -> str :
74
+ async def open_file (self , file_path : str | Path ) -> str :
75
75
"""Opens a file for editing or continued viewing;
76
76
creates it if it does not exist yet.
77
77
Note: If you only need to read or write a file once,
78
78
use `write_to_file` instead.
79
79
80
80
Args:
81
- file_path (Path): The path of the file to open
81
+ file_path (str | Path): The path of the file to open
82
82
83
83
Returns:
84
84
str: A status message indicating what happened
85
85
"""
86
- # Try to make the file path relative
87
- relative_file_path = None
88
- with contextlib .suppress (ValueError ):
89
- relative_file_path = file_path .relative_to (self .workspace .root )
86
+ if not isinstance (file_path , Path ):
87
+ file_path = Path (file_path )
90
88
91
89
created = False
92
90
if not self .workspace .exists (file_path ):
93
91
await self .workspace .write_file (file_path , "" )
94
92
created = True
95
93
96
- file_path = relative_file_path or file_path
94
+ # Try to make the file path relative
95
+ with contextlib .suppress (ValueError ):
96
+ file_path = file_path .relative_to (self .workspace .root )
97
97
98
98
file = FileContextItem (path = file_path )
99
-
100
99
self .context .add (file )
101
-
102
100
return (
103
101
f"File { file_path } { ' created,' if created else '' } has been opened"
104
102
" and added to the context ✅"
@@ -113,31 +111,29 @@ async def open_file(self, file_path: Path) -> str:
113
111
)
114
112
}
115
113
)
116
- def open_folder (self , path : Path ) -> str :
114
+ def open_folder (self , path : str | Path ) -> str :
117
115
"""Open a folder to keep track of its content
118
116
119
117
Args:
120
- path (Path): The path of the folder to open
118
+ path (str | Path): The path of the folder to open
121
119
122
120
Returns:
123
121
str: A status message indicating what happened
124
122
"""
125
- # Try to make the path relative
126
- relative_path = None
127
- with contextlib .suppress (ValueError ):
128
- relative_path = path .relative_to (self .workspace .root )
123
+ if not isinstance (path , Path ):
124
+ path = Path (path )
129
125
130
126
if not self .workspace .exists (path ):
131
127
raise FileNotFoundError (
132
128
f"open_folder { path } failed: no such file or directory"
133
129
)
134
130
135
- path = relative_path or path
131
+ # Try to make the path relative
132
+ with contextlib .suppress (ValueError ):
133
+ path = path .relative_to (self .workspace .root )
136
134
137
135
folder = FolderContextItem (path = path )
138
-
139
136
self .context .add (folder )
140
-
141
137
return f"Folder { path } has been opened and added to the context ✅"
142
138
143
139
@command (
0 commit comments