-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathcljConnection.ts
232 lines (189 loc) · 7.62 KB
/
cljConnection.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
import * as vscode from 'vscode';
import * as path from 'path';
import * as fs from 'fs';
import { nreplClient } from './nreplClient';
import { nreplController } from './nreplController';
export interface CljConnectionInformation {
host: string;
port: number;
}
export interface REPLSession {
type: 'ClojureScript' | 'Clojure';
id?: string;
}
const CONNECTION_STATE_KEY: string = 'CLJ_CONNECTION';
const DEFAULT_LOCAL_IP: string = '127.0.0.1';
const CLJS_SESSION_KEY: string = 'CLJS_SESSION';
const connectionIndicator: vscode.StatusBarItem = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left);
let cljContext: vscode.ExtensionContext;
const setCljContext = (context: vscode.ExtensionContext) => cljContext = context;
const getConnection = (): CljConnectionInformation | undefined => cljContext.workspaceState.get(CONNECTION_STATE_KEY);
const isConnected = (): boolean => !!getConnection();
const saveConnection = (connection: CljConnectionInformation): void => {
cljContext.workspaceState.update(CONNECTION_STATE_KEY, connection);
connectionIndicator.text = `⚡nrepl://${connection.host}:${connection.port}`;
connectionIndicator.show();
vscode.window.showInformationMessage('Connected to nREPL.');
};
const saveDisconnection = (showMessage: boolean = true): void => {
cljContext.workspaceState.update(CONNECTION_STATE_KEY, undefined);
cljContext.workspaceState.update(CLJS_SESSION_KEY, undefined);
connectionIndicator.text = '';
connectionIndicator.show();
if (showMessage)
vscode.window.showInformationMessage('Disconnected from nREPL.');
};
let loadingHandler: NodeJS.Timer | null
const startLoadingAnimation = () => {
if (loadingHandler)
return;
const maxAnimationDots: number = 3;
let animationTime: number = 0;
loadingHandler = setInterval(() => {
connectionIndicator.text = '⚡Starting nREPL' + '.'.repeat(animationTime);
connectionIndicator.show();
animationTime += animationTime < maxAnimationDots ? 1 : -maxAnimationDots;
}, 500);
};
const stopLoadingAnimation = () => {
if (loadingHandler) {
clearInterval(loadingHandler);
loadingHandler = null;
connectionIndicator.text = '';
connectionIndicator.show();
}
};
const manuallyConnect = (): void => {
if (loadingHandler) {
vscode.window.showWarningMessage('Already starting a nREPL. Disconnect first.');
return;
}
if (isConnected()) {
vscode.window.showWarningMessage('Already connected to nREPL. Disconnect first.');
return;
}
let host: string;
let port: number;
vscode.window.showInputBox({ prompt: 'nREPL host', value: DEFAULT_LOCAL_IP })
.then(hostFromUser => {
if (!hostFromUser)
return Promise.reject({ connectionError: 'Host must be informed.' });
host = hostFromUser;
const portNumberPromptOptions: vscode.InputBoxOptions = { prompt: 'nREPL port number' };
if (hostFromUser === DEFAULT_LOCAL_IP || hostFromUser.toLowerCase() === 'localhost') {
const localPort = getLocalNReplPort();
if (localPort)
portNumberPromptOptions.value = String(localPort);
}
return <PromiseLike<string>>vscode.window.showInputBox(portNumberPromptOptions); // cast needed to chain promises
})
.then(portFromUser => {
if (!portFromUser)
return Promise.reject({ connectionError: 'Port number must be informed.' });
const intPort = Number.parseInt(portFromUser);
if (!intPort)
return Promise.reject({ connectionError: 'Port number must be an integer.' });
port = intPort;
})
.then(() => nreplClient.test({ host, port }))
.then(() => {
saveConnection({ host, port });
}
, ({ connectionError }) => {
if (!connectionError)
connectionError = "Can't connect to the nREPL.";
vscode.window.showErrorMessage(connectionError);
});
};
const startNRepl = (): void => {
if (isConnected()) {
vscode.window.showWarningMessage('Already connected to nREPL. Disconnect first.');
return;
}
startLoadingAnimation();
let nreplConnection: CljConnectionInformation;
nreplController.start()
.then(connectionInfo => nreplConnection = connectionInfo)
.then(() => nreplClient.test(nreplConnection))
.then(stopLoadingAnimation)
.then(() => saveConnection(nreplConnection), ({ nreplError }) => {
stopLoadingAnimation();
if (!nreplError)
nreplError = "Can't start nREPL.";
disconnect(false);
vscode.window.showErrorMessage(nreplError);
});
};
const disconnect = (showMessage: boolean = true): void => {
if (isConnected() || loadingHandler) {
stopLoadingAnimation();
nreplController.stop();
saveDisconnection(showMessage);
} else if (showMessage)
vscode.window.showWarningMessage('Not connected to any nREPL.');
};
const getLocalNReplPort = (): number | undefined => {
const projectDir = vscode.workspace.rootPath;
if (projectDir) {
const projectPort: number = getPortFromFS(path.join(projectDir, '.nrepl-port'));
if (projectPort)
return projectPort;
}
const homeDir = process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE;
if (homeDir) {
return getPortFromFS(path.join(homeDir, '.lein', 'repl-port'));
}
};
const getPortFromFS = (path: string): number => fs.existsSync(path) ? Number.parseInt(fs.readFileSync(path, 'utf-8')) : NaN;
const findClojureScriptSession = (sessions: string[]): Promise<string> => {
if (sessions.length == 0)
return Promise.reject(null);
const base_session = sessions.shift();
if (!base_session) {
return Promise.reject("no base session");
}
return nreplClient.evaluate('(js/parseInt "42")', base_session).then(results => {
let { session, value } = results[0];
nreplClient.close(session);
if (value == 42) {
return Promise.resolve(base_session);
}
return findClojureScriptSession(sessions);
});
}
const discoverSessions = (): Promise<string> => {
return nreplClient.listSessions().then(sessions => {
return findClojureScriptSession(sessions).then(cljs_session => {
console.log("found ClojureScript session", cljs_session);
cljContext.workspaceState.update(CLJS_SESSION_KEY, cljs_session);
return cljs_session;
}).catch(reason => {
cljContext.workspaceState.update(CLJS_SESSION_KEY, undefined);
throw reason;
});
});
}
const sessionForFilename = (filename: string): Promise<REPLSession> => {
return new Promise((resolve, reject) => {
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 session_id = cljContext.workspaceState.get<string>(CLJS_SESSION_KEY);
if (session_id)
return resolve({ type: sessionType, id: session_id });
return discoverSessions().then(session_id => {
resolve({ type: sessionType, id: session_id });
});
});
}
export const cljConnection = {
setCljContext,
getConnection,
isConnected,
manuallyConnect,
startNRepl,
disconnect,
sessionForFilename
};