@@ -5,80 +5,40 @@ import {props} from "@nextjournal/lezer-clojure"
55import { evalString } from "./sci"
66import { NodeProp } from "@lezer/common"
77
8- // Node props are marked in the grammar and distinguish categories of nodes
9-
10- // primitive collection
11- const collProp = props . coll
12- // prefix collection - a prefix token that wraps the next element
13- const prefixCollProp = props . prefixColl
14- // the prefix edge itself
15- const prefixEdgeProp = props . prefixEdge
16- // prefix form - pair of [metadata, target]
17- const prefixContainerProp = props . prefixContainer
18- // edges at the beginning/end of collections, + "same" edges (string quotes)
19- const startEdgeProp = NodeProp . closedBy
20- const endEdgeProp = NodeProp . openedBy
21- const sameEdgeProp = props . sameEdge
228const up = ( node ) => node . parent ;
239const isTopType = ( nodeType ) => nodeType . isTop
2410const isTop = ( node ) => isTopType ( node . type )
2511const mainSelection = ( state ) => state . selection . asSingle ( ) . ranges [ 0 ]
26-
27- function tree ( state , pos , dir ) {
28- switch ( arguments [ "length" ] ) {
29- case 1 :
30- return syntaxTree ( state ) ;
31- case 2 :
32- return syntaxTree ( state ) . resolveInner ( pos ) ;
33- case 3 :
34- return syntaxTree ( state ) . resolveInner ( pos , dir ) ;
35- }
36- }
37-
12+ const tree = ( state , pos , dir ) => syntaxTree ( state ) . resolveInner ( pos , dir )
3813const nearestTouching = ( state , pos ) => tree ( state , pos , - 1 )
3914
40- const isTerminalType = ( nodeType ) => {
41- if ( isTopType ( nodeType || nodeType . prefixCollProp . prop ( ) ||
42- nodeType . collProp . prop ( ) || nodeType . name == "Meta" ||
43- nodeType . name == "TaggedLiteral" || nodeType . name == "ConstructorCall" ) ) {
44- return false
45- } else {
46- return true
47- }
48- }
49-
5015const children = ( parent , from , dir ) => {
5116 let child = parent . childBefore ( from )
5217 return children ( parent , child . from ) . unshift ( child )
5318}
5419
55- function parents ( node , p ) {
20+ const parents = ( node , p ) => {
5621 if ( isTop ( node ) ) return p ;
57- return parents ( up ( node ) , p . concat ( node ) ) ;
22+ return parents ( up ( node ) , p . concat ( node ) )
5823}
5924
6025const rangeStr = ( state , selection ) => state . doc . slice ( selection . from , selection . to ) . toString ( )
6126
6227// Return node or its highest parent that ends at the cursor position
63- function uppermostEdge ( pos , node ) {
28+ const uppermostEdge = ( pos , node ) => {
6429 const p = parents ( node , [ ] ) . filter ( n => pos == n . to && pos == node . to ) ;
6530 return p [ p . length - 1 ] || node
6631}
6732
68- function isTerminal ( node , pos ) {
69- return isTerminalType ( node . type ) ||
70- pos === node . from || pos === node . to
71- }
72-
73- function nodeAtCursor ( state ) {
33+ const nodeAtCursor = ( state ) => {
7434 const pos = mainSelection ( state ) . from
7535 const n = nearestTouching ( state , pos )
7636 return uppermostEdge ( pos , n )
7737}
7838
7939let posAtFormEnd = 0
8040
81- function topLevelNode ( state ) {
41+ const topLevelNode = ( state ) => {
8242 const pos = mainSelection ( state ) . from
8343 const p = parents ( nearestTouching ( state , pos ) , [ ] )
8444 if ( p . length === 0 ) {
@@ -95,7 +55,7 @@ let evalResult = ""
9555let codeBeforeEval = ""
9656let posBeforeEval = 0
9757
98- function updateEditor ( view , text , pos ) {
58+ const updateEditor = ( view , text , pos ) => {
9959 const doc = view . state . doc . toString ( )
10060 codeBeforeEval = doc
10161 const end = doc . length
@@ -107,14 +67,14 @@ function updateEditor(view, text, pos) {
10767
10868export function tryEval ( s ) {
10969 try {
110- return evalString ( s )
70+ return evalString ( s ) . trimEnd ( )
11171 } catch ( err ) {
11272 console . log ( err )
11373 return "\nError: " + err . message
11474 }
11575}
11676
117- function evalAtCursor ( view ) {
77+ const evalAtCursor = ( view ) => {
11878 const doc = view . state . doc . toString ( )
11979 codeBeforeEval = doc
12080 posBeforeEval = view . state . selection . main . head
@@ -127,14 +87,14 @@ function evalAtCursor(view) {
12787 return true
12888}
12989
130- function clearEval ( view ) {
90+ const clearEval = ( view ) => {
13191 if ( evalResult . length != 0 ) {
13292 evalResult = ""
13393 updateEditor ( view , codeBeforeEval , posBeforeEval )
13494 }
13595}
13696
137- function evalTopLevel ( view ) {
97+ const evalTopLevel = ( view ) => {
13898 posAtFormEnd = topLevelNode ( view . state ) . to
13999 const doc = view . state . doc . toString ( )
140100 posBeforeEval = view . state . selection . main . head
@@ -147,8 +107,9 @@ function evalTopLevel(view) {
147107 return true
148108}
149109
150- function evalCell ( view ) {
110+ const evalCell = ( view ) => {
151111 const doc = view . state . doc . toString ( )
112+ posBeforeEval = view . state . selection . main . head
152113 evalResult = tryEval ( view . state . doc . text . join ( " " ) )
153114 const codeWithResult = doc + "\n" + " => " + evalResult
154115 updateEditor ( view , codeWithResult , posBeforeEval )
0 commit comments