aboutsummaryrefslogtreecommitdiff
path: root/imports/codemirror/lib/util/pig-hint.js
diff options
context:
space:
mode:
authorAnanya Sen2012-07-17 14:25:53 -0700
committerAnanya Sen2012-07-17 14:25:53 -0700
commit09457e39532e9b35afb9c635266755bfcb9b488a (patch)
tree665dcbd41377b98dcab15ff995e9434d9db1a462 /imports/codemirror/lib/util/pig-hint.js
parentf2dbca782bbaca3bed96dff808693693ba083ea9 (diff)
parent4f737b24c19ddc02d20f9783b8b080fc6ef11142 (diff)
downloadninja-09457e39532e9b35afb9c635266755bfcb9b488a.tar.gz
Merge branch 'refs/heads/ninja-internal-master' into copy-paste-bugfix
Diffstat (limited to 'imports/codemirror/lib/util/pig-hint.js')
-rw-r--r--imports/codemirror/lib/util/pig-hint.js123
1 files changed, 123 insertions, 0 deletions
diff --git a/imports/codemirror/lib/util/pig-hint.js b/imports/codemirror/lib/util/pig-hint.js
new file mode 100644
index 00000000..233b72bf
--- /dev/null
+++ b/imports/codemirror/lib/util/pig-hint.js
@@ -0,0 +1,123 @@
1(function () {
2 function forEach(arr, f) {
3 for (var i = 0, e = arr.length; i < e; ++i) f(arr[i]);
4 }
5
6 function arrayContains(arr, item) {
7 if (!Array.prototype.indexOf) {
8 var i = arr.length;
9 while (i--) {
10 if (arr[i] === item) {
11 return true;
12 }
13 }
14 return false;
15 }
16 return arr.indexOf(item) != -1;
17 }
18
19 function scriptHint(editor, keywords, getToken) {
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
24 if (!/^[\w$_]*$/.test(token.string)) {
25 token = tprop = {start: cur.ch, end: cur.ch, string: "", state: token.state,
26 className: token.string == ":" ? "pig-type" : null};
27 }
28
29 if (!context) var context = [];
30 context.push(tprop);
31
32 completionList = getCompletions(token, context);
33 completionList = completionList.sort();
34 //prevent autocomplete for last word, instead show dropdown with one word
35 if(completionList.length == 1) {
36 completionList.push(" ");
37 }
38
39 return {list: completionList,
40 from: {line: cur.line, ch: token.start},
41 to: {line: cur.line, ch: token.end}};
42 }
43
44 CodeMirror.pigHint = function(editor) {
45 return scriptHint(editor, pigKeywordsU, function (e, cur) {return e.getTokenAt(cur);});
46 }
47
48 function toTitleCase(str) {
49 return str.replace(/(?:^|\s)\w/g, function(match) {
50 return match.toUpperCase();
51 });
52 }
53
54 var pigKeywords = "VOID IMPORT RETURNS DEFINE LOAD FILTER FOREACH ORDER CUBE DISTINCT COGROUP "
55 + "JOIN CROSS UNION SPLIT INTO IF OTHERWISE ALL AS BY USING INNER OUTER ONSCHEMA PARALLEL "
56 + "PARTITION GROUP AND OR NOT GENERATE FLATTEN ASC DESC IS STREAM THROUGH STORE MAPREDUCE "
57 + "SHIP CACHE INPUT OUTPUT STDERROR STDIN STDOUT LIMIT SAMPLE LEFT RIGHT FULL EQ GT LT GTE LTE "
58 + "NEQ MATCHES TRUE FALSE";
59 var pigKeywordsU = pigKeywords.split(" ");
60 var pigKeywordsL = pigKeywords.toLowerCase().split(" ");
61
62 var pigTypes = "BOOLEAN INT LONG FLOAT DOUBLE CHARARRAY BYTEARRAY BAG TUPLE MAP";
63 var pigTypesU = pigTypes.split(" ");
64 var pigTypesL = pigTypes.toLowerCase().split(" ");
65
66 var pigBuiltins = "ABS ACOS ARITY ASIN ATAN AVG BAGSIZE BINSTORAGE BLOOM BUILDBLOOM CBRT CEIL "
67 + "CONCAT COR COS COSH COUNT COUNT_STAR COV CONSTANTSIZE CUBEDIMENSIONS DIFF DISTINCT DOUBLEABS "
68 + "DOUBLEAVG DOUBLEBASE DOUBLEMAX DOUBLEMIN DOUBLEROUND DOUBLESUM EXP FLOOR FLOATABS FLOATAVG "
69 + "FLOATMAX FLOATMIN FLOATROUND FLOATSUM GENERICINVOKER INDEXOF INTABS INTAVG INTMAX INTMIN "
70 + "INTSUM INVOKEFORDOUBLE INVOKEFORFLOAT INVOKEFORINT INVOKEFORLONG INVOKEFORSTRING INVOKER "
71 + "ISEMPTY JSONLOADER JSONMETADATA JSONSTORAGE LAST_INDEX_OF LCFIRST LOG LOG10 LOWER LONGABS "
72 + "LONGAVG LONGMAX LONGMIN LONGSUM MAX MIN MAPSIZE MONITOREDUDF NONDETERMINISTIC OUTPUTSCHEMA "
73 + "PIGSTORAGE PIGSTREAMING RANDOM REGEX_EXTRACT REGEX_EXTRACT_ALL REPLACE ROUND SIN SINH SIZE "
74 + "SQRT STRSPLIT SUBSTRING SUM STRINGCONCAT STRINGMAX STRINGMIN STRINGSIZE TAN TANH TOBAG "
75 + "TOKENIZE TOMAP TOP TOTUPLE TRIM TEXTLOADER TUPLESIZE UCFIRST UPPER UTF8STORAGECONVERTER";
76 var pigBuiltinsU = pigBuiltins.split(" ").join("() ").split(" ");
77 var pigBuiltinsL = pigBuiltins.toLowerCase().split(" ").join("() ").split(" ");
78 var pigBuiltinsC = ("BagSize BinStorage Bloom BuildBloom ConstantSize CubeDimensions DoubleAbs "
79 + "DoubleAvg DoubleBase DoubleMax DoubleMin DoubleRound DoubleSum FloatAbs FloatAvg FloatMax "
80 + "FloatMin FloatRound FloatSum GenericInvoker IntAbs IntAvg IntMax IntMin IntSum "
81 + "InvokeForDouble InvokeForFloat InvokeForInt InvokeForLong InvokeForString Invoker "
82 + "IsEmpty JsonLoader JsonMetadata JsonStorage LongAbs LongAvg LongMax LongMin LongSum MapSize "
83 + "MonitoredUDF Nondeterministic OutputSchema PigStorage PigStreaming StringConcat StringMax "
84 + "StringMin StringSize TextLoader TupleSize Utf8StorageConverter").split(" ").join("() ").split(" ");
85
86 function getCompletions(token, context) {
87 var found = [], start = token.string;
88 function maybeAdd(str) {
89 if (str.indexOf(start) == 0 && !arrayContains(found, str)) found.push(str);
90 }
91
92 function gatherCompletions(obj) {
93 if(obj == ":") {
94 forEach(pigTypesL, maybeAdd);
95 }
96 else {
97 forEach(pigBuiltinsU, maybeAdd);
98 forEach(pigBuiltinsL, maybeAdd);
99 forEach(pigBuiltinsC, maybeAdd);
100 forEach(pigTypesU, maybeAdd);
101 forEach(pigTypesL, maybeAdd);
102 forEach(pigKeywordsU, maybeAdd);
103 forEach(pigKeywordsL, maybeAdd);
104 }
105 }
106
107 if (context) {
108 // If this is a property, see if it belongs to some object we can
109 // find in the current environment.
110 var obj = context.pop(), base;
111
112 if (obj.className == "pig-word")
113 base = obj.string;
114 else if(obj.className == "pig-type")
115 base = ":" + obj.string;
116
117 while (base != null && context.length)
118 base = base[context.pop().string];
119 if (base != null) gatherCompletions(base);
120 }
121 return found;
122 }
123})();