@@ -5,80 +5,40 @@ import {props} from "@nextjournal/lezer-clojure"
5
5
import { evalString } from "./sci"
6
6
import { NodeProp } from "@lezer/common"
7
7
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
22
8
const up = ( node ) => node . parent ;
23
9
const isTopType = ( nodeType ) => nodeType . isTop
24
10
const isTop = ( node ) => isTopType ( node . type )
25
11
const 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 )
38
13
const nearestTouching = ( state , pos ) => tree ( state , pos , - 1 )
39
14
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
-
50
15
const children = ( parent , from , dir ) => {
51
16
let child = parent . childBefore ( from )
52
17
return children ( parent , child . from ) . unshift ( child )
53
18
}
54
19
55
- function parents ( node , p ) {
20
+ const parents = ( node , p ) => {
56
21
if ( isTop ( node ) ) return p ;
57
- return parents ( up ( node ) , p . concat ( node ) ) ;
22
+ return parents ( up ( node ) , p . concat ( node ) )
58
23
}
59
24
60
25
const rangeStr = ( state , selection ) => state . doc . slice ( selection . from , selection . to ) . toString ( )
61
26
62
27
// Return node or its highest parent that ends at the cursor position
63
- function uppermostEdge ( pos , node ) {
28
+ const uppermostEdge = ( pos , node ) => {
64
29
const p = parents ( node , [ ] ) . filter ( n => pos == n . to && pos == node . to ) ;
65
30
return p [ p . length - 1 ] || node
66
31
}
67
32
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 ) => {
74
34
const pos = mainSelection ( state ) . from
75
35
const n = nearestTouching ( state , pos )
76
36
return uppermostEdge ( pos , n )
77
37
}
78
38
79
39
let posAtFormEnd = 0
80
40
81
- function topLevelNode ( state ) {
41
+ const topLevelNode = ( state ) => {
82
42
const pos = mainSelection ( state ) . from
83
43
const p = parents ( nearestTouching ( state , pos ) , [ ] )
84
44
if ( p . length === 0 ) {
@@ -95,7 +55,7 @@ let evalResult = ""
95
55
let codeBeforeEval = ""
96
56
let posBeforeEval = 0
97
57
98
- function updateEditor ( view , text , pos ) {
58
+ const updateEditor = ( view , text , pos ) => {
99
59
const doc = view . state . doc . toString ( )
100
60
codeBeforeEval = doc
101
61
const end = doc . length
@@ -107,14 +67,14 @@ function updateEditor(view, text, pos) {
107
67
108
68
export function tryEval ( s ) {
109
69
try {
110
- return evalString ( s )
70
+ return evalString ( s ) . trimEnd ( )
111
71
} catch ( err ) {
112
72
console . log ( err )
113
73
return "\nError: " + err . message
114
74
}
115
75
}
116
76
117
- function evalAtCursor ( view ) {
77
+ const evalAtCursor = ( view ) => {
118
78
const doc = view . state . doc . toString ( )
119
79
codeBeforeEval = doc
120
80
posBeforeEval = view . state . selection . main . head
@@ -127,14 +87,14 @@ function evalAtCursor(view) {
127
87
return true
128
88
}
129
89
130
- function clearEval ( view ) {
90
+ const clearEval = ( view ) => {
131
91
if ( evalResult . length != 0 ) {
132
92
evalResult = ""
133
93
updateEditor ( view , codeBeforeEval , posBeforeEval )
134
94
}
135
95
}
136
96
137
- function evalTopLevel ( view ) {
97
+ const evalTopLevel = ( view ) => {
138
98
posAtFormEnd = topLevelNode ( view . state ) . to
139
99
const doc = view . state . doc . toString ( )
140
100
posBeforeEval = view . state . selection . main . head
@@ -147,8 +107,9 @@ function evalTopLevel(view) {
147
107
return true
148
108
}
149
109
150
- function evalCell ( view ) {
110
+ const evalCell = ( view ) => {
151
111
const doc = view . state . doc . toString ( )
112
+ posBeforeEval = view . state . selection . main . head
152
113
evalResult = tryEval ( view . state . doc . text . join ( " " ) )
153
114
const codeWithResult = doc + "\n" + " => " + evalResult
154
115
updateEditor ( view , codeWithResult , posBeforeEval )
0 commit comments