Skip to content

Commit 9f5f655

Browse files
committed
Reorganization
1 parent 0cfa8e9 commit 9f5f655

File tree

6 files changed

+43
-15
lines changed

6 files changed

+43
-15
lines changed

src/cedarscript_editor/__init__.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,21 @@
88
"read_file", "write_file", "bow_to_search_range"
99
]
1010

11-
__all__ = ["CEDARScriptEditor"]
12-
11+
def find_commands(content: str):
12+
# Regex pattern to match CEDARScript blocks
13+
pattern = r'```CEDARScript\n(.*?)```'
14+
cedar_script_blocks = re.findall(pattern, content, re.DOTALL)
15+
print(f'[find_cedar_commands] Script block count: {len(cedar_script_blocks)}')
16+
if len(cedar_script_blocks) == 0:
17+
raise ValueError(
18+
"No CEDARScript block detected. "
19+
"Perhaps you forgot to enclose the block using ```CEDARScript and ``` ? "
20+
"Or was that intentional? If so, just write tag <NOSCRIPT/> and nothing else."
21+
)
22+
cedarscript_parser = CEDARScriptASTParser()
23+
for cedar_script in cedar_script_blocks:
24+
parsed_commands, parse_errors = cedarscript_parser.parse_script(cedar_script)
25+
if parse_errors:
26+
raise ValueError(f"CEDARScript parsing errors: {[str(pe) for pe in parse_errors]}")
27+
for cedar_command in parsed_commands:
28+
yield cedar_command

src/cedarscript_editor/cedarscript_editor.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
InsertClause, ReplaceClause, EditingAction, BodyOrWhole, RegionClause, MarkerType
88
from cedarscript_ast_parser.cedarscript_ast_parser import MarkerCompatible, RelativeMarker, \
99
RelativePositionType
10-
from ..text_manipulation.indentation_kit import IndentationInfo
11-
from ..text_manipulation.range_spec import IdentifierBoundaries, RangeSpec
12-
from ..text_manipulation.text_editor_kit import read_file, write_file, bow_to_search_range
10+
from text_manipulation.indentation_kit import IndentationInfo
11+
from text_manipulation.range_spec import IdentifierBoundaries, RangeSpec
12+
from text_manipulation.text_editor_kit import read_file, write_file, bow_to_search_range
1313

1414
from .identifier_selector import select_finder
1515

src/text_manipulation/__init__.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from .indentation_kit import IndentationInfo
2+
from .range_spec import IdentifierBoundaries, RangeSpec
3+
from .text_editor_kit import read_file, write_file, bow_to_search_range
4+
5+
__all__ = [
6+
"IndentationInfo",
7+
"IdentifierBoundaries",
8+
"RangeSpec",
9+
"read_file",
10+
"write_file",
11+
"bow_to_search_range",
12+
]

src/text_manipulation/indentation_kit.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class IndentationInfo(NamedTuple):
6767
message: str | None = None
6868

6969
@classmethod
70-
def from_content[T: IndentationInfo, S: Sequence[str]](cls: T, content: str | S) -> T:
70+
def from_content(cls, content: str | Sequence[str]) -> 'IndentationInfo':
7171
"""
7272
Analyzes the indentation in the given content and creates an IndentationInfo instance.
7373
@@ -185,7 +185,7 @@ def adjust_line(line: str) -> str:
185185
return new_indent + line.lstrip()
186186
return adjust_line
187187

188-
def apply_relative_indents[S: Sequence[str]](self, content: str | S, context_indent_count: int = 0) -> list[str]:
188+
def apply_relative_indents(self, content: str | Sequence[str], context_indent_count: int = 0) -> list[str]:
189189
"""
190190
Applies relative indentation based on annotations in the content.
191191

src/text_manipulation/range_spec.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from typing import NamedTuple
44

55
from cedarscript_ast_parser import Marker, RelativeMarker, RelativePositionType, MarkerType, BodyOrWhole
6-
from text_manipulation.indentation_kit import get_line_indent_count
6+
from .indentation_kit import get_line_indent_count
77

88
MATCH_TYPES = ('exact', 'stripped', 'normalized', 'partial')
99

@@ -36,13 +36,13 @@ def inc(self, count: int = 1):
3636
def dec(self, count: int = 1):
3737
return self._replace(start=self.start - count, end=self.end - count)
3838

39-
def read[S: Sequence[str]](self, src: S) -> S:
39+
def read(self, src: Sequence[str]) -> Sequence[str]:
4040
return src[self.start:self.end]
4141

42-
def write[S: Sequence[str]](self, src: S, target: S):
42+
def write(self, src: Sequence[str], target: Sequence[str]):
4343
target[self.start:self.end] = src
4444

45-
def delete[S: Sequence[str]](self, src: S) -> S:
45+
def delete(self, src: Sequence[str]) -> Sequence[str]:
4646
result = self.read(src)
4747
del src[self.start:self.end]
4848
return result
@@ -52,12 +52,12 @@ def normalize_line(line: str):
5252
return re.sub(r'[^\w]', '.', line.strip(), flags=re.UNICODE)
5353

5454
@classmethod
55-
def from_line_marker[T: RangeSpec](
56-
cls: T,
55+
def from_line_marker(
56+
cls,
5757
lines: Sequence[str],
5858
search_term: Marker,
5959
search_range: 'RangeSpec' = None
60-
) -> T | None:
60+
):
6161
"""
6262
Find the index of a specified line within a list of strings, considering different match types and an offset.
6363

src/text_manipulation/text_editor_kit.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from typing import Protocol, runtime_checkable
33

44
from cedarscript_ast_parser import Marker, RelativeMarker, RelativePositionType, Segment, MarkerType, BodyOrWhole
5-
from text_manipulation.range_spec import IdentifierBoundaries, RangeSpec
5+
from .range_spec import IdentifierBoundaries, RangeSpec
66

77

88
def read_file(file_path: str) -> str:

0 commit comments

Comments
 (0)