blob: 3e760fcc7a36cc2170fe57d507aa44f004e1f0f7 [file] [log] [blame]
adminbae64d82013-08-01 10:50:15 -07001(function() {
2 CodeMirror.simpleHint = function(editor, getHints) {
3 // We want a single cursor position.
4 if (editor.somethingSelected()) return;
5 var result = getHints(editor);
6 if (!result || !result.list.length) return;
7 var completions = result.list;
8 function insert(str) {
9 editor.replaceRange(str, result.from, result.to);
10 }
11 // When there is only one completion, use it directly.
12 if (completions.length == 1) {insert(completions[0]); return true;}
13
14 // Build the select widget
15 var complete = document.createElement("div");
16 complete.className = "CodeMirror-completions";
17 var sel = complete.appendChild(document.createElement("select"));
18 // Opera doesn't move the selection when pressing up/down in a
19 // multi-select, but it does properly support the size property on
20 // single-selects, so no multi-select is necessary.
21 if (!window.opera) sel.multiple = true;
22 for (var i = 0; i < completions.length; ++i) {
23 var opt = sel.appendChild(document.createElement("option"));
24 opt.appendChild(document.createTextNode(completions[i]));
25 }
26 sel.firstChild.selected = true;
27 sel.size = Math.min(10, completions.length);
28 var pos = editor.cursorCoords();
29 complete.style.left = pos.x + "px";
30 complete.style.top = pos.yBot + "px";
31 document.body.appendChild(complete);
32 // If we're at the edge of the screen, then we want the menu to appear on the left of the cursor.
33 var winW = window.innerWidth || Math.max(document.body.offsetWidth, document.documentElement.offsetWidth);
34 if(winW - pos.x < sel.clientWidth)
35 complete.style.left = (pos.x - sel.clientWidth) + "px";
36 // Hack to hide the scrollbar.
37 if (completions.length <= 10)
38 complete.style.width = (sel.clientWidth - 1) + "px";
39
40 var done = false;
41 function close() {
42 if (done) return;
43 done = true;
44 complete.parentNode.removeChild(complete);
45 }
46 function pick() {
47 insert(completions[sel.selectedIndex]);
48 close();
49 setTimeout(function(){editor.focus();}, 50);
50 }
51 CodeMirror.connect(sel, "blur", close);
52 CodeMirror.connect(sel, "keydown", function(event) {
53 var code = event.keyCode;
54 // Enter
55 if (code == 13) {CodeMirror.e_stop(event); pick();}
56 // Escape
57 else if (code == 27) {CodeMirror.e_stop(event); close(); editor.focus();}
58 else if (code != 38 && code != 40) {
59 close(); editor.focus();
60 // Pass the event to the CodeMirror instance so that it can handle things like backspace properly.
61 editor.triggerOnKeyDown(event);
62 setTimeout(function(){CodeMirror.simpleHint(editor, getHints);}, 50);
63 }
64 });
65 CodeMirror.connect(sel, "dblclick", pick);
66
67 sel.focus();
68 // Opera sometimes ignores focusing a freshly created node
69 if (window.opera) setTimeout(function(){if (!done) sel.focus();}, 100);
70 return true;
71 };
72})();
73