aboutsummaryrefslogtreecommitdiff
path: root/js/code-editor/codemirror-ninja/lib/ninja-simple-hint.js
diff options
context:
space:
mode:
authorValerio Virgillito2012-05-03 16:08:49 -0700
committerValerio Virgillito2012-05-03 16:08:49 -0700
commit0e1c87871489c1d5d8deb609174d8876eb579169 (patch)
treef37c26fbab9151e6c4f50e3e786bdc96b9960014 /js/code-editor/codemirror-ninja/lib/ninja-simple-hint.js
parenteb17fe1bbd05ad260e8a56918a3b396a03767e04 (diff)
parent01ecdc4bda1aff7d39f429c76e57b10af6079c53 (diff)
downloadninja-0e1c87871489c1d5d8deb609174d8876eb579169.tar.gz
Merge pull request #192 from ananyasen/Codeview-improvements
submitting project : Code Editor Improvements [Phase 1]
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.js86
1 files changed, 86 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..4787b4f8
--- /dev/null
+++ b/js/code-editor/codemirror-ninja/lib/ninja-simple-hint.js
@@ -0,0 +1,86 @@
1/* <copyright>
2This file contains proprietary software owned by Motorola Mobility, Inc.<br/>
3No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.<br/>
4(c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved.
5</copyright> */
6
7/* adopted from CodeMirror 2.23 (http://codemirror.net/) */
8
9(function() {
10 CodeMirror.simpleHint = function(editor, getHints) {
11 // We want a single cursor position.
12 if (editor.somethingSelected()) return;
13
14 // Ninja override: don't show code hinting if the token is empty
15 var tempToken = editor.getTokenAt(editor.getCursor());
16 if(tempToken && ((tempToken.string === "") || !(/[\S]/gi.test(tempToken.string)))) return;//check that token has only spaces
17
18
19 var result = getHints(editor);
20 if (!result || !result.list.length) return;
21 var completions = result.list;
22 function insert(str) {
23 editor.replaceRange(str, result.from, result.to);
24 }
25 // Ninja override: don't autocomplete to reduce user's typing errors
26// if (completions.length == 1) {insert(completions[0]); return true;}
27
28 // Build the select widget
29 var complete = document.createElement("div");
30 complete.className = "CodeMirror-completions";
31 var sel = complete.appendChild(document.createElement("select"));
32 // Opera doesn't move the selection when pressing up/down in a
33 // multi-select, but it does properly support the size property on
34 // single-selects, so no multi-select is necessary.
35 if (!window.opera) sel.multiple = true;
36 for (var i = 0; i < completions.length; ++i) {
37 var opt = sel.appendChild(document.createElement("option"));
38 opt.appendChild(document.createTextNode(completions[i]));
39 }
40 sel.firstChild.selected = true;
41 sel.size = Math.min(10, completions.length);
42 var pos = editor.cursorCoords();
43 complete.style.left = pos.x + "px";
44 complete.style.top = pos.yBot + "px";
45 document.body.appendChild(complete);
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 if(winW - pos.x < sel.clientWidth)
49 complete.style.left = (pos.x - sel.clientWidth) + "px";
50 // Hack to hide the scrollbar.
51 if (completions.length <= 10)
52 complete.style.width = (sel.clientWidth - 1) + "px";
53
54 var done = false;
55 function close() {
56 if (done) return;
57 done = true;
58 complete.parentNode.removeChild(complete);
59 }
60 function pick() {
61 insert(completions[sel.selectedIndex]);
62 close();
63 setTimeout(function(){editor.focus();}, 50);
64 }
65 CodeMirror.connect(sel, "blur", close);
66 CodeMirror.connect(sel, "keydown", function(event) {
67 var code = event.keyCode;
68 // Enter
69 if (code == 13) {CodeMirror.e_stop(event); pick();}
70 // Escape
71 else if (code == 27) {CodeMirror.e_stop(event); close(); editor.focus();}
72 else if (code != 38 && code != 40) {
73 close(); editor.focus();
74 // Pass the event to the CodeMirror instance so that it can handle things like backspace properly.
75 editor.triggerOnKeyDown(event);
76 setTimeout(function(){CodeMirror.simpleHint(editor, getHints);}, 50);
77 }
78 });
79 CodeMirror.connect(sel, "dblclick", pick);
80
81 sel.focus();
82 // Opera sometimes ignores focusing a freshly created node
83 if (window.opera) setTimeout(function(){if (!done) sel.focus();}, 100);
84 return true;
85 };
86})();