Skip to content

Commit 4e267b3

Browse files
committed
feat: Add support for line numbers and regular expressions in UPDATE FILE statements
1 parent 1f3cd1e commit 4e267b3

File tree

1 file changed

+44
-19
lines changed

1 file changed

+44
-19
lines changed

grammar.js

Lines changed: 44 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,22 @@ module.exports = grammar({
238238

239239
maxdepth_clause: $ => seq('MAX DEPTH', field('depth', $.number)),
240240

241+
/*TODO
242+
1 Add support for line numbers as reference points:
243+
UPDATE FILE "file.py"
244+
INSERT AFTER LINE 123
245+
WITH CONTENT '''...''';
246+
247+
2 Add support for regular expressions to match lines:
248+
UPDATE FILE "file.py"
249+
INSERT AFTER LINE REGEX r'@@\s*={10,}\s*PERFORMANCE\s+METRICS\s*={10,}\s*@@'
250+
WITH CONTENT '''...''';
251+
252+
3 Add support for "anchored" line markers that must match from start/end of line:
253+
UPDATE FILE "file.py"
254+
INSERT AFTER LINE START "@@ ============ PERFORMANCE METRICS"
255+
WITH CONTENT '''...''';
256+
*/
241257
// <specifying-locations-in-code>
242258
/**
243259
lineMarker: Points to specific line via its trimmed contents.
@@ -323,13 +339,31 @@ module.exports = grammar({
323339

324340
/**
325341
<details topic="Relative Indent Strings">
326-
<summary>A relative indent prefix is used within strings in CONTENT blocks to simplify matching indentation with the existing code being changed</summary>
327-
<p>Syntax:</p>
328-
<ol>
329-
<li>`@N:` is the relative indent prefix</li>
330-
<li>`N` is an integer representing the relative indent *level* (can be negative)</li>
331-
<li>`content` is the actual code or text for that line</li>
332-
</ol>
342+
<summary>A *relative* indent prefix is used for each line within strings in CONTENT blocks to simplify matching indentation with the existing code being changed</summary>
343+
<syntax>
344+
The patter is @N:line where:
345+
- `@N:` is the *RELATIVE* indent prefix;
346+
- `N` is an integer representing the relative indent *level* (can be negative) compared to the *reference point*;
347+
- `line` is the actual content for that line (comes immediately after prefix)
348+
</syntax>
349+
<prefix-examples>
350+
- `@0:` Same level as reference point
351+
- `@1:` 1 more indent level than reference point
352+
- `@-1:` 1 less indent level than reference point</li>
353+
</prefix-examples>
354+
<reference-point>
355+
Some reference points:
356+
- AFTER/BEFORE LINE/FUNCTION/CLASS: The referenced item (rather than one line after or before it)
357+
- INSERT INSIDE FUNCTION/CLASS TOP/BOTTOM: The function/class body
358+
- `BODY`: The body of the function/class being modified
359+
- `WHOLE`: The first line of the block where the function/class etc is defined
360+
</reference-point>
361+
<key-points>
362+
The relative indent level *MUST* change logically with code structure:
363+
- Increment N when entering a nested block (if/for/while/try etc...)
364+
- Decrement N when exiting a nested block
365+
NOTE: If you get `E999 IndentationError` message or any other indentation error, check that your relative indent levels follow these rules.
366+
</key-points>
333367
<examples>
334368
<li>'@7:single-quote-string'</li>
335369
<li>"@-3:double-quote-string"</li>
@@ -338,22 +372,12 @@ module.exports = grammar({
338372
@0:multi
339373
@-1:line
340374
'''</li>
341-
<li>"""
375+
<li>\"\"\"
342376
@0:multi
343377
@-1:line
344-
"""</li>
378+
\"\"\"</li>
345379
</examples>
346380
347-
<p>Key points:</p>
348-
<ol>
349-
<li>Each line must start with `@N:` where `N` represents the indentation level</li>
350-
<li>Indentation level *MUST* change logically with code structure:
351-
- *MUST* increment N when entering a new block (class body, function body, if statement, loop, etc.)
352-
- *MUST* Decrement N when exiting a block
353-
</li>
354-
<li>The actual content follows immediately after the prefix (@N:)</li>
355-
</ol>
356-
357381
<example>
358382
[...] WITH CONTENT '''
359383
@0:class myClass:
@@ -370,6 +394,7 @@ module.exports = grammar({
370394
Remember: The relative indentation prefix (@N:) is used to indicate the logical structure
371395
of the code. The CEDARScript interpreter will handle the actual formatting and indentation
372396
in the target code file.
397+
</details>
373398
*/
374399
content_clause: $ => seq('CONTENT', field('content', $.string)),
375400

0 commit comments

Comments
 (0)