]>
git.r.bdr.sh - rbdr/dotfiles/blob - atom/packages/pretty-json/node_modules/jsonminify/report/assets/scripts/vendor/codemirror/util/javascript-hint.js
07caba8766f6aaf89817d584b229d0b2d32dbc99
2 function forEach(arr
, f
) {
3 for (var i
= 0, e
= arr
.length
; i
< e
; ++i
) f(arr
[i
]);
6 function arrayContains(arr
, item
) {
7 if (!Array
.prototype.indexOf
) {
10 if (arr
[i
] === item
) {
16 return arr
.indexOf(item
) != -1;
19 function scriptHint(editor
, keywords
, getToken
, options
) {
20 // Find the token at the cursor
21 var cur
= editor
.getCursor(), token
= getToken(editor
, cur
), tprop
= token
;
22 // If it's not a 'word-style' token, ignore the token.
23 if (!/^[\w$_]*$/.test(token
.string
)) {
24 token
= tprop
= {start: cur
.ch
, end: cur
.ch
, string: "", state: token
.state
,
25 type: token
.string
== "." ? "property" : null};
27 // If it is a property, find out what it is a property of.
28 while (tprop
.type
== "property") {
29 tprop
= getToken(editor
, {line: cur
.line
, ch: tprop
.start
});
30 if (tprop
.string
!= ".") return;
31 tprop
= getToken(editor
, {line: cur
.line
, ch: tprop
.start
});
32 if (tprop
.string
== ')') {
35 tprop
= getToken(editor
, {line: cur
.line
, ch: tprop
.start
});
36 switch (tprop
.string
) {
37 case ')': level
++; break;
38 case '(': level
--; break;
42 tprop
= getToken(editor
, {line: cur
.line
, ch: tprop
.start
});
43 if (tprop
.type
== 'variable')
44 tprop
.type
= 'function';
45 else return; // no clue
47 if (!context
) var context
= [];
50 return {list: getCompletions(token
, context
, keywords
, options
),
51 from: {line: cur
.line
, ch: token
.start
},
52 to: {line: cur
.line
, ch: token
.end
}};
55 CodeMirror
.javascriptHint = function(editor
, options
) {
56 return scriptHint(editor
, javascriptKeywords
,
57 function (e
, cur
) {return e
.getTokenAt(cur
);},
61 function getCoffeeScriptToken(editor
, cur
) {
62 // This getToken, it is for coffeescript, imitates the behavior of
63 // getTokenAt method in javascript.js, that is, returning "property"
64 // type and treat "." as indepenent token.
65 var token
= editor
.getTokenAt(cur
);
66 if (cur
.ch
== token
.start
+ 1 && token
.string
.charAt(0) == '.') {
67 token
.end
= token
.start
;
69 token
.type
= "property";
71 else if (/^\.[\w$_]*$/.test(token
.string
)) {
72 token
.type
= "property";
74 token
.string
= token
.string
.replace(/\./, '');
79 CodeMirror
.coffeescriptHint = function(editor
, options
) {
80 return scriptHint(editor
, coffeescriptKeywords
, getCoffeeScriptToken
, options
);
83 var stringProps
= ("charAt charCodeAt indexOf lastIndexOf substring substr slice trim trimLeft trimRight " +
84 "toUpperCase toLowerCase split concat match replace search").split(" ");
85 var arrayProps
= ("length concat join splice push pop shift unshift slice reverse sort indexOf " +
86 "lastIndexOf every some filter forEach map reduce reduceRight ").split(" ");
87 var funcProps
= "prototype apply call bind".split(" ");
88 var javascriptKeywords
= ("break case catch continue debugger default delete do else false finally for function " +
89 "if in instanceof new null return switch throw true try typeof var void while with").split(" ");
90 var coffeescriptKeywords
= ("and break catch class continue delete do else extends false finally for " +
91 "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(" ");
93 function getCompletions(token
, context
, keywords
, options
) {
94 var found
= [], start
= token
.string
;
95 function maybeAdd(str
) {
96 if (str
.indexOf(start
) == 0 && !arrayContains(found
, str
)) found
.push(str
);
98 function gatherCompletions(obj
) {
99 if (typeof obj
== "string") forEach(stringProps
, maybeAdd
);
100 else if (obj
instanceof Array
) forEach(arrayProps
, maybeAdd
);
101 else if (obj
instanceof Function
) forEach(funcProps
, maybeAdd
);
102 for (var name
in obj
) maybeAdd(name
);
106 // If this is a property, see if it belongs to some object we can
107 // find in the current environment.
108 var obj
= context
.pop(), base
;
109 if (obj
.type
== "variable") {
110 if (options
&& options
.additionalContext
)
111 base
= options
.additionalContext
[obj
.string
];
112 base
= base
|| window
[obj
.string
];
113 } else if (obj
.type
== "string") {
115 } else if (obj
.type
== "atom") {
117 } else if (obj
.type
== "function") {
118 if (window
.jQuery
!= null && (obj
.string
== '$' || obj
.string
== 'jQuery') &&
119 (typeof window
.jQuery
== 'function'))
120 base
= window
.jQuery();
121 else if (window
._
!= null && (obj
.string
== '_') && (typeof window
._
== 'function'))
124 while (base
!= null && context
.length
)
125 base
= base
[context
.pop().string
];
126 if (base
!= null) gatherCompletions(base
);
129 // If not, just look in the window object and any local scope
130 // (reading into JS mode internals to get at the local variables)
131 for (var v
= token
.state
.localVars
; v
; v
= v
.next
) maybeAdd(v
.name
);
132 gatherCompletions(window
);
133 forEach(keywords
, maybeAdd
);