Skip to content

Commit d107a0e

Browse files
committed
Eval previous form at current level #118
1 parent 0d10dbf commit d107a0e

File tree

6 files changed

+56
-7
lines changed

6 files changed

+56
-7
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ __pycache__
44
.cpcache
55
TODO.md
66
.repl-port
7-
.nrepl-port
7+
.nrepl-port
8+
.clj-kondo

CHANGELOG.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
### WIP
2+
3+
- Eval previous form at current level #118
4+
15
### 4.0.0 - Aug 23, 2024
26

37
Syntax has been significantly reworked.
@@ -9,7 +13,7 @@ Syntax has been significantly reworked.
913
- Properly highlight `entity.name` in `def*` forms only at second position, skipping all meta/comments
1014
- Quote & syntax quote highlight following form as `meta.quoted` and `meta.quoted.syntax`
1115
- Metadata highlights following form as `meta.metadata`
12-
- Octal & arbitrary radix integers
16+
- Octal & arbitrary radix integers #71
1317
- Better keyword detection
1418

1519
Other changes:
@@ -18,8 +22,8 @@ Other changes:
1822
- Allow using `cljfmt` for formatting (requires `cljfmt` binary on `$PATH`)
1923
- Removed separate EDN syntax, merged with main Clojure (Sublimed)
2024
- Settings can now be specified in main `Preferences.sublime-settings` as well. Just prepend `clojure_sublimed_` to each setting’s name.
21-
- REPL can detect namespaces with meta on ns form
22-
- Detect `.shadow-cljs/nrepl.port` and `.shadow-cljs/socket-repl.port`
25+
- REPL can detect namespaces with meta on ns form #116
26+
- Detect `.shadow-cljs/nrepl.port` and `.shadow-cljs/socket-repl.port` #114
2327
- Connect commands now accept `timeout` argument for automation scenarios like “start clojure, start trying to connect to REPL until port is available”
2428

2529
### 3.8.0 - Aug 8, 2024

Clojure Sublimed Light.sublime-color-scheme

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"magenta": "#7A3E9D",
1414
"yellow": "#FFFABC",
1515
"orange": "#FFBC5D",
16-
"gray": "#808080",
16+
"gray": "#A0A0A0",
1717
},
1818
"globals":
1919
{

Default.sublime-commands

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@
3636
"caption": "Clojure Sublimed: Evaluate Buffer",
3737
"command": "clojure_sublimed_eval_buffer"
3838
},
39+
{
40+
"caption": "Clojure Sublimed: Evaluate Previous Form at Current Level",
41+
"command": "clojure_sublimed_eval_previous_form"
42+
},
3943
{
4044
"caption": "Clojure Sublimed: Interrupt Pending Evaluations",
4145
"command": "clojure_sublimed_interrupt_eval"

cs_eval.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ class ClojureSublimedEvalCommand(sublime_plugin.WindowCommand):
284284
"""
285285
Eval selected code or topmost form is selection is collapsed
286286
"""
287-
def run(self, print_quota = None, transform = None, expand = False):
287+
def run(self, regions = None, print_quota = None, transform = None, expand = False):
288288
view = self.window.active_view()
289289
state = cs_common.get_state(self.window)
290290

@@ -295,8 +295,26 @@ def run(self, print_quota = None, transform = None, expand = False):
295295
on_finish = None
296296
if expand:
297297
on_finish = lambda _: view.run_command("clojure_sublimed_toggle_info", {})
298+
299+
regions = [sublime.Region(a, b) for (a, b) in regions]
300+
state.conn.eval(view, regions or view.sel(), transform_fn = transform_fn, print_quota = print_quota, on_finish = on_finish)
301+
302+
def is_enabled(self):
303+
return self.window.active_view() and cs_conn.ready(self.window)
304+
305+
class ClojureSublimedEvalPreviousFormCommand(sublime_plugin.WindowCommand):
306+
def run(self, print_quota = None, transform = None, expand = False):
307+
view = self.window.active_view()
308+
regions = []
309+
310+
for sel in view.sel():
311+
if sel.empty():
312+
if form := cs_parser.previous_form_at_level(view, sel.begin()):
313+
regions.append((form.start, form.end))
298314

299-
state.conn.eval(view, view.sel(), transform_fn = transform_fn, print_quota = print_quota, on_finish = on_finish)
315+
if regions:
316+
args = {"regions": regions, "print_quota": print_quota, "transform": transform, "expand": expand}
317+
self.window.run_command("clojure_sublimed_eval", args)
300318

301319
def is_enabled(self):
302320
return self.window.active_view() and cs_conn.ready(self.window)

cs_parser.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,28 @@ def topmost_form(view, point):
419419
node = inner
420420
return sublime.Region(node.start, node.end)
421421

422+
def previous_form_at_level(view, point):
423+
"""
424+
Finds last form before cursor on the same level. E.g.
425+
426+
(+ 1 (* 2 3) 3)
427+
^ cursor
428+
^^^^^^^ result
429+
"""
430+
parsed = parse_tree(view)
431+
def find_previous(node):
432+
if not node:
433+
return
434+
children = node.body.children if node.body else node.children
435+
if not children:
436+
return
437+
for child in reversed(children):
438+
if child.end <= point:
439+
return child
440+
if child.start < point < child.end:
441+
return find_previous(child)
442+
return find_previous(parsed)
443+
422444
def namespace(view, point):
423445
"""
424446
Finds name of last namespace defined in buffer up to the point

0 commit comments

Comments
 (0)