Skip to content

Commit a702af8

Browse files
committed
feat: Add support for replacements using CASE..WHEN..THEN filters and ED scripts
1 parent a24c736 commit a702af8

File tree

3 files changed

+334
-143
lines changed

3 files changed

+334
-143
lines changed

README.md

Lines changed: 62 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ editing_ by locating the exact line numbers and characters to change, which inde
7878
so on, allowing the _CEDARScript commands_ to focus instead on higher levels of abstraction, like
7979
[identifier](grammar.js#L248-L251) names, [line](grammar.js#L243-L246) markers, relative
8080
[indentations](grammar.js#L306-L370) and [positions](grammar.js#L241-L300)
81-
(`AFTER`, `BEFORE`, `INSIDE` a function, its `BODY`, at the `TOP` or `BOTTOM` of it...)
81+
(`AFTER`, `BEFORE`, `INTO` a function, its `BODY`, at the `TOP` or `BOTTOM` of it...)
8282

8383
## Key Features:
8484

@@ -148,16 +148,56 @@ IDEs can store the local history of files in CEDARScript format, and this can al
148148

149149
## Examples
150150

151-
Quick example:
151+
Quick example: turn a method into a top-level function, using `CASE` filter with REGEX:
152152

153153
```sql
154-
UPDATE FUNCTION "my_func"
155-
FROM FILE "functional.py"
156-
MOVE WHOLE
157-
INSERT BEFORE LINE "def get_config(self):"
154+
UPDATE FILE "baseconverter.py"
155+
MOVE FUNCTION "convert"
156+
INSERT BEFORE class "BaseConverter"
158157
RELATIVE INDENTATION 0;
158+
159+
-- Update the call sites in encode() and decode() methods to use the top-level convert() function
160+
UPDATE CLASS "BaseConverter"
161+
FROM FILE "baseconverter".py
162+
REPLACE BODY
163+
WITH CASE -- Filter each line in the function body through this CASE filter
164+
WHEN REGEX r"self\.convert\((.*?)\)"
165+
THEN REPLACE r"convert(\1)"
166+
END;
159167
```
160168

169+
Use an ED script to change a function:
170+
171+
```sql
172+
UPDATE FILE "app/main.py" REPLACE FUNCTION "calculate_total" WITH ED '''
173+
-- Add type hints to parameters
174+
1s/calculate_total(base_amount, tax_rate, discount, apply_shipping)/calculate_total(base_amount: float, tax_rate: float, discount: float, apply_shipping: bool) -> float/
175+
176+
-- Add docstring after function definition
177+
1a
178+
"""
179+
Calculate the total amount including tax, shipping, and discount.
180+
181+
Args:
182+
base_amount: Base price of the item
183+
tax_rate: Tax rate as decimal (e.g., 0.1 for 10%)
184+
discount: Discount as decimal (e.g., 0.2 for 20%)
185+
apply_shipping: Whether to add shipping cost
186+
187+
Returns:
188+
float: Final calculated amount rounded to 2 decimal places
189+
"""
190+
.
191+
192+
-- Add logging before return
193+
/return/i
194+
logger.info(f"Calculated total amount: {subtotal:.2f}")
195+
.
196+
''';
197+
```
198+
199+
200+
161201
There are [many more examples](test/corpus) to look at...
162202

163203
# Planned Features
@@ -203,10 +243,11 @@ Ideas to explore:
203243
# Future Work
204244

205245
1. [Tree-Sitter query language](https://cycode.com/blog/tips-for-using-tree-sitter-queries/) integration, which could open up many possibilities;
206-
2. Create a browser extension that allows web-chat interfaces of Large Language Models to tackle larger file changes;
207-
3. Select a model to fine-tune so that it natively understands `CEDARScript`;
208-
4. Provide language extensions that will improve how LLMs interact with other resource types;
209-
5. Explore using it as an **LLM-Tool Interface**;
246+
2. [Comby](https://github.com/comby-tools/comby) notation for an alternate syntax to express refactorings on code or data formats;
247+
3. Create a browser extension that allows web-chat interfaces of Large Language Models to tackle larger file changes;
248+
4. Select a model to fine-tune so that it natively understands `CEDARScript`;
249+
5. Provide language extensions that will improve how LLMs interact with other resource types;
250+
6. Explore using it as an **LLM-Tool Interface**;
210251

211252
## Tree-Sitter Query Language Integration
212253

@@ -283,6 +324,17 @@ WITH LINT
283324
MESSAGE "Direct import of system modules discouraged. Use custom wrappers instead.";
284325
```
285326

327+
## Comby Notation
328+
329+
To replace 'failUnlessEqual' with 'assertEqual':
330+
```sql
331+
UPDATE PROJECT
332+
REAFCTOR LANGUAGE "comby"
333+
WITH PATTERN '''
334+
comby 'failUnlessEqual(:[a],:[b])' 'assertEqual(:[a],:[b])' example.py
335+
'''
336+
```
337+
286338
## CEDARScript Browser Extension for LLM Web Interfaces
287339

288340
<details>

0 commit comments

Comments
 (0)