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

Persist session #126

Open
wants to merge 1 commit into
base: master
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
5 changes: 4 additions & 1 deletion src/cljConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { nreplController } from './nreplController';
export interface CljConnectionInformation {
host: string;
port: number;
session?: string;
}
export interface REPLSession {
type: 'ClojureScript' | 'Clojure';
Expand All @@ -17,6 +18,7 @@ export interface REPLSession {
const CONNECTION_STATE_KEY: string = 'CLJ_CONNECTION';
const DEFAULT_LOCAL_IP: string = '127.0.0.1';
const CLJS_SESSION_KEY: string = 'CLJS_SESSION';
const CL_SESSION_KEY: string = 'CL_SESSION';
const connectionIndicator: vscode.StatusBarItem = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left);

let cljContext: vscode.ExtensionContext;
Expand Down Expand Up @@ -209,7 +211,8 @@ const sessionForFilename = (filename: string): Promise<REPLSession> => {
const sessionType = filename.endsWith('.cljs') ? "ClojureScript" : "Clojure";
if (sessionType == "Clojure") {
// Assume that the default session is Clojure. This is always the case with cider.
return resolve({ type: sessionType, id: undefined });
const connection = getConnection()
return resolve({ type: sessionType, id: !connection ? connection : connection.session });
}

const session_id = cljContext.workspaceState.get<string>(CLJS_SESSION_KEY);
Expand Down
13 changes: 9 additions & 4 deletions src/clojureEval.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,13 +147,14 @@ function evaluate(outputChannel: vscode.OutputChannel, showResults: boolean): vo
const selection = editor.selection;
let text = editor.document.getText();
if (!selection.isEmpty) {
const ns: string = cljParser.getNamespace(text);
text = `(ns ${ns})\n${editor.document.getText(selection)}`;
// const ns: string = cljParser.getNamespace(text);
// text = `(ns ${ns})\n${editor.document.getText(selection)}`;
text = editor.document.getText(selection);
}

cljConnection.sessionForFilename(editor.document.fileName).then(session => {
let response;
if (!selection.isEmpty && session.type == 'ClojureScript') {
if (!selection.isEmpty) {
// Piggieback's evalFile() ignores the text sent as part of the request
// and just loads the whole file content from disk. So we use eval()
// here, which as a drawback will give us a random temporary filename in
Expand Down Expand Up @@ -204,6 +205,7 @@ function handleSuccess(outputChannel: vscode.OutputChannel, showResults: boolean
if (!showResults) {
vscode.window.showInformationMessage('Successfully compiled');
} else {
let connection = cljConnection.getConnection();
respObjs.forEach(respObj => {
if (respObj.out)
outputChannel.append(respObj.out);
Expand All @@ -212,7 +214,10 @@ function handleSuccess(outputChannel: vscode.OutputChannel, showResults: boolean
if (respObj.value)
outputChannel.appendLine(`=> ${respObj.value}`);
outputChannel.show(true);

if(connection && !connection.session) {
connection.session = respObj.session;
}
});
}
nreplClient.close(respObjs[0].session);
}
41 changes: 30 additions & 11 deletions src/nreplClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@ import { Buffer } from 'buffer';

import * as bencodeUtil from './bencodeUtil';
import { cljConnection, CljConnectionInformation } from './cljConnection';
import { resolve } from 'url';

interface nREPLCompleteMessage {
id: string,
op: string;
symbol: string;
ns?: string
}

interface nREPLInfoMessage {
id: string,
op: string;
symbol: string;
ns: string;
Expand All @@ -25,54 +28,59 @@ type TestMessage = {
}

interface nREPLEvalMessage {
id: string,
op: string;
file: string;
'file-path'?: string;
session: string;
}

interface nREPLSingleEvalMessage {
id: string,
op: string;
code: string;
session: string;
}

interface nREPLStacktraceMessage {
id: string,
op: string;
session: string;
}

interface nREPLCloneMessage {
id: string,
op: string;
session?: string;
}

interface nREPLCloseMessage {
id: string,
op: string;
session?: string;
}

const complete = (symbol: string, ns: string): Promise<any> => {
const msg: nREPLCompleteMessage = { op: 'complete', symbol, ns };
const msg: nREPLCompleteMessage = { id: create_UUID(), op: 'complete', symbol, ns };
return send(msg).then(respObjs => respObjs[0]);
};

const info = (symbol: string, ns: string, session?: string): Promise<any> => {
const msg: nREPLInfoMessage = { op: 'info', symbol, ns, session };
const msg: nREPLInfoMessage = { id: create_UUID(), op: 'info', symbol, ns, session };
return send(msg).then(respObjs => respObjs[0]);
};

const evaluate = (code: string, session?: string): Promise<any[]> => clone(session).then((session_id) => {
const msg: nREPLSingleEvalMessage = { op: 'eval', code: code, session: session_id };
const evaluate = (code: string, session?: string): Promise<any[]> => (!session ? clone(session) : Promise.resolve(session)).then((session_id) => {
const msg: nREPLSingleEvalMessage = { id: create_UUID(), op: 'eval', code: code, session: session_id };
return send(msg);
});

const evaluateFile = (code: string, filepath: string, session?: string): Promise<any[]> => clone(session).then((session_id) => {
const msg: nREPLEvalMessage = { op: 'load-file', file: code, 'file-path': filepath, session: session_id };
const evaluateFile = (code: string, filepath: string, session?: string): Promise<any[]> => (!session ? clone(session) : Promise.resolve(session)).then((session_id) => {
const msg: nREPLEvalMessage = { id: create_UUID(), op: 'load-file', file: code, 'file-path': filepath, session: session_id };
return send(msg);
});

const stacktrace = (session: string): Promise<any> => send({ op: 'stacktrace', session: session });
const stacktrace = (session: string): Promise<any> => send({ id: create_UUID(), op: 'stacktrace', session: session });

const runTests = function (namespace: string | undefined): Promise<any[]> {
const message: TestMessage = {
Expand All @@ -84,10 +92,10 @@ const runTests = function (namespace: string | undefined): Promise<any[]> {
}


const clone = (session?: string): Promise<string> => send({ op: 'clone', session: session }).then(respObjs => respObjs[0]['new-session']);
const clone = (session?: string): Promise<string> => send({ id: create_UUID(), op: 'clone', session: session }).then(respObjs => respObjs[0]['new-session']);

const test = (connectionInfo: CljConnectionInformation): Promise<any[]> => {
return send({ op: 'clone' }, connectionInfo)
return send({ id: create_UUID(), op: 'clone' }, connectionInfo)
.then(respObjs => respObjs[0])
.then(response => {
if (!('new-session' in response))
Expand All @@ -98,10 +106,10 @@ const test = (connectionInfo: CljConnectionInformation): Promise<any[]> => {
});
};

const close = (session?: string): Promise<any[]> => send({ op: 'close', session: session });
const close = (session?: string): Promise<any[]> => send({ id: create_UUID(), op: 'close', session: session });

const listSessions = (): Promise<[string]> => {
return send({ op: 'ls-sessions' }).then(respObjs => {
return send({ id: create_UUID(), op: 'ls-sessions' }).then(respObjs => {
const response = respObjs[0];
if (response.status[0] == "done") {
return Promise.resolve(response.sessions);
Expand Down Expand Up @@ -162,6 +170,17 @@ const isLastNreplObject = (nreplObjects: any[]): boolean => {
return lastObj && lastObj.status && lastObj.status.indexOf('done') > -1;
}

const create_UUID = () => {
var dt = new Date().getTime();
var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = (dt + Math.random()*16)%16 | 0;
dt = Math.floor(dt/16);
return (c=='x' ? r :(r&0x3|0x8)).toString(16);
});
return uuid;
}


export const nreplClient = {
complete,
info,
Expand Down