Skip to content

Commit 41b11d8

Browse files
authored
Eval Region: cursor after Discard node should evaluate child (#47)
1 parent 5594ed7 commit 41b11d8

File tree

5 files changed

+30
-12
lines changed

5 files changed

+30
-12
lines changed

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Changelog
2+
3+
## Unreleased
4+
5+
- Fix [#45](https://github.com/nextjournal/clojure-mode/issues/45): cursor after Discard node should evaluate child

public/squint/js/demo.mjs

+6-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { default_extensions, complete_keymap } from '@nextjournal/clojure-mode';
22
import { extension as eval_ext, cursor_node_string, top_level_string } from '@nextjournal/clojure-mode/extensions/eval-region';
33
import { EditorView, drawSelection, keymap } from '@codemirror/view';
44
import { EditorState } from '@codemirror/state';
5+
import { history, historyKeymap } from '@codemirror/commands';
56
import { syntaxHighlighting, defaultHighlightStyle, foldGutter } from '@codemirror/language';
67
import { compileStringEx } from 'squint-cljs';
78

@@ -89,10 +90,11 @@ let squintExtension = ( opts ) => {
8990
}])}
9091

9192

92-
let extensions = [ theme, foldGutter(),
93+
let extensions = [ history(), theme, foldGutter(),
9394
syntaxHighlighting(defaultHighlightStyle),
9495
drawSelection(),
9596
keymap.of(complete_keymap),
97+
keymap.of(historyKeymap),
9698
...default_extensions,
9799
eval_ext({modifier: "Meta"}),
98100
squintExtension({modifier: "Meta"})
@@ -120,6 +122,8 @@ let doc = `(comment
120122
(+ 1 2 3))
121123
` ;
122124

125+
// doc = `(do #_#_1 (+ 1 2 3) )`
126+
123127
evalCode(doc);
124128

125129
let state = EditorState.create( {doc: doc,
@@ -129,6 +133,7 @@ let editorElt = document.querySelector('#editor');
129133
let editor = new EditorView({state: state,
130134
parent: editorElt,
131135
extensions: extensions })
136+
globalThis.editor = editor;
132137

133138
let keys = {"ArrowUp": "↑",
134139
"ArrowDown": "↓",

squint-demo/index.mjs

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { EditorView, drawSelection, keymap } from '@codemirror/view';
44
import { EditorState } from '@codemirror/state';
55
import { syntaxHighlighting, defaultHighlightStyle, foldGutter } from '@codemirror/language';
66
import { javascript } from '@codemirror/lang-javascript';
7+
import { history, historyKeymap } from '@codemirror/commands';
78

89
let theme = EditorView.theme({
910
".cm-content": {whitespace: "pre-wrap",
@@ -25,12 +26,13 @@ let theme = EditorView.theme({
2526
"&.cm-focused .cm-cursor": {visibility: "visible"}
2627
});
2728

28-
let extensions = [
29+
let extensions = [ history(),
2930
theme,
3031
foldGutter(),
3132
syntaxHighlighting(defaultHighlightStyle),
3233
drawSelection(),
3334
keymap.of(complete_keymap),
35+
keymap.of(historyKeymap),
3436
...default_extensions
3537
];
3638

src-shared/nextjournal/clojure_mode/extensions/eval_region.cljc

+13-9
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,15 @@
1212
(defn uppermost-edge-here
1313
"Returns node or its highest ancestor that starts or ends at the cursor position."
1414
[pos node]
15-
(or (->> (iterate n/up node)
16-
(take-while (every-pred (complement n/top?)
17-
#(or (= pos (n/end %) (n/end node))
18-
(= pos (n/start %) (n/start node)))))
19-
(last))
20-
node))
15+
(let [node (or (->> (iterate n/up node)
16+
(take-while (every-pred (complement n/top?)
17+
#(or (= pos (n/end %) (n/end node))
18+
(= pos (n/start %) (n/start node)))))
19+
(last))
20+
node)]
21+
(if (= "Discard" (n/name node))
22+
(last (n/children node))
23+
node)))
2124

2225
(defn main-selection [state]
2326
(->
@@ -32,9 +35,10 @@
3235
(<= (n/start %) from)
3336
(<= (n/end %) from))
3437
(cond-> %
35-
(or (n/top? %)
36-
(and (not (n/terminal-type? (n/type %)))
37-
(< (n/start %) from (n/end %))))
38+
(or
39+
(n/top? %)
40+
(and (not (n/terminal-type? (n/type %)))
41+
(< (n/start %) from (n/end %))))
3842
(-> (n/children from -1) first))))
3943
(uppermost-edge-here from)
4044
(n/balanced-range state))))

test/nextjournal/clojure_mode_tests.cljc

+3-1
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,9 @@
331331
(= (f (test-utils/make-state extensions input)) expected)
332332
"(+ |1 2 3)" eval-region/cursor-node-string "1"
333333
"(+ |(+ 1 2) 2 3)" eval-region/cursor-node-string "(+ 1 2)"
334-
"(+ (+ 1 2)| 2 3)" eval-region/cursor-node-string "(+ 1 2)")
334+
"(+ (+ 1 2)| 2 3)" eval-region/cursor-node-string "(+ 1 2)"
335+
"(+ #_(+ 1 2)| 2 3)" eval-region/cursor-node-string "(+ 1 2)"
336+
"(+ #_#_1 (+ 1 2)| 2 3)" eval-region/cursor-node-string "(+ 1 2)")
335337
(let [state (test-utils/make-state extensions ";; dude\n|{:a 1}")]
336338
(is (= "{:a 1}" (->> (eval-region/top-level-node state)
337339
(util/range-str state)))))))

0 commit comments

Comments
 (0)