Merge branch '2.0'
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / js / 3rdParty / codemirror / addon / tern / tern.js
1 // Glue code between CodeMirror and Tern.
2 //
3 // Create a CodeMirror.TernServer to wrap an actual Tern server,
4 // register open documents (CodeMirror.Doc instances) with it, and
5 // call its methods to activate the assisting functions that Tern
6 // provides.
7 //
8 // Options supported (all optional):
9 // * defs: An array of JSON definition data structures.
10 // * plugins: An object mapping plugin names to configuration
11 // options.
12 // * getFile: A function(name, c) that can be used to access files in
13 // the project that haven't been loaded yet. Simply do c(null) to
14 // indicate that a file is not available.
15 // * fileFilter: A function(value, docName, doc) that will be applied
16 // to documents before passing them on to Tern.
17 // * switchToDoc: A function(name) that should, when providing a
18 // multi-file view, switch the view or focus to the named file.
19 // * showError: A function(editor, message) that can be used to
20 // override the way errors are displayed.
21 // * completionTip: Customize the content in tooltips for completions.
22 // Is passed a single argument—the completion's data as returned by
23 // Tern—and may return a string, DOM node, or null to indicate that
24 // no tip should be shown. By default the docstring is shown.
25 // * typeTip: Like completionTip, but for the tooltips shown for type
26 // queries.
27 // * responseFilter: A function(doc, query, request, error, data) that
28 // will be applied to the Tern responses before treating them
29 //
30 //
31 // It is possible to run the Tern server in a web worker by specifying
32 // these additional options:
33 // * useWorker: Set to true to enable web worker mode. You'll probably
34 // want to feature detect the actual value you use here, for example
35 // !!window.Worker.
36 // * workerScript: The main script of the worker. Point this to
37 // wherever you are hosting worker.js from this directory.
38 // * workerDeps: An array of paths pointing (relative to workerScript)
39 // to the Acorn and Tern libraries and any Tern plugins you want to
40 // load. Or, if you minified those into a single script and included
41 // them in the workerScript, simply leave this undefined.
42
43 (function(mod) {
44 if (typeof exports == "object" && typeof module == "object") // CommonJS
45 mod(require("../../lib/codemirror"));
46 else if (typeof define == "function" && define.amd) // AMD
47 define(["../../lib/codemirror"], mod);
48 else // Plain browser env
49 mod(CodeMirror);
50 })(function(CodeMirror) {
51 "use strict";
52 // declare global: tern
53
54 CodeMirror.TernServer = function(options) {
55 var self = this;
56 this.options = options || {};
57 var plugins = this.options.plugins || (this.options.plugins = {});
58 if (!plugins.doc_comment) plugins.doc_comment = true;
59 if (this.options.useWorker) {
60 this.server = new WorkerServer(this);
61 } else {
62 this.server = new tern.Server({
63 getFile: function(name, c) { return getFile(self, name, c); },
64 async: true,
65 defs: this.options.defs || [],
66 plugins: plugins
67 });
68 }
69 this.docs = Object.create(null);
70 this.trackChange = function(doc, change) { trackChange(self, doc, change); };
71
72 this.cachedArgHints = null;
73 this.activeArgHints = null;
74 this.jumpStack = [];
75 };
76
77 CodeMirror.TernServer.prototype = {
78 addDoc: function(name, doc) {
79 var data = {doc: doc, name: name, changed: null};
80 this.server.addFile(name, docValue(this, data));
81 CodeMirror.on(doc, "change", this.trackChange);
82 return this.docs[name] = data;
83 },
84
85 delDoc: function(name) {
86 var found = this.docs[name];
87 if (!found) return;
88 CodeMirror.off(found.doc, "change", this.trackChange);
89 delete this.docs[name];
90 this.server.delFile(name);
91 },
92
93 hideDoc: function(name) {
94 closeArgHints(this);
95 var found = this.docs[name];
96 if (found && found.changed) sendDoc(this, found);
97 },
98
99 complete: function(cm) {
100 var self = this;
101 CodeMirror.showHint(cm, function(cm, c) { return hint(self, cm, c); }, {async: true});
102 },
103
104 getHint: function(cm, c) { return hint(this, cm, c); },
105
106 showType: function(cm, pos) { showType(this, cm, pos); },
107
108 updateArgHints: function(cm) { updateArgHints(this, cm); },
109
110 jumpToDef: function(cm) { jumpToDef(this, cm); },
111
112 jumpBack: function(cm) { jumpBack(this, cm); },
113
114 rename: function(cm) { rename(this, cm); },
115
116 selectName: function(cm) { selectName(this, cm); },
117
118 request: function (cm, query, c, pos) {
119 var self = this;
120 var doc = findDoc(this, cm.getDoc());
121 var request = buildRequest(this, doc, query, pos);
122
123 this.server.request(request, function (error, data) {
124 if (!error && self.options.responseFilter)
125 data = self.options.responseFilter(doc, query, request, error, data);
126 c(error, data);
127 });
128 }
129 };
130
131 var Pos = CodeMirror.Pos;
132 var cls = "CodeMirror-Tern-";
133 var bigDoc = 250;
134
135 function getFile(ts, name, c) {
136 var buf = ts.docs[name];
137 if (buf)
138 c(docValue(ts, buf));
139 else if (ts.options.getFile)
140 ts.options.getFile(name, c);
141 else
142 c(null);
143 }
144
145 function findDoc(ts, doc, name) {
146 for (var n in ts.docs) {
147 var cur = ts.docs[n];
148 if (cur.doc == doc) return cur;
149 }
150 if (!name) for (var i = 0;; ++i) {
151 n = "[doc" + (i || "") + "]";
152 if (!ts.docs[n]) { name = n; break; }
153 }
154 return ts.addDoc(name, doc);
155 }
156
157 function trackChange(ts, doc, change) {
158 var data = findDoc(ts, doc);
159
160 var argHints = ts.cachedArgHints;
161 if (argHints && argHints.doc == doc && cmpPos(argHints.start, change.to) <= 0)
162 ts.cachedArgHints = null;
163
164 var changed = data.changed;
165 if (changed == null)
166 data.changed = changed = {from: change.from.line, to: change.from.line};
167 var end = change.from.line + (change.text.length - 1);
168 if (change.from.line < changed.to) changed.to = changed.to - (change.to.line - end);
169 if (end >= changed.to) changed.to = end + 1;
170 if (changed.from > change.from.line) changed.from = change.from.line;
171
172 if (doc.lineCount() > bigDoc && change.to - changed.from > 100) setTimeout(function() {
173 if (data.changed && data.changed.to - data.changed.from > 100) sendDoc(ts, data);
174 }, 200);
175 }
176
177 function sendDoc(ts, doc) {
178 ts.server.request({files: [{type: "full", name: doc.name, text: docValue(ts, doc)}]}, function(error) {
179 if (error) window.console.error(error);
180 else doc.changed = null;
181 });
182 }
183
184 // Completion
185
186 function hint(ts, cm, c) {
187 ts.request(cm, {type: "completions", types: true, docs: true, urls: true}, function(error, data) {
188 if (error) return showError(ts, cm, error);
189 var completions = [], after = "";
190 var from = data.start, to = data.end;
191 if (cm.getRange(Pos(from.line, from.ch - 2), from) == "[\"" &&
192 cm.getRange(to, Pos(to.line, to.ch + 2)) != "\"]")
193 after = "\"]";
194
195 for (var i = 0; i < data.completions.length; ++i) {
196 var completion = data.completions[i], className = typeToIcon(completion.type);
197 if (data.guess) className += " " + cls + "guess";
198 completions.push({text: completion.name + after,
199 displayText: completion.name,
200 className: className,
201 data: completion});
202 }
203
204 var obj = {from: from, to: to, list: completions};
205 var tooltip = null;
206 CodeMirror.on(obj, "close", function() { remove(tooltip); });
207 CodeMirror.on(obj, "update", function() { remove(tooltip); });
208 CodeMirror.on(obj, "select", function(cur, node) {
209 remove(tooltip);
210 var content = ts.options.completionTip ? ts.options.completionTip(cur.data) : cur.data.doc;
211 if (content) {
212 tooltip = makeTooltip(node.parentNode.getBoundingClientRect().right + window.pageXOffset,
213 node.getBoundingClientRect().top + window.pageYOffset, content);
214 tooltip.className += " " + cls + "hint-doc";
215 }
216 });
217 c(obj);
218 });
219 }
220
221 function typeToIcon(type) {
222 var suffix;
223 if (type == "?") suffix = "unknown";
224 else if (type == "number" || type == "string" || type == "bool") suffix = type;
225 else if (/^fn\(/.test(type)) suffix = "fn";
226 else if (/^\[/.test(type)) suffix = "array";
227 else suffix = "object";
228 return cls + "completion " + cls + "completion-" + suffix;
229 }
230
231 // Type queries
232
233 function showType(ts, cm, pos) {
234 ts.request(cm, "type", function(error, data) {
235 if (error) return showError(ts, cm, error);
236 if (ts.options.typeTip) {
237 var tip = ts.options.typeTip(data);
238 } else {
239 var tip = elt("span", null, elt("strong", null, data.type || "not found"));
240 if (data.doc)
241 tip.appendChild(document.createTextNode(" — " + data.doc));
242 if (data.url) {
243 tip.appendChild(document.createTextNode(" "));
244 tip.appendChild(elt("a", null, "[docs]")).href = data.url;
245 }
246 }
247 tempTooltip(cm, tip);
248 }, pos);
249 }
250
251 // Maintaining argument hints
252
253 function updateArgHints(ts, cm) {
254 closeArgHints(ts);
255
256 if (cm.somethingSelected()) return;
257 var state = cm.getTokenAt(cm.getCursor()).state;
258 var inner = CodeMirror.innerMode(cm.getMode(), state);
259 if (inner.mode.name != "javascript") return;
260 var lex = inner.state.lexical;
261 if (lex.info != "call") return;
262
263 var ch, argPos = lex.pos || 0, tabSize = cm.getOption("tabSize");
264 for (var line = cm.getCursor().line, e = Math.max(0, line - 9), found = false; line >= e; --line) {
265 var str = cm.getLine(line), extra = 0;
266 for (var pos = 0;;) {
267 var tab = str.indexOf("\t", pos);
268 if (tab == -1) break;
269 extra += tabSize - (tab + extra) % tabSize - 1;
270 pos = tab + 1;
271 }
272 ch = lex.column - extra;
273 if (str.charAt(ch) == "(") {found = true; break;}
274 }
275 if (!found) return;
276
277 var start = Pos(line, ch);
278 var cache = ts.cachedArgHints;
279 if (cache && cache.doc == cm.getDoc() && cmpPos(start, cache.start) == 0)
280 return showArgHints(ts, cm, argPos);
281
282 ts.request(cm, {type: "type", preferFunction: true, end: start}, function(error, data) {
283 if (error || !data.type || !(/^fn\(/).test(data.type)) return;
284 ts.cachedArgHints = {
285 start: pos,
286 type: parseFnType(data.type),
287 name: data.exprName || data.name || "fn",
288 guess: data.guess,
289 doc: cm.getDoc()
290 };
291 showArgHints(ts, cm, argPos);
292 });
293 }
294
295 function showArgHints(ts, cm, pos) {
296 closeArgHints(ts);
297
298 var cache = ts.cachedArgHints, tp = cache.type;
299 var tip = elt("span", cache.guess ? cls + "fhint-guess" : null,
300 elt("span", cls + "fname", cache.name), "(");
301 for (var i = 0; i < tp.args.length; ++i) {
302 if (i) tip.appendChild(document.createTextNode(", "));
303 var arg = tp.args[i];
304 tip.appendChild(elt("span", cls + "farg" + (i == pos ? " " + cls + "farg-current" : ""), arg.name || "?"));
305 if (arg.type != "?") {
306 tip.appendChild(document.createTextNode(":\u00a0"));
307 tip.appendChild(elt("span", cls + "type", arg.type));
308 }
309 }
310 tip.appendChild(document.createTextNode(tp.rettype ? ") ->\u00a0" : ")"));
311 if (tp.rettype) tip.appendChild(elt("span", cls + "type", tp.rettype));
312 var place = cm.cursorCoords(null, "page");
313 ts.activeArgHints = makeTooltip(place.right + 1, place.bottom, tip);
314 }
315
316 function parseFnType(text) {
317 var args = [], pos = 3;
318
319 function skipMatching(upto) {
320 var depth = 0, start = pos;
321 for (;;) {
322 var next = text.charAt(pos);
323 if (upto.test(next) && !depth) return text.slice(start, pos);
324 if (/[{\[\(]/.test(next)) ++depth;
325 else if (/[}\]\)]/.test(next)) --depth;
326 ++pos;
327 }
328 }
329
330 // Parse arguments
331 if (text.charAt(pos) != ")") for (;;) {
332 var name = text.slice(pos).match(/^([^, \(\[\{]+): /);
333 if (name) {
334 pos += name[0].length;
335 name = name[1];
336 }
337 args.push({name: name, type: skipMatching(/[\),]/)});
338 if (text.charAt(pos) == ")") break;
339 pos += 2;
340 }
341
342 var rettype = text.slice(pos).match(/^\) -> (.*)$/);
343
344 return {args: args, rettype: rettype && rettype[1]};
345 }
346
347 // Moving to the definition of something
348
349 function jumpToDef(ts, cm) {
350 function inner(varName) {
351 var req = {type: "definition", variable: varName || null};
352 var doc = findDoc(ts, cm.getDoc());
353 ts.server.request(buildRequest(ts, doc, req), function(error, data) {
354 if (error) return showError(ts, cm, error);
355 if (!data.file && data.url) { window.open(data.url); return; }
356
357 if (data.file) {
358 var localDoc = ts.docs[data.file], found;
359 if (localDoc && (found = findContext(localDoc.doc, data))) {
360 ts.jumpStack.push({file: doc.name,
361 start: cm.getCursor("from"),
362 end: cm.getCursor("to")});
363 moveTo(ts, doc, localDoc, found.start, found.end);
364 return;
365 }
366 }
367 showError(ts, cm, "Could not find a definition.");
368 });
369 }
370
371 if (!atInterestingExpression(cm))
372 dialog(cm, "Jump to variable", function(name) { if (name) inner(name); });
373 else
374 inner();
375 }
376
377 function jumpBack(ts, cm) {
378 var pos = ts.jumpStack.pop(), doc = pos && ts.docs[pos.file];
379 if (!doc) return;
380 moveTo(ts, findDoc(ts, cm.getDoc()), doc, pos.start, pos.end);
381 }
382
383 function moveTo(ts, curDoc, doc, start, end) {
384 doc.doc.setSelection(end, start);
385 if (curDoc != doc && ts.options.switchToDoc) {
386 closeArgHints(ts);
387 ts.options.switchToDoc(doc.name);
388 }
389 }
390
391 // The {line,ch} representation of positions makes this rather awkward.
392 function findContext(doc, data) {
393 var before = data.context.slice(0, data.contextOffset).split("\n");
394 var startLine = data.start.line - (before.length - 1);
395 var start = Pos(startLine, (before.length == 1 ? data.start.ch : doc.getLine(startLine).length) - before[0].length);
396
397 var text = doc.getLine(startLine).slice(start.ch);
398 for (var cur = startLine + 1; cur < doc.lineCount() && text.length < data.context.length; ++cur)
399 text += "\n" + doc.getLine(cur);
400 if (text.slice(0, data.context.length) == data.context) return data;
401
402 var cursor = doc.getSearchCursor(data.context, 0, false);
403 var nearest, nearestDist = Infinity;
404 while (cursor.findNext()) {
405 var from = cursor.from(), dist = Math.abs(from.line - start.line) * 10000;
406 if (!dist) dist = Math.abs(from.ch - start.ch);
407 if (dist < nearestDist) { nearest = from; nearestDist = dist; }
408 }
409 if (!nearest) return null;
410
411 if (before.length == 1)
412 nearest.ch += before[0].length;
413 else
414 nearest = Pos(nearest.line + (before.length - 1), before[before.length - 1].length);
415 if (data.start.line == data.end.line)
416 var end = Pos(nearest.line, nearest.ch + (data.end.ch - data.start.ch));
417 else
418 var end = Pos(nearest.line + (data.end.line - data.start.line), data.end.ch);
419 return {start: nearest, end: end};
420 }
421
422 function atInterestingExpression(cm) {
423 var pos = cm.getCursor("end"), tok = cm.getTokenAt(pos);
424 if (tok.start < pos.ch && (tok.type == "comment" || tok.type == "string")) return false;
425 return /\w/.test(cm.getLine(pos.line).slice(Math.max(pos.ch - 1, 0), pos.ch + 1));
426 }
427
428 // Variable renaming
429
430 function rename(ts, cm) {
431 var token = cm.getTokenAt(cm.getCursor());
432 if (!/\w/.test(token.string)) showError(ts, cm, "Not at a variable");
433 dialog(cm, "New name for " + token.string, function(newName) {
434 ts.request(cm, {type: "rename", newName: newName, fullDocs: true}, function(error, data) {
435 if (error) return showError(ts, cm, error);
436 applyChanges(ts, data.changes);
437 });
438 });
439 }
440
441 function selectName(ts, cm) {
442 var cur = cm.getCursor(), token = cm.getTokenAt(cur);
443 if (!/\w/.test(token.string)) showError(ts, cm, "Not at a variable");
444 var name = findDoc(ts, cm.doc).name;
445 ts.request(cm, {type: "refs"}, function(error, data) {
446 if (error) return showError(ts, cm, error);
447 var ranges = [], cur = 0;
448 for (var i = 0; i < data.refs.length; i++) {
449 var ref = data.refs[i];
450 if (ref.file == name) {
451 ranges.push({anchor: ref.start, head: ref.end});
452 if (cmpPos(cur, ref.start) >= 0 && cmpPos(cur, ref.end) <= 0)
453 cur = ranges.length - 1;
454 }
455 }
456 cm.setSelections(ranges, cur);
457 });
458 }
459
460 var nextChangeOrig = 0;
461 function applyChanges(ts, changes) {
462 var perFile = Object.create(null);
463 for (var i = 0; i < changes.length; ++i) {
464 var ch = changes[i];
465 (perFile[ch.file] || (perFile[ch.file] = [])).push(ch);
466 }
467 for (var file in perFile) {
468 var known = ts.docs[file], chs = perFile[file];;
469 if (!known) continue;
470 chs.sort(function(a, b) { return cmpPos(b.start, a.start); });
471 var origin = "*rename" + (++nextChangeOrig);
472 for (var i = 0; i < chs.length; ++i) {
473 var ch = chs[i];
474 known.doc.replaceRange(ch.text, ch.start, ch.end, origin);
475 }
476 }
477 }
478
479 // Generic request-building helper
480
481 function buildRequest(ts, doc, query, pos) {
482 var files = [], offsetLines = 0, allowFragments = !query.fullDocs;
483 if (!allowFragments) delete query.fullDocs;
484 if (typeof query == "string") query = {type: query};
485 query.lineCharPositions = true;
486 if (query.end == null) {
487 query.end = pos || doc.doc.getCursor("end");
488 if (doc.doc.somethingSelected())
489 query.start = doc.doc.getCursor("start");
490 }
491 var startPos = query.start || query.end;
492
493 if (doc.changed) {
494 if (doc.doc.lineCount() > bigDoc && allowFragments !== false &&
495 doc.changed.to - doc.changed.from < 100 &&
496 doc.changed.from <= startPos.line && doc.changed.to > query.end.line) {
497 files.push(getFragmentAround(doc, startPos, query.end));
498 query.file = "#0";
499 var offsetLines = files[0].offsetLines;
500 if (query.start != null) query.start = Pos(query.start.line - -offsetLines, query.start.ch);
501 query.end = Pos(query.end.line - offsetLines, query.end.ch);
502 } else {
503 files.push({type: "full",
504 name: doc.name,
505 text: docValue(ts, doc)});
506 query.file = doc.name;
507 doc.changed = null;
508 }
509 } else {
510 query.file = doc.name;
511 }
512 for (var name in ts.docs) {
513 var cur = ts.docs[name];
514 if (cur.changed && cur != doc) {
515 files.push({type: "full", name: cur.name, text: docValue(ts, cur)});
516 cur.changed = null;
517 }
518 }
519
520 return {query: query, files: files};
521 }
522
523 function getFragmentAround(data, start, end) {
524 var doc = data.doc;
525 var minIndent = null, minLine = null, endLine, tabSize = 4;
526 for (var p = start.line - 1, min = Math.max(0, p - 50); p >= min; --p) {
527 var line = doc.getLine(p), fn = line.search(/\bfunction\b/);
528 if (fn < 0) continue;
529 var indent = CodeMirror.countColumn(line, null, tabSize);
530 if (minIndent != null && minIndent <= indent) continue;
531 minIndent = indent;
532 minLine = p;
533 }
534 if (minLine == null) minLine = min;
535 var max = Math.min(doc.lastLine(), end.line + 20);
536 if (minIndent == null || minIndent == CodeMirror.countColumn(doc.getLine(start.line), null, tabSize))
537 endLine = max;
538 else for (endLine = end.line + 1; endLine < max; ++endLine) {
539 var indent = CodeMirror.countColumn(doc.getLine(endLine), null, tabSize);
540 if (indent <= minIndent) break;
541 }
542 var from = Pos(minLine, 0);
543
544 return {type: "part",
545 name: data.name,
546 offsetLines: from.line,
547 text: doc.getRange(from, Pos(endLine, 0))};
548 }
549
550 // Generic utilities
551
552 var cmpPos = CodeMirror.cmpPos;
553
554 function elt(tagname, cls /*, ... elts*/) {
555 var e = document.createElement(tagname);
556 if (cls) e.className = cls;
557 for (var i = 2; i < arguments.length; ++i) {
558 var elt = arguments[i];
559 if (typeof elt == "string") elt = document.createTextNode(elt);
560 e.appendChild(elt);
561 }
562 return e;
563 }
564
565 function dialog(cm, text, f) {
566 if (cm.openDialog)
567 cm.openDialog(text + ": <input type=text>", f);
568 else
569 f(prompt(text, ""));
570 }
571
572 // Tooltips
573
574 function tempTooltip(cm, content) {
575 var where = cm.cursorCoords();
576 var tip = makeTooltip(where.right + 1, where.bottom, content);
577 function clear() {
578 if (!tip.parentNode) return;
579 cm.off("cursorActivity", clear);
580 fadeOut(tip);
581 }
582 setTimeout(clear, 1700);
583 cm.on("cursorActivity", clear);
584 }
585
586 function makeTooltip(x, y, content) {
587 var node = elt("div", cls + "tooltip", content);
588 node.style.left = x + "px";
589 node.style.top = y + "px";
590 document.body.appendChild(node);
591 return node;
592 }
593
594 function remove(node) {
595 var p = node && node.parentNode;
596 if (p) p.removeChild(node);
597 }
598
599 function fadeOut(tooltip) {
600 tooltip.style.opacity = "0";
601 setTimeout(function() { remove(tooltip); }, 1100);
602 }
603
604 function showError(ts, cm, msg) {
605 if (ts.options.showError)
606 ts.options.showError(cm, msg);
607 else
608 tempTooltip(cm, String(msg));
609 }
610
611 function closeArgHints(ts) {
612 if (ts.activeArgHints) { remove(ts.activeArgHints); ts.activeArgHints = null; }
613 }
614
615 function docValue(ts, doc) {
616 var val = doc.doc.getValue();
617 if (ts.options.fileFilter) val = ts.options.fileFilter(val, doc.name, doc.doc);
618 return val;
619 }
620
621 // Worker wrapper
622
623 function WorkerServer(ts) {
624 var worker = new Worker(ts.options.workerScript);
625 worker.postMessage({type: "init",
626 defs: ts.options.defs,
627 plugins: ts.options.plugins,
628 scripts: ts.options.workerDeps});
629 var msgId = 0, pending = {};
630
631 function send(data, c) {
632 if (c) {
633 data.id = ++msgId;
634 pending[msgId] = c;
635 }
636 worker.postMessage(data);
637 }
638 worker.onmessage = function(e) {
639 var data = e.data;
640 if (data.type == "getFile") {
641 getFile(ts, data.name, function(err, text) {
642 send({type: "getFile", err: String(err), text: text, id: data.id});
643 });
644 } else if (data.type == "debug") {
645 window.console.log(data.message);
646 } else if (data.id && pending[data.id]) {
647 pending[data.id](data.err, data.body);
648 delete pending[data.id];
649 }
650 };
651 worker.onerror = function(e) {
652 for (var id in pending) pending[id](e);
653 pending = {};
654 };
655
656 this.addFile = function(name, text) { send({type: "add", name: name, text: text}); };
657 this.delFile = function(name) { send({type: "del", name: name}); };
658 this.request = function(body, c) { send({type: "req", body: body}, c); };
659 }
660 });