Skip to content

Commit 04d9e4b

Browse files
committed
handle errors
1 parent 64cc724 commit 04d9e4b

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

src/eval-region.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,13 +131,22 @@ function updateEditor(view, text, pos) {
131131
})
132132
}
133133

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+
134143
function evalAtCursor(view) {
135144
const doc = view.state.doc.toString()
136145
codeBeforeEval = doc
137146
posBeforeEval = view.state.selection.main.head
138147
const codeBeforeCursor = codeBeforeEval.slice(0, posBeforeEval)
139148
const codeAfterCursor = codeBeforeEval.slice(posBeforeEval, codeBeforeEval.length)
140-
evalResult = evalString(ctx, cursorNodeString(view.state))
149+
evalResult = tryEval(ctx, cursorNodeString(view.state))
141150
const codeWithResult = codeBeforeCursor + " => " + evalResult + " " + codeAfterCursor
142151
updateEditor(view, codeWithResult, posBeforeEval)
143152
view.dispatch({selection: {anchor: posBeforeEval, head: posBeforeEval}})
@@ -158,17 +167,15 @@ function evalTopLevel(view) {
158167
codeBeforeEval = doc
159168
const codeBeforeFormEnd = codeBeforeEval.slice(0, posAtFormEnd)
160169
const codeAfterFormEnd = codeBeforeEval.slice(posAtFormEnd, codeBeforeEval.length)
161-
evalResult = evalString(ctx, topLevelString(view.state))
170+
evalResult = tryEval(ctx, topLevelString(view.state))
162171
const codeWithResult = codeBeforeFormEnd + " => " + evalResult + " " + codeAfterFormEnd
163172
updateEditor(view, codeWithResult, posBeforeEval)
164-
//view.dispatch({selection: {anchor: posBeforeEval, head: posBeforeEval}})
165-
//console.log("evalTopLevel>", evalString(ctx, topLevelString(view.state)))
166173
return true
167174
}
168175

169176
function evalCell(view) {
170177
const doc = view.state.doc.toString()
171-
evalResult = evalString(ctx, view.state.doc.text.join(" "))
178+
evalResult = tryEval(ctx, view.state.doc.text.join(" "))
172179
const codeWithResult = doc + "\n" + " => " + evalResult
173180
updateEditor(view, codeWithResult, posBeforeEval)
174181
//console.log("evalCell>", evalString(ctx, view.state.doc.text.join(" ")))

0 commit comments

Comments
 (0)