Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add documentation tooltip for autocompleter #2148

Merged
merged 1 commit into from
Oct 6, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 87 additions & 2 deletions lib/ace/autocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ var AcePopup = require("./autocomplete/popup").AcePopup;
var util = require("./autocomplete/util");
var event = require("./lib/event");
var lang = require("./lib/lang");
var dom = require("./lib/dom");
var snippetManager = require("./snippets").snippetManager;

var Autocomplete = function() {
Expand All @@ -52,6 +53,8 @@ var Autocomplete = function() {
this.changeTimer = lang.delayedCall(function() {
this.updateCompletions(true);
}.bind(this));

this.tooltipTimer = lang.delayedCall(this.updateDocTooltip.bind(this), 50);
};

(function() {
Expand All @@ -64,6 +67,13 @@ var Autocomplete = function() {
e.stop();
}.bind(this));
this.popup.focus = this.editor.focus.bind(this.editor);
this.popup.on("select", this.tooltipTimer.bind(null, null));
this.popup.on("changeHoverMarker", this.tooltipTimer.bind(null, null));
return this.popup;
};

this.getPopup = function() {
return this.popup || this.$init();
};

this.openPopup = function(editor, prefix, keepPopupPosition) {
Expand Down Expand Up @@ -101,6 +111,7 @@ var Autocomplete = function() {
this.editor.off("mousedown", this.mousedownListener);
this.editor.off("mousewheel", this.mousewheelListener);
this.changeTimer.cancel();
this.hideDocTooltip();

if (this.popup && this.popup.isOpen) {
this.gatherCompletionsId += 1;
Expand All @@ -124,12 +135,17 @@ var Autocomplete = function() {
this.detach();
};

this.blurListener = function() {
this.blurListener = function(e) {
// we have to check if activeElement is a child of popup because
// on IE preventDefault doesn't stop scrollbar from being focussed
var el = document.activeElement;
if (el != this.editor.textInput.getElement() && el.parentNode != this.popup.container)
var text = this.editor.textInput.getElement()
if (el != text && el.parentNode != this.popup.container
&& el != this.tooltipNode && e.relatedTarget != this.tooltipNode
&& e.relatedTarget != text
) {
this.detach();
}
};

this.mousedownListener = function(e) {
Expand Down Expand Up @@ -178,6 +194,7 @@ var Autocomplete = function() {
this.detach();
};


this.commands = {
"Up": function(editor) { editor.completer.goTo("up"); },
"Down": function(editor) { editor.completer.goTo("down"); },
Expand Down Expand Up @@ -310,6 +327,74 @@ var Autocomplete = function() {
this.cancelContextMenu = function() {
this.editor.$mouseHandler.cancelContextMenu();
};

this.updateDocTooltip = function() {
var popup = this.popup;
var all = popup.data;
var selected = all && (all[popup.getHoveredRow()] || all[popup.getRow()]);
var doc = null;
if (!selected || !this.editor || !this.popup.isOpen)
return this.hideDocTooltip();
this.editor.completers.some(function(completer) {
if (completer.getDocTooltip)
doc = completer.getDocTooltip(selected);
return doc;
});
if (!doc)
doc = selected;

if (typeof doc == "string")
doc = {tooltipText: doc}
if (!doc || !(doc.docHTML || doc.docText))
return this.hideDocTooltip();
this.showDocTooltip(doc);
};

this.showDocTooltip = function(item) {
if (!this.tooltipNode) {
this.tooltipNode = dom.createElement("pre");
this.tooltipNode.className = "ace_tooltip ace_doc-tooltip";
this.tooltipNode.style.margin = 0;
this.tooltipNode.style.pointerEvents = "auto";
this.tooltipNode.tabIndex = -1;
this.tooltipNode.onblur = this.blurListener.bind(this);
}

var tooltipNode = this.tooltipNode;
if (item.docHTML) {
tooltipNode.innerHTML = item.docHTML;
} else if (item.docText) {
tooltipNode.textContent = item.docText;
}

if (!tooltipNode.parentNode)
document.body.appendChild(tooltipNode);
var popup = this.popup;
var rect = popup.container.getBoundingClientRect();
tooltipNode.style.top = popup.container.style.top;
tooltipNode.style.bottom = popup.container.style.bottom;

if (window.innerWidth - rect.right < 320) {
tooltipNode.style.right = window.innerWidth - rect.left + "px";
tooltipNode.style.left = "";
} else {
tooltipNode.style.left = (rect.right + 1) + "px";
tooltipNode.style.right = "";
}
// tooltipNode.style.height = rect.height + "px";
tooltipNode.style.display = "block";
};

this.hideDocTooltip = function() {
this.tooltipTimer.cancel();
if (!this.tooltipNode) return;
var el = this.tooltipNode;
if (!this.editor.isFocused() && document.activeElement == el)
this.editor.focus();
this.tooltipNode = null;
if (el.parentNode)
el.parentNode.removeChild(el);
};

}).call(Autocomplete.prototype);

Expand Down
8 changes: 4 additions & 4 deletions lib/ace/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -662,13 +662,13 @@ var Editor = function(renderer, session) {
*
*
**/
this.onFocus = function() {
this.onFocus = function(e) {
if (this.$isFocused)
return;
this.$isFocused = true;
this.renderer.showCursor();
this.renderer.visualizeFocus();
this._emit("focus");
this._emit("focus", e);
};

/**
Expand All @@ -677,13 +677,13 @@ var Editor = function(renderer, session) {
*
*
**/
this.onBlur = function() {
this.onBlur = function(e) {
if (!this.$isFocused)
return;
this.$isFocused = false;
this.renderer.hideCursor();
this.renderer.visualizeBlur();
this._emit("blur");
this._emit("blur", e);
};

this.$cursorChange = function() {
Expand Down
12 changes: 11 additions & 1 deletion lib/ace/ext/language_tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ define(function(require, exports, module) {
var snippetManager = require("../snippets").snippetManager;
var Autocomplete = require("../autocomplete").Autocomplete;
var config = require("../config");
var lang = require("../lib/lang");
var util = require("../autocomplete/util");

var textCompleter = require("../autocomplete/text_completer");
Expand All @@ -59,11 +60,20 @@ var snippetCompleter = {
completions.push({
caption: caption,
snippet: s.content,
meta: s.tabTrigger && !s.name ? s.tabTrigger + "\u21E5 " : "snippet"
meta: s.tabTrigger && !s.name ? s.tabTrigger + "\u21E5 " : "snippet",
type: "snippet"
});
}
}, this);
callback(null, completions);
},
getDocTooltip: function(item) {
if (item.type == "snippet" && !item.docHTML) {
item.docHTML = [
"<b>", lang.escapeHTML(item.caption), "</b>", "<hr></hr>",
lang.escapeHTML(item.snippet)
].join("");
}
}
};

Expand Down
8 changes: 4 additions & 4 deletions lib/ace/keyboard/textinput.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,13 @@ var TextInput = function(parentNode, host) {
// ie9 throws error if document.activeElement is accessed too soon
try { var isFocused = document.activeElement === text; } catch(e) {}

event.addListener(text, "blur", function() {
host.onBlur();
event.addListener(text, "blur", function(e) {
host.onBlur(e);
isFocused = false;
});
event.addListener(text, "focus", function() {
event.addListener(text, "focus", function(e) {
isFocused = true;
host.onFocus();
host.onFocus(e);
resetSelection();
});
this.focus = function() { text.focus(); };
Expand Down