2014-01-28 07:08:41 -08:00
|
|
|
/* vim:set ts=2 sw=2 sts=2 et tw=80:
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
const cssAutoCompleter = require("devtools/sourceeditor/css-autocompleter");
|
|
|
|
const { AutocompletePopup } = require("devtools/shared/autocomplete-popup");
|
|
|
|
|
2014-04-16 13:49:28 -07:00
|
|
|
const CM_TERN_SCRIPTS = [
|
|
|
|
"chrome://browser/content/devtools/codemirror/tern.js",
|
|
|
|
"chrome://browser/content/devtools/codemirror/show-hint.js"
|
|
|
|
];
|
|
|
|
|
2014-01-28 07:08:41 -08:00
|
|
|
const privates = new WeakMap();
|
|
|
|
|
|
|
|
/**
|
2014-04-16 13:49:28 -07:00
|
|
|
* Prepares an editor instance for autocompletion.
|
2014-01-28 07:08:41 -08:00
|
|
|
*/
|
2014-04-16 13:49:28 -07:00
|
|
|
function setupAutoCompletion(ctx, options) {
|
2014-01-28 07:08:41 -08:00
|
|
|
let { cm, ed, Editor } = ctx;
|
|
|
|
|
|
|
|
let win = ed.container.contentWindow.wrappedJSObject;
|
2014-06-18 04:14:00 -07:00
|
|
|
let { CodeMirror, document } = win;
|
2014-01-28 07:08:41 -08:00
|
|
|
|
|
|
|
let completer = null;
|
2014-04-16 13:49:28 -07:00
|
|
|
let autocompleteKey = "Ctrl-" +
|
|
|
|
Editor.keyFor("autocompletion", { noaccel: true });
|
|
|
|
if (ed.config.mode == Editor.modes.js) {
|
|
|
|
let defs = [
|
|
|
|
"tern/browser",
|
|
|
|
"tern/ecma5",
|
|
|
|
].map(require);
|
|
|
|
|
|
|
|
CM_TERN_SCRIPTS.forEach(ed.loadScript, ed);
|
|
|
|
win.tern = require("tern/tern");
|
2014-06-18 04:14:00 -07:00
|
|
|
cm.tern = new CodeMirror.TernServer({
|
|
|
|
defs: defs,
|
|
|
|
typeTip: function(data) {
|
|
|
|
let tip = document.createElement("span");
|
|
|
|
tip.className = "CodeMirror-Tern-information";
|
|
|
|
let tipType = document.createElement("strong");
|
|
|
|
tipType.appendChild(document.createTextNode(data.type || cm.l10n("autocompletion.notFound")));
|
|
|
|
tip.appendChild(tipType);
|
|
|
|
|
|
|
|
if (data.doc) {
|
|
|
|
tip.appendChild(document.createTextNode(" — " + data.doc));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (data.url) {
|
|
|
|
tip.appendChild(document.createTextNode(" "));
|
|
|
|
let docLink = document.createElement("a");
|
|
|
|
docLink.textContent = "[" + cm.l10n("autocompletion.docsLink") + "]";
|
|
|
|
docLink.href = data.url;
|
|
|
|
docLink.className = "theme-link";
|
|
|
|
docLink.setAttribute("target", "_blank");
|
|
|
|
tip.appendChild(docLink);
|
|
|
|
}
|
|
|
|
|
|
|
|
return tip;
|
|
|
|
}
|
|
|
|
});
|
2014-04-16 13:49:28 -07:00
|
|
|
cm.on("cursorActivity", (cm) => {
|
|
|
|
cm.tern.updateArgHints(cm);
|
|
|
|
});
|
|
|
|
|
|
|
|
let keyMap = {};
|
|
|
|
|
|
|
|
keyMap[autocompleteKey] = (cm) => {
|
|
|
|
cm.tern.getHint(cm, (data) => {
|
|
|
|
CodeMirror.on(data, "shown", () => ed.emit("before-suggest"));
|
|
|
|
CodeMirror.on(data, "close", () => ed.emit("after-suggest"));
|
|
|
|
CodeMirror.on(data, "select", () => ed.emit("suggestion-entered"));
|
|
|
|
CodeMirror.showHint(cm, (cm, cb) => cb(data), { async: true });
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
keyMap[Editor.keyFor("showInformation", { noaccel: true })] = (cm) => {
|
|
|
|
cm.tern.showType(cm, null, () => {
|
|
|
|
ed.emit("show-information");
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
cm.addKeyMap(keyMap);
|
|
|
|
|
|
|
|
// TODO: Integrate tern autocompletion with this autocomplete API.
|
|
|
|
return;
|
|
|
|
} else if (ed.config.mode == Editor.modes.css) {
|
|
|
|
completer = new cssAutoCompleter({walker: options.walker});
|
|
|
|
}
|
2014-01-28 07:08:41 -08:00
|
|
|
|
|
|
|
let popup = new AutocompletePopup(win.parent.document, {
|
|
|
|
position: "after_start",
|
|
|
|
fixedWidth: true,
|
|
|
|
theme: "auto",
|
|
|
|
autoSelect: true
|
|
|
|
});
|
|
|
|
|
2014-02-16 00:26:26 -08:00
|
|
|
let cycle = (reverse) => {
|
|
|
|
if (popup && popup.isOpen) {
|
|
|
|
cycleSuggestions(ed, reverse == true);
|
|
|
|
return;
|
|
|
|
}
|
2014-01-28 07:08:41 -08:00
|
|
|
|
2014-04-16 13:49:28 -07:00
|
|
|
return CodeMirror.Pass;
|
2014-02-16 00:26:26 -08:00
|
|
|
};
|
2014-01-31 15:29:52 -08:00
|
|
|
|
2014-02-16 00:26:26 -08:00
|
|
|
let keyMap = {
|
|
|
|
"Tab": cycle,
|
|
|
|
"Down": cycle,
|
2014-04-16 13:49:28 -07:00
|
|
|
"Shift-Tab": cycle.bind(null, true),
|
|
|
|
"Up": cycle.bind(null, true),
|
2014-02-16 00:26:26 -08:00
|
|
|
"Enter": () => {
|
2014-01-31 15:29:52 -08:00
|
|
|
if (popup && popup.isOpen) {
|
2014-02-16 00:26:26 -08:00
|
|
|
if (!privates.get(ed).suggestionInsertedOnce) {
|
|
|
|
privates.get(ed).insertingSuggestion = true;
|
2014-03-25 04:55:00 -07:00
|
|
|
let {label, preLabel, text} = popup.getItemAtIndex(0);
|
2014-02-16 00:26:26 -08:00
|
|
|
let cur = ed.getCursor();
|
2014-03-25 04:55:00 -07:00
|
|
|
ed.replaceText(text.slice(preLabel.length), cur, cur);
|
2014-02-16 00:26:26 -08:00
|
|
|
}
|
|
|
|
popup.hidePopup();
|
2014-03-25 04:55:00 -07:00
|
|
|
// This event is used in tests
|
|
|
|
ed.emit("popup-hidden");
|
2014-01-31 15:29:52 -08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-04-16 13:49:28 -07:00
|
|
|
return CodeMirror.Pass;
|
2014-02-16 00:26:26 -08:00
|
|
|
}
|
2014-01-28 07:08:41 -08:00
|
|
|
};
|
2014-04-16 13:49:28 -07:00
|
|
|
keyMap[autocompleteKey] = cm => autoComplete(ctx);
|
2014-01-28 07:08:41 -08:00
|
|
|
cm.addKeyMap(keyMap);
|
|
|
|
|
2014-03-13 10:19:54 -07:00
|
|
|
cm.on("keydown", (cm, e) => onEditorKeypress(ctx, e));
|
2014-01-28 07:08:41 -08:00
|
|
|
ed.on("change", () => autoComplete(ctx));
|
|
|
|
ed.on("destroy", () => {
|
2014-03-13 10:19:54 -07:00
|
|
|
cm.off("keydown", (cm, e) => onEditorKeypress(ctx, e));
|
2014-01-28 07:08:41 -08:00
|
|
|
ed.off("change", () => autoComplete(ctx));
|
|
|
|
popup.destroy();
|
|
|
|
popup = null;
|
|
|
|
completer = null;
|
|
|
|
});
|
|
|
|
|
|
|
|
privates.set(ed, {
|
|
|
|
popup: popup,
|
|
|
|
completer: completer,
|
|
|
|
insertingSuggestion: false,
|
|
|
|
suggestionInsertedOnce: false
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Provides suggestions to autocomplete the current token/word being typed.
|
|
|
|
*/
|
|
|
|
function autoComplete({ ed, cm }) {
|
|
|
|
let private = privates.get(ed);
|
|
|
|
let { completer, popup } = private;
|
|
|
|
if (!completer || private.insertingSuggestion || private.doNotAutocomplete) {
|
|
|
|
private.insertingSuggestion = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let cur = ed.getCursor();
|
|
|
|
completer.complete(cm.getRange({line: 0, ch: 0}, cur), cur)
|
|
|
|
.then(suggestions => {
|
2014-03-16 13:28:47 -07:00
|
|
|
if (!suggestions || !suggestions.length || suggestions[0].preLabel == null) {
|
2014-01-28 07:08:41 -08:00
|
|
|
private.suggestionInsertedOnce = false;
|
|
|
|
popup.hidePopup();
|
2014-01-28 07:11:22 -08:00
|
|
|
ed.emit("after-suggest");
|
2014-01-28 07:08:41 -08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// The cursor is at the end of the currently entered part of the token, like
|
|
|
|
// "backgr|" but we need to open the popup at the beginning of the character
|
|
|
|
// "b". Thus we need to calculate the width of the entered part of the token
|
|
|
|
// ("backgr" here). 4 comes from the popup's left padding.
|
2014-04-01 07:59:00 -07:00
|
|
|
|
|
|
|
let cursorElement = cm.display.cursorDiv.querySelector(".CodeMirror-cursor");
|
2014-01-28 07:08:41 -08:00
|
|
|
let left = suggestions[0].preLabel.length * cm.defaultCharWidth() + 4;
|
|
|
|
popup.hidePopup();
|
|
|
|
popup.setItems(suggestions);
|
2014-04-01 07:59:00 -07:00
|
|
|
popup.openPopup(cursorElement, -1 * left, 0);
|
2014-01-28 07:08:41 -08:00
|
|
|
private.suggestionInsertedOnce = false;
|
2014-01-28 07:11:22 -08:00
|
|
|
// This event is used in tests.
|
|
|
|
ed.emit("after-suggest");
|
2014-01-28 07:08:41 -08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Cycles through provided suggestions by the popup in a top to bottom manner
|
|
|
|
* when `reverse` is not true. Opposite otherwise.
|
|
|
|
*/
|
|
|
|
function cycleSuggestions(ed, reverse) {
|
|
|
|
let private = privates.get(ed);
|
|
|
|
let { popup, completer } = private;
|
|
|
|
let cur = ed.getCursor();
|
|
|
|
private.insertingSuggestion = true;
|
|
|
|
if (!private.suggestionInsertedOnce) {
|
|
|
|
private.suggestionInsertedOnce = true;
|
|
|
|
let firstItem;
|
|
|
|
if (reverse) {
|
|
|
|
firstItem = popup.getItemAtIndex(popup.itemCount - 1);
|
|
|
|
popup.selectPreviousItem();
|
|
|
|
} else {
|
|
|
|
firstItem = popup.getItemAtIndex(0);
|
|
|
|
if (firstItem.label == firstItem.preLabel && popup.itemCount > 1) {
|
|
|
|
firstItem = popup.getItemAtIndex(1);
|
|
|
|
popup.selectNextItem();
|
|
|
|
}
|
|
|
|
}
|
2014-01-28 07:11:22 -08:00
|
|
|
if (popup.itemCount == 1)
|
2014-01-28 07:08:41 -08:00
|
|
|
popup.hidePopup();
|
2014-03-25 04:55:00 -07:00
|
|
|
ed.replaceText(firstItem.text.slice(firstItem.preLabel.length), cur, cur);
|
2014-01-28 07:08:41 -08:00
|
|
|
} else {
|
|
|
|
let fromCur = {
|
|
|
|
line: cur.line,
|
2014-03-25 04:55:00 -07:00
|
|
|
ch : cur.ch - popup.selectedItem.text.length
|
2014-01-28 07:08:41 -08:00
|
|
|
};
|
|
|
|
if (reverse)
|
|
|
|
popup.selectPreviousItem();
|
|
|
|
else
|
|
|
|
popup.selectNextItem();
|
2014-03-25 04:55:00 -07:00
|
|
|
ed.replaceText(popup.selectedItem.text, fromCur, cur);
|
2014-01-28 07:08:41 -08:00
|
|
|
}
|
2014-01-28 07:11:22 -08:00
|
|
|
// This event is used in tests.
|
|
|
|
ed.emit("suggestion-entered");
|
2014-01-28 07:08:41 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* onkeydown handler for the editor instance to prevent autocompleting on some
|
|
|
|
* keypresses.
|
|
|
|
*/
|
2014-03-13 10:19:54 -07:00
|
|
|
function onEditorKeypress({ ed, Editor }, event) {
|
2014-01-28 07:08:41 -08:00
|
|
|
let private = privates.get(ed);
|
2014-04-01 07:59:00 -07:00
|
|
|
|
|
|
|
// Do not try to autocomplete with multiple selections.
|
|
|
|
if (ed.hasMultipleSelections()) {
|
|
|
|
private.doNotAutocomplete = true;
|
|
|
|
private.popup.hidePopup();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-04-19 05:45:51 -07:00
|
|
|
if ((event.ctrlKey || event.metaKey) && event.keyCode == event.DOM_VK_SPACE) {
|
|
|
|
// When Ctrl/Cmd + Space is pressed, two simultaneous keypresses are emitted
|
|
|
|
// first one for just the Ctrl/Cmd and second one for combo. The first one
|
|
|
|
// leave the private.doNotAutocomplete as true, so we have to make it false
|
|
|
|
private.doNotAutocomplete = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (event.ctrlKey || event.metaKey || event.altKey) {
|
|
|
|
private.doNotAutocomplete = true;
|
|
|
|
private.popup.hidePopup();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-01-28 07:08:41 -08:00
|
|
|
switch (event.keyCode) {
|
2014-04-19 05:45:51 -07:00
|
|
|
case event.DOM_VK_RETURN:
|
|
|
|
private.doNotAutocomplete = true;
|
|
|
|
break;
|
|
|
|
|
2014-01-31 15:29:52 -08:00
|
|
|
case event.DOM_VK_ESCAPE:
|
|
|
|
if (private.popup.isOpen)
|
|
|
|
event.preventDefault();
|
2014-01-28 07:08:41 -08:00
|
|
|
case event.DOM_VK_LEFT:
|
|
|
|
case event.DOM_VK_RIGHT:
|
2014-01-28 07:11:22 -08:00
|
|
|
case event.DOM_VK_HOME:
|
|
|
|
case event.DOM_VK_END:
|
2014-03-13 10:19:54 -07:00
|
|
|
private.doNotAutocomplete = true;
|
|
|
|
private.popup.hidePopup();
|
|
|
|
break;
|
|
|
|
|
2014-01-28 07:08:41 -08:00
|
|
|
case event.DOM_VK_BACK_SPACE:
|
|
|
|
case event.DOM_VK_DELETE:
|
2014-03-13 10:19:54 -07:00
|
|
|
if (ed.config.mode == Editor.modes.css)
|
|
|
|
private.completer.invalidateCache(ed.getCursor().line)
|
2014-01-28 07:08:41 -08:00
|
|
|
private.doNotAutocomplete = true;
|
|
|
|
private.popup.hidePopup();
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
private.doNotAutocomplete = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-28 07:11:22 -08:00
|
|
|
/**
|
|
|
|
* Returns the private popup. This method is used by tests to test the feature.
|
|
|
|
*/
|
|
|
|
function getPopup({ ed }) {
|
|
|
|
return privates.get(ed).popup;
|
|
|
|
}
|
2014-03-25 08:08:52 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns contextual information about the token covered by the caret if the
|
|
|
|
* implementation of completer supports it.
|
|
|
|
*/
|
|
|
|
function getInfoAt({ ed }, caret) {
|
|
|
|
let completer = privates.get(ed).completer;
|
|
|
|
if (completer && completer.getInfoAt)
|
|
|
|
return completer.getInfoAt(ed.getText(), caret);
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2014-01-28 07:08:41 -08:00
|
|
|
// Export functions
|
|
|
|
|
|
|
|
module.exports.setupAutoCompletion = setupAutoCompletion;
|
2014-01-28 07:11:22 -08:00
|
|
|
module.exports.getAutocompletionPopup = getPopup;
|
2014-03-25 08:08:52 -07:00
|
|
|
module.exports.getInfoAt = getInfoAt;
|