Skip to content

Commit

Permalink
Check for editor before accessing
Browse files Browse the repository at this point in the history
Fixes #2025
  • Loading branch information
PEZ committed Jan 22, 2023
1 parent 627eaaf commit ba1e29f
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
18 changes: 12 additions & 6 deletions src/evaluate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ async function addAsComment(
editor.selection = selection;
}

// TODO: Clean up this mess
async function evaluateCodeUpdatingUI(
code: string,
options,
Expand All @@ -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) {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -183,7 +189,7 @@ async function evaluateCodeUpdatingUI(
editor.selection
);
}
if (!outputWindow.isResultsDoc(editor.document)) {
if (editor && !outputWindow.isResultsDoc(editor.document)) {
annotations.decorateSelection(
editorError,
selection,
Expand Down
2 changes: 1 addition & 1 deletion test-data/projects/repl-connected-code/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit ba1e29f

Please sign in to comment.