Skip to content

Commit 7356abb

Browse files
committed
Test corpus
1 parent 445ec69 commit 7356abb

File tree

11 files changed

+221
-15
lines changed

11 files changed

+221
-15
lines changed

src/cedarscript_editor/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"__version__", "find_commands", "CEDARScriptEditor"
88
]
99

10-
10+
# TODO Move to cedarscript-ast-parser
1111
def find_commands(content: str):
1212
# Regex pattern to match CEDARScript blocks
1313
pattern = r'```CEDARScript\n(.*?)```'

src/cedarscript_editor/cedarscript_editor.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import os
22
from collections.abc import Sequence
3+
from pathlib import Path
34

45
from cedarscript_ast_parser import Command, RmFileCommand, MvFileCommand, UpdateCommand, \
56
SelectCommand, IdentifierFromFile, Segment, Marker, MoveClause, DeleteClause, \
@@ -65,7 +66,7 @@ def __init__(self, command_ordinal: int, description: str):
6566

6667
class CEDARScriptEditor:
6768
def __init__(self, root_path: os.PathLike):
68-
self.root_path = os.path.abspath(root_path)
69+
self.root_path = Path(os.path.abspath(root_path))
6970
print(f'[{self.__class__.__name__}] root: {self.root_path}')
7071

7172
# TODO Add 'target_search_range: RangeSpec' parameter
@@ -257,14 +258,14 @@ def _rm_command(self, cmd: RmFileCommand):
257258
def _delete_function(self, cmd): # TODO
258259
file_path = os.path.join(self.root_path, cmd.file_path)
259260

260-
# def _create_command(self, cmd: CreateCommand):
261-
# file_path = os.path.join(self.root_path, cmd.file_path)
262-
#
263-
# os.makedirs(os.path.dirname(file_path), exist_ok=False)
264-
# with open(file_path, 'w') as file:
265-
# file.write(content)
266-
#
267-
# return f"Created file: {command['file']}"
261+
def _create_command(self, cmd: CreateCommand):
262+
file_path = os.path.join(self.root_path, cmd.file_path)
263+
264+
os.makedirs(os.path.dirname(file_path), exist_ok=True)
265+
with open(file_path, 'w') as file:
266+
file.write(cmd.content)
267+
268+
return f"Created file: {cmd.file_path}"
268269

269270

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

src/cedarscript_editor/tree_sitter_identifier_finder.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,18 @@ def find_identifier(source_info: tuple[str, str | Sequence[str]], search_rage: R
2424

2525
def _select_finder(file_path: str, source: str, search_range: RangeSpec = RangeSpec.EMPTY) -> IdentifierFinder:
2626
langstr = filename_to_lang(file_path)
27-
language = get_language(langstr)
28-
parser = get_parser(langstr)
29-
_log.info(f"[select_finder] Selected {language}")
27+
match langstr:
28+
case None:
29+
language = None
30+
query_info = None
31+
_log.info(f"[select_finder] NO LANGUAGE for `{file_path}`")
32+
case _:
33+
query_info = LANG_TO_TREE_SITTER_QUERY[langstr]
34+
language = get_language(langstr)
35+
_log.info(f"[select_finder] Selected {language}")
36+
tree = get_parser(langstr).parse(bytes(source, "utf-8"))
3037

31-
tree = parser.parse(bytes(source, "utf-8"))
3238
source = source.splitlines()
33-
query_info = LANG_TO_TREE_SITTER_QUERY[langstr]
3439

3540
def find_by_marker(mos: Marker | Segment) -> IdentifierBoundaries | RangeSpec | None:
3641
match mos:

tests/__init__.py

Whitespace-only changes.

tests/corpus/test-1/chat.xml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<train>
2+
<user>
3+
Refactor the `_candidate` method to be a stand alone, top level function.
4+
</user>
5+
<assistant>
6+
Here's my plan to implement the requested changes:
7+
1. Move the `_candidate()` method from the `A` class, placing it at the top level, just before the line where its class starts;
8+
2. Update the copied function to remove references to `self`;
9+
3. Update ALL call sites of the method `_candidate()` to call the new top-level function with the same name;
10+
11+
Here's the CEDARScript script:
12+
13+
```CEDARScript
14+
-- 1. Move the `_candidate()` method from the `A` class, placing it at the top level, just before the line where its class starts
15+
UPDATE CLASS "A" -- As method `calc1` exists in more than 1 class, we specify which one we want to reference
16+
FROM FILE "main.py"
17+
MOVE FUNCTION "_candidate"
18+
INSERT BEFORE CLASS "A"
19+
RELATIVE INDENTATION 0;
20+
21+
-- 2. Update the copied function to remove references to `self`
22+
UPDATE FUNCTION "_candidate"
23+
FROM FILE "main.py"
24+
REPLACE LINE "def _candidate(self, a, b, c):"
25+
WITH CONTENT '''
26+
@0:def _candidate(a, b, c):
27+
''';
28+
29+
-- 3. Update ALL call sites of the method `_candidate()` to call the new top-level function with the same name
30+
UPDATE FUNCTION "_check"
31+
FROM FILE "main.py"
32+
REPLACE LINE 'self._candidate(' -- There's only 1 call site and this line marker is unambiguous, so it's more concise to just replace a LINE instead of a SEGMENT
33+
WITH CONTENT '''
34+
@0:_candidate(
35+
''';
36+
```
37+
</assistant>
38+
</train>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
def _candidate(a, b, c):
2+
return a
3+
class A:
4+
def _calc(self, a):
5+
return a
6+
def _check(self, a: int, b, c):
7+
if a > 0:
8+
return self._calc(
9+
"a thing or two"
10+
)
11+
else:
12+
return list(
13+
self._calc(
14+
_candidate(
15+
a, "string 1", 4
16+
)
17+
for index, item in enumerate(a.the_elements)
18+
)
19+
)
20+

tests/corpus/test-1/main.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class A:
2+
def _calc(self, a):
3+
return a
4+
def _check(self, a: int, b, c):
5+
if a > 0:
6+
return self._calc(
7+
"a thing or two"
8+
)
9+
else:
10+
return list(
11+
self._calc(
12+
self._candidate(
13+
a, "string 1", 4
14+
)
15+
for index, item in enumerate(a.the_elements)
16+
)
17+
)
18+
19+
def _candidate(self, a, b, c):
20+
return a
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<no-train>
2+
```CEDARScript
3+
UPDATE FILE "main.something" DELETE LINE 'risus cursus'
4+
UPDATE FILE "main.something" DELETE LINE "etiam a"
5+
```
6+
</no-train>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Lorem ipsum odor amet,
2+
consectetuer adipiscing elit.
3+
Ligula vestibulum semper sagittis sapien class.
4+
Dolor nascetur litora feugiat urna,
5+
natoque venenatis fames.
6+
At elementum urna suspendisse
7+
himenaeos massa dui.
8+
Vivamus in
9+
ac vulputate dolor;
10+
amet pulvinar.
11+
Fames tempus habitasse
12+
parturient
13+
mollis.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
Lorem ipsum odor amet,
2+
consectetuer adipiscing elit.
3+
Ligula vestibulum semper sagittis sapien class.
4+
Dolor nascetur litora feugiat urna,
5+
natoque venenatis fames.
6+
At elementum urna suspendisse
7+
himenaeos massa dui.
8+
Vivamus in
9+
risus cursus
10+
ac vulputate dolor;
11+
amet pulvinar.
12+
Fames tempus habitasse
13+
etiam a
14+
parturient
15+
mollis.

0 commit comments

Comments
 (0)