Skip to content

Commit 96b970d

Browse files
committed
Execute code from inside top-level ; ... and #_... (closes #124)
1 parent 6a41a97 commit 96b970d

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
- Remove bg on quoted strings inside metadata
44
- Handle eval of `#_` forms in nREPL JVM
5+
- Execute code from inside top-level `; ...` and `#_...` #124
56

67
### 4.2.2 - Sep 27, 2024
78

cs_parser.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ def __getattr__(self, name):
2929
if child.name == "." + name:
3030
return child
3131

32+
def restore_text(self, text):
33+
return text[self.start:self.end]
34+
3235
class Named:
3336
"""
3437
Parser that assigns name to Node
@@ -411,7 +414,22 @@ def topmost_form(view, point):
411414

412415
parsed = parse_tree(view)
413416
if node := search(parsed, point, max_depth = 1):
414-
if body := node.body:
417+
# look inside ;; ...
418+
if node.name == "comment":
419+
comment_start = node.start
420+
comment_text = node.restore_text(view.substr(sublime.Region(0, view.size())))
421+
comment_inside_start = re.match(r'^[; ]*', comment_text).end()
422+
delta = comment_start + comment_inside_start
423+
comment_inside_text = comment_text[comment_inside_start:]
424+
parsed2 = parse(comment_inside_text)
425+
if node2 := search(parsed2, point - delta, max_depth = 1):
426+
return sublime.Region(delta + node2.start, delta + node2.end)
427+
# look inside #_...
428+
elif node.name == "discard" and node.body.children[0] and point >= node.body.start:
429+
node2 = node.body.children[0]
430+
return sublime.Region(node2.start, node2.end)
431+
# look inside (comment ...)
432+
elif body := node.body:
415433
if body.children:
416434
first_form = body.children[0]
417435
if first_form.name == "token" and first_form.text == "comment" and point > first_form.end:

0 commit comments

Comments
 (0)