aboutsummaryrefslogtreecommitdiff
path: root/js/code-editor/codemirror-ninja/lib
diff options
context:
space:
mode:
Diffstat (limited to 'js/code-editor/codemirror-ninja/lib')
-rw-r--r--js/code-editor/codemirror-ninja/lib/ninja-simple-hint.js78
1 files changed, 78 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..866e1a8b
--- /dev/null
+++ b/js/code-editor/codemirror-ninja/lib/ninja-simple-hint.js
@@ -0,0 +1,78 @@
1(function() {
2 CodeMirror.simpleHint = function(editor, getHints) {
3 // We want a single cursor position.
4 if (editor.somethingSelected()) return;
5
6 // Ninja override: don't show code hinting if the token is empty
7 var tempToken = editor.getTokenAt(editor.getCursor());
8 if(tempToken && ((tempToken.string === "") || !(/[\S]/gi.test(tempToken.string)))) return;//check that token has only spaces
9
10
11 var result = getHints(editor);
12 if (!result || !result.list.length) return;
13 var completions = result.list;
14 function insert(str) {
15 editor.replaceRange(str, result.from, result.to);
16 }
17 // Ninja override: don't autocomplete to reduce user's typing errors
18// if (completions.length == 1) {insert(completions[0]); return true;}
19
20 // Build the select widget
21 var complete = document.createElement("div");
22 complete.className = "CodeMirror-completions";
23 var sel = complete.appendChild(document.createElement("select"));
24 // Opera doesn't move the selection when pressing up/down in a
25 // multi-select, but it does properly support the size property on
26 // single-selects, so no multi-select is necessary.
27 if (!window.opera) sel.multiple = true;
28 for (var i = 0; i < completions.length; ++i) {
29 var opt = sel.appendChild(document.createElement("option"));
30 opt.appendChild(document.createTextNode(completions[i]));
31 }
32 sel.firstChild.selected = true;
33 sel.size = Math.min(10, completions.length);
34 var pos = editor.cursorCoords();
35 complete.style.left = pos.x + "px";
36 complete.style.top = pos.yBot + "px";
37 document.body.appendChild(complete);
38 // If we're at the edge of the screen, then we want the menu to appear on the left of the cursor.
39 var winW = window.innerWidth || Math.max(document.body.offsetWidth, document.documentElement.offsetWidth);
40 if(winW - pos.x < sel.clientWidth)
41 complete.style.left = (pos.x - sel.clientWidth) + "px";
42 // Hack to hide the scrollbar.
43 if (completions.length <= 10)
44 complete.style.width = (sel.clientWidth - 1) + "px";
45
46 var done = false;
47 function close() {
48 if (done) return;
49 done = true;
50 complete.parentNode.removeChild(complete);
51 }
52 function pick() {
53 insert(completions[sel.selectedIndex]);
54 close();
55 setTimeout(function(){editor.focus();}, 50);
56 }
57 CodeMirror.connect(sel, "blur", close);
58 CodeMirror.connect(sel, "keydown", function(event) {
59 var code = event.keyCode;
60 // Enter
61 if (code == 13) {CodeMirror.e_stop(event); pick();}
62 // Escape
63 else if (code == 27) {CodeMirror.e_stop(event); close(); editor.focus();}
64 else if (code != 38 && code != 40) {
65 close(); editor.focus();
66 // Pass the event to the CodeMirror instance so that it can handle things like backspace properly.
67 editor.triggerOnKeyDown(event);
68 setTimeout(function(){CodeMirror.simpleHint(editor, getHints);}, 50);
69 }
70 });
71 CodeMirror.connect(sel, "dblclick", pick);
72
73 sel.focus();
74 // Opera sometimes ignores focusing a freshly created node
75 if (window.opera) setTimeout(function(){if (!done) sel.focus();}, 100);
76 return true;
77 };
78})();