diff options
Diffstat (limited to 'js/code-editor/codemirror-ninja-lib/ninja-simple-hint.js')
-rw-r--r-- | js/code-editor/codemirror-ninja-lib/ninja-simple-hint.js | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/js/code-editor/codemirror-ninja-lib/ninja-simple-hint.js b/js/code-editor/codemirror-ninja-lib/ninja-simple-hint.js new file mode 100644 index 00000000..e2334a45 --- /dev/null +++ b/js/code-editor/codemirror-ninja-lib/ninja-simple-hint.js | |||
@@ -0,0 +1,72 @@ | |||
1 | (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 | // Ninja override: don't autocomplete to reduce user's typing errors | ||
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 | })(); | ||