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 nREPL evaluation status bar indicator #1

Open
wants to merge 2 commits into
base: stefan/refactor-status-bars
Choose a base branch
from
Open
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
13 changes: 13 additions & 0 deletions src/nrepl/events.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
interface Event {
type: string
}

export class NReplEvaluationStartedEvent implements Event {
type = "started"
constructor(public fileName: string, public filePath: string){}
}

export class NReplEvaluationFinishedEvent implements Event {
type = "finished";
constructor(public fileName: string, public filePath: string, public error?:string){}
}
25 changes: 22 additions & 3 deletions src/nrepl/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import { Event, EventEmitter } from "vscode";
import * as net from "net";
import { BEncoderStream, BDecoderStream } from "./bencode";
import * as state from './../state';
import * as replWindow from './../repl-window';
import * as util from '../utilities';
import { prettyPrint } from '../../out/cljs-lib/cljs-lib';
import { PrettyPrintingOptions, disabledPrettyPrinter, getServerSidePrinter } from "../printer";
import { NReplEvaluationStartedEvent, NReplEvaluationFinishedEvent } from "./events";

const eventEmitter = new EventEmitter<NReplEvaluationStartedEvent | NReplEvaluationFinishedEvent>();
export const onNReplEvent = eventEmitter.event;

/** An nRREPL client */
export class NReplClient {
Expand All @@ -29,6 +34,7 @@ export class NReplClient {
ns: string = "user";

private constructor(socket: net.Socket) {
// TODO: Emit client connection events
this.socket = socket;
this.socket.on("error", e => {
console.error(e);
Expand Down Expand Up @@ -305,14 +311,27 @@ export class NReplSession {
stderr?: (x: string) => void,
stdout?: (x: string) => void,
pprintOptions: PrettyPrintingOptions
} = {
} = {
pprintOptions: disabledPrettyPrinter
}) {

eventEmitter.fire(new NReplEvaluationStartedEvent(opts.fileName, opts.filePath));

const fireEventOnFin = (
finalize: ((response: string) => any),
createEvent: ((response: string) => any)
) => (response: string) => {
eventEmitter.fire(createEvent(response));
finalize(response);
}

let id = this.client.nextId;
let evaluation = new NReplEvaluation(id, this, opts.stderr, opts.stdout, null, new Promise((resolve, reject) => {
this.messageHandlers[id] = (msg) => {
evaluation.setHandlers(resolve, reject);
evaluation.setHandlers(
fireEventOnFin(resolve, _ => new NReplEvaluationFinishedEvent(opts.fileName, opts.filePath)),
fireEventOnFin(reject, (reason: string) => new NReplEvaluationFinishedEvent(opts.fileName, opts.filePath, reason))
);
if (evaluation.onMessage(msg, opts.pprintOptions)) {
return true;
}
Expand Down Expand Up @@ -762,4 +781,4 @@ export class NReplEvaluation {
});
return (num);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { window, StatusBarAlignment, StatusBarItem } from "vscode";
import * as state from '../state';
import * as util from '../utilities';

export class CljsBuildStatusBar {
export class CljsBuildStatusBarItem {
private statusBarItem: StatusBarItem;
constructor(alignment: StatusBarAlignment) {
this.statusBarItem = window.createStatusBarItem(alignment);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import configReader from "../configReader";
import * as state from '../state';
import * as util from '../utilities';

export class ConnectionStatusBar {
export class ConnectionStatusBarItem {
private statusBarItem: StatusBarItem;

constructor(alignment: StatusBarAlignment) {
Expand Down
143 changes: 143 additions & 0 deletions src/statusbar/file-type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
import { window, StatusBarAlignment, StatusBarItem } from "vscode";
import { activeReplWindow } from '../repl-window';

import { onNReplEvent } from "../nrepl";
import { NReplEvaluationStartedEvent, NReplEvaluationFinishedEvent } from "../nrepl/events";

import configReader from "../configReader";
import * as state from '../state';
import * as util from '../utilities';

export interface EvaluationError {
fileName: string;
filePath: string;
message: string;
}

export class FileTypeStatusBarItem {
private statusBarItem: StatusBarItem;

private activeEvals = new Array<string>();

private errors = new Array<EvaluationError>();

private get isEvaluating() {
return this.activeEvals.length > 0;
}

private get hasErrors(){
return this.errors.length > 0;
}

constructor(alignment: StatusBarAlignment) {
this.statusBarItem = window.createStatusBarItem(alignment);
// TODO: Event handling and resulting state should be moved to global state
onNReplEvent(this.handleNreplEvent);
}

update() {
const connected = state.deref().get("connected");
const doc = util.getDocument({});
const fileType = util.getFileType(doc);
const sessionType = util.getREPLSessionType();

let command = null;
let text = "Disconnected";
let tooltip = "No active REPL session";
let color = configReader.colors.disconnected;

if(connected) {
if (fileType == 'cljc' && sessionType !== null && !activeReplWindow()) {
text = this.statusTextDecorator("cljc/" + sessionType);
if (util.getSession('clj') !== null && util.getSession('cljs') !== null) {
command = "calva.toggleCLJCSession";
tooltip = `Click to use ${(sessionType === 'clj' ? 'cljs' : 'clj')} REPL for cljc`;
}
} else if (sessionType === 'cljs') {
text = this.statusTextDecorator("cljs");
tooltip = "Connected to ClojureScript REPL";
} else if (sessionType === 'clj') {
text = this.statusTextDecorator("clj");
tooltip = "Connected to Clojure REPL";
}
color = this.connectedStatusColor();
if(this.hasErrors) {
tooltip = this.errorTooltip();
}
}

this.statusBarItem.command = command;
this.statusBarItem.text = text;
this.statusBarItem.tooltip = tooltip;
this.statusBarItem.color = color
this.statusBarItem.show();
}

private statusTextDecorator(text): string {
if(this.hasErrors) {
const c = this.errors.length;
text = `${text} $(alert) ${c > 1 ? c : ""}`
Copy link

Choose a reason for hiding this comment

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

Just curious, what's this $(alert) do in this template literal? Or is it just a temporary placeholder?

Copy link
Owner Author

Choose a reason for hiding this comment

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

Copy link

Choose a reason for hiding this comment

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

Ah thanks!

}
if(this.isEvaluating) {
text = text + " $(gear~spin)";
}
return text;
}

private connectedStatusColor(): string {
const c = configReader.colors;
if(this.hasErrors) {
return c.error;
} else if (this.isEvaluating) {
return c.launching;
}
return c.typeStatus;
}

private errorTooltip(): string {
if(this.errors.length > 1){
return "There are errors in multiple files";
}
const err = this.errors[0];
return `Error: ${err.fileName}: ${err.message}`;
}

private handleNreplEvent = (e: NReplEvaluationStartedEvent | NReplEvaluationFinishedEvent) => {
switch(e.type){
case "started":
this.removeError(e.filePath);
this.activeEvals.push(e.filePath);
break;
case "finished":
this.removeActive(e.filePath);
const fe = <NReplEvaluationFinishedEvent> e;
if(fe.error) {
this.errors.push({
fileName: fe.fileName,
filePath: fe.filePath,
message: fe.error
});
}
break;
}
this.update();
}

private removeActive(filePath: string) {
const idx = this.activeEvals.indexOf(filePath);
if(idx !== -1) {
this.activeEvals.splice(idx, 1);
}
}

private removeError(filePath: string) {
const idx = this.errors.findIndex(e => e.filePath === filePath);
if(idx !== -1) {
this.errors.splice(idx, 1);
}
}

dispose() {
this.statusBarItem.dispose();
}
}
16 changes: 8 additions & 8 deletions src/statusbar/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { StatusBarAlignment } from "vscode";
import { TypeStatusBar } from "./typeStatusBar";
import { PrettyPrintStatusBar } from "./prettyPrintStatusBar";
import { CljsBuildStatusBar } from "./cljsBuildStatusBar";
import { ConnectionStatusBar } from "./connectionStatusBar";
import { FileTypeStatusBarItem } from "./file-type";
import { PrettyPrintStatusBarItem } from "./pretty-print";
import { CljsBuildStatusBarItem } from "./cljs-build";
import { ConnectionStatusBarItem } from "./connection";

const statusBarItems = [];

function init(): any[] {
statusBarItems.push(new ConnectionStatusBar(StatusBarAlignment.Left));
statusBarItems.push(new TypeStatusBar(StatusBarAlignment.Left));
statusBarItems.push(new CljsBuildStatusBar(StatusBarAlignment.Left));
statusBarItems.push(new PrettyPrintStatusBar(StatusBarAlignment.Right));
statusBarItems.push(new ConnectionStatusBarItem(StatusBarAlignment.Left));
Copy link

Choose a reason for hiding this comment

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

I like the +Item renames

statusBarItems.push(new FileTypeStatusBarItem(StatusBarAlignment.Left));
statusBarItems.push(new CljsBuildStatusBarItem(StatusBarAlignment.Left));
statusBarItems.push(new PrettyPrintStatusBarItem(StatusBarAlignment.Right));
update();
return statusBarItems;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { window, StatusBarAlignment, StatusBarItem } from "vscode";
import configReader from "../configReader";
import * as state from '../state';

export class PrettyPrintStatusBar {
export class PrettyPrintStatusBarItem {
private statusBarItem: StatusBarItem;
constructor(alignment: StatusBarAlignment) {
this.statusBarItem = window.createStatusBarItem(alignment);
Expand Down
51 changes: 0 additions & 51 deletions src/statusbar/typeStatusBar.ts

This file was deleted.