diff options
Diffstat (limited to 'imports/codemirror/lib/util/javascript-hint.js')
-rw-r--r--[-rwxr-xr-x] | imports/codemirror/lib/util/javascript-hint.js | 67 |
1 files changed, 58 insertions, 9 deletions
diff --git a/imports/codemirror/lib/util/javascript-hint.js b/imports/codemirror/lib/util/javascript-hint.js index 4e88a7e4..2b904a51 100755..100644 --- a/imports/codemirror/lib/util/javascript-hint.js +++ b/imports/codemirror/lib/util/javascript-hint.js | |||
@@ -15,37 +15,81 @@ | |||
15 | } | 15 | } |
16 | return arr.indexOf(item) != -1; | 16 | return arr.indexOf(item) != -1; |
17 | } | 17 | } |
18 | 18 | ||
19 | CodeMirror.javascriptHint = function(editor) { | 19 | function scriptHint(editor, keywords, getToken) { |
20 | // Find the token at the cursor | 20 | // Find the token at the cursor |
21 | var cur = editor.getCursor(), token = editor.getTokenAt(cur), tprop = token; | 21 | var cur = editor.getCursor(), token = getToken(editor, cur), tprop = token; |
22 | // If it's not a 'word-style' token, ignore the token. | 22 | // If it's not a 'word-style' token, ignore the token. |
23 | if (!/^[\w$_]*$/.test(token.string)) { | 23 | if (!/^[\w$_]*$/.test(token.string)) { |
24 | token = tprop = {start: cur.ch, end: cur.ch, string: "", state: token.state, | 24 | token = tprop = {start: cur.ch, end: cur.ch, string: "", state: token.state, |
25 | className: token.string == "." ? "property" : null}; | 25 | className: token.string == "." ? "property" : null}; |
26 | } | 26 | } |
27 | // If it is a property, find out what it is a property of. | 27 | // If it is a property, find out what it is a property of. |
28 | while (tprop.className == "property") { | 28 | while (tprop.className == "property") { |
29 | tprop = editor.getTokenAt({line: cur.line, ch: tprop.start}); | 29 | tprop = getToken(editor, {line: cur.line, ch: tprop.start}); |
30 | if (tprop.string != ".") return; | 30 | if (tprop.string != ".") return; |
31 | tprop = editor.getTokenAt({line: cur.line, ch: tprop.start}); | 31 | tprop = getToken(editor, {line: cur.line, ch: tprop.start}); |
32 | if (tprop.string == ')') { | ||
33 | var level = 1; | ||
34 | do { | ||
35 | tprop = getToken(editor, {line: cur.line, ch: tprop.start}); | ||
36 | switch (tprop.string) { | ||
37 | case ')': level++; break; | ||
38 | case '(': level--; break; | ||
39 | default: break; | ||
40 | } | ||
41 | } while (level > 0) | ||
42 | tprop = getToken(editor, {line: cur.line, ch: tprop.start}); | ||
43 | if (tprop.className == 'variable') | ||
44 | tprop.className = 'function'; | ||
45 | else return; // no clue | ||
46 | } | ||
32 | if (!context) var context = []; | 47 | if (!context) var context = []; |
33 | context.push(tprop); | 48 | context.push(tprop); |
34 | } | 49 | } |
35 | return {list: getCompletions(token, context), | 50 | return {list: getCompletions(token, context, keywords), |
36 | from: {line: cur.line, ch: token.start}, | 51 | from: {line: cur.line, ch: token.start}, |
37 | to: {line: cur.line, ch: token.end}}; | 52 | to: {line: cur.line, ch: token.end}}; |
38 | } | 53 | } |
39 | 54 | ||
55 | CodeMirror.javascriptHint = function(editor) { | ||
56 | return scriptHint(editor, javascriptKeywords, | ||
57 | function (e, cur) {return e.getTokenAt(cur);}); | ||
58 | } | ||
59 | |||
60 | function getCoffeeScriptToken(editor, cur) { | ||
61 | // This getToken, it is for coffeescript, imitates the behavior of | ||
62 | // getTokenAt method in javascript.js, that is, returning "property" | ||
63 | // type and treat "." as indepenent token. | ||
64 | var token = editor.getTokenAt(cur); | ||
65 | if (cur.ch == token.start + 1 && token.string.charAt(0) == '.') { | ||
66 | token.end = token.start; | ||
67 | token.string = '.'; | ||
68 | token.className = "property"; | ||
69 | } | ||
70 | else if (/^\.[\w$_]*$/.test(token.string)) { | ||
71 | token.className = "property"; | ||
72 | token.start++; | ||
73 | token.string = token.string.replace(/\./, ''); | ||
74 | } | ||
75 | return token; | ||
76 | } | ||
77 | |||
78 | CodeMirror.coffeescriptHint = function(editor) { | ||
79 | return scriptHint(editor, coffeescriptKeywords, getCoffeeScriptToken); | ||
80 | } | ||
81 | |||
40 | var stringProps = ("charAt charCodeAt indexOf lastIndexOf substring substr slice trim trimLeft trimRight " + | 82 | var stringProps = ("charAt charCodeAt indexOf lastIndexOf substring substr slice trim trimLeft trimRight " + |
41 | "toUpperCase toLowerCase split concat match replace search").split(" "); | 83 | "toUpperCase toLowerCase split concat match replace search").split(" "); |
42 | var arrayProps = ("length concat join splice push pop shift unshift slice reverse sort indexOf " + | 84 | var arrayProps = ("length concat join splice push pop shift unshift slice reverse sort indexOf " + |
43 | "lastIndexOf every some filter forEach map reduce reduceRight ").split(" "); | 85 | "lastIndexOf every some filter forEach map reduce reduceRight ").split(" "); |
44 | var funcProps = "prototype apply call bind".split(" "); | 86 | var funcProps = "prototype apply call bind".split(" "); |
45 | var keywords = ("break case catch continue debugger default delete do else false finally for function " + | 87 | var javascriptKeywords = ("break case catch continue debugger default delete do else false finally for function " + |
46 | "if in instanceof new null return switch throw true try typeof var void while with").split(" "); | 88 | "if in instanceof new null return switch throw true try typeof var void while with").split(" "); |
89 | var coffeescriptKeywords = ("and break catch class continue delete do else extends false finally for " + | ||
90 | "if in instanceof isnt new no not null of off on or return switch then throw true try typeof until void while with yes").split(" "); | ||
47 | 91 | ||
48 | function getCompletions(token, context) { | 92 | function getCompletions(token, context, keywords) { |
49 | var found = [], start = token.string; | 93 | var found = [], start = token.string; |
50 | function maybeAdd(str) { | 94 | function maybeAdd(str) { |
51 | if (str.indexOf(start) == 0 && !arrayContains(found, str)) found.push(str); | 95 | if (str.indexOf(start) == 0 && !arrayContains(found, str)) found.push(str); |
@@ -67,6 +111,11 @@ | |||
67 | base = ""; | 111 | base = ""; |
68 | else if (obj.className == "atom") | 112 | else if (obj.className == "atom") |
69 | base = 1; | 113 | base = 1; |
114 | else if (obj.className == "function") { | ||
115 | if (window.jQuery != null && (obj.string == '$' || obj.string == 'jQuery') && | ||
116 | (typeof jQuery == 'function')) base = jQuery(); | ||
117 | else if (window._ != null && (obj.string == '_') && (typeof _ == 'function')) base = _(); | ||
118 | } | ||
70 | while (base != null && context.length) | 119 | while (base != null && context.length) |
71 | base = base[context.pop().string]; | 120 | base = base[context.pop().string]; |
72 | if (base != null) gatherCompletions(base); | 121 | if (base != null) gatherCompletions(base); |