show-hint.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. CodeMirror.showHint = function(cm, getHints, options) {
  2. if (!options) options = {};
  3. var startCh = cm.getCursor().ch, continued = false;
  4. var closeOn = options.closeCharacters || /[\s()\[\]{};:]/;
  5. function startHinting() {
  6. // We want a single cursor position.
  7. if (cm.somethingSelected()) return;
  8. if (options.async)
  9. getHints(cm, showHints, options);
  10. else
  11. return showHints(getHints(cm, options));
  12. }
  13. function getText(completion) {
  14. if (typeof completion == "string") return completion;
  15. else return completion.text;
  16. }
  17. function pickCompletion(cm, data, completion) {
  18. if (completion.hint) completion.hint(cm, data, completion);
  19. else cm.replaceRange(getText(completion), data.from, data.to);
  20. }
  21. function showHints(data) {
  22. if (!data || !data.list.length) return;
  23. var completions = data.list;
  24. // When there is only one completion, use it directly.
  25. if (!continued && options.completeSingle !== false && completions.length == 1) {
  26. pickCompletion(cm, data, completions[0]);
  27. return true;
  28. }
  29. // Build the select widget
  30. var hints = document.createElement("ul"), selectedHint = 0;
  31. hints.className = "CodeMirror-hints";
  32. for (var i = 0; i < completions.length; ++i) {
  33. var elt = hints.appendChild(document.createElement("li")), completion = completions[i];
  34. var className = "CodeMirror-hint" + (i ? "" : " CodeMirror-hint-active");
  35. if (completion.className != null) className = completion.className + " " + className;
  36. elt.className = className;
  37. if (completion.render) completion.render(elt, data, completion);
  38. else elt.appendChild(document.createTextNode(getText(completion)));
  39. elt.hintId = i;
  40. }
  41. var pos = cm.cursorCoords(options.alignWithWord !== false ? data.from : null);
  42. var left = pos.left, top = pos.bottom, below = true;
  43. hints.style.left = left + "px";
  44. hints.style.top = top + "px";
  45. document.body.appendChild(hints);
  46. // If we're at the edge of the screen, then we want the menu to appear on the left of the cursor.
  47. var winW = window.innerWidth || Math.max(document.body.offsetWidth, document.documentElement.offsetWidth);
  48. var winH = window.innerHeight || Math.max(document.body.offsetHeight, document.documentElement.offsetHeight);
  49. var box = hints.getBoundingClientRect();
  50. var overlapX = box.right - winW, overlapY = box.bottom - winH;
  51. if (overlapX > 0) {
  52. if (box.right - box.left > winW) {
  53. hints.style.width = (winW - 5) + "px";
  54. overlapX -= (box.right - box.left) - winW;
  55. }
  56. hints.style.left = (left = pos.left - overlapX) + "px";
  57. }
  58. if (overlapY > 0) {
  59. var height = box.bottom - box.top;
  60. if (box.top - (pos.bottom - pos.top) - height > 0) {
  61. overlapY = height + (pos.bottom - pos.top);
  62. below = false;
  63. } else if (height > winH) {
  64. hints.style.height = (winH - 5) + "px";
  65. overlapY -= height - winH;
  66. }
  67. hints.style.top = (top = pos.bottom - overlapY) + "px";
  68. }
  69. function changeActive(i) {
  70. i = Math.max(0, Math.min(i, completions.length - 1));
  71. if (selectedHint == i) return;
  72. var node = hints.childNodes[selectedHint];
  73. node.className = node.className.replace(" CodeMirror-hint-active", "");
  74. node = hints.childNodes[selectedHint = i];
  75. node.className += " CodeMirror-hint-active";
  76. if (node.offsetTop < hints.scrollTop)
  77. hints.scrollTop = node.offsetTop - 3;
  78. else if (node.offsetTop + node.offsetHeight > hints.scrollTop + hints.clientHeight)
  79. hints.scrollTop = node.offsetTop + node.offsetHeight - hints.clientHeight + 3;
  80. }
  81. function screenAmount() {
  82. return Math.floor(hints.clientHeight / hints.firstChild.offsetHeight) || 1;
  83. }
  84. var ourMap = {
  85. Up: function() {changeActive(selectedHint - 1);},
  86. Down: function() {changeActive(selectedHint + 1);},
  87. PageUp: function() {changeActive(selectedHint - screenAmount());},
  88. PageDown: function() {changeActive(selectedHint + screenAmount());},
  89. Home: function() {changeActive(0);},
  90. End: function() {changeActive(completions.length - 1);},
  91. Enter: pick,
  92. Tab: pick,
  93. Esc: close
  94. };
  95. if (options.customKeys) for (var key in options.customKeys) if (options.customKeys.hasOwnProperty(key)) {
  96. var val = options.customKeys[key];
  97. if (/^(Up|Down|Enter|Esc)$/.test(key)) val = ourMap[val];
  98. ourMap[key] = val;
  99. }
  100. cm.addKeyMap(ourMap);
  101. cm.on("cursorActivity", cursorActivity);
  102. var closingOnBlur;
  103. function onBlur(){ closingOnBlur = setTimeout(close, 100); };
  104. function onFocus(){ clearTimeout(closingOnBlur); };
  105. cm.on("blur", onBlur);
  106. cm.on("focus", onFocus);
  107. var startScroll = cm.getScrollInfo();
  108. function onScroll() {
  109. var curScroll = cm.getScrollInfo(), editor = cm.getWrapperElement().getBoundingClientRect();
  110. var newTop = top + startScroll.top - curScroll.top, point = newTop;
  111. if (!below) point += hints.offsetHeight;
  112. if (point <= editor.top || point >= editor.bottom) return close();
  113. hints.style.top = newTop + "px";
  114. hints.style.left = (left + startScroll.left - curScroll.left) + "px";
  115. }
  116. cm.on("scroll", onScroll);
  117. CodeMirror.on(hints, "dblclick", function(e) {
  118. var t = e.target || e.srcElement;
  119. if (t.hintId != null) {selectedHint = t.hintId; pick();}
  120. });
  121. CodeMirror.on(hints, "click", function(e) {
  122. var t = e.target || e.srcElement;
  123. if (t.hintId != null) changeActive(t.hintId);
  124. });
  125. CodeMirror.on(hints, "mousedown", function() {
  126. setTimeout(function(){cm.focus();}, 20);
  127. });
  128. var done = false, once;
  129. function close() {
  130. if (done) return;
  131. done = true;
  132. clearTimeout(once);
  133. hints.parentNode.removeChild(hints);
  134. cm.removeKeyMap(ourMap);
  135. cm.off("cursorActivity", cursorActivity);
  136. cm.off("blur", onBlur);
  137. cm.off("focus", onFocus);
  138. cm.off("scroll", onScroll);
  139. }
  140. function pick() {
  141. pickCompletion(cm, data, completions[selectedHint]);
  142. close();
  143. }
  144. var once, lastPos = cm.getCursor(), lastLen = cm.getLine(lastPos.line).length;
  145. function cursorActivity() {
  146. clearTimeout(once);
  147. var pos = cm.getCursor(), line = cm.getLine(pos.line);
  148. if (pos.line != lastPos.line || line.length - pos.ch != lastLen - lastPos.ch ||
  149. pos.ch < startCh || cm.somethingSelected() ||
  150. (pos.ch && closeOn.test(line.charAt(pos.ch - 1))))
  151. close();
  152. else
  153. once = setTimeout(function(){close(); continued = true; startHinting();}, 70);
  154. }
  155. return true;
  156. }
  157. return startHinting();
  158. };