Skip to content

Commit ee150b8

Browse files
authored
[tern addon] Move tooltips that would be off screen
1 parent 0762bf2 commit ee150b8

2 files changed

Lines changed: 36 additions & 4 deletions

File tree

addon/hint/show-hint.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,7 @@
293293
}
294294
}
295295
var overlapX = box.right - winW;
296+
if (scrolls) overlapX += cm.display.nativeBarWidth;
296297
if (overlapX > 0) {
297298
if (box.right - box.left > winW) {
298299
hints.style.width = (winW - 5) + "px";

addon/tern/tern.js

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -231,8 +231,7 @@
231231
var content = ts.options.completionTip ? ts.options.completionTip(cur.data) : cur.data.doc;
232232
if (content) {
233233
tooltip = makeTooltip(node.parentNode.getBoundingClientRect().right + window.pageXOffset,
234-
node.getBoundingClientRect().top + window.pageYOffset, content, cm);
235-
tooltip.className += " " + cls + "hint-doc";
234+
node.getBoundingClientRect().top + window.pageYOffset, content, cm, cls + "hint-doc");
236235
}
237236
});
238237
c(obj);
@@ -637,12 +636,44 @@
637636
}
638637
}
639638

640-
function makeTooltip(x, y, content, cm) {
641-
var node = elt("div", cls + "tooltip", content);
639+
function makeTooltip(x, y, content, cm, className) {
640+
var node = elt("div", cls + "tooltip" + " " + (className || ""), content);
642641
node.style.left = x + "px";
643642
node.style.top = y + "px";
644643
var container = ((cm.options || {}).hintOptions || {}).container || document.body;
645644
container.appendChild(node);
645+
646+
var pos = cm.cursorCoords();
647+
var winW = window.innerWidth;
648+
var winH = window.innerHeight;
649+
var box = node.getBoundingClientRect();
650+
var hints = document.querySelector(".CodeMirror-hints");
651+
var overlapY = box.bottom - winH;
652+
var overlapX = box.right - winW;
653+
654+
if (hints && overlapX > 0) {
655+
node.style.left = 0;
656+
var box = node.getBoundingClientRect();
657+
node.style.left = (x = x - hints.offsetWidth - box.width) + "px";
658+
overlapX = box.right - winW;
659+
}
660+
if (overlapY > 0) {
661+
var height = box.bottom - box.top, curTop = pos.top - (pos.bottom - box.top);
662+
if (curTop - height > 0) { // Fits above cursor
663+
node.style.top = (pos.top - height) + "px";
664+
} else if (height > winH) {
665+
node.style.height = (winH - 5) + "px";
666+
node.style.top = (pos.bottom - box.top) + "px";
667+
}
668+
}
669+
if (overlapX > 0) {
670+
if (box.right - box.left > winW) {
671+
node.style.width = (winW - 5) + "px";
672+
overlapX -= (box.right - box.left) - winW;
673+
}
674+
node.style.left = (x - overlapX) + "px";
675+
}
676+
646677
return node;
647678
}
648679

0 commit comments

Comments
 (0)