2651 lines
95 KiB
JavaScript
2651 lines
95 KiB
JavaScript
// ========= show-hint.js ========= //
|
|
|
|
// CodeMirror, copyright (c) by Marijn Haverbeke and others
|
|
// Distributed under an MIT license: https://codemirror.net/LICENSE
|
|
|
|
(function(mod) {
|
|
if (typeof exports == "object" && typeof module == "object") // CommonJS
|
|
mod(require("../../lib/codemirror"));
|
|
else if (typeof define == "function" && define.amd) // AMD
|
|
define(["../../lib/codemirror"], mod);
|
|
else // Plain browser env
|
|
mod(CodeMirror);
|
|
})(function(CodeMirror) {
|
|
"use strict";
|
|
|
|
var HINT_ELEMENT_CLASS = "CodeMirror-hint";
|
|
var ACTIVE_HINT_ELEMENT_CLASS = "CodeMirror-hint-active";
|
|
|
|
// This is the old interface, kept around for now to stay
|
|
// backwards-compatible.
|
|
CodeMirror.showHint = function(cm, getHints, options) {
|
|
if (!getHints) return cm.showHint(options);
|
|
if (options && options.async) getHints.async = true;
|
|
var newOpts = {hint: getHints};
|
|
if (options) for (var prop in options) newOpts[prop] = options[prop];
|
|
return cm.showHint(newOpts);
|
|
};
|
|
|
|
CodeMirror.defineExtension("showHint", function(options) {
|
|
options = parseOptions(this, this.getCursor("start"), options);
|
|
var selections = this.listSelections()
|
|
if (selections.length > 1) return;
|
|
// By default, don't allow completion when something is selected.
|
|
// A hint function can have a `supportsSelection` property to
|
|
// indicate that it can handle selections.
|
|
if (this.somethingSelected()) {
|
|
if (!options.hint.supportsSelection) return;
|
|
// Don't try with cross-line selections
|
|
for (var i = 0; i < selections.length; i++)
|
|
if (selections[i].head.line != selections[i].anchor.line) return;
|
|
}
|
|
|
|
if (this.state.completionActive) this.state.completionActive.close();
|
|
var completion = this.state.completionActive = new Completion(this, options);
|
|
if (!completion.options.hint) return;
|
|
|
|
CodeMirror.signal(this, "startCompletion", this);
|
|
completion.update(true);
|
|
});
|
|
|
|
CodeMirror.defineExtension("closeHint", function() {
|
|
if (this.state.completionActive) this.state.completionActive.close()
|
|
})
|
|
|
|
function Completion(cm, options) {
|
|
this.cm = cm;
|
|
this.options = options;
|
|
this.widget = null;
|
|
this.debounce = 0;
|
|
this.tick = 0;
|
|
this.startPos = this.cm.getCursor("start");
|
|
this.startLen = this.cm.getLine(this.startPos.line).length - this.cm.getSelection().length;
|
|
|
|
var self = this;
|
|
cm.on("cursorActivity", this.activityFunc = function() { self.cursorActivity(); });
|
|
}
|
|
|
|
var requestAnimationFrame = window.requestAnimationFrame || function(fn) {
|
|
return setTimeout(fn, 1000/60);
|
|
};
|
|
var cancelAnimationFrame = window.cancelAnimationFrame || clearTimeout;
|
|
|
|
Completion.prototype = {
|
|
close: function() {
|
|
if (!this.active()) return;
|
|
this.cm.state.completionActive = null;
|
|
this.tick = null;
|
|
this.cm.off("cursorActivity", this.activityFunc);
|
|
|
|
if (this.widget && this.data) CodeMirror.signal(this.data, "close");
|
|
if (this.widget) this.widget.close();
|
|
CodeMirror.signal(this.cm, "endCompletion", this.cm);
|
|
},
|
|
|
|
active: function() {
|
|
return this.cm.state.completionActive == this;
|
|
},
|
|
|
|
pick: function(data, i) {
|
|
var completion = data.list[i];
|
|
if (completion.hint) completion.hint(this.cm, data, completion);
|
|
else this.cm.replaceRange(getText(completion), completion.from || data.from,
|
|
completion.to || data.to, "complete");
|
|
CodeMirror.signal(data, "pick", completion);
|
|
this.close();
|
|
},
|
|
|
|
cursorActivity: function() {
|
|
if (this.debounce) {
|
|
cancelAnimationFrame(this.debounce);
|
|
this.debounce = 0;
|
|
}
|
|
|
|
var pos = this.cm.getCursor(), line = this.cm.getLine(pos.line);
|
|
if (pos.line != this.startPos.line || line.length - pos.ch != this.startLen - this.startPos.ch ||
|
|
pos.ch < this.startPos.ch || this.cm.somethingSelected() ||
|
|
(!pos.ch || this.options.closeCharacters.test(line.charAt(pos.ch - 1)))) {
|
|
this.close();
|
|
} else {
|
|
var self = this;
|
|
this.debounce = requestAnimationFrame(function() {self.update();});
|
|
if (this.widget) this.widget.disable();
|
|
}
|
|
},
|
|
|
|
update: function(first) {
|
|
if (this.tick == null) return
|
|
var self = this, myTick = ++this.tick
|
|
fetchHints(this.options.hint, this.cm, this.options, function(data) {
|
|
if (self.tick == myTick) self.finishUpdate(data, first)
|
|
})
|
|
},
|
|
|
|
finishUpdate: function(data, first) {
|
|
if (this.data) CodeMirror.signal(this.data, "update");
|
|
|
|
var picked = (this.widget && this.widget.picked);
|
|
if (this.widget) this.widget.close();
|
|
|
|
this.data = data;
|
|
|
|
if (data && data.list.length) {
|
|
if (picked && data.list.length == 1) {
|
|
this.pick(data, 0);
|
|
} else {
|
|
this.widget = new Widget(this, data);
|
|
CodeMirror.signal(data, "shown");
|
|
}
|
|
}
|
|
}
|
|
};
|
|
|
|
function parseOptions(cm, pos, options) {
|
|
var editor = cm.options.hintOptions;
|
|
var out = {};
|
|
for (var prop in defaultOptions) out[prop] = defaultOptions[prop];
|
|
if (editor) for (var prop in editor)
|
|
if (editor[prop] !== undefined) out[prop] = editor[prop];
|
|
if (options) for (var prop in options)
|
|
if (options[prop] !== undefined) out[prop] = options[prop];
|
|
if (out.hint.resolve) out.hint = out.hint.resolve(cm, pos)
|
|
return out;
|
|
}
|
|
|
|
function getText(completion) {
|
|
if (typeof completion == "string") return completion;
|
|
else return completion.text;
|
|
}
|
|
|
|
function buildKeyMap(completion, handle) {
|
|
var baseMap = {
|
|
Up: function() {handle.moveFocus(-1);},
|
|
Down: function() {handle.moveFocus(1);},
|
|
PageUp: function() {handle.moveFocus(-handle.menuSize() + 1, true);},
|
|
PageDown: function() {handle.moveFocus(handle.menuSize() - 1, true);},
|
|
Home: function() {handle.setFocus(0);},
|
|
End: function() {handle.setFocus(handle.length - 1);},
|
|
Enter: handle.pick,
|
|
Tab: handle.pick,
|
|
Esc: handle.close
|
|
};
|
|
|
|
var mac = /Mac/.test(navigator.platform);
|
|
|
|
if (mac) {
|
|
baseMap["Ctrl-P"] = function() {handle.moveFocus(-1);};
|
|
baseMap["Ctrl-N"] = function() {handle.moveFocus(1);};
|
|
}
|
|
|
|
var custom = completion.options.customKeys;
|
|
var ourMap = custom ? {} : baseMap;
|
|
function addBinding(key, val) {
|
|
var bound;
|
|
if (typeof val != "string")
|
|
bound = function(cm) { return val(cm, handle); };
|
|
// This mechanism is deprecated
|
|
else if (baseMap.hasOwnProperty(val))
|
|
bound = baseMap[val];
|
|
else
|
|
bound = val;
|
|
ourMap[key] = bound;
|
|
}
|
|
if (custom)
|
|
for (var key in custom) if (custom.hasOwnProperty(key))
|
|
addBinding(key, custom[key]);
|
|
var extra = completion.options.extraKeys;
|
|
if (extra)
|
|
for (var key in extra) if (extra.hasOwnProperty(key))
|
|
addBinding(key, extra[key]);
|
|
return ourMap;
|
|
}
|
|
|
|
function getHintElement(hintsElement, el) {
|
|
while (el && el != hintsElement) {
|
|
if (el.nodeName.toUpperCase() === "LI" && el.parentNode == hintsElement) return el;
|
|
el = el.parentNode;
|
|
}
|
|
}
|
|
|
|
function Widget(completion, data) {
|
|
this.completion = completion;
|
|
this.data = data;
|
|
this.picked = false;
|
|
var widget = this, cm = completion.cm;
|
|
var ownerDocument = cm.getInputField().ownerDocument;
|
|
var parentWindow = ownerDocument.defaultView || ownerDocument.parentWindow;
|
|
|
|
var hints = this.hints = ownerDocument.createElement("ul");
|
|
var theme = completion.cm.options.theme;
|
|
hints.className = "CodeMirror-hints " + theme;
|
|
this.selectedHint = data.selectedHint || 0;
|
|
|
|
var completions = data.list;
|
|
for (var i = 0; i < completions.length; ++i) {
|
|
var elt = hints.appendChild(ownerDocument.createElement("li")), cur = completions[i];
|
|
var className = HINT_ELEMENT_CLASS + (i != this.selectedHint ? "" : " " + ACTIVE_HINT_ELEMENT_CLASS);
|
|
if (cur.className != null) className = cur.className + " " + className;
|
|
elt.className = className;
|
|
if (cur.render) cur.render(elt, data, cur);
|
|
else elt.appendChild(ownerDocument.createTextNode(cur.displayText || getText(cur)));
|
|
elt.hintId = i;
|
|
}
|
|
|
|
var container = completion.options.container || ownerDocument.body;
|
|
var pos = cm.cursorCoords(completion.options.alignWithWord ? data.from : null);
|
|
var left = pos.left, top = pos.bottom, below = true;
|
|
var offsetLeft = 0, offsetTop = 0;
|
|
if (container !== ownerDocument.body) {
|
|
// We offset the cursor position because left and top are relative to the offsetParent's top left corner.
|
|
var isContainerPositioned = ['absolute', 'relative', 'fixed'].indexOf(parentWindow.getComputedStyle(container).position) !== -1;
|
|
var offsetParent = isContainerPositioned ? container : container.offsetParent;
|
|
var offsetParentPosition = offsetParent.getBoundingClientRect();
|
|
var bodyPosition = ownerDocument.body.getBoundingClientRect();
|
|
offsetLeft = (offsetParentPosition.left - bodyPosition.left - offsetParent.scrollLeft);
|
|
offsetTop = (offsetParentPosition.top - bodyPosition.top - offsetParent.scrollTop);
|
|
}
|
|
hints.style.left = (left - offsetLeft) + "px";
|
|
hints.style.top = (top - offsetTop) + "px";
|
|
|
|
// If we're at the edge of the screen, then we want the menu to appear on the left of the cursor.
|
|
var winW = parentWindow.innerWidth || Math.max(ownerDocument.body.offsetWidth, ownerDocument.documentElement.offsetWidth);
|
|
var winH = parentWindow.innerHeight || Math.max(ownerDocument.body.offsetHeight, ownerDocument.documentElement.offsetHeight);
|
|
container.appendChild(hints);
|
|
var box = hints.getBoundingClientRect(), overlapY = box.bottom - winH;
|
|
var scrolls = hints.scrollHeight > hints.clientHeight + 1
|
|
var startScroll = cm.getScrollInfo();
|
|
|
|
if (overlapY > 0) {
|
|
var height = box.bottom - box.top, curTop = pos.top - (pos.bottom - box.top);
|
|
if (curTop - height > 0) { // Fits above cursor
|
|
hints.style.top = (top = pos.top - height - offsetTop) + "px";
|
|
below = false;
|
|
} else if (height > winH) {
|
|
hints.style.height = (winH - 5) + "px";
|
|
hints.style.top = (top = pos.bottom - box.top - offsetTop) + "px";
|
|
var cursor = cm.getCursor();
|
|
if (data.from.ch != cursor.ch) {
|
|
pos = cm.cursorCoords(cursor);
|
|
hints.style.left = (left = pos.left - offsetLeft) + "px";
|
|
box = hints.getBoundingClientRect();
|
|
}
|
|
}
|
|
}
|
|
var overlapX = box.right - winW;
|
|
if (overlapX > 0) {
|
|
if (box.right - box.left > winW) {
|
|
hints.style.width = (winW - 5) + "px";
|
|
overlapX -= (box.right - box.left) - winW;
|
|
}
|
|
hints.style.left = (left = pos.left - overlapX - offsetLeft) + "px";
|
|
}
|
|
if (scrolls) for (var node = hints.firstChild; node; node = node.nextSibling)
|
|
node.style.paddingRight = cm.display.nativeBarWidth + "px"
|
|
|
|
cm.addKeyMap(this.keyMap = buildKeyMap(completion, {
|
|
moveFocus: function(n, avoidWrap) { widget.changeActive(widget.selectedHint + n, avoidWrap); },
|
|
setFocus: function(n) { widget.changeActive(n); },
|
|
menuSize: function() { return widget.screenAmount(); },
|
|
length: completions.length,
|
|
close: function() { completion.close(); },
|
|
pick: function() { widget.pick(); },
|
|
data: data
|
|
}));
|
|
|
|
if (completion.options.closeOnUnfocus) {
|
|
var closingOnBlur;
|
|
cm.on("blur", this.onBlur = function() { closingOnBlur = setTimeout(function() { completion.close(); }, 100); });
|
|
cm.on("focus", this.onFocus = function() { clearTimeout(closingOnBlur); });
|
|
}
|
|
|
|
cm.on("scroll", this.onScroll = function() {
|
|
var curScroll = cm.getScrollInfo(), editor = cm.getWrapperElement().getBoundingClientRect();
|
|
var newTop = top + startScroll.top - curScroll.top;
|
|
var point = newTop - (parentWindow.pageYOffset || (ownerDocument.documentElement || ownerDocument.body).scrollTop);
|
|
if (!below) point += hints.offsetHeight;
|
|
if (point <= editor.top || point >= editor.bottom) return completion.close();
|
|
hints.style.top = newTop + "px";
|
|
hints.style.left = (left + startScroll.left - curScroll.left) + "px";
|
|
});
|
|
|
|
CodeMirror.on(hints, "dblclick", function(e) {
|
|
var t = getHintElement(hints, e.target || e.srcElement);
|
|
if (t && t.hintId != null) {widget.changeActive(t.hintId); widget.pick();}
|
|
});
|
|
|
|
CodeMirror.on(hints, "click", function(e) {
|
|
var t = getHintElement(hints, e.target || e.srcElement);
|
|
if (t && t.hintId != null) {
|
|
widget.changeActive(t.hintId);
|
|
if (completion.options.completeOnSingleClick) widget.pick();
|
|
}
|
|
});
|
|
|
|
CodeMirror.on(hints, "mousedown", function() {
|
|
setTimeout(function(){cm.focus();}, 20);
|
|
});
|
|
|
|
CodeMirror.signal(data, "select", completions[this.selectedHint], hints.childNodes[this.selectedHint]);
|
|
return true;
|
|
}
|
|
|
|
Widget.prototype = {
|
|
close: function() {
|
|
if (this.completion.widget != this) return;
|
|
this.completion.widget = null;
|
|
this.hints.parentNode.removeChild(this.hints);
|
|
this.completion.cm.removeKeyMap(this.keyMap);
|
|
|
|
var cm = this.completion.cm;
|
|
if (this.completion.options.closeOnUnfocus) {
|
|
cm.off("blur", this.onBlur);
|
|
cm.off("focus", this.onFocus);
|
|
}
|
|
cm.off("scroll", this.onScroll);
|
|
},
|
|
|
|
disable: function() {
|
|
this.completion.cm.removeKeyMap(this.keyMap);
|
|
var widget = this;
|
|
this.keyMap = {Enter: function() { widget.picked = true; }};
|
|
this.completion.cm.addKeyMap(this.keyMap);
|
|
},
|
|
|
|
pick: function() {
|
|
this.completion.pick(this.data, this.selectedHint);
|
|
},
|
|
|
|
changeActive: function(i, avoidWrap) {
|
|
if (i >= this.data.list.length)
|
|
i = avoidWrap ? this.data.list.length - 1 : 0;
|
|
else if (i < 0)
|
|
i = avoidWrap ? 0 : this.data.list.length - 1;
|
|
if (this.selectedHint == i) return;
|
|
var node = this.hints.childNodes[this.selectedHint];
|
|
if (node) node.className = node.className.replace(" " + ACTIVE_HINT_ELEMENT_CLASS, "");
|
|
node = this.hints.childNodes[this.selectedHint = i];
|
|
node.className += " " + ACTIVE_HINT_ELEMENT_CLASS;
|
|
if (node.offsetTop < this.hints.scrollTop)
|
|
this.hints.scrollTop = node.offsetTop - 3;
|
|
else if (node.offsetTop + node.offsetHeight > this.hints.scrollTop + this.hints.clientHeight)
|
|
this.hints.scrollTop = node.offsetTop + node.offsetHeight - this.hints.clientHeight + 3;
|
|
CodeMirror.signal(this.data, "select", this.data.list[this.selectedHint], node);
|
|
},
|
|
|
|
screenAmount: function() {
|
|
return Math.floor(this.hints.clientHeight / this.hints.firstChild.offsetHeight) || 1;
|
|
}
|
|
};
|
|
|
|
function applicableHelpers(cm, helpers) {
|
|
if (!cm.somethingSelected()) return helpers
|
|
var result = []
|
|
for (var i = 0; i < helpers.length; i++)
|
|
if (helpers[i].supportsSelection) result.push(helpers[i])
|
|
return result
|
|
}
|
|
|
|
function fetchHints(hint, cm, options, callback) {
|
|
if (hint.async) {
|
|
hint(cm, callback, options)
|
|
} else {
|
|
var result = hint(cm, options)
|
|
if (result && result.then) result.then(callback)
|
|
else callback(result)
|
|
}
|
|
}
|
|
|
|
function resolveAutoHints(cm, pos) {
|
|
var helpers = cm.getHelpers(pos, "hint"), words
|
|
if (helpers.length) {
|
|
var resolved = function(cm, callback, options) {
|
|
var app = applicableHelpers(cm, helpers);
|
|
function run(i) {
|
|
if (i == app.length) return callback(null)
|
|
fetchHints(app[i], cm, options, function(result) {
|
|
if (result && result.list.length > 0) callback(result)
|
|
else run(i + 1)
|
|
})
|
|
}
|
|
run(0)
|
|
}
|
|
resolved.async = true
|
|
resolved.supportsSelection = true
|
|
return resolved
|
|
} else if (words = cm.getHelper(cm.getCursor(), "hintWords")) {
|
|
return function(cm) { return CodeMirror.hint.fromList(cm, {words: words}) }
|
|
} else if (CodeMirror.hint.anyword) {
|
|
return function(cm, options) { return CodeMirror.hint.anyword(cm, options) }
|
|
} else {
|
|
return function() {}
|
|
}
|
|
}
|
|
|
|
CodeMirror.registerHelper("hint", "auto", {
|
|
resolve: resolveAutoHints
|
|
});
|
|
|
|
CodeMirror.registerHelper("hint", "fromList", function(cm, options) {
|
|
var cur = cm.getCursor(), token = cm.getTokenAt(cur)
|
|
var term, from = CodeMirror.Pos(cur.line, token.start), to = cur
|
|
if (token.start < cur.ch && /\w/.test(token.string.charAt(cur.ch - token.start - 1))) {
|
|
term = token.string.substr(0, cur.ch - token.start)
|
|
} else {
|
|
term = ""
|
|
from = cur
|
|
}
|
|
var found = [];
|
|
for (var i = 0; i < options.words.length; i++) {
|
|
var word = options.words[i];
|
|
if (word.slice(0, term.length) == term)
|
|
found.push(word);
|
|
}
|
|
|
|
if (found.length) return {list: found, from: from, to: to};
|
|
});
|
|
|
|
CodeMirror.commands.autocomplete = CodeMirror.showHint;
|
|
|
|
var defaultOptions = {
|
|
hint: CodeMirror.hint.auto,
|
|
completeSingle: true,
|
|
alignWithWord: true,
|
|
closeCharacters: /[\s()\[\]{};:>,]/,
|
|
closeOnUnfocus: true,
|
|
completeOnSingleClick: true,
|
|
container: null,
|
|
customKeys: null,
|
|
extraKeys: null
|
|
};
|
|
|
|
CodeMirror.defineOption("hintOptions", null);
|
|
});
|
|
|
|
// ========= dialog.js ========= //
|
|
|
|
// CodeMirror, copyright (c) by Marijn Haverbeke and others
|
|
// Distributed under an MIT license: https://codemirror.net/LICENSE
|
|
|
|
// Open simple dialogs on top of an editor. Relies on dialog.css.
|
|
|
|
(function(mod) {
|
|
if (typeof exports == "object" && typeof module == "object") // CommonJS
|
|
mod(require("../../lib/codemirror"));
|
|
else if (typeof define == "function" && define.amd) // AMD
|
|
define(["../../lib/codemirror"], mod);
|
|
else // Plain browser env
|
|
mod(CodeMirror);
|
|
})(function(CodeMirror) {
|
|
function dialogDiv(cm, template, bottom) {
|
|
var wrap = cm.getWrapperElement();
|
|
var dialog;
|
|
dialog = wrap.appendChild(document.createElement("div"));
|
|
if (bottom)
|
|
dialog.className = "CodeMirror-dialog CodeMirror-dialog-bottom";
|
|
else
|
|
dialog.className = "CodeMirror-dialog CodeMirror-dialog-top";
|
|
|
|
if (typeof template == "string") {
|
|
dialog.innerHTML = template;
|
|
} else { // Assuming it's a detached DOM element.
|
|
dialog.appendChild(template);
|
|
}
|
|
CodeMirror.addClass(wrap, 'dialog-opened');
|
|
return dialog;
|
|
}
|
|
|
|
function closeNotification(cm, newVal) {
|
|
if (cm.state.currentNotificationClose)
|
|
cm.state.currentNotificationClose();
|
|
cm.state.currentNotificationClose = newVal;
|
|
}
|
|
|
|
CodeMirror.defineExtension("openDialog", function(template, callback, options) {
|
|
if (!options) options = {};
|
|
|
|
closeNotification(this, null);
|
|
|
|
var dialog = dialogDiv(this, template, options.bottom);
|
|
var closed = false, me = this;
|
|
function close(newVal) {
|
|
if (typeof newVal == 'string') {
|
|
inp.value = newVal;
|
|
} else {
|
|
if (closed) return;
|
|
closed = true;
|
|
CodeMirror.rmClass(dialog.parentNode, 'dialog-opened');
|
|
dialog.parentNode.removeChild(dialog);
|
|
me.focus();
|
|
|
|
if (options.onClose) options.onClose(dialog);
|
|
}
|
|
}
|
|
|
|
var inp = dialog.getElementsByTagName("input")[0], button;
|
|
if (inp) {
|
|
inp.focus();
|
|
|
|
if (options.value) {
|
|
inp.value = options.value;
|
|
if (options.selectValueOnOpen !== false) {
|
|
inp.select();
|
|
}
|
|
}
|
|
|
|
if (options.onInput)
|
|
CodeMirror.on(inp, "input", function(e) { options.onInput(e, inp.value, close);});
|
|
if (options.onKeyUp)
|
|
CodeMirror.on(inp, "keyup", function(e) {options.onKeyUp(e, inp.value, close);});
|
|
|
|
CodeMirror.on(inp, "keydown", function(e) {
|
|
if (options && options.onKeyDown && options.onKeyDown(e, inp.value, close)) { return; }
|
|
if (e.keyCode == 27 || (options.closeOnEnter !== false && e.keyCode == 13)) {
|
|
inp.blur();
|
|
CodeMirror.e_stop(e);
|
|
close();
|
|
}
|
|
if (e.keyCode == 13) callback(inp.value, e);
|
|
});
|
|
|
|
if (options.closeOnBlur !== false) CodeMirror.on(inp, "blur", close);
|
|
} else if (button = dialog.getElementsByTagName("button")[0]) {
|
|
CodeMirror.on(button, "click", function() {
|
|
close();
|
|
me.focus();
|
|
});
|
|
|
|
if (options.closeOnBlur !== false) CodeMirror.on(button, "blur", close);
|
|
|
|
button.focus();
|
|
}
|
|
return close;
|
|
});
|
|
|
|
CodeMirror.defineExtension("openConfirm", function(template, callbacks, options) {
|
|
closeNotification(this, null);
|
|
var dialog = dialogDiv(this, template, options && options.bottom);
|
|
var buttons = dialog.getElementsByTagName("button");
|
|
var closed = false, me = this, blurring = 1;
|
|
function close() {
|
|
if (closed) return;
|
|
closed = true;
|
|
CodeMirror.rmClass(dialog.parentNode, 'dialog-opened');
|
|
dialog.parentNode.removeChild(dialog);
|
|
me.focus();
|
|
}
|
|
buttons[0].focus();
|
|
for (var i = 0; i < buttons.length; ++i) {
|
|
var b = buttons[i];
|
|
(function(callback) {
|
|
CodeMirror.on(b, "click", function(e) {
|
|
CodeMirror.e_preventDefault(e);
|
|
close();
|
|
if (callback) callback(me);
|
|
});
|
|
})(callbacks[i]);
|
|
CodeMirror.on(b, "blur", function() {
|
|
--blurring;
|
|
setTimeout(function() { if (blurring <= 0) close(); }, 200);
|
|
});
|
|
CodeMirror.on(b, "focus", function() { ++blurring; });
|
|
}
|
|
});
|
|
|
|
/*
|
|
* openNotification
|
|
* Opens a notification, that can be closed with an optional timer
|
|
* (default 5000ms timer) and always closes on click.
|
|
*
|
|
* If a notification is opened while another is opened, it will close the
|
|
* currently opened one and open the new one immediately.
|
|
*/
|
|
CodeMirror.defineExtension("openNotification", function(template, options) {
|
|
closeNotification(this, close);
|
|
var dialog = dialogDiv(this, template, options && options.bottom);
|
|
var closed = false, doneTimer;
|
|
var duration = options && typeof options.duration !== "undefined" ? options.duration : 5000;
|
|
|
|
function close() {
|
|
if (closed) return;
|
|
closed = true;
|
|
clearTimeout(doneTimer);
|
|
CodeMirror.rmClass(dialog.parentNode, 'dialog-opened');
|
|
dialog.parentNode.removeChild(dialog);
|
|
}
|
|
|
|
CodeMirror.on(dialog, 'click', function(e) {
|
|
CodeMirror.e_preventDefault(e);
|
|
close();
|
|
});
|
|
|
|
if (duration)
|
|
doneTimer = setTimeout(close, duration);
|
|
|
|
return close;
|
|
});
|
|
});
|
|
|
|
// ========= searchcursor.js ========= //
|
|
|
|
// CodeMirror, copyright (c) by Marijn Haverbeke and others
|
|
// Distributed under an MIT license: https://codemirror.net/LICENSE
|
|
|
|
(function(mod) {
|
|
if (typeof exports == "object" && typeof module == "object") // CommonJS
|
|
mod(require("../../lib/codemirror"))
|
|
else if (typeof define == "function" && define.amd) // AMD
|
|
define(["../../lib/codemirror"], mod)
|
|
else // Plain browser env
|
|
mod(CodeMirror)
|
|
})(function(CodeMirror) {
|
|
"use strict"
|
|
var Pos = CodeMirror.Pos
|
|
|
|
function regexpFlags(regexp) {
|
|
var flags = regexp.flags
|
|
return flags != null ? flags : (regexp.ignoreCase ? "i" : "")
|
|
+ (regexp.global ? "g" : "")
|
|
+ (regexp.multiline ? "m" : "")
|
|
}
|
|
|
|
function ensureFlags(regexp, flags) {
|
|
var current = regexpFlags(regexp), target = current
|
|
for (var i = 0; i < flags.length; i++) if (target.indexOf(flags.charAt(i)) == -1)
|
|
target += flags.charAt(i)
|
|
return current == target ? regexp : new RegExp(regexp.source, target)
|
|
}
|
|
|
|
function maybeMultiline(regexp) {
|
|
return /\\s|\\n|\n|\\W|\\D|\[\^/.test(regexp.source)
|
|
}
|
|
|
|
function searchRegexpForward(doc, regexp, start) {
|
|
regexp = ensureFlags(regexp, "g")
|
|
for (var line = start.line, ch = start.ch, last = doc.lastLine(); line <= last; line++, ch = 0) {
|
|
regexp.lastIndex = ch
|
|
var string = doc.getLine(line), match = regexp.exec(string)
|
|
if (match)
|
|
return {from: Pos(line, match.index),
|
|
to: Pos(line, match.index + match[0].length),
|
|
match: match}
|
|
}
|
|
}
|
|
|
|
function searchRegexpForwardMultiline(doc, regexp, start) {
|
|
if (!maybeMultiline(regexp)) return searchRegexpForward(doc, regexp, start)
|
|
|
|
regexp = ensureFlags(regexp, "gm")
|
|
var string, chunk = 1
|
|
for (var line = start.line, last = doc.lastLine(); line <= last;) {
|
|
// This grows the search buffer in exponentially-sized chunks
|
|
// between matches, so that nearby matches are fast and don't
|
|
// require concatenating the whole document (in case we're
|
|
// searching for something that has tons of matches), but at the
|
|
// same time, the amount of retries is limited.
|
|
for (var i = 0; i < chunk; i++) {
|
|
if (line > last) break
|
|
var curLine = doc.getLine(line++)
|
|
string = string == null ? curLine : string + "\n" + curLine
|
|
}
|
|
chunk = chunk * 2
|
|
regexp.lastIndex = start.ch
|
|
var match = regexp.exec(string)
|
|
if (match) {
|
|
var before = string.slice(0, match.index).split("\n"), inside = match[0].split("\n")
|
|
var startLine = start.line + before.length - 1, startCh = before[before.length - 1].length
|
|
return {from: Pos(startLine, startCh),
|
|
to: Pos(startLine + inside.length - 1,
|
|
inside.length == 1 ? startCh + inside[0].length : inside[inside.length - 1].length),
|
|
match: match}
|
|
}
|
|
}
|
|
}
|
|
|
|
function lastMatchIn(string, regexp) {
|
|
var cutOff = 0, match
|
|
for (;;) {
|
|
regexp.lastIndex = cutOff
|
|
var newMatch = regexp.exec(string)
|
|
if (!newMatch) return match
|
|
match = newMatch
|
|
cutOff = match.index + (match[0].length || 1)
|
|
if (cutOff == string.length) return match
|
|
}
|
|
}
|
|
|
|
function searchRegexpBackward(doc, regexp, start) {
|
|
regexp = ensureFlags(regexp, "g")
|
|
for (var line = start.line, ch = start.ch, first = doc.firstLine(); line >= first; line--, ch = -1) {
|
|
var string = doc.getLine(line)
|
|
if (ch > -1) string = string.slice(0, ch)
|
|
var match = lastMatchIn(string, regexp)
|
|
if (match)
|
|
return {from: Pos(line, match.index),
|
|
to: Pos(line, match.index + match[0].length),
|
|
match: match}
|
|
}
|
|
}
|
|
|
|
function searchRegexpBackwardMultiline(doc, regexp, start) {
|
|
regexp = ensureFlags(regexp, "gm")
|
|
var string, chunk = 1
|
|
for (var line = start.line, first = doc.firstLine(); line >= first;) {
|
|
for (var i = 0; i < chunk; i++) {
|
|
var curLine = doc.getLine(line--)
|
|
string = string == null ? curLine.slice(0, start.ch) : curLine + "\n" + string
|
|
}
|
|
chunk *= 2
|
|
|
|
var match = lastMatchIn(string, regexp)
|
|
if (match) {
|
|
var before = string.slice(0, match.index).split("\n"), inside = match[0].split("\n")
|
|
var startLine = line + before.length, startCh = before[before.length - 1].length
|
|
return {from: Pos(startLine, startCh),
|
|
to: Pos(startLine + inside.length - 1,
|
|
inside.length == 1 ? startCh + inside[0].length : inside[inside.length - 1].length),
|
|
match: match}
|
|
}
|
|
}
|
|
}
|
|
|
|
var doFold, noFold
|
|
if (String.prototype.normalize) {
|
|
doFold = function(str) { return str.normalize("NFD").toLowerCase() }
|
|
noFold = function(str) { return str.normalize("NFD") }
|
|
} else {
|
|
doFold = function(str) { return str.toLowerCase() }
|
|
noFold = function(str) { return str }
|
|
}
|
|
|
|
// Maps a position in a case-folded line back to a position in the original line
|
|
// (compensating for codepoints increasing in number during folding)
|
|
function adjustPos(orig, folded, pos, foldFunc) {
|
|
if (orig.length == folded.length) return pos
|
|
for (var min = 0, max = pos + Math.max(0, orig.length - folded.length);;) {
|
|
if (min == max) return min
|
|
var mid = (min + max) >> 1
|
|
var len = foldFunc(orig.slice(0, mid)).length
|
|
if (len == pos) return mid
|
|
else if (len > pos) max = mid
|
|
else min = mid + 1
|
|
}
|
|
}
|
|
|
|
function searchStringForward(doc, query, start, caseFold) {
|
|
// Empty string would match anything and never progress, so we
|
|
// define it to match nothing instead.
|
|
if (!query.length) return null
|
|
var fold = caseFold ? doFold : noFold
|
|
var lines = fold(query).split(/\r|\n\r?/)
|
|
|
|
search: for (var line = start.line, ch = start.ch, last = doc.lastLine() + 1 - lines.length; line <= last; line++, ch = 0) {
|
|
var orig = doc.getLine(line).slice(ch), string = fold(orig)
|
|
if (lines.length == 1) {
|
|
var found = string.indexOf(lines[0])
|
|
if (found == -1) continue search
|
|
var start = adjustPos(orig, string, found, fold) + ch
|
|
return {from: Pos(line, adjustPos(orig, string, found, fold) + ch),
|
|
to: Pos(line, adjustPos(orig, string, found + lines[0].length, fold) + ch)}
|
|
} else {
|
|
var cutFrom = string.length - lines[0].length
|
|
if (string.slice(cutFrom) != lines[0]) continue search
|
|
for (var i = 1; i < lines.length - 1; i++)
|
|
if (fold(doc.getLine(line + i)) != lines[i]) continue search
|
|
var end = doc.getLine(line + lines.length - 1), endString = fold(end), lastLine = lines[lines.length - 1]
|
|
if (endString.slice(0, lastLine.length) != lastLine) continue search
|
|
return {from: Pos(line, adjustPos(orig, string, cutFrom, fold) + ch),
|
|
to: Pos(line + lines.length - 1, adjustPos(end, endString, lastLine.length, fold))}
|
|
}
|
|
}
|
|
}
|
|
|
|
function searchStringBackward(doc, query, start, caseFold) {
|
|
if (!query.length) return null
|
|
var fold = caseFold ? doFold : noFold
|
|
var lines = fold(query).split(/\r|\n\r?/)
|
|
|
|
search: for (var line = start.line, ch = start.ch, first = doc.firstLine() - 1 + lines.length; line >= first; line--, ch = -1) {
|
|
var orig = doc.getLine(line)
|
|
if (ch > -1) orig = orig.slice(0, ch)
|
|
var string = fold(orig)
|
|
if (lines.length == 1) {
|
|
var found = string.lastIndexOf(lines[0])
|
|
if (found == -1) continue search
|
|
return {from: Pos(line, adjustPos(orig, string, found, fold)),
|
|
to: Pos(line, adjustPos(orig, string, found + lines[0].length, fold))}
|
|
} else {
|
|
var lastLine = lines[lines.length - 1]
|
|
if (string.slice(0, lastLine.length) != lastLine) continue search
|
|
for (var i = 1, start = line - lines.length + 1; i < lines.length - 1; i++)
|
|
if (fold(doc.getLine(start + i)) != lines[i]) continue search
|
|
var top = doc.getLine(line + 1 - lines.length), topString = fold(top)
|
|
if (topString.slice(topString.length - lines[0].length) != lines[0]) continue search
|
|
return {from: Pos(line + 1 - lines.length, adjustPos(top, topString, top.length - lines[0].length, fold)),
|
|
to: Pos(line, adjustPos(orig, string, lastLine.length, fold))}
|
|
}
|
|
}
|
|
}
|
|
|
|
function SearchCursor(doc, query, pos, options) {
|
|
this.atOccurrence = false
|
|
this.doc = doc
|
|
pos = pos ? doc.clipPos(pos) : Pos(0, 0)
|
|
this.pos = {from: pos, to: pos}
|
|
|
|
var caseFold
|
|
if (typeof options == "object") {
|
|
caseFold = options.caseFold
|
|
} else { // Backwards compat for when caseFold was the 4th argument
|
|
caseFold = options
|
|
options = null
|
|
}
|
|
|
|
if (typeof query == "string") {
|
|
if (caseFold == null) caseFold = false
|
|
this.matches = function(reverse, pos) {
|
|
return (reverse ? searchStringBackward : searchStringForward)(doc, query, pos, caseFold)
|
|
}
|
|
} else {
|
|
query = ensureFlags(query, "gm")
|
|
if (!options || options.multiline !== false)
|
|
this.matches = function(reverse, pos) {
|
|
return (reverse ? searchRegexpBackwardMultiline : searchRegexpForwardMultiline)(doc, query, pos)
|
|
}
|
|
else
|
|
this.matches = function(reverse, pos) {
|
|
return (reverse ? searchRegexpBackward : searchRegexpForward)(doc, query, pos)
|
|
}
|
|
}
|
|
}
|
|
|
|
SearchCursor.prototype = {
|
|
findNext: function() {return this.find(false)},
|
|
findPrevious: function() {return this.find(true)},
|
|
|
|
find: function(reverse) {
|
|
var result = this.matches(reverse, this.doc.clipPos(reverse ? this.pos.from : this.pos.to))
|
|
|
|
// Implements weird auto-growing behavior on null-matches for
|
|
// backwards-compatiblity with the vim code (unfortunately)
|
|
while (result && CodeMirror.cmpPos(result.from, result.to) == 0) {
|
|
if (reverse) {
|
|
if (result.from.ch) result.from = Pos(result.from.line, result.from.ch - 1)
|
|
else if (result.from.line == this.doc.firstLine()) result = null
|
|
else result = this.matches(reverse, this.doc.clipPos(Pos(result.from.line - 1)))
|
|
} else {
|
|
if (result.to.ch < this.doc.getLine(result.to.line).length) result.to = Pos(result.to.line, result.to.ch + 1)
|
|
else if (result.to.line == this.doc.lastLine()) result = null
|
|
else result = this.matches(reverse, Pos(result.to.line + 1, 0))
|
|
}
|
|
}
|
|
|
|
if (result) {
|
|
this.pos = result
|
|
this.atOccurrence = true
|
|
return this.pos.match || true
|
|
} else {
|
|
var end = Pos(reverse ? this.doc.firstLine() : this.doc.lastLine() + 1, 0)
|
|
this.pos = {from: end, to: end}
|
|
return this.atOccurrence = false
|
|
}
|
|
},
|
|
|
|
from: function() {if (this.atOccurrence) return this.pos.from},
|
|
to: function() {if (this.atOccurrence) return this.pos.to},
|
|
|
|
replace: function(newText, origin) {
|
|
if (!this.atOccurrence) return
|
|
var lines = CodeMirror.splitLines(newText)
|
|
this.doc.replaceRange(lines, this.pos.from, this.pos.to, origin)
|
|
this.pos.to = Pos(this.pos.from.line + lines.length - 1,
|
|
lines[lines.length - 1].length + (lines.length == 1 ? this.pos.from.ch : 0))
|
|
}
|
|
}
|
|
|
|
CodeMirror.defineExtension("getSearchCursor", function(query, pos, caseFold) {
|
|
return new SearchCursor(this.doc, query, pos, caseFold)
|
|
})
|
|
CodeMirror.defineDocExtension("getSearchCursor", function(query, pos, caseFold) {
|
|
return new SearchCursor(this, query, pos, caseFold)
|
|
})
|
|
|
|
CodeMirror.defineExtension("selectMatches", function(query, caseFold) {
|
|
var ranges = []
|
|
var cur = this.getSearchCursor(query, this.getCursor("from"), caseFold)
|
|
while (cur.findNext()) {
|
|
if (CodeMirror.cmpPos(cur.to(), this.getCursor("to")) > 0) break
|
|
ranges.push({anchor: cur.from(), head: cur.to()})
|
|
}
|
|
if (ranges.length)
|
|
this.setSelections(ranges, 0)
|
|
})
|
|
});
|
|
|
|
// ========= search.js ========= //
|
|
|
|
// CodeMirror, copyright (c) by Marijn Haverbeke and others
|
|
// Distributed under an MIT license: https://codemirror.net/LICENSE
|
|
|
|
// Define search commands. Depends on dialog.js or another
|
|
// implementation of the openDialog method.
|
|
|
|
// Replace works a little oddly -- it will do the replace on the next
|
|
// Ctrl-G (or whatever is bound to findNext) press. You prevent a
|
|
// replace by making sure the match is no longer selected when hitting
|
|
// Ctrl-G.
|
|
|
|
(function(mod) {
|
|
if (typeof exports == "object" && typeof module == "object") // CommonJS
|
|
mod(require("../../lib/codemirror"), require("./searchcursor"), require("../dialog/dialog"));
|
|
else if (typeof define == "function" && define.amd) // AMD
|
|
define(["../../lib/codemirror", "./searchcursor", "../dialog/dialog"], mod);
|
|
else // Plain browser env
|
|
mod(CodeMirror);
|
|
})(function(CodeMirror) {
|
|
"use strict";
|
|
|
|
function searchOverlay(query, caseInsensitive) {
|
|
if (typeof query == "string")
|
|
query = new RegExp(query.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&"), caseInsensitive ? "gi" : "g");
|
|
else if (!query.global)
|
|
query = new RegExp(query.source, query.ignoreCase ? "gi" : "g");
|
|
|
|
return {token: function(stream) {
|
|
query.lastIndex = stream.pos;
|
|
var match = query.exec(stream.string);
|
|
if (match && match.index == stream.pos) {
|
|
stream.pos += match[0].length || 1;
|
|
return "searching";
|
|
} else if (match) {
|
|
stream.pos = match.index;
|
|
} else {
|
|
stream.skipToEnd();
|
|
}
|
|
}};
|
|
}
|
|
|
|
function SearchState() {
|
|
this.posFrom = this.posTo = this.lastQuery = this.query = null;
|
|
this.overlay = null;
|
|
}
|
|
|
|
function getSearchState(cm) {
|
|
return cm.state.search || (cm.state.search = new SearchState());
|
|
}
|
|
|
|
function queryCaseInsensitive(query) {
|
|
return typeof query == "string" && query == query.toLowerCase();
|
|
}
|
|
|
|
function getSearchCursor(cm, query, pos) {
|
|
// Heuristic: if the query string is all lowercase, do a case insensitive search.
|
|
return cm.getSearchCursor(query, pos, {caseFold: queryCaseInsensitive(query), multiline: true});
|
|
}
|
|
|
|
function persistentDialog(cm, text, deflt, onEnter, onKeyDown) {
|
|
cm.openDialog(text, onEnter, {
|
|
value: deflt,
|
|
selectValueOnOpen: true,
|
|
closeOnEnter: false,
|
|
onClose: function() { clearSearch(cm); },
|
|
onKeyDown: onKeyDown
|
|
});
|
|
}
|
|
|
|
function dialog(cm, text, shortText, deflt, f) {
|
|
if (cm.openDialog) cm.openDialog(text, f, {value: deflt, selectValueOnOpen: true});
|
|
else f(prompt(shortText, deflt));
|
|
}
|
|
|
|
function confirmDialog(cm, text, shortText, fs) {
|
|
if (cm.openConfirm) cm.openConfirm(text, fs);
|
|
else if (confirm(shortText)) fs[0]();
|
|
}
|
|
|
|
function parseString(string) {
|
|
return string.replace(/\\(.)/g, function(_, ch) {
|
|
if (ch == "n") return "\n"
|
|
if (ch == "r") return "\r"
|
|
return ch
|
|
})
|
|
}
|
|
|
|
function parseQuery(query) {
|
|
var isRE = query.match(/^\/(.*)\/([a-z]*)$/);
|
|
if (isRE) {
|
|
try { query = new RegExp(isRE[1], isRE[2].indexOf("i") == -1 ? "" : "i"); }
|
|
catch(e) {} // Not a regular expression after all, do a string search
|
|
} else {
|
|
query = parseString(query)
|
|
}
|
|
if (typeof query == "string" ? query == "" : query.test(""))
|
|
query = /x^/;
|
|
return query;
|
|
}
|
|
|
|
function startSearch(cm, state, query) {
|
|
state.queryText = query;
|
|
state.query = parseQuery(query);
|
|
cm.removeOverlay(state.overlay, queryCaseInsensitive(state.query));
|
|
state.overlay = searchOverlay(state.query, queryCaseInsensitive(state.query));
|
|
cm.addOverlay(state.overlay);
|
|
if (cm.showMatchesOnScrollbar) {
|
|
if (state.annotate) { state.annotate.clear(); state.annotate = null; }
|
|
state.annotate = cm.showMatchesOnScrollbar(state.query, queryCaseInsensitive(state.query));
|
|
}
|
|
}
|
|
|
|
function doSearch(cm, rev, persistent, immediate) {
|
|
var state = getSearchState(cm);
|
|
if (state.query) return findNext(cm, rev);
|
|
var q = cm.getSelection() || state.lastQuery;
|
|
if (q instanceof RegExp && q.source == "x^") q = null
|
|
if (persistent && cm.openDialog) {
|
|
var hiding = null
|
|
var searchNext = function(query, event) {
|
|
CodeMirror.e_stop(event);
|
|
if (!query) return;
|
|
if (query != state.queryText) {
|
|
startSearch(cm, state, query);
|
|
state.posFrom = state.posTo = cm.getCursor();
|
|
}
|
|
if (hiding) hiding.style.opacity = 1
|
|
findNext(cm, event.shiftKey, function(_, to) {
|
|
var dialog
|
|
if (to.line < 3 && document.querySelector &&
|
|
(dialog = cm.display.wrapper.querySelector(".CodeMirror-dialog")) &&
|
|
dialog.getBoundingClientRect().bottom - 4 > cm.cursorCoords(to, "window").top)
|
|
(hiding = dialog).style.opacity = .4
|
|
})
|
|
};
|
|
persistentDialog(cm, getQueryDialog(cm), q, searchNext, function(event, query) {
|
|
var keyName = CodeMirror.keyName(event)
|
|
var extra = cm.getOption('extraKeys'), cmd = (extra && extra[keyName]) || CodeMirror.keyMap[cm.getOption("keyMap")][keyName]
|
|
if (cmd == "findNext" || cmd == "findPrev" ||
|
|
cmd == "findPersistentNext" || cmd == "findPersistentPrev") {
|
|
CodeMirror.e_stop(event);
|
|
startSearch(cm, getSearchState(cm), query);
|
|
cm.execCommand(cmd);
|
|
} else if (cmd == "find" || cmd == "findPersistent") {
|
|
CodeMirror.e_stop(event);
|
|
searchNext(query, event);
|
|
}
|
|
});
|
|
if (immediate && q) {
|
|
startSearch(cm, state, q);
|
|
findNext(cm, rev);
|
|
}
|
|
} else {
|
|
dialog(cm, getQueryDialog(cm), "Search for:", q, function(query) {
|
|
if (query && !state.query) cm.operation(function() {
|
|
startSearch(cm, state, query);
|
|
state.posFrom = state.posTo = cm.getCursor();
|
|
findNext(cm, rev);
|
|
});
|
|
});
|
|
}
|
|
}
|
|
|
|
function findNext(cm, rev, callback) {cm.operation(function() {
|
|
var state = getSearchState(cm);
|
|
var cursor = getSearchCursor(cm, state.query, rev ? state.posFrom : state.posTo);
|
|
if (!cursor.find(rev)) {
|
|
cursor = getSearchCursor(cm, state.query, rev ? CodeMirror.Pos(cm.lastLine()) : CodeMirror.Pos(cm.firstLine(), 0));
|
|
if (!cursor.find(rev)) return;
|
|
}
|
|
cm.setSelection(cursor.from(), cursor.to());
|
|
cm.scrollIntoView({from: cursor.from(), to: cursor.to()}, 20);
|
|
state.posFrom = cursor.from(); state.posTo = cursor.to();
|
|
if (callback) callback(cursor.from(), cursor.to())
|
|
});}
|
|
|
|
function clearSearch(cm) {cm.operation(function() {
|
|
var state = getSearchState(cm);
|
|
state.lastQuery = state.query;
|
|
if (!state.query) return;
|
|
state.query = state.queryText = null;
|
|
cm.removeOverlay(state.overlay);
|
|
if (state.annotate) { state.annotate.clear(); state.annotate = null; }
|
|
});}
|
|
|
|
|
|
function getQueryDialog(cm) {
|
|
return '<span class="CodeMirror-search-label">' + cm.phrase("Search:") + '</span> <input type="text" style="width: 10em" class="CodeMirror-search-field"/> <span style="color: #888" class="CodeMirror-search-hint">' + cm.phrase("(Use /re/ syntax for regexp search)") + '</span>';
|
|
}
|
|
function getReplaceQueryDialog(cm) {
|
|
return ' <input type="text" style="width: 10em" class="CodeMirror-search-field"/> <span style="color: #888" class="CodeMirror-search-hint">' + cm.phrase("(Use /re/ syntax for regexp search)") + '</span>';
|
|
}
|
|
function getReplacementQueryDialog(cm) {
|
|
return '<span class="CodeMirror-search-label">' + cm.phrase("With:") + '</span> <input type="text" style="width: 10em" class="CodeMirror-search-field"/>';
|
|
}
|
|
function getDoReplaceConfirm(cm) {
|
|
return '<span class="CodeMirror-search-label">' + cm.phrase("Replace?") + '</span> <button>' + cm.phrase("Yes") + '</button> <button>' + cm.phrase("No") + '</button> <button>' + cm.phrase("All") + '</button> <button>' + cm.phrase("Stop") + '</button> ';
|
|
}
|
|
|
|
function replaceAll(cm, query, text) {
|
|
cm.operation(function() {
|
|
for (var cursor = getSearchCursor(cm, query); cursor.findNext();) {
|
|
if (typeof query != "string") {
|
|
var match = cm.getRange(cursor.from(), cursor.to()).match(query);
|
|
cursor.replace(text.replace(/\$(\d)/g, function(_, i) {return match[i];}));
|
|
} else cursor.replace(text);
|
|
}
|
|
});
|
|
}
|
|
|
|
function replace(cm, all) {
|
|
if (cm.getOption("readOnly")) return;
|
|
var query = cm.getSelection() || getSearchState(cm).lastQuery;
|
|
var dialogText = '<span class="CodeMirror-search-label">' + (all ? cm.phrase("Replace all:") : cm.phrase("Replace:")) + '</span>';
|
|
dialog(cm, dialogText + getReplaceQueryDialog(cm), dialogText, query, function(query) {
|
|
if (!query) return;
|
|
query = parseQuery(query);
|
|
dialog(cm, getReplacementQueryDialog(cm), cm.phrase("Replace with:"), "", function(text) {
|
|
text = parseString(text)
|
|
if (all) {
|
|
replaceAll(cm, query, text)
|
|
} else {
|
|
clearSearch(cm);
|
|
var cursor = getSearchCursor(cm, query, cm.getCursor("from"));
|
|
var advance = function() {
|
|
var start = cursor.from(), match;
|
|
if (!(match = cursor.findNext())) {
|
|
cursor = getSearchCursor(cm, query);
|
|
if (!(match = cursor.findNext()) ||
|
|
(start && cursor.from().line == start.line && cursor.from().ch == start.ch)) return;
|
|
}
|
|
cm.setSelection(cursor.from(), cursor.to());
|
|
cm.scrollIntoView({from: cursor.from(), to: cursor.to()});
|
|
confirmDialog(cm, getDoReplaceConfirm(cm), cm.phrase("Replace?"),
|
|
[function() {doReplace(match);}, advance,
|
|
function() {replaceAll(cm, query, text)}]);
|
|
};
|
|
var doReplace = function(match) {
|
|
cursor.replace(typeof query == "string" ? text :
|
|
text.replace(/\$(\d)/g, function(_, i) {return match[i];}));
|
|
advance();
|
|
};
|
|
advance();
|
|
}
|
|
});
|
|
});
|
|
}
|
|
|
|
CodeMirror.commands.find = function(cm) {clearSearch(cm); doSearch(cm);};
|
|
CodeMirror.commands.findPersistent = function(cm) {clearSearch(cm); doSearch(cm, false, true);};
|
|
CodeMirror.commands.findPersistentNext = function(cm) {doSearch(cm, false, true, true);};
|
|
CodeMirror.commands.findPersistentPrev = function(cm) {doSearch(cm, true, true, true);};
|
|
CodeMirror.commands.findNext = doSearch;
|
|
CodeMirror.commands.findPrev = function(cm) {doSearch(cm, true);};
|
|
CodeMirror.commands.clearSearch = clearSearch;
|
|
CodeMirror.commands.replace = replace;
|
|
CodeMirror.commands.replaceAll = function(cm) {replace(cm, true);};
|
|
});
|
|
|
|
// ========= matchbrackets.js ========= //
|
|
|
|
// CodeMirror, copyright (c) by Marijn Haverbeke and others
|
|
// Distributed under an MIT license: https://codemirror.net/LICENSE
|
|
|
|
(function(mod) {
|
|
if (typeof exports == "object" && typeof module == "object") // CommonJS
|
|
mod(require("../../lib/codemirror"));
|
|
else if (typeof define == "function" && define.amd) // AMD
|
|
define(["../../lib/codemirror"], mod);
|
|
else // Plain browser env
|
|
mod(CodeMirror);
|
|
})(function(CodeMirror) {
|
|
var ie_lt8 = /MSIE \d/.test(navigator.userAgent) &&
|
|
(document.documentMode == null || document.documentMode < 8);
|
|
|
|
var Pos = CodeMirror.Pos;
|
|
|
|
var matching = {"(": ")>", ")": "(<", "[": "]>", "]": "[<", "{": "}>", "}": "{<", "<": ">>", ">": "<<"};
|
|
|
|
function bracketRegex(config) {
|
|
return config && config.bracketRegex || /[(){}[\]]/
|
|
}
|
|
|
|
function findMatchingBracket(cm, where, config) {
|
|
var line = cm.getLineHandle(where.line), pos = where.ch - 1;
|
|
var afterCursor = config && config.afterCursor
|
|
if (afterCursor == null)
|
|
afterCursor = /(^| )cm-fat-cursor($| )/.test(cm.getWrapperElement().className)
|
|
var re = bracketRegex(config)
|
|
|
|
// A cursor is defined as between two characters, but in in vim command mode
|
|
// (i.e. not insert mode), the cursor is visually represented as a
|
|
// highlighted box on top of the 2nd character. Otherwise, we allow matches
|
|
// from before or after the cursor.
|
|
var match = (!afterCursor && pos >= 0 && re.test(line.text.charAt(pos)) && matching[line.text.charAt(pos)]) ||
|
|
re.test(line.text.charAt(pos + 1)) && matching[line.text.charAt(++pos)];
|
|
if (!match) return null;
|
|
var dir = match.charAt(1) == ">" ? 1 : -1;
|
|
if (config && config.strict && (dir > 0) != (pos == where.ch)) return null;
|
|
var style = cm.getTokenTypeAt(Pos(where.line, pos + 1));
|
|
|
|
var found = scanForBracket(cm, Pos(where.line, pos + (dir > 0 ? 1 : 0)), dir, style || null, config);
|
|
if (found == null) return null;
|
|
return {from: Pos(where.line, pos), to: found && found.pos,
|
|
match: found && found.ch == match.charAt(0), forward: dir > 0};
|
|
}
|
|
|
|
// bracketRegex is used to specify which type of bracket to scan
|
|
// should be a regexp, e.g. /[[\]]/
|
|
//
|
|
// Note: If "where" is on an open bracket, then this bracket is ignored.
|
|
//
|
|
// Returns false when no bracket was found, null when it reached
|
|
// maxScanLines and gave up
|
|
function scanForBracket(cm, where, dir, style, config) {
|
|
var maxScanLen = (config && config.maxScanLineLength) || 10000;
|
|
var maxScanLines = (config && config.maxScanLines) || 1000;
|
|
|
|
var stack = [];
|
|
var re = bracketRegex(config)
|
|
var lineEnd = dir > 0 ? Math.min(where.line + maxScanLines, cm.lastLine() + 1)
|
|
: Math.max(cm.firstLine() - 1, where.line - maxScanLines);
|
|
for (var lineNo = where.line; lineNo != lineEnd; lineNo += dir) {
|
|
var line = cm.getLine(lineNo);
|
|
if (!line) continue;
|
|
var pos = dir > 0 ? 0 : line.length - 1, end = dir > 0 ? line.length : -1;
|
|
if (line.length > maxScanLen) continue;
|
|
if (lineNo == where.line) pos = where.ch - (dir < 0 ? 1 : 0);
|
|
for (; pos != end; pos += dir) {
|
|
var ch = line.charAt(pos);
|
|
if (re.test(ch) && (style === undefined || cm.getTokenTypeAt(Pos(lineNo, pos + 1)) == style)) {
|
|
var match = matching[ch];
|
|
if (match && (match.charAt(1) == ">") == (dir > 0)) stack.push(ch);
|
|
else if (!stack.length) return {pos: Pos(lineNo, pos), ch: ch};
|
|
else stack.pop();
|
|
}
|
|
}
|
|
}
|
|
return lineNo - dir == (dir > 0 ? cm.lastLine() : cm.firstLine()) ? false : null;
|
|
}
|
|
|
|
function matchBrackets(cm, autoclear, config) {
|
|
// Disable brace matching in long lines, since it'll cause hugely slow updates
|
|
var maxHighlightLen = cm.state.matchBrackets.maxHighlightLineLength || 1000;
|
|
var marks = [], ranges = cm.listSelections();
|
|
for (var i = 0; i < ranges.length; i++) {
|
|
var match = ranges[i].empty() && findMatchingBracket(cm, ranges[i].head, config);
|
|
if (match && cm.getLine(match.from.line).length <= maxHighlightLen) {
|
|
var style = match.match ? "CodeMirror-matchingbracket" : "CodeMirror-nonmatchingbracket";
|
|
marks.push(cm.markText(match.from, Pos(match.from.line, match.from.ch + 1), {className: style}));
|
|
if (match.to && cm.getLine(match.to.line).length <= maxHighlightLen)
|
|
marks.push(cm.markText(match.to, Pos(match.to.line, match.to.ch + 1), {className: style}));
|
|
}
|
|
}
|
|
|
|
if (marks.length) {
|
|
// Kludge to work around the IE bug from issue #1193, where text
|
|
// input stops going to the textare whever this fires.
|
|
if (ie_lt8 && cm.state.focused) cm.focus();
|
|
|
|
var clear = function() {
|
|
cm.operation(function() {
|
|
for (var i = 0; i < marks.length; i++) marks[i].clear();
|
|
});
|
|
};
|
|
if (autoclear) setTimeout(clear, 800);
|
|
else return clear;
|
|
}
|
|
}
|
|
|
|
function doMatchBrackets(cm) {
|
|
cm.operation(function() {
|
|
if (cm.state.matchBrackets.currentlyHighlighted) {
|
|
cm.state.matchBrackets.currentlyHighlighted();
|
|
cm.state.matchBrackets.currentlyHighlighted = null;
|
|
}
|
|
cm.state.matchBrackets.currentlyHighlighted = matchBrackets(cm, false, cm.state.matchBrackets);
|
|
});
|
|
}
|
|
|
|
CodeMirror.defineOption("matchBrackets", false, function(cm, val, old) {
|
|
if (old && old != CodeMirror.Init) {
|
|
cm.off("cursorActivity", doMatchBrackets);
|
|
if (cm.state.matchBrackets && cm.state.matchBrackets.currentlyHighlighted) {
|
|
cm.state.matchBrackets.currentlyHighlighted();
|
|
cm.state.matchBrackets.currentlyHighlighted = null;
|
|
}
|
|
}
|
|
if (val) {
|
|
cm.state.matchBrackets = typeof val == "object" ? val : {};
|
|
cm.on("cursorActivity", doMatchBrackets);
|
|
}
|
|
});
|
|
|
|
CodeMirror.defineExtension("matchBrackets", function() {matchBrackets(this, true);});
|
|
CodeMirror.defineExtension("findMatchingBracket", function(pos, config, oldConfig){
|
|
// Backwards-compatibility kludge
|
|
if (oldConfig || typeof config == "boolean") {
|
|
if (!oldConfig) {
|
|
config = config ? {strict: true} : null
|
|
} else {
|
|
oldConfig.strict = config
|
|
config = oldConfig
|
|
}
|
|
}
|
|
return findMatchingBracket(this, pos, config)
|
|
});
|
|
CodeMirror.defineExtension("scanForBracket", function(pos, dir, style, config){
|
|
return scanForBracket(this, pos, dir, style, config);
|
|
});
|
|
});
|
|
|
|
// ========= close-bracket.js ========= //
|
|
|
|
// CodeMirror, copyright (c) by Marijn Haverbeke and others
|
|
// Distributed under an MIT license: https://codemirror.net/LICENSE
|
|
|
|
(function(mod) {
|
|
if (typeof exports == "object" && typeof module == "object") // CommonJS
|
|
mod(require("../../lib/codemirror"));
|
|
else if (typeof define == "function" && define.amd) // AMD
|
|
define(["../../lib/codemirror"], mod);
|
|
else // Plain browser env
|
|
mod(CodeMirror);
|
|
})(function(CodeMirror) {
|
|
var defaults = {
|
|
pairs: "()[]{}''\"\"",
|
|
closeBefore: ")]}'\":;>",
|
|
triples: "",
|
|
explode: "[]{}"
|
|
};
|
|
|
|
var Pos = CodeMirror.Pos;
|
|
|
|
CodeMirror.defineOption("autoCloseBrackets", false, function(cm, val, old) {
|
|
if (old && old != CodeMirror.Init) {
|
|
cm.removeKeyMap(keyMap);
|
|
cm.state.closeBrackets = null;
|
|
}
|
|
if (val) {
|
|
ensureBound(getOption(val, "pairs"))
|
|
cm.state.closeBrackets = val;
|
|
cm.addKeyMap(keyMap);
|
|
}
|
|
});
|
|
|
|
function getOption(conf, name) {
|
|
if (name == "pairs" && typeof conf == "string") return conf;
|
|
if (typeof conf == "object" && conf[name] != null) return conf[name];
|
|
return defaults[name];
|
|
}
|
|
|
|
var keyMap = {Backspace: handleBackspace, Enter: handleEnter};
|
|
function ensureBound(chars) {
|
|
for (var i = 0; i < chars.length; i++) {
|
|
var ch = chars.charAt(i), key = "'" + ch + "'"
|
|
if (!keyMap[key]) keyMap[key] = handler(ch)
|
|
}
|
|
}
|
|
ensureBound(defaults.pairs + "`")
|
|
|
|
function handler(ch) {
|
|
return function(cm) { return handleChar(cm, ch); };
|
|
}
|
|
|
|
function getConfig(cm) {
|
|
var deflt = cm.state.closeBrackets;
|
|
if (!deflt || deflt.override) return deflt;
|
|
var mode = cm.getModeAt(cm.getCursor());
|
|
return mode.closeBrackets || deflt;
|
|
}
|
|
|
|
function handleBackspace(cm) {
|
|
var conf = getConfig(cm);
|
|
if (!conf || cm.getOption("disableInput")) return CodeMirror.Pass;
|
|
|
|
var pairs = getOption(conf, "pairs");
|
|
var ranges = cm.listSelections();
|
|
for (var i = 0; i < ranges.length; i++) {
|
|
if (!ranges[i].empty()) return CodeMirror.Pass;
|
|
var around = charsAround(cm, ranges[i].head);
|
|
if (!around || pairs.indexOf(around) % 2 != 0) return CodeMirror.Pass;
|
|
}
|
|
for (var i = ranges.length - 1; i >= 0; i--) {
|
|
var cur = ranges[i].head;
|
|
cm.replaceRange("", Pos(cur.line, cur.ch - 1), Pos(cur.line, cur.ch + 1), "+delete");
|
|
}
|
|
}
|
|
|
|
function handleEnter(cm) {
|
|
var conf = getConfig(cm);
|
|
var explode = conf && getOption(conf, "explode");
|
|
if (!explode || cm.getOption("disableInput")) return CodeMirror.Pass;
|
|
|
|
var ranges = cm.listSelections();
|
|
for (var i = 0; i < ranges.length; i++) {
|
|
if (!ranges[i].empty()) return CodeMirror.Pass;
|
|
var around = charsAround(cm, ranges[i].head);
|
|
if (!around || explode.indexOf(around) % 2 != 0) return CodeMirror.Pass;
|
|
}
|
|
cm.operation(function() {
|
|
var linesep = cm.lineSeparator() || "\n";
|
|
cm.replaceSelection(linesep + linesep, null);
|
|
cm.execCommand("goCharLeft");
|
|
ranges = cm.listSelections();
|
|
for (var i = 0; i < ranges.length; i++) {
|
|
var line = ranges[i].head.line;
|
|
cm.indentLine(line, null, true);
|
|
cm.indentLine(line + 1, null, true);
|
|
}
|
|
});
|
|
}
|
|
|
|
function contractSelection(sel) {
|
|
var inverted = CodeMirror.cmpPos(sel.anchor, sel.head) > 0;
|
|
return {anchor: new Pos(sel.anchor.line, sel.anchor.ch + (inverted ? -1 : 1)),
|
|
head: new Pos(sel.head.line, sel.head.ch + (inverted ? 1 : -1))};
|
|
}
|
|
|
|
function handleChar(cm, ch) {
|
|
var conf = getConfig(cm);
|
|
if (!conf || cm.getOption("disableInput")) return CodeMirror.Pass;
|
|
|
|
var pairs = getOption(conf, "pairs");
|
|
var pos = pairs.indexOf(ch);
|
|
if (pos == -1) return CodeMirror.Pass;
|
|
|
|
var closeBefore = getOption(conf,"closeBefore");
|
|
|
|
var triples = getOption(conf, "triples");
|
|
|
|
var identical = pairs.charAt(pos + 1) == ch;
|
|
var ranges = cm.listSelections();
|
|
var opening = pos % 2 == 0;
|
|
|
|
var type;
|
|
for (var i = 0; i < ranges.length; i++) {
|
|
var range = ranges[i], cur = range.head, curType;
|
|
var next = cm.getRange(cur, Pos(cur.line, cur.ch + 1));
|
|
if (opening && !range.empty()) {
|
|
curType = "surround";
|
|
} else if ((identical || !opening) && next == ch) {
|
|
if (identical && stringStartsAfter(cm, cur))
|
|
curType = "both";
|
|
else if (triples.indexOf(ch) >= 0 && cm.getRange(cur, Pos(cur.line, cur.ch + 3)) == ch + ch + ch)
|
|
curType = "skipThree";
|
|
else
|
|
curType = "skip";
|
|
} else if (identical && cur.ch > 1 && triples.indexOf(ch) >= 0 &&
|
|
cm.getRange(Pos(cur.line, cur.ch - 2), cur) == ch + ch) {
|
|
if (cur.ch > 2 && /\bstring/.test(cm.getTokenTypeAt(Pos(cur.line, cur.ch - 2)))) return CodeMirror.Pass;
|
|
curType = "addFour";
|
|
} else if (identical) {
|
|
var prev = cur.ch == 0 ? " " : cm.getRange(Pos(cur.line, cur.ch - 1), cur)
|
|
if (!CodeMirror.isWordChar(next) && prev != ch && !CodeMirror.isWordChar(prev)) curType = "both";
|
|
else return CodeMirror.Pass;
|
|
} else if (opening && (next.length === 0 || /\s/.test(next) || closeBefore.indexOf(next) > -1)) {
|
|
curType = "both";
|
|
} else {
|
|
return CodeMirror.Pass;
|
|
}
|
|
if (!type) type = curType;
|
|
else if (type != curType) return CodeMirror.Pass;
|
|
}
|
|
|
|
var left = pos % 2 ? pairs.charAt(pos - 1) : ch;
|
|
var right = pos % 2 ? ch : pairs.charAt(pos + 1);
|
|
cm.operation(function() {
|
|
if (type == "skip") {
|
|
cm.execCommand("goCharRight");
|
|
} else if (type == "skipThree") {
|
|
for (var i = 0; i < 3; i++)
|
|
cm.execCommand("goCharRight");
|
|
} else if (type == "surround") {
|
|
var sels = cm.getSelections();
|
|
for (var i = 0; i < sels.length; i++)
|
|
sels[i] = left + sels[i] + right;
|
|
cm.replaceSelections(sels, "around");
|
|
sels = cm.listSelections().slice();
|
|
for (var i = 0; i < sels.length; i++)
|
|
sels[i] = contractSelection(sels[i]);
|
|
cm.setSelections(sels);
|
|
} else if (type == "both") {
|
|
cm.replaceSelection(left + right, null);
|
|
cm.triggerElectric(left + right);
|
|
cm.execCommand("goCharLeft");
|
|
} else if (type == "addFour") {
|
|
cm.replaceSelection(left + left + left + left, "before");
|
|
cm.execCommand("goCharRight");
|
|
}
|
|
});
|
|
}
|
|
|
|
function charsAround(cm, pos) {
|
|
var str = cm.getRange(Pos(pos.line, pos.ch - 1),
|
|
Pos(pos.line, pos.ch + 1));
|
|
return str.length == 2 ? str : null;
|
|
}
|
|
|
|
function stringStartsAfter(cm, pos) {
|
|
var token = cm.getTokenAt(Pos(pos.line, pos.ch + 1))
|
|
return /\bstring/.test(token.type) && token.start == pos.ch &&
|
|
(pos.ch == 0 || !/\bstring/.test(cm.getTokenTypeAt(pos)))
|
|
}
|
|
});
|
|
|
|
// ========= active-line.js ========= //
|
|
// CodeMirror, copyright (c) by Marijn Haverbeke and others
|
|
// Distributed under an MIT license: https://codemirror.net/LICENSE
|
|
|
|
(function(mod) {
|
|
if (typeof exports == "object" && typeof module == "object") // CommonJS
|
|
mod(require("../../lib/codemirror"));
|
|
else if (typeof define == "function" && define.amd) // AMD
|
|
define(["../../lib/codemirror"], mod);
|
|
else // Plain browser env
|
|
mod(CodeMirror);
|
|
})(function(CodeMirror) {
|
|
"use strict";
|
|
var WRAP_CLASS = "CodeMirror-activeline";
|
|
var BACK_CLASS = "CodeMirror-activeline-background";
|
|
var GUTT_CLASS = "CodeMirror-activeline-gutter";
|
|
|
|
CodeMirror.defineOption("styleActiveLine", false, function(cm, val, old) {
|
|
var prev = old == CodeMirror.Init ? false : old;
|
|
if (val == prev) return
|
|
if (prev) {
|
|
cm.off("beforeSelectionChange", selectionChange);
|
|
clearActiveLines(cm);
|
|
delete cm.state.activeLines;
|
|
}
|
|
if (val) {
|
|
cm.state.activeLines = [];
|
|
updateActiveLines(cm, cm.listSelections());
|
|
cm.on("beforeSelectionChange", selectionChange);
|
|
}
|
|
});
|
|
|
|
function clearActiveLines(cm) {
|
|
for (var i = 0; i < cm.state.activeLines.length; i++) {
|
|
cm.removeLineClass(cm.state.activeLines[i], "wrap", WRAP_CLASS);
|
|
cm.removeLineClass(cm.state.activeLines[i], "background", BACK_CLASS);
|
|
cm.removeLineClass(cm.state.activeLines[i], "gutter", GUTT_CLASS);
|
|
}
|
|
}
|
|
|
|
function sameArray(a, b) {
|
|
if (a.length != b.length) return false;
|
|
for (var i = 0; i < a.length; i++)
|
|
if (a[i] != b[i]) return false;
|
|
return true;
|
|
}
|
|
|
|
function updateActiveLines(cm, ranges) {
|
|
var active = [];
|
|
for (var i = 0; i < ranges.length; i++) {
|
|
var range = ranges[i];
|
|
var option = cm.getOption("styleActiveLine");
|
|
if (typeof option == "object" && option.nonEmpty ? range.anchor.line != range.head.line : !range.empty())
|
|
continue
|
|
var line = cm.getLineHandleVisualStart(range.head.line);
|
|
if (active[active.length - 1] != line) active.push(line);
|
|
}
|
|
if (sameArray(cm.state.activeLines, active)) return;
|
|
cm.operation(function() {
|
|
clearActiveLines(cm);
|
|
for (var i = 0; i < active.length; i++) {
|
|
cm.addLineClass(active[i], "wrap", WRAP_CLASS);
|
|
cm.addLineClass(active[i], "background", BACK_CLASS);
|
|
cm.addLineClass(active[i], "gutter", GUTT_CLASS);
|
|
}
|
|
cm.state.activeLines = active;
|
|
});
|
|
}
|
|
|
|
function selectionChange(cm, sel) {
|
|
updateActiveLines(cm, sel.ranges);
|
|
}
|
|
});
|
|
|
|
// ========= tern.js ========= //
|
|
|
|
// CodeMirror, copyright (c) by Marijn Haverbeke and others
|
|
// Distributed under an MIT license: https://codemirror.net/LICENSE
|
|
|
|
// Glue code between CodeMirror and Tern.
|
|
//
|
|
// Create a CodeMirror.TernServer to wrap an actual Tern server,
|
|
// register open documents (CodeMirror.Doc instances) with it, and
|
|
// call its methods to activate the assisting functions that Tern
|
|
// provides.
|
|
//
|
|
// Options supported (all optional):
|
|
// * defs: An array of JSON definition data structures.
|
|
// * plugins: An object mapping plugin names to configuration
|
|
// options.
|
|
// * getFile: A function(name, c) that can be used to access files in
|
|
// the project that haven't been loaded yet. Simply do c(null) to
|
|
// indicate that a file is not available.
|
|
// * fileFilter: A function(value, docName, doc) that will be applied
|
|
// to documents before passing them on to Tern.
|
|
// * switchToDoc: A function(name, doc) that should, when providing a
|
|
// multi-file view, switch the view or focus to the named file.
|
|
// * showError: A function(editor, message) that can be used to
|
|
// override the way errors are displayed.
|
|
// * completionTip: Customize the content in tooltips for completions.
|
|
// Is passed a single argument—the completion's data as returned by
|
|
// Tern—and may return a string, DOM node, or null to indicate that
|
|
// no tip should be shown. By default the docstring is shown.
|
|
// * typeTip: Like completionTip, but for the tooltips shown for type
|
|
// queries.
|
|
// * responseFilter: A function(doc, query, request, error, data) that
|
|
// will be applied to the Tern responses before treating them
|
|
//
|
|
//
|
|
// It is possible to run the Tern server in a web worker by specifying
|
|
// these additional options:
|
|
// * useWorker: Set to true to enable web worker mode. You'll probably
|
|
// want to feature detect the actual value you use here, for example
|
|
// !!window.Worker.
|
|
// * workerScript: The main script of the worker. Point this to
|
|
// wherever you are hosting worker.js from this directory.
|
|
// * workerDeps: An array of paths pointing (relative to workerScript)
|
|
// to the Acorn and Tern libraries and any Tern plugins you want to
|
|
// load. Or, if you minified those into a single script and included
|
|
// them in the workerScript, simply leave this undefined.
|
|
|
|
(function(mod) {
|
|
if (typeof exports == "object" && typeof module == "object") // CommonJS
|
|
mod(require("../../lib/codemirror"));
|
|
else if (typeof define == "function" && define.amd) // AMD
|
|
define(["../../lib/codemirror"], mod);
|
|
else // Plain browser env
|
|
mod(CodeMirror);
|
|
})(function(CodeMirror) {
|
|
"use strict";
|
|
// declare global: tern
|
|
|
|
CodeMirror.TernServer = function(options) {
|
|
var self = this;
|
|
this.options = options || {};
|
|
var plugins = this.options.plugins || (this.options.plugins = {});
|
|
if (!plugins.doc_comment) plugins.doc_comment = true;
|
|
this.docs = Object.create(null);
|
|
if (this.options.useWorker) {
|
|
this.server = new WorkerServer(this);
|
|
} else {
|
|
this.server = new tern.Server({
|
|
getFile: function(name, c) { return getFile(self, name, c); },
|
|
async: true,
|
|
defs: this.options.defs || [],
|
|
plugins: plugins
|
|
});
|
|
}
|
|
this.trackChange = function(doc, change) { trackChange(self, doc, change); };
|
|
|
|
this.cachedArgHints = null;
|
|
this.activeArgHints = null;
|
|
this.jumpStack = [];
|
|
|
|
this.getHint = function(cm, c) { return hint(self, cm, c); };
|
|
this.getHint.async = true;
|
|
};
|
|
|
|
CodeMirror.TernServer.prototype = {
|
|
addDoc: function(name, doc) {
|
|
var data = {doc: doc, name: name, changed: null};
|
|
this.server.addFile(name, docValue(this, data));
|
|
CodeMirror.on(doc, "change", this.trackChange);
|
|
return this.docs[name] = data;
|
|
},
|
|
|
|
delDoc: function(id) {
|
|
var found = resolveDoc(this, id);
|
|
if (!found) return;
|
|
CodeMirror.off(found.doc, "change", this.trackChange);
|
|
delete this.docs[found.name];
|
|
this.server.delFile(found.name);
|
|
},
|
|
|
|
hideDoc: function(id) {
|
|
closeArgHints(this);
|
|
var found = resolveDoc(this, id);
|
|
if (found && found.changed) sendDoc(this, found);
|
|
},
|
|
|
|
complete: function(cm) {
|
|
cm.showHint({hint: this.getHint});
|
|
},
|
|
|
|
showType: function(cm, pos, c) { showContextInfo(this, cm, pos, "type", c); },
|
|
|
|
showDocs: function(cm, pos, c) { showContextInfo(this, cm, pos, "documentation", c); },
|
|
|
|
updateArgHints: function(cm) { updateArgHints(this, cm); },
|
|
|
|
jumpToDef: function(cm) { jumpToDef(this, cm); },
|
|
|
|
jumpBack: function(cm) { jumpBack(this, cm); },
|
|
|
|
rename: function(cm) { rename(this, cm); },
|
|
|
|
selectName: function(cm) { selectName(this, cm); },
|
|
|
|
request: function (cm, query, c, pos) {
|
|
var self = this;
|
|
var doc = findDoc(this, cm.getDoc());
|
|
var request = buildRequest(this, doc, query, pos);
|
|
var extraOptions = request.query && this.options.queryOptions && this.options.queryOptions[request.query.type]
|
|
if (extraOptions) for (var prop in extraOptions) request.query[prop] = extraOptions[prop];
|
|
|
|
this.server.request(request, function (error, data) {
|
|
if (!error && self.options.responseFilter)
|
|
data = self.options.responseFilter(doc, query, request, error, data);
|
|
c(error, data);
|
|
});
|
|
},
|
|
|
|
destroy: function () {
|
|
closeArgHints(this)
|
|
if (this.worker) {
|
|
this.worker.terminate();
|
|
this.worker = null;
|
|
}
|
|
}
|
|
};
|
|
|
|
var Pos = CodeMirror.Pos;
|
|
var cls = "CodeMirror-Tern-";
|
|
var bigDoc = 250;
|
|
|
|
function getFile(ts, name, c) {
|
|
var buf = ts.docs[name];
|
|
if (buf)
|
|
c(docValue(ts, buf));
|
|
else if (ts.options.getFile)
|
|
ts.options.getFile(name, c);
|
|
else
|
|
c(null);
|
|
}
|
|
|
|
function findDoc(ts, doc, name) {
|
|
for (var n in ts.docs) {
|
|
var cur = ts.docs[n];
|
|
if (cur.doc == doc) return cur;
|
|
}
|
|
if (!name) for (var i = 0;; ++i) {
|
|
n = "[doc" + (i || "") + "]";
|
|
if (!ts.docs[n]) { name = n; break; }
|
|
}
|
|
return ts.addDoc(name, doc);
|
|
}
|
|
|
|
function resolveDoc(ts, id) {
|
|
if (typeof id == "string") return ts.docs[id];
|
|
if (id instanceof CodeMirror) id = id.getDoc();
|
|
if (id instanceof CodeMirror.Doc) return findDoc(ts, id);
|
|
}
|
|
|
|
function trackChange(ts, doc, change) {
|
|
var data = findDoc(ts, doc);
|
|
|
|
var argHints = ts.cachedArgHints;
|
|
if (argHints && argHints.doc == doc && cmpPos(argHints.start, change.to) >= 0)
|
|
ts.cachedArgHints = null;
|
|
|
|
var changed = data.changed;
|
|
if (changed == null)
|
|
data.changed = changed = {from: change.from.line, to: change.from.line};
|
|
var end = change.from.line + (change.text.length - 1);
|
|
if (change.from.line < changed.to) changed.to = changed.to - (change.to.line - end);
|
|
if (end >= changed.to) changed.to = end + 1;
|
|
if (changed.from > change.from.line) changed.from = change.from.line;
|
|
|
|
if (doc.lineCount() > bigDoc && change.to - changed.from > 100) setTimeout(function() {
|
|
if (data.changed && data.changed.to - data.changed.from > 100) sendDoc(ts, data);
|
|
}, 200);
|
|
}
|
|
|
|
function sendDoc(ts, doc) {
|
|
ts.server.request({files: [{type: "full", name: doc.name, text: docValue(ts, doc)}]}, function(error) {
|
|
if (error) window.console.error(error);
|
|
else doc.changed = null;
|
|
});
|
|
}
|
|
|
|
// Completion
|
|
|
|
function hint(ts, cm, c) {
|
|
ts.request(cm, {type: "completions", types: true, docs: true, urls: true}, function(error, data) {
|
|
if (error) return showError(ts, cm, error);
|
|
var completions = [], after = "";
|
|
var from = data.start, to = data.end;
|
|
if (cm.getRange(Pos(from.line, from.ch - 2), from) == "[\"" &&
|
|
cm.getRange(to, Pos(to.line, to.ch + 2)) != "\"]")
|
|
after = "\"]";
|
|
|
|
for (var i = 0; i < data.completions.length; ++i) {
|
|
var completion = data.completions[i], className = typeToIcon(completion.type);
|
|
if (data.guess) className += " " + cls + "guess";
|
|
completions.push({text: completion.name + after,
|
|
displayText: completion.displayName || completion.name,
|
|
className: className,
|
|
data: completion});
|
|
}
|
|
|
|
var obj = {from: from, to: to, list: completions};
|
|
var tooltip = null;
|
|
CodeMirror.on(obj, "close", function() { remove(tooltip); });
|
|
CodeMirror.on(obj, "update", function() { remove(tooltip); });
|
|
CodeMirror.on(obj, "select", function(cur, node) {
|
|
remove(tooltip);
|
|
var content = ts.options.completionTip ? ts.options.completionTip(cur.data) : cur.data.doc;
|
|
if (content) {
|
|
tooltip = makeTooltip(node.parentNode.getBoundingClientRect().right + window.pageXOffset,
|
|
node.getBoundingClientRect().top + window.pageYOffset, content);
|
|
tooltip.className += " " + cls + "hint-doc";
|
|
}
|
|
});
|
|
c(obj);
|
|
});
|
|
}
|
|
|
|
function typeToIcon(type) {
|
|
var suffix;
|
|
if (type == "?") suffix = "unknown";
|
|
else if (type == "number" || type == "string" || type == "bool") suffix = type;
|
|
else if (/^fn\(/.test(type)) suffix = "fn";
|
|
else if (/^\[/.test(type)) suffix = "array";
|
|
else suffix = "object";
|
|
return cls + "completion " + cls + "completion-" + suffix;
|
|
}
|
|
|
|
// Type queries
|
|
|
|
function showContextInfo(ts, cm, pos, queryName, c) {
|
|
ts.request(cm, queryName, function(error, data) {
|
|
if (error) return showError(ts, cm, error);
|
|
if (ts.options.typeTip) {
|
|
var tip = ts.options.typeTip(data);
|
|
} else {
|
|
var tip = elt("span", null, elt("strong", null, data.type || "not found"));
|
|
if (data.doc)
|
|
tip.appendChild(document.createTextNode(" — " + data.doc));
|
|
if (data.url) {
|
|
tip.appendChild(document.createTextNode(" "));
|
|
var child = tip.appendChild(elt("a", null, "[docs]"));
|
|
child.href = data.url;
|
|
child.target = "_blank";
|
|
}
|
|
}
|
|
tempTooltip(cm, tip, ts);
|
|
if (c) c();
|
|
}, pos);
|
|
}
|
|
|
|
// Maintaining argument hints
|
|
|
|
function updateArgHints(ts, cm) {
|
|
closeArgHints(ts);
|
|
|
|
if (cm.somethingSelected()) return;
|
|
var state = cm.getTokenAt(cm.getCursor()).state;
|
|
var inner = CodeMirror.innerMode(cm.getMode(), state);
|
|
if (inner.mode.name != "javascript") return;
|
|
var lex = inner.state.lexical;
|
|
if (lex.info != "call") return;
|
|
|
|
var ch, argPos = lex.pos || 0, tabSize = cm.getOption("tabSize");
|
|
for (var line = cm.getCursor().line, e = Math.max(0, line - 9), found = false; line >= e; --line) {
|
|
var str = cm.getLine(line), extra = 0;
|
|
for (var pos = 0;;) {
|
|
var tab = str.indexOf("\t", pos);
|
|
if (tab == -1) break;
|
|
extra += tabSize - (tab + extra) % tabSize - 1;
|
|
pos = tab + 1;
|
|
}
|
|
ch = lex.column - extra;
|
|
if (str.charAt(ch) == "(") {found = true; break;}
|
|
}
|
|
if (!found) return;
|
|
|
|
var start = Pos(line, ch);
|
|
var cache = ts.cachedArgHints;
|
|
if (cache && cache.doc == cm.getDoc() && cmpPos(start, cache.start) == 0)
|
|
return showArgHints(ts, cm, argPos);
|
|
|
|
ts.request(cm, {type: "type", preferFunction: true, end: start}, function(error, data) {
|
|
if (error || !data.type || !(/^fn\(/).test(data.type)) return;
|
|
ts.cachedArgHints = {
|
|
start: start,
|
|
type: parseFnType(data.type),
|
|
name: data.exprName || data.name || "fn",
|
|
guess: data.guess,
|
|
doc: cm.getDoc()
|
|
};
|
|
showArgHints(ts, cm, argPos);
|
|
});
|
|
}
|
|
|
|
function showArgHints(ts, cm, pos) {
|
|
closeArgHints(ts);
|
|
|
|
var cache = ts.cachedArgHints, tp = cache.type;
|
|
var tip = elt("span", cache.guess ? cls + "fhint-guess" : null,
|
|
elt("span", cls + "fname", cache.name), "(");
|
|
for (var i = 0; i < tp.args.length; ++i) {
|
|
if (i) tip.appendChild(document.createTextNode(", "));
|
|
var arg = tp.args[i];
|
|
tip.appendChild(elt("span", cls + "farg" + (i == pos ? " " + cls + "farg-current" : ""), arg.name || "?"));
|
|
if (arg.type != "?") {
|
|
tip.appendChild(document.createTextNode(":\u00a0"));
|
|
tip.appendChild(elt("span", cls + "type", arg.type));
|
|
}
|
|
}
|
|
tip.appendChild(document.createTextNode(tp.rettype ? ") ->\u00a0" : ")"));
|
|
if (tp.rettype) tip.appendChild(elt("span", cls + "type", tp.rettype));
|
|
var place = cm.cursorCoords(null, "page");
|
|
var tooltip = ts.activeArgHints = makeTooltip(place.right + 1, place.bottom, tip)
|
|
setTimeout(function() {
|
|
tooltip.clear = onEditorActivity(cm, function() {
|
|
if (ts.activeArgHints == tooltip) closeArgHints(ts) })
|
|
}, 20)
|
|
}
|
|
|
|
function parseFnType(text) {
|
|
var args = [], pos = 3;
|
|
|
|
function skipMatching(upto) {
|
|
var depth = 0, start = pos;
|
|
for (;;) {
|
|
var next = text.charAt(pos);
|
|
if (upto.test(next) && !depth) return text.slice(start, pos);
|
|
if (/[{\[\(]/.test(next)) ++depth;
|
|
else if (/[}\]\)]/.test(next)) --depth;
|
|
++pos;
|
|
}
|
|
}
|
|
|
|
// Parse arguments
|
|
if (text.charAt(pos) != ")") for (;;) {
|
|
var name = text.slice(pos).match(/^([^, \(\[\{]+): /);
|
|
if (name) {
|
|
pos += name[0].length;
|
|
name = name[1];
|
|
}
|
|
args.push({name: name, type: skipMatching(/[\),]/)});
|
|
if (text.charAt(pos) == ")") break;
|
|
pos += 2;
|
|
}
|
|
|
|
var rettype = text.slice(pos).match(/^\) -> (.*)$/);
|
|
|
|
return {args: args, rettype: rettype && rettype[1]};
|
|
}
|
|
|
|
// Moving to the definition of something
|
|
|
|
function jumpToDef(ts, cm) {
|
|
function inner(varName) {
|
|
var req = {type: "definition", variable: varName || null};
|
|
var doc = findDoc(ts, cm.getDoc());
|
|
ts.server.request(buildRequest(ts, doc, req), function(error, data) {
|
|
if (error) return showError(ts, cm, error);
|
|
if (!data.file && data.url) { window.open(data.url); return; }
|
|
|
|
if (data.file) {
|
|
var localDoc = ts.docs[data.file], found;
|
|
if (localDoc && (found = findContext(localDoc.doc, data))) {
|
|
ts.jumpStack.push({file: doc.name,
|
|
start: cm.getCursor("from"),
|
|
end: cm.getCursor("to")});
|
|
moveTo(ts, doc, localDoc, found.start, found.end);
|
|
return;
|
|
}
|
|
}
|
|
showError(ts, cm, "Could not find a definition.");
|
|
});
|
|
}
|
|
|
|
if (!atInterestingExpression(cm))
|
|
dialog(cm, "Jump to variable", function(name) { if (name) inner(name); });
|
|
else
|
|
inner();
|
|
}
|
|
|
|
function jumpBack(ts, cm) {
|
|
var pos = ts.jumpStack.pop(), doc = pos && ts.docs[pos.file];
|
|
if (!doc) return;
|
|
moveTo(ts, findDoc(ts, cm.getDoc()), doc, pos.start, pos.end);
|
|
}
|
|
|
|
function moveTo(ts, curDoc, doc, start, end) {
|
|
doc.doc.setSelection(start, end);
|
|
if (curDoc != doc && ts.options.switchToDoc) {
|
|
closeArgHints(ts);
|
|
ts.options.switchToDoc(doc.name, doc.doc);
|
|
}
|
|
}
|
|
|
|
// The {line,ch} representation of positions makes this rather awkward.
|
|
function findContext(doc, data) {
|
|
var before = data.context.slice(0, data.contextOffset).split("\n");
|
|
var startLine = data.start.line - (before.length - 1);
|
|
var start = Pos(startLine, (before.length == 1 ? data.start.ch : doc.getLine(startLine).length) - before[0].length);
|
|
|
|
var text = doc.getLine(startLine).slice(start.ch);
|
|
for (var cur = startLine + 1; cur < doc.lineCount() && text.length < data.context.length; ++cur)
|
|
text += "\n" + doc.getLine(cur);
|
|
if (text.slice(0, data.context.length) == data.context) return data;
|
|
|
|
var cursor = doc.getSearchCursor(data.context, 0, false);
|
|
var nearest, nearestDist = Infinity;
|
|
while (cursor.findNext()) {
|
|
var from = cursor.from(), dist = Math.abs(from.line - start.line) * 10000;
|
|
if (!dist) dist = Math.abs(from.ch - start.ch);
|
|
if (dist < nearestDist) { nearest = from; nearestDist = dist; }
|
|
}
|
|
if (!nearest) return null;
|
|
|
|
if (before.length == 1)
|
|
nearest.ch += before[0].length;
|
|
else
|
|
nearest = Pos(nearest.line + (before.length - 1), before[before.length - 1].length);
|
|
if (data.start.line == data.end.line)
|
|
var end = Pos(nearest.line, nearest.ch + (data.end.ch - data.start.ch));
|
|
else
|
|
var end = Pos(nearest.line + (data.end.line - data.start.line), data.end.ch);
|
|
return {start: nearest, end: end};
|
|
}
|
|
|
|
function atInterestingExpression(cm) {
|
|
var pos = cm.getCursor("end"), tok = cm.getTokenAt(pos);
|
|
if (tok.start < pos.ch && tok.type == "comment") return false;
|
|
return /[\w)\]]/.test(cm.getLine(pos.line).slice(Math.max(pos.ch - 1, 0), pos.ch + 1));
|
|
}
|
|
|
|
// Variable renaming
|
|
|
|
function rename(ts, cm) {
|
|
var token = cm.getTokenAt(cm.getCursor());
|
|
if (!/\w/.test(token.string)) return showError(ts, cm, "Not at a variable");
|
|
dialog(cm, "New name for " + token.string, function(newName) {
|
|
ts.request(cm, {type: "rename", newName: newName, fullDocs: true}, function(error, data) {
|
|
if (error) return showError(ts, cm, error);
|
|
applyChanges(ts, data.changes);
|
|
});
|
|
});
|
|
}
|
|
|
|
function selectName(ts, cm) {
|
|
var name = findDoc(ts, cm.doc).name;
|
|
ts.request(cm, {type: "refs"}, function(error, data) {
|
|
if (error) return showError(ts, cm, error);
|
|
var ranges = [], cur = 0;
|
|
var curPos = cm.getCursor();
|
|
for (var i = 0; i < data.refs.length; i++) {
|
|
var ref = data.refs[i];
|
|
if (ref.file == name) {
|
|
ranges.push({anchor: ref.start, head: ref.end});
|
|
if (cmpPos(curPos, ref.start) >= 0 && cmpPos(curPos, ref.end) <= 0)
|
|
cur = ranges.length - 1;
|
|
}
|
|
}
|
|
cm.setSelections(ranges, cur);
|
|
});
|
|
}
|
|
|
|
var nextChangeOrig = 0;
|
|
function applyChanges(ts, changes) {
|
|
var perFile = Object.create(null);
|
|
for (var i = 0; i < changes.length; ++i) {
|
|
var ch = changes[i];
|
|
(perFile[ch.file] || (perFile[ch.file] = [])).push(ch);
|
|
}
|
|
for (var file in perFile) {
|
|
var known = ts.docs[file], chs = perFile[file];;
|
|
if (!known) continue;
|
|
chs.sort(function(a, b) { return cmpPos(b.start, a.start); });
|
|
var origin = "*rename" + (++nextChangeOrig);
|
|
for (var i = 0; i < chs.length; ++i) {
|
|
var ch = chs[i];
|
|
known.doc.replaceRange(ch.text, ch.start, ch.end, origin);
|
|
}
|
|
}
|
|
}
|
|
|
|
// Generic request-building helper
|
|
|
|
function buildRequest(ts, doc, query, pos) {
|
|
var files = [], offsetLines = 0, allowFragments = !query.fullDocs;
|
|
if (!allowFragments) delete query.fullDocs;
|
|
if (typeof query == "string") query = {type: query};
|
|
query.lineCharPositions = true;
|
|
if (query.end == null) {
|
|
query.end = pos || doc.doc.getCursor("end");
|
|
if (doc.doc.somethingSelected())
|
|
query.start = doc.doc.getCursor("start");
|
|
}
|
|
var startPos = query.start || query.end;
|
|
|
|
if (doc.changed) {
|
|
if (doc.doc.lineCount() > bigDoc && allowFragments !== false &&
|
|
doc.changed.to - doc.changed.from < 100 &&
|
|
doc.changed.from <= startPos.line && doc.changed.to > query.end.line) {
|
|
files.push(getFragmentAround(doc, startPos, query.end));
|
|
query.file = "#0";
|
|
var offsetLines = files[0].offsetLines;
|
|
if (query.start != null) query.start = Pos(query.start.line - -offsetLines, query.start.ch);
|
|
query.end = Pos(query.end.line - offsetLines, query.end.ch);
|
|
} else {
|
|
files.push({type: "full",
|
|
name: doc.name,
|
|
text: docValue(ts, doc)});
|
|
query.file = doc.name;
|
|
doc.changed = null;
|
|
}
|
|
} else {
|
|
query.file = doc.name;
|
|
}
|
|
for (var name in ts.docs) {
|
|
var cur = ts.docs[name];
|
|
if (cur.changed && cur != doc) {
|
|
files.push({type: "full", name: cur.name, text: docValue(ts, cur)});
|
|
cur.changed = null;
|
|
}
|
|
}
|
|
|
|
return {query: query, files: files};
|
|
}
|
|
|
|
function getFragmentAround(data, start, end) {
|
|
var doc = data.doc;
|
|
var minIndent = null, minLine = null, endLine, tabSize = 4;
|
|
for (var p = start.line - 1, min = Math.max(0, p - 50); p >= min; --p) {
|
|
var line = doc.getLine(p), fn = line.search(/\bfunction\b/);
|
|
if (fn < 0) continue;
|
|
var indent = CodeMirror.countColumn(line, null, tabSize);
|
|
if (minIndent != null && minIndent <= indent) continue;
|
|
minIndent = indent;
|
|
minLine = p;
|
|
}
|
|
if (minLine == null) minLine = min;
|
|
var max = Math.min(doc.lastLine(), end.line + 20);
|
|
if (minIndent == null || minIndent == CodeMirror.countColumn(doc.getLine(start.line), null, tabSize))
|
|
endLine = max;
|
|
else for (endLine = end.line + 1; endLine < max; ++endLine) {
|
|
var indent = CodeMirror.countColumn(doc.getLine(endLine), null, tabSize);
|
|
if (indent <= minIndent) break;
|
|
}
|
|
var from = Pos(minLine, 0);
|
|
|
|
return {type: "part",
|
|
name: data.name,
|
|
offsetLines: from.line,
|
|
text: doc.getRange(from, Pos(endLine, end.line == endLine ? null : 0))};
|
|
}
|
|
|
|
// Generic utilities
|
|
|
|
var cmpPos = CodeMirror.cmpPos;
|
|
|
|
function elt(tagname, cls /*, ... elts*/) {
|
|
var e = document.createElement(tagname);
|
|
if (cls) e.className = cls;
|
|
for (var i = 2; i < arguments.length; ++i) {
|
|
var elt = arguments[i];
|
|
if (typeof elt == "string") elt = document.createTextNode(elt);
|
|
e.appendChild(elt);
|
|
}
|
|
return e;
|
|
}
|
|
|
|
function dialog(cm, text, f) {
|
|
if (cm.openDialog)
|
|
cm.openDialog(text + ": <input type=text>", f);
|
|
else
|
|
f(prompt(text, ""));
|
|
}
|
|
|
|
// Tooltips
|
|
|
|
function tempTooltip(cm, content, ts) {
|
|
if (cm.state.ternTooltip) remove(cm.state.ternTooltip);
|
|
var where = cm.cursorCoords();
|
|
var tip = cm.state.ternTooltip = makeTooltip(where.right + 1, where.bottom, content);
|
|
function maybeClear() {
|
|
old = true;
|
|
if (!mouseOnTip) clear();
|
|
}
|
|
function clear() {
|
|
cm.state.ternTooltip = null;
|
|
if (tip.parentNode) fadeOut(tip)
|
|
clearActivity()
|
|
}
|
|
var mouseOnTip = false, old = false;
|
|
CodeMirror.on(tip, "mousemove", function() { mouseOnTip = true; });
|
|
CodeMirror.on(tip, "mouseout", function(e) {
|
|
var related = e.relatedTarget || e.toElement
|
|
if (!related || !CodeMirror.contains(tip, related)) {
|
|
if (old) clear();
|
|
else mouseOnTip = false;
|
|
}
|
|
});
|
|
setTimeout(maybeClear, ts.options.hintDelay ? ts.options.hintDelay : 1700);
|
|
var clearActivity = onEditorActivity(cm, clear)
|
|
}
|
|
|
|
function onEditorActivity(cm, f) {
|
|
cm.on("cursorActivity", f)
|
|
cm.on("blur", f)
|
|
cm.on("scroll", f)
|
|
cm.on("setDoc", f)
|
|
return function() {
|
|
cm.off("cursorActivity", f)
|
|
cm.off("blur", f)
|
|
cm.off("scroll", f)
|
|
cm.off("setDoc", f)
|
|
}
|
|
}
|
|
|
|
function makeTooltip(x, y, content) {
|
|
var node = elt("div", cls + "tooltip", content);
|
|
node.style.left = x + "px";
|
|
node.style.top = y + "px";
|
|
document.body.appendChild(node);
|
|
return node;
|
|
}
|
|
|
|
function remove(node) {
|
|
var p = node && node.parentNode;
|
|
if (p) p.removeChild(node);
|
|
}
|
|
|
|
function fadeOut(tooltip) {
|
|
tooltip.style.opacity = "0";
|
|
setTimeout(function() { remove(tooltip); }, 1100);
|
|
}
|
|
|
|
function showError(ts, cm, msg) {
|
|
if (ts.options.showError)
|
|
ts.options.showError(cm, msg);
|
|
else
|
|
tempTooltip(cm, String(msg), ts);
|
|
}
|
|
|
|
function closeArgHints(ts) {
|
|
if (ts.activeArgHints) {
|
|
if (ts.activeArgHints.clear) ts.activeArgHints.clear()
|
|
remove(ts.activeArgHints)
|
|
ts.activeArgHints = null
|
|
}
|
|
}
|
|
|
|
function docValue(ts, doc) {
|
|
var val = doc.doc.getValue();
|
|
if (ts.options.fileFilter) val = ts.options.fileFilter(val, doc.name, doc.doc);
|
|
return val;
|
|
}
|
|
|
|
// Worker wrapper
|
|
|
|
function WorkerServer(ts) {
|
|
var worker = ts.worker = new Worker(ts.options.workerScript);
|
|
worker.postMessage({type: "init",
|
|
defs: ts.options.defs,
|
|
plugins: ts.options.plugins,
|
|
scripts: ts.options.workerDeps});
|
|
var msgId = 0, pending = {};
|
|
|
|
function send(data, c) {
|
|
if (c) {
|
|
data.id = ++msgId;
|
|
pending[msgId] = c;
|
|
}
|
|
worker.postMessage(data);
|
|
}
|
|
worker.onmessage = function(e) {
|
|
var data = e.data;
|
|
if (data.type == "getFile") {
|
|
getFile(ts, data.name, function(err, text) {
|
|
send({type: "getFile", err: String(err), text: text, id: data.id});
|
|
});
|
|
} else if (data.type == "debug") {
|
|
window.console.log(data.message);
|
|
} else if (data.id && pending[data.id]) {
|
|
pending[data.id](data.err, data.body);
|
|
delete pending[data.id];
|
|
}
|
|
};
|
|
worker.onerror = function(e) {
|
|
for (var id in pending) pending[id](e);
|
|
pending = {};
|
|
};
|
|
|
|
this.addFile = function(name, text) { send({type: "add", name: name, text: text}); };
|
|
this.delFile = function(name) { send({type: "del", name: name}); };
|
|
this.request = function(body, c) { send({type: "req", body: body}, c); };
|
|
}
|
|
});
|
|
|
|
// ========= lint.js ========= //
|
|
|
|
// CodeMirror, copyright (c) by Marijn Haverbeke and others
|
|
// Distributed under an MIT license: https://codemirror.net/LICENSE
|
|
|
|
(function(mod) {
|
|
if (typeof exports == "object" && typeof module == "object") // CommonJS
|
|
mod(require("../../lib/codemirror"));
|
|
else if (typeof define == "function" && define.amd) // AMD
|
|
define(["../../lib/codemirror"], mod);
|
|
else // Plain browser env
|
|
mod(CodeMirror);
|
|
})(function(CodeMirror) {
|
|
"use strict";
|
|
var GUTTER_ID = "CodeMirror-lint-markers";
|
|
|
|
function showTooltip(cm, e, content) {
|
|
var tt = document.createElement("div");
|
|
tt.className = "CodeMirror-lint-tooltip cm-s-" + cm.options.theme;
|
|
tt.appendChild(content.cloneNode(true));
|
|
if (cm.state.lint.options.selfContain)
|
|
cm.getWrapperElement().appendChild(tt);
|
|
else
|
|
document.body.appendChild(tt);
|
|
|
|
function position(e) {
|
|
if (!tt.parentNode) return CodeMirror.off(document, "mousemove", position);
|
|
tt.style.top = Math.max(0, e.clientY - tt.offsetHeight - 5) + "px";
|
|
tt.style.left = (e.clientX + 5) + "px";
|
|
}
|
|
CodeMirror.on(document, "mousemove", position);
|
|
position(e);
|
|
if (tt.style.opacity != null) tt.style.opacity = 1;
|
|
return tt;
|
|
}
|
|
function rm(elt) {
|
|
if (elt.parentNode) elt.parentNode.removeChild(elt);
|
|
}
|
|
function hideTooltip(tt) {
|
|
if (!tt.parentNode) return;
|
|
if (tt.style.opacity == null) rm(tt);
|
|
tt.style.opacity = 0;
|
|
setTimeout(function() { rm(tt); }, 600);
|
|
}
|
|
|
|
function showTooltipFor(cm, e, content, node) {
|
|
var tooltip = showTooltip(cm, e, content);
|
|
function hide() {
|
|
CodeMirror.off(node, "mouseout", hide);
|
|
if (tooltip) { hideTooltip(tooltip); tooltip = null; }
|
|
}
|
|
var poll = setInterval(function() {
|
|
if (tooltip) for (var n = node;; n = n.parentNode) {
|
|
if (n && n.nodeType == 11) n = n.host;
|
|
if (n == document.body) return;
|
|
if (!n) { hide(); break; }
|
|
}
|
|
if (!tooltip) return clearInterval(poll);
|
|
}, 400);
|
|
CodeMirror.on(node, "mouseout", hide);
|
|
}
|
|
|
|
function LintState(cm, options, hasGutter) {
|
|
this.marked = [];
|
|
this.options = options;
|
|
this.timeout = null;
|
|
this.hasGutter = hasGutter;
|
|
this.onMouseOver = function(e) { onMouseOver(cm, e); };
|
|
this.waitingFor = 0
|
|
}
|
|
|
|
function parseOptions(_cm, options) {
|
|
if (options instanceof Function) return {getAnnotations: options};
|
|
if (!options || options === true) options = {};
|
|
return options;
|
|
}
|
|
|
|
function clearMarks(cm) {
|
|
var state = cm.state.lint;
|
|
if (state.hasGutter) cm.clearGutter(GUTTER_ID);
|
|
for (var i = 0; i < state.marked.length; ++i)
|
|
state.marked[i].clear();
|
|
state.marked.length = 0;
|
|
}
|
|
|
|
function makeMarker(cm, labels, severity, multiple, tooltips) {
|
|
var marker = document.createElement("div"), inner = marker;
|
|
marker.className = "CodeMirror-lint-marker-" + severity;
|
|
if (multiple) {
|
|
inner = marker.appendChild(document.createElement("div"));
|
|
inner.className = "CodeMirror-lint-marker-multiple";
|
|
}
|
|
|
|
if (tooltips != false) CodeMirror.on(inner, "mouseover", function(e) {
|
|
showTooltipFor(cm, e, labels, inner);
|
|
});
|
|
|
|
return marker;
|
|
}
|
|
|
|
function getMaxSeverity(a, b) {
|
|
if (a == "error") return a;
|
|
else return b;
|
|
}
|
|
|
|
function groupByLine(annotations) {
|
|
var lines = [];
|
|
for (var i = 0; i < annotations.length; ++i) {
|
|
var ann = annotations[i], line = ann.from.line;
|
|
(lines[line] || (lines[line] = [])).push(ann);
|
|
}
|
|
return lines;
|
|
}
|
|
|
|
function annotationTooltip(ann) {
|
|
var severity = ann.severity;
|
|
if (!severity) severity = "error";
|
|
var tip = document.createElement("div");
|
|
tip.className = "CodeMirror-lint-message-" + severity;
|
|
if (typeof ann.messageHTML != 'undefined') {
|
|
tip.innerHTML = ann.messageHTML;
|
|
} else {
|
|
tip.appendChild(document.createTextNode(ann.message));
|
|
}
|
|
return tip;
|
|
}
|
|
|
|
function lintAsync(cm, getAnnotations, passOptions) {
|
|
var state = cm.state.lint
|
|
var id = ++state.waitingFor
|
|
function abort() {
|
|
id = -1
|
|
cm.off("change", abort)
|
|
}
|
|
cm.on("change", abort)
|
|
getAnnotations(cm.getValue(), function(annotations, arg2) {
|
|
cm.off("change", abort)
|
|
if (state.waitingFor != id) return
|
|
if (arg2 && annotations instanceof CodeMirror) annotations = arg2
|
|
cm.operation(function() {updateLinting(cm, annotations)})
|
|
}, passOptions, cm);
|
|
}
|
|
|
|
function startLinting(cm) {
|
|
var state = cm.state.lint, options = state.options;
|
|
/*
|
|
* Passing rules in `options` property prevents JSHint (and other linters) from complaining
|
|
* about unrecognized rules like `onUpdateLinting`, `delay`, `lintOnChange`, etc.
|
|
*/
|
|
var passOptions = options.options || options;
|
|
var getAnnotations = options.getAnnotations || cm.getHelper(CodeMirror.Pos(0, 0), "lint");
|
|
if (!getAnnotations) return;
|
|
if (options.async || getAnnotations.async) {
|
|
lintAsync(cm, getAnnotations, passOptions)
|
|
} else {
|
|
var annotations = getAnnotations(cm.getValue(), passOptions, cm);
|
|
if (!annotations) return;
|
|
if (annotations.then) annotations.then(function(issues) {
|
|
cm.operation(function() {updateLinting(cm, issues)})
|
|
});
|
|
else cm.operation(function() {updateLinting(cm, annotations)})
|
|
}
|
|
}
|
|
|
|
function updateLinting(cm, annotationsNotSorted) {
|
|
clearMarks(cm);
|
|
var state = cm.state.lint, options = state.options;
|
|
|
|
var annotations = groupByLine(annotationsNotSorted);
|
|
|
|
for (var line = 0; line < annotations.length; ++line) {
|
|
var anns = annotations[line];
|
|
if (!anns) continue;
|
|
|
|
var maxSeverity = null;
|
|
var tipLabel = state.hasGutter && document.createDocumentFragment();
|
|
|
|
for (var i = 0; i < anns.length; ++i) {
|
|
var ann = anns[i];
|
|
var severity = ann.severity;
|
|
if (!severity) severity = "error";
|
|
maxSeverity = getMaxSeverity(maxSeverity, severity);
|
|
|
|
if (options.formatAnnotation) ann = options.formatAnnotation(ann);
|
|
if (state.hasGutter) tipLabel.appendChild(annotationTooltip(ann));
|
|
|
|
if (ann.to) state.marked.push(cm.markText(ann.from, ann.to, {
|
|
className: "CodeMirror-lint-mark-" + severity,
|
|
__annotation: ann
|
|
}));
|
|
}
|
|
|
|
if (state.hasGutter)
|
|
cm.setGutterMarker(line, GUTTER_ID, makeMarker(cm, tipLabel, maxSeverity, anns.length > 1,
|
|
state.options.tooltips));
|
|
}
|
|
if (options.onUpdateLinting) options.onUpdateLinting(annotationsNotSorted, annotations, cm);
|
|
}
|
|
|
|
function onChange(cm) {
|
|
var state = cm.state.lint;
|
|
if (!state) return;
|
|
clearTimeout(state.timeout);
|
|
state.timeout = setTimeout(function(){startLinting(cm);}, state.options.delay || 500);
|
|
}
|
|
|
|
function popupTooltips(cm, annotations, e) {
|
|
var target = e.target || e.srcElement;
|
|
var tooltip = document.createDocumentFragment();
|
|
for (var i = 0; i < annotations.length; i++) {
|
|
var ann = annotations[i];
|
|
tooltip.appendChild(annotationTooltip(ann));
|
|
}
|
|
showTooltipFor(cm, e, tooltip, target);
|
|
}
|
|
|
|
function onMouseOver(cm, e) {
|
|
var target = e.target || e.srcElement;
|
|
if (!/\bCodeMirror-lint-mark-/.test(target.className)) return;
|
|
var box = target.getBoundingClientRect(), x = (box.left + box.right) / 2, y = (box.top + box.bottom) / 2;
|
|
var spans = cm.findMarksAt(cm.coordsChar({left: x, top: y}, "client"));
|
|
|
|
var annotations = [];
|
|
for (var i = 0; i < spans.length; ++i) {
|
|
var ann = spans[i].__annotation;
|
|
if (ann) annotations.push(ann);
|
|
}
|
|
if (annotations.length) popupTooltips(cm, annotations, e);
|
|
}
|
|
|
|
CodeMirror.defineOption("lint", false, function(cm, val, old) {
|
|
if (old && old != CodeMirror.Init) {
|
|
clearMarks(cm);
|
|
if (cm.state.lint.options.lintOnChange !== false)
|
|
cm.off("change", onChange);
|
|
CodeMirror.off(cm.getWrapperElement(), "mouseover", cm.state.lint.onMouseOver);
|
|
clearTimeout(cm.state.lint.timeout);
|
|
delete cm.state.lint;
|
|
}
|
|
|
|
if (val) {
|
|
var gutters = cm.getOption("gutters"), hasLintGutter = false;
|
|
for (var i = 0; i < gutters.length; ++i) if (gutters[i] == GUTTER_ID) hasLintGutter = true;
|
|
var state = cm.state.lint = new LintState(cm, parseOptions(cm, val), hasLintGutter);
|
|
if (state.options.lintOnChange !== false)
|
|
cm.on("change", onChange);
|
|
if (state.options.tooltips != false && state.options.tooltips != "gutter")
|
|
CodeMirror.on(cm.getWrapperElement(), "mouseover", state.onMouseOver);
|
|
|
|
startLinting(cm);
|
|
}
|
|
});
|
|
|
|
CodeMirror.defineExtension("performLint", function() {
|
|
if (this.state.lint) startLinting(this);
|
|
});
|
|
});
|
|
|
|
// ========= lint ========= //
|
|
|
|
// CodeMirror, copyright (c) by Marijn Haverbeke and others
|
|
// Distributed under an MIT license: https://codemirror.net/LICENSE
|
|
|
|
(function(mod) {
|
|
if (typeof exports == "object" && typeof module == "object") // CommonJS
|
|
mod(require("../../lib/codemirror"));
|
|
else if (typeof define == "function" && define.amd) // AMD
|
|
define(["../../lib/codemirror"], mod);
|
|
else // Plain browser env
|
|
mod(CodeMirror);
|
|
})(function(CodeMirror) {
|
|
"use strict";
|
|
// declare global: JSHINT
|
|
|
|
function validator(text, options) {
|
|
if (!window.JSHINT) {
|
|
if (window.console) {
|
|
window.console.error("Error: window.JSHINT not defined, CodeMirror JavaScript linting cannot run.");
|
|
}
|
|
return [];
|
|
}
|
|
if (!options.indent) // JSHint error.character actually is a column index, this fixes underlining on lines using tabs for indentation
|
|
options.indent = 1; // JSHint default value is 4
|
|
JSHINT(text, options, options.globals);
|
|
var errors = JSHINT.data().errors, result = [];
|
|
if (errors) parseErrors(errors, result);
|
|
return result;
|
|
}
|
|
|
|
CodeMirror.registerHelper("lint", "javascript", validator);
|
|
|
|
function parseErrors(errors, output) {
|
|
for ( var i = 0; i < errors.length; i++) {
|
|
var error = errors[i];
|
|
if (error) {
|
|
if (error.line <= 0) {
|
|
if (window.console) {
|
|
window.console.warn("Cannot display JSHint error (invalid line " + error.line + ")", error);
|
|
}
|
|
continue;
|
|
}
|
|
|
|
var start = error.character - 1, end = start + 1;
|
|
if (error.evidence) {
|
|
var index = error.evidence.substring(start).search(/.\b/);
|
|
if (index > -1) {
|
|
end += index;
|
|
}
|
|
}
|
|
|
|
// Convert to format expected by validation service
|
|
var hint = {
|
|
message: error.reason,
|
|
severity: error.code ? (error.code.startsWith('W') ? "warning" : "error") : "error",
|
|
from: CodeMirror.Pos(error.line - 1, start),
|
|
to: CodeMirror.Pos(error.line - 1, end)
|
|
};
|
|
|
|
output.push(hint);
|
|
}
|
|
}
|
|
}
|
|
});
|
|
|