Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add evaluation status bar item #480

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Changes to Calva.

## [Unreleased]
- Add status bar item for evaluation status.
- Fix: ["Load current Namespace in REPL Window" command not working](https://github.com/BetterThanTomorrow/calva/issues/477)
- Fix: [Tokenization errors with quotes, derefs, etcetera](https://github.com/BetterThanTomorrow/calva/issues/467)
- Fix: [Wrong current form in the REPL window when cursor is to the right of a form](https://github.com/BetterThanTomorrow/calva/issues/472)
Expand Down
42 changes: 42 additions & 0 deletions src/animation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@

import * as vscode from 'vscode';

export interface TextIncrementor {
next(): string;
reset(): void;
}

export class Incrementor {
constructor(
private start: number,
private end: number
) {
this._val = start;
}
private _val: number;

next() {
if(this._val > this.end) {
this._val = this.start;
}
return this._val++;
}
reset = () => this._val = this.start;
}

export class StatusBarAnimator {
constructor(
private incrementor: TextIncrementor,
private interval: number
) { }
timer: NodeJS.Timer;

start(sbi: vscode.StatusBarItem) {
this.incrementor.reset();
this.timer = setInterval(
() => sbi.text = this.incrementor.next(),
this.interval
);
}
stop = () => clearInterval(this.timer);
}
13 changes: 11 additions & 2 deletions src/evaluate.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as vscode from 'vscode';
import * as _ from 'lodash';
import * as state from './state';
import { EvaluationStatus } from "./state";
import annotations from './providers/annotations';
import * as path from 'path';
import select from './select';
Expand Down Expand Up @@ -168,6 +169,11 @@ function evaluateCurrentForm(document = {}, options = {}) {
.catch(e => console.warn(`Unhandled error: ${e.message}`));
}

function setEvalStatus(status: EvaluationStatus) {
state.cursor.set("evaluation", status);
statusbar.update();
}

async function loadFile(document, callback: () => { }, pprintOptions: PrettyPrintingOptions) {
let current = state.deref(),
doc = util.getDocument(document),
Expand All @@ -181,7 +187,7 @@ async function loadFile(document, callback: () => { }, pprintOptions: PrettyPrin
if (doc && doc.languageId == "clojure" && fileType != "edn" && current.get('connected')) {
state.analytics().logEvent("Evaluation", "LoadFile").send();
chan.appendLine("Evaluating file: " + fileName);

setEvalStatus(EvaluationStatus.evaluating);
let res = client.loadFile(doc.getText(), {
fileName: fileName,
filePath: doc.fileName,
Expand All @@ -191,18 +197,21 @@ async function loadFile(document, callback: () => { }, pprintOptions: PrettyPrin
})
await res.value.then((value) => {
if (value) {
setEvalStatus(EvaluationStatus.success);
chan.appendLine("=> " + value);
} else {
setEvalStatus(EvaluationStatus.none);
chan.appendLine("No results from file evaluation.");
}
}).catch((e) => {
chan.appendLine(`Evaluation of file ${fileName} failed: ${e}`);
setEvalStatus(EvaluationStatus.error);
});
}
if (callback) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was recently added, I'm not familiar with the purpose. Would it make sense for this to also be represented in the status bar indicator?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this might be from @cfehse 's fix for the load file callback. And yes, would probably be material for a status indicator.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes it is.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The purpose was solely to show that an error occurred instead of ignoring the error condition completely.

try {
callback();
} catch (e) {
} catch (e) {
chan.appendLine(`After evaluation callback for file ${fileName} failed: ${e}`);
};
}
Expand Down
13 changes: 11 additions & 2 deletions src/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ export function setExtensionContext(context: vscode.ExtensionContext) {
}
}

enum EvaluationStatus {
none,
evaluating,
success,
error
}

// include the 'file' and 'untitled' to the
// document selector. All other schemes are
// not known and therefore not supported.
Expand All @@ -39,7 +46,8 @@ const initialData = {
outputChannel: vscode.window.createOutputChannel("Calva says"),
connectionLogChannel: vscode.window.createOutputChannel("Calva Connection Log"),
diagnosticCollection: vscode.languages.createDiagnosticCollection('calva: Evaluation errors'),
analytics: null
analytics: null,
evaluation: EvaluationStatus.none
};

reset();
Expand Down Expand Up @@ -240,5 +248,6 @@ export {
extensionContext,
outputChannel,
connectionLogChannel,
analytics
analytics,
EvaluationStatus
};
49 changes: 49 additions & 0 deletions src/statusbar.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import * as vscode from 'vscode';
import {
Incrementor,
TextIncrementor,
StatusBarAnimator
} from "./animation";
import { activeReplWindow } from './repl-window';
import * as state from './state';
import * as util from './utilities';

const connectionStatus = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left);
const typeStatus = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left);
const cljsBuildStatus = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left);
const evalStatus = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left);
const prettyPrintToggle = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Right);
const color = {
active: "white",
Expand Down Expand Up @@ -99,6 +105,49 @@ function update() {
cljsBuildStatus.hide();
}
prettyPrintToggle.show();

updateEvalStatus(
evalStatus,
current.get("evaluation"),
current.get('connected')
);
}

class EvalIncrementor implements TextIncrementor {
constructor() {
this.incrementor = new Incrementor(1, 3);
}
incrementor: Incrementor;
next = () => "🔨".repeat(this.incrementor.next());
reset = () => this.incrementor.reset();
}

const evalAnimation = new StatusBarAnimator(new EvalIncrementor(), 500);

function updateEvalStatus(
sbi: vscode.StatusBarItem,
evaluationStatus: state.EvaluationStatus,
visible: boolean
) {
sbi.color = color.inactive;
evalAnimation.stop();
// TODO: Toggle calva says output
sbi.command = "workbench.action.output.toggleOutput";
switch(evaluationStatus) {
case state.EvaluationStatus.none:
sbi.text = "🔵";
break;
case state.EvaluationStatus.evaluating:
evalAnimation.start(sbi);
break;
case state.EvaluationStatus.success:
sbi.text = "✅";
break;
case state.EvaluationStatus.error:
sbi.text = "❌";
break;
}
visible ? sbi.show() : sbi.hide();
}

export default {
Expand Down