This repository has been archived by the owner on Apr 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathworker_util_helper.js
328 lines (291 loc) · 13 KB
/
worker_util_helper.js
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/*
* Cloud9 Language Foundation
*
* @copyright 2013, Ajax.org B.V.
*/
define(function(require, exports, module) {
main.consumes = [
"Plugin", "c9", "language", "proc", "fs", "tabManager", "save",
"watcher", "tree", "dialog.error", "dialog.info"
];
main.provides = ["language.worker_util_helper"];
return main;
function main(options, imports, register) {
var c9 = imports.c9;
var Plugin = imports.Plugin;
var plugin = new Plugin("Ajax.org", main.consumes);
var language = imports.language;
var proc = imports.proc;
var fs = imports.fs;
var tabs = imports.tabManager;
var save = imports.save;
var watcher = imports.watcher;
var tree = imports.tree;
var showError = imports["dialog.error"].show;
var showInfo = imports["dialog.info"].show;
var hideError = imports["dialog.error"].hide;
var async = require("async");
var syntaxDetector = require("plugins/c9.ide.language.core/syntax_detector");
var readFileQueue = [];
var readFileBusy = false;
var worker;
var watched = {};
var loaded;
function load() {
if (loaded) return;
loaded = true;
language.getWorker(function(err, _worker) {
if (err)
return console.error(err);
worker = _worker;
worker.on("watchDir", watchDir);
worker.on("unwatchDir", unwatchDir);
watcher.on("unwatch", onWatchRemoved);
watcher.on("directory.all", onWatchChange);
worker.on("refreshAllMarkers", language.refreshAllMarkers.bind(language));
worker.on("execFile", function(e) {
e.data.options.cwd = e.data.options.cwd || c9.workspaceDir;
ensureConnected(
proc.execFile.bind(proc, e.data.path, e.data.options),
function(err, stdout, stderr) {
worker.emit("execFileResult", { data: {
id: e.data.id,
err: err,
stdout: stdout,
stderr: stderr
}});
}
);
});
worker.on("spawn", function(e) {
var id = e.data.id;
e.data.options.cwd = e.data.options.cwd || c9.workspaceDir;
ensureConnected(
function(next) {
proc.spawn(e.data.path, e.data.options, next);
},
function(err, child) {
if (err)
return worker.emit("spawnResult", { data: { id: id, err: err, }});
forwardEvents(child, "child", ["exit", "error", "close", "disconnect", "message"]);
forwardEvents(child.stdout, "stdout", ["close", "data", "end", "error", "readable"]);
forwardEvents(child.stderr, "stderr", ["close", "data", "end", "error", "readable"]);
worker.on("spawn_kill$" + id, kill);
child.on("exit", function gc() {
worker.off("spawn_kill$" + id, kill);
});
worker.emit("spawnResult", { data: { id: id, pid: child.pid }});
function kill(e) {
child.kill(e.signal);
}
function forwardEvents(source, sourceName, events) {
events.forEach(function(event) {
source.on(event, function(e) {
worker.emit("spawnEvent$" + id + sourceName + event, { data: e });
});
});
}
}
);
});
worker.on("readFile", function tryIt(e) {
readTabOrFile(e.data.path, e.data.options, function(err, value) {
if (err && err.code === "EDISCONNECT")
return ensureConnected(tryIt.bind(null, e));
worker.emit("readFileResult", { data: {
id: e.data.id,
err: err && JSON.stringify(err),
data: value
}});
});
});
worker.on("stat", function(e) {
ensureConnected(function tryIt() {
fs.stat(e.data.path, function(err, value) {
if (err && err.code === "EDISCONNECT")
return ensureConnected(tryIt);
worker.emit("statResult", { data: {
id: e.data.id,
err: err && JSON.stringify(err),
data: value
}});
});
});
});
worker.on("showError", function(e) {
var token = e.data.info
? showInfo(e.data.message, e.data.timeout)
: showError(e.data.message, e.data.timeout);
worker.emit("showErrorResult", { data: { token: token }});
});
worker.on("hideError", function(e) {
hideError(e.data.token);
});
worker.on("getTokens", function tryGetTokens(e) {
var path = e.data.path;
var identifiers = e.data.identifiers;
var region = e.data.region;
var tab = tabs.findTab(path);
if (!tab || !tab.editor || !tab.editor.ace)
return done("Tab is no longer open");
var session = tab.editor.ace.getSession();
if (session.bgTokenizer.running)
return setTimeout(tryGetTokens.bind(null, e), 20);
var results = [];
for (var i = 0, len = session.getLength(); i < len; i++) {
if (region && !(region.sl <= i && i <= region.el))
continue;
var offset = 0;
session.getTokens(i).forEach(function(t) {
var myOffset = offset;
offset += t.value.length;
if (identifiers && identifiers.indexOf(t.value) === -1)
return;
if (region && region.sl === i && myOffset < region.sc)
return;
if (region && region.el === i && myOffset > region.ec)
return;
var result = {
row: i,
column: myOffset
};
if (region)
result = syntaxDetector.posToRegion(region, result);
result.type = t.type;
result.value = t.value;
results.push(result);
});
}
done(null, results);
function done(err, results) {
worker.emit("getTokensResult", { data: {
id: e.data.id,
err: err,
results: results
}});
}
});
});
}
function ensureConnected(f, callback, timeout) {
timeout = timeout || 200;
if (!c9.NETWORK) {
return c9.once("stateChange", function(e) {
setTimeout(
ensureConnected.bind(null, f, callback, timeout * 2),
timeout
);
});
}
f(function(err) {
if (err && err.code === "EDISCONNECT")
return ensureConnected(f, callback, timeout);
callback.apply(null, arguments);
});
}
function readTabOrFile(path, options, callback) {
if (typeof options === "string")
options = { encoding: options };
var allowUnsaved = options.allowUnsaved;
delete options.allowUnsaved;
var tab = tabs.findTab(path);
if (tab) {
if (allowUnsaved) {
var unsavedValue = tab.value
|| tab.document && tab.document.hasValue && tab.document.hasValue()
&& tab.document.value;
if (unsavedValue)
return callback(null, unsavedValue);
}
else {
var saved = save.getSavingState(tab) === "saved";
var value = saved
? tab.value || tab.document && tab.document.value
: tab.document.meta && typeof tab.document.meta.$savedValue === "string"
&& tab.document.meta.$savedValue;
if (value)
return callback(null, value);
}
}
if (!options.encoding)
options.encoding = "utf8"; // TODO: get from c9?
if (readFileBusy)
return readFileQueue.push(startDownload);
readFileBusy = true;
startDownload();
function startDownload() {
ensureConnected(
function(next) {
fs.exists(path, function(exists) {
if (!exists) {
var err = new Error("Does not exist: " + path);
err.code = "ENOENT";
return next(err);
}
fs.readFile(path, options, next);
});
},
function(err, result) {
callback(err, result);
if (!readFileQueue.length)
return readFileBusy = false;
var task = readFileQueue.pop();
task();
}
);
}
}
function watchDir(e) {
var path = e.data.path;
watcher.watch(path);
watched[path] = true;
// Send initial directory listing
async.parallel([
fs.stat.bind(fs, path),
function(callback) {
fs.readdir(path, function(err, results) {
// We have to use the elaborate callback form here
// because of argument-counting "magic" in the callback caller
callback(err, results);
});
}
],
function(err, results) {
worker.emit("watchDirResult", { data: {
initial: true,
path: path,
err: err && { message: err.message },
stat: results[0],
files: results[1] || [],
}});
}
);
}
function unwatchDir(e) {
var path = e.data.path;
watched[path] = false;
// HACK: don't unwatch if visible in tree
if (tree.getAllExpanded().indexOf(path) > -1)
return;
watcher.unwatch(path);
}
function onWatchRemoved(e) {
// HACK: check if someone removed my watcher
if (watched[e.path])
watchDir({ data: { path: e.path }});
}
function onWatchChange(e) {
if (watched[e.path])
worker.emit("watchDirResult", { data: e });
}
plugin.on("load", function() {
load();
});
plugin.freezePublicAPI({
readTabOrFile: readTabOrFile
});
register(null, {
"language.worker_util_helper": plugin
});
}
});