@@ -131,13 +131,22 @@ function updateEditor(view, text, pos) {
131
131
} )
132
132
}
133
133
134
+ function tryEval ( ctx , s ) {
135
+ try {
136
+ evalString ( ctx , s )
137
+ } catch ( err ) {
138
+ console . log ( err )
139
+ return "\nError: " + err . message
140
+ }
141
+ }
142
+
134
143
function evalAtCursor ( view ) {
135
144
const doc = view . state . doc . toString ( )
136
145
codeBeforeEval = doc
137
146
posBeforeEval = view . state . selection . main . head
138
147
const codeBeforeCursor = codeBeforeEval . slice ( 0 , posBeforeEval )
139
148
const codeAfterCursor = codeBeforeEval . slice ( posBeforeEval , codeBeforeEval . length )
140
- evalResult = evalString ( ctx , cursorNodeString ( view . state ) )
149
+ evalResult = tryEval ( ctx , cursorNodeString ( view . state ) )
141
150
const codeWithResult = codeBeforeCursor + " => " + evalResult + " " + codeAfterCursor
142
151
updateEditor ( view , codeWithResult , posBeforeEval )
143
152
view . dispatch ( { selection : { anchor : posBeforeEval , head : posBeforeEval } } )
@@ -158,17 +167,15 @@ function evalTopLevel(view) {
158
167
codeBeforeEval = doc
159
168
const codeBeforeFormEnd = codeBeforeEval . slice ( 0 , posAtFormEnd )
160
169
const codeAfterFormEnd = codeBeforeEval . slice ( posAtFormEnd , codeBeforeEval . length )
161
- evalResult = evalString ( ctx , topLevelString ( view . state ) )
170
+ evalResult = tryEval ( ctx , topLevelString ( view . state ) )
162
171
const codeWithResult = codeBeforeFormEnd + " => " + evalResult + " " + codeAfterFormEnd
163
172
updateEditor ( view , codeWithResult , posBeforeEval )
164
- //view.dispatch({selection: {anchor: posBeforeEval, head: posBeforeEval}})
165
- //console.log("evalTopLevel>", evalString(ctx, topLevelString(view.state)))
166
173
return true
167
174
}
168
175
169
176
function evalCell ( view ) {
170
177
const doc = view . state . doc . toString ( )
171
- evalResult = evalString ( ctx , view . state . doc . text . join ( " " ) )
178
+ evalResult = tryEval ( ctx , view . state . doc . text . join ( " " ) )
172
179
const codeWithResult = doc + "\n" + " => " + evalResult
173
180
updateEditor ( view , codeWithResult , posBeforeEval )
174
181
//console.log("evalCell>", evalString(ctx, view.state.doc.text.join(" ")))
0 commit comments