Skip to content

Commit f545579

Browse files
committed
Implement CreateCommand
1 parent 4656784 commit f545579

File tree

4 files changed

+61
-9
lines changed

4 files changed

+61
-9
lines changed

src/cedarscript_editor/cedarscript_editor.py

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from pathlib import Path
44

55
from cedarscript_ast_parser import Command, RmFileCommand, MvFileCommand, UpdateCommand, \
6-
SelectCommand, IdentifierFromFile, Segment, Marker, MoveClause, DeleteClause, \
6+
SelectCommand, CreateCommand, IdentifierFromFile, Segment, Marker, MoveClause, DeleteClause, \
77
InsertClause, ReplaceClause, EditingAction, BodyOrWhole, RegionClause, MarkerType
88
from cedarscript_ast_parser.cedarscript_ast_parser import MarkerCompatible, RelativeMarker, \
99
RelativePositionType
@@ -78,8 +78,8 @@ def apply_commands(self, commands: Sequence[Command]):
7878
match command:
7979
case UpdateCommand() as cmd:
8080
result.append(self._update_command(cmd))
81-
# case CreateCommand() as cmd:
82-
# result.append(self._create_command(cmd))
81+
case CreateCommand() as cmd:
82+
result.append(self._create_command(cmd))
8383
case RmFileCommand() as cmd:
8484
result.append(self._rm_command(cmd))
8585
case MvFileCommand():
@@ -255,14 +255,33 @@ def _rm_command(self, cmd: RmFileCommand):
255255
def _delete_function(self, cmd): # TODO
256256
file_path = os.path.join(self.root_path, cmd.file_path)
257257

258-
def _create_command(self, cmd: CreateCommand):
259-
file_path = os.path.join(self.root_path, cmd.file_path)
258+
def _create_command(self, cmd: CreateCommand) -> str:
259+
"""Handle the CREATE command to create new files with content.
260+
261+
Args:
262+
cmd: The CreateCommand instance containing file_path and content
260263
261-
os.makedirs(os.path.dirname(file_path), exist_ok=True)
262-
with open(file_path, 'w') as file:
263-
file.write(cmd.content)
264+
Returns:
265+
str: A message describing the result
264266
265-
return f"Created file: {cmd.file_path}"
267+
Raises:
268+
ValueError: If the file already exists
269+
"""
270+
file_path = os.path.join(self.root_path, cmd.file_path)
271+
272+
if os.path.exists(file_path):
273+
raise ValueError(f"File already exists: {cmd.file_path}")
274+
275+
os.makedirs(os.path.dirname(file_path), exist_ok=True)
276+
277+
content = cmd.content
278+
if isinstance(content, (list, tuple)):
279+
content = '\n'.join(content)
280+
281+
# Process relative indentation in content
282+
write_file(file_path, IndentationInfo.default().apply_relative_indents(content))
283+
284+
return f"Created file: {cmd.file_path}"
266285

267286

268287
def find_index_range_for_region(region: BodyOrWhole | Marker | Segment | RelativeMarker,

src/text_manipulation/indentation_kit.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,10 @@ class IndentationInfo(NamedTuple):
106106
consistency: bool = True
107107
message: str | None = None
108108

109+
@classmethod
110+
def default(cls) -> 'IndentationInfo':
111+
return cls(4, ' ', 0)
112+
109113
@classmethod
110114
def from_content(cls, content: str | Sequence[str]) -> 'IndentationInfo':
111115
"""

tests/corpus/create/chat.xml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<no-train>
2+
```CEDARScript
3+
CREATE FILE "1.py"
4+
WITH CONTENT '''
5+
@0:def calculate_sum(a, b):
6+
@1:"""Calculate sum of two numbers.
7+
@0:
8+
@1:Args:
9+
@2:a: First number
10+
@2:b: Second number
11+
@0:
12+
@1:Returns:
13+
@2:Sum of a and b
14+
@1:"""
15+
@1:return a + b
16+
''';
17+
```
18+
</no-train>

tests/corpus/create/expected.1.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
def calculate_sum(a, b):
2+
"""Calculate sum of two numbers.
3+
4+
Args:
5+
a: First number
6+
b: Second number
7+
8+
Returns:
9+
Sum of a and b
10+
"""
11+
return a + b

0 commit comments

Comments
 (0)