From ba1e29ff61182d3f590c666bc7a1ec5a73f62c71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Peter=20Str=C3=B6mberg?= Date: Sun, 22 Jan 2023 11:19:57 +0100 Subject: [PATCH] Check for editor before accessing Fixes #2025 --- CHANGELOG.md | 2 ++ src/evaluate.ts | 18 ++++++++++++------ .../projects/repl-connected-code/README.md | 2 +- 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9f27bf94b..3c67fab4e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ Changes to Calva. ## [Unreleased] +- Fix: [`afterCLJReplJackInCode` fails if no editor is open](https://github.com/BetterThanTomorrow/calva/issues/2025) + ## [2.0.325] - 2023-01-21 - Fix: [Setting calva.testOnSave broken: no tests found](https://github.com/BetterThanTomorrow/calva/issues/2005) diff --git a/src/evaluate.ts b/src/evaluate.ts index f1e9ccc8e..db31d018a 100644 --- a/src/evaluate.ts +++ b/src/evaluate.ts @@ -63,6 +63,7 @@ async function addAsComment( editor.selection = selection; } +// TODO: Clean up this mess async function evaluateCodeUpdatingUI( code: string, options, @@ -86,7 +87,12 @@ async function evaluateCodeUpdatingUI( const filePath = options.filePath; const session: NReplSession = options.session; const ns = options.ns; - const editor = util.getActiveTextEditor(); + let editor: vscode.TextEditor; + try { + editor = util.getActiveTextEditor(); + } catch (error) { + console.log('No active editor'); + } let result = null; if (code.length > 0) { @@ -126,17 +132,17 @@ async function evaluateCodeUpdatingUI( outputWindow.appendLine(value, async (resultLocation) => { if (selection) { const c = selection.start.character; - if (options.replace) { + if (editor && options.replace) { const indent = `${' '.repeat(c)}`, edit = vscode.TextEdit.replace(selection, value.replace(/\n/gm, '\n' + indent)), wsEdit = new vscode.WorkspaceEdit(); wsEdit.set(editor.document.uri, [edit]); void vscode.workspace.applyEdit(wsEdit); } else { - if (options.comment) { + if (editor && options.comment) { await addAsComment(c, value, selection, editor, editor.selection); } - if (!outputWindow.isResultsDoc(editor.document)) { + if (editor && !outputWindow.isResultsDoc(editor.document)) { annotations.decorateSelection( value, selection, @@ -174,7 +180,7 @@ async function evaluateCodeUpdatingUI( if (selection) { const editorError = util.stripAnsi(err.length ? err.join('\n') : e); const currentCursorPos = editor.selection.active; - if (options.comment) { + if (editor && options.comment) { await addAsComment( selection.start.character, editorError, @@ -183,7 +189,7 @@ async function evaluateCodeUpdatingUI( editor.selection ); } - if (!outputWindow.isResultsDoc(editor.document)) { + if (editor && !outputWindow.isResultsDoc(editor.document)) { annotations.decorateSelection( editorError, selection, diff --git a/test-data/projects/repl-connected-code/README.md b/test-data/projects/repl-connected-code/README.md index 15c88d0ab..2a1a30fce 100644 --- a/test-data/projects/repl-connected-code/README.md +++ b/test-data/projects/repl-connected-code/README.md @@ -9,4 +9,4 @@ This project has a connect sequence with a simple `afterCLJReplJackInCode` code 2. Jack-in, selecting the **Test broken jack-in code evaluation** sequence. * **Expected**: `"afterCLJReplJackInCode evaluated"` is printed in the REPL window -* **Actual**: It ^ is not happening +* **Actual**: `; Evaluation failed.` is printed in the REPL window