show-hint.js 7.0 KB

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