Skip to content

Commit

Permalink
Eval Region: cursor after Discard node should evaluate child (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
borkdude authored Dec 12, 2023
1 parent 5594ed7 commit 41b11d8
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 12 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Changelog

## Unreleased

- Fix [#45](https://github.com/nextjournal/clojure-mode/issues/45): cursor after Discard node should evaluate child
7 changes: 6 additions & 1 deletion public/squint/js/demo.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { default_extensions, complete_keymap } from '@nextjournal/clojure-mode';
import { extension as eval_ext, cursor_node_string, top_level_string } from '@nextjournal/clojure-mode/extensions/eval-region';
import { EditorView, drawSelection, keymap } from '@codemirror/view';
import { EditorState } from '@codemirror/state';
import { history, historyKeymap } from '@codemirror/commands';
import { syntaxHighlighting, defaultHighlightStyle, foldGutter } from '@codemirror/language';
import { compileStringEx } from 'squint-cljs';

Expand Down Expand Up @@ -89,10 +90,11 @@ let squintExtension = ( opts ) => {
}])}


let extensions = [ theme, foldGutter(),
let extensions = [ history(), theme, foldGutter(),
syntaxHighlighting(defaultHighlightStyle),
drawSelection(),
keymap.of(complete_keymap),
keymap.of(historyKeymap),
...default_extensions,
eval_ext({modifier: "Meta"}),
squintExtension({modifier: "Meta"})
Expand Down Expand Up @@ -120,6 +122,8 @@ let doc = `(comment
(+ 1 2 3))
` ;

// doc = `(do #_#_1 (+ 1 2 3) )`

evalCode(doc);

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

let keys = {"ArrowUp": "↑",
"ArrowDown": "↓",
Expand Down
4 changes: 3 additions & 1 deletion squint-demo/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { EditorView, drawSelection, keymap } from '@codemirror/view';
import { EditorState } from '@codemirror/state';
import { syntaxHighlighting, defaultHighlightStyle, foldGutter } from '@codemirror/language';
import { javascript } from '@codemirror/lang-javascript';
import { history, historyKeymap } from '@codemirror/commands';

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

let extensions = [
let extensions = [ history(),
theme,
foldGutter(),
syntaxHighlighting(defaultHighlightStyle),
drawSelection(),
keymap.of(complete_keymap),
keymap.of(historyKeymap),
...default_extensions
];

Expand Down
22 changes: 13 additions & 9 deletions src-shared/nextjournal/clojure_mode/extensions/eval_region.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@
(defn uppermost-edge-here
"Returns node or its highest ancestor that starts or ends at the cursor position."
[pos node]
(or (->> (iterate n/up node)
(take-while (every-pred (complement n/top?)
#(or (= pos (n/end %) (n/end node))
(= pos (n/start %) (n/start node)))))
(last))
node))
(let [node (or (->> (iterate n/up node)
(take-while (every-pred (complement n/top?)
#(or (= pos (n/end %) (n/end node))
(= pos (n/start %) (n/start node)))))
(last))
node)]
(if (= "Discard" (n/name node))
(last (n/children node))
node)))

(defn main-selection [state]
(->
Expand All @@ -32,9 +35,10 @@
(<= (n/start %) from)
(<= (n/end %) from))
(cond-> %
(or (n/top? %)
(and (not (n/terminal-type? (n/type %)))
(< (n/start %) from (n/end %))))
(or
(n/top? %)
(and (not (n/terminal-type? (n/type %)))
(< (n/start %) from (n/end %))))
(-> (n/children from -1) first))))
(uppermost-edge-here from)
(n/balanced-range state))))
Expand Down
4 changes: 3 additions & 1 deletion test/nextjournal/clojure_mode_tests.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,9 @@
(= (f (test-utils/make-state extensions input)) expected)
"(+ |1 2 3)" eval-region/cursor-node-string "1"
"(+ |(+ 1 2) 2 3)" eval-region/cursor-node-string "(+ 1 2)"
"(+ (+ 1 2)| 2 3)" eval-region/cursor-node-string "(+ 1 2)")
"(+ (+ 1 2)| 2 3)" eval-region/cursor-node-string "(+ 1 2)"
"(+ #_(+ 1 2)| 2 3)" eval-region/cursor-node-string "(+ 1 2)"
"(+ #_#_1 (+ 1 2)| 2 3)" eval-region/cursor-node-string "(+ 1 2)")
(let [state (test-utils/make-state extensions ";; dude\n|{:a 1}")]
(is (= "{:a 1}" (->> (eval-region/top-level-node state)
(util/range-str state)))))))
Expand Down

0 comments on commit 41b11d8

Please sign in to comment.