-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathjQuery.CodeComplete.js
More file actions
346 lines (289 loc) · 9.61 KB
/
jQuery.CodeComplete.js
File metadata and controls
346 lines (289 loc) · 9.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/*
model: {
keywords: <Array of key/value pairs of type String> ex. [ { name: 'if', type: 'syntax' }, { name: 'else', type: 'syntax' }, { name: 'firstname', type: 'string' } ],
dataTypes: <Array of key/value pairs> ex. [ { type: 'string', methods: [ 'tostring', 'toupper', 'tolower' ] }, { type: 'int', methods: [ 'tostring' ] } ]
}
keywords do not need a value for type as it will be checked against datatypes property when a period is pressed.
*/
function populateSelect(items, codeTextArea, withFocus) {
var select = jQuery("<select id='matches'></select>").css("position", "absolute").appendTo(jQuery("body"));
positionSelectObject(select, codeTextArea);
jQuery.each(items, function () {
select.get(0).options[select.get(0).options.length] = new Option(this, this);
});
select.attr('size', 3).bind('keyup', function (e) {
if (e.keyCode == 13 || e.keyCode == 32) {
commitSelectOptionToTextElement($(this), codeTextArea);
}
});
select.bind("change", function() {
// TODO need to allow selection for non-datatypes
//commitSelectOptionToTextElement($(this), codeTextArea);
});
select.bind("keydown", function(e) {
// Stop backspace from going to previous page in browser history
if (e.keyCode === 8) { e.preventDefault(); }
});
if (withFocus) {
select.focus();
}
}
function positionSelectObject(select, codeTextArea) {
var range;
var left;
var top;
if (codeTextArea.get(0).createTextRange) {
range = document.selection.createRange();
var rect = range.getClientRects()[range.getClientRects().length - 1];
top = rect.bottom;
left = rect.left;
}
else {
range = window.getSelection();
left = codeTextArea.offset().left + codeTextArea.width();
top = codeTextArea.offset().top;
}
log('Left: ' + left + ' - Top:' + top);
select.offset({
top: top,
left: left
});
}
function commitSelectOptionToTextElement(self, codeTextArea) {
self.remove();
codeTextArea.appendText(self.val()).focus();
selectText(codeTextArea, getCaret(codeTextArea) + self.val().length, getCaret(codeTextArea) + self.val().length); // puts cursor at the end of the selected value
}
function selectText(element, start, end) {
"use strict";
if (element[0].createTextRange) {
var selRange = element[0].createTextRange();
selRange.collapse(true);
selRange.moveStart('character', start);
selRange.moveEnd('character', end);
selRange.select();
} else if (element[0].setSelectionRange) {
element[0].setSelectionRange(start, end);
} else if (element[0].selectionStart) {
element[0].selectionStart = start;
element[0].selectionEnd = end;
}
element.focus();
}
function log(message) {
"use strict";
window.console && console.log && console.log(message);
}
function replaceTextAndHighlight(self, lastWord, newWord) {
self.appendText(newWord.replace(lastWord, ''));
var start = getCaret(self) - (newWord.length - lastWord.length); // Set start of the highlight
var end = start + (newWord.length - lastWord.length); // Set end of the highlight
selectText(self, start, end); // Set the selected text to the remainder of the letters from what was actually input
return self;
}
function evaluateTypes(self, lastWord, model) {
var matches = [];
jQuery.each(model.keywords, function() {
var keyword = this;
if (typeof keyword.name === 'function') { return; }
if (keyword.name.charAt(0) !== lastWord.charAt(0)) { return; }
var pattern = new RegExp('^' + lastWord);
var isMatch = pattern.test(keyword.name);
if (isMatch) {
// Checks the datatypes for match and then adds the methods to the matches array
jQuery.each(model.dataTypes, function() {
var dataType = this;
var isDataTypeMatch = keyword.type === dataType.type;
if (isDataTypeMatch) {
jQuery.each(dataType.methods, function() {
var method = this;
if (typeof method === 'function') { return; }
var tempObj = new Object();
var i = matches.length;
while(i--)
{
tempObj[matches[i]]='';
}
if (!(method in tempObj)) { matches.push(method); }
});
}
});
}
});
if (matches.length > 0) {
populateSelect(matches, self, true);
}
return matches;
}
function evaluateKeywords(self, lastWord, model) {
var keywords = [];
jQuery.each(model.keywords, function() {
if (this.name.charAt(0) !== lastWord.charAt(0)) { return; }
keywords.push(this.name);
});
var matches = autoComplete(self, lastWord, keywords);
if (matches.length > 1) {
// TODO
populateSelect(matches, self, false);
}
return self;
}
function autoComplete(self, wordToMatch, keywords) {
var matches = new Array();
jQuery.each(keywords, function() {
if (typeof this === 'function') { return; }
var pattern = new RegExp('^' + wordToMatch);
var isMatch = pattern.test(this);
if (isMatch) {
matches.push(this);
}
});
if (matches.length > 0) {
replaceTextAndHighlight(self, wordToMatch, matches[0]);
}
return matches;
}
function executeCodeCompletion(self, model, forDataTypes) {
var lastWord = getWord(self)
// Doesn't evaluate keywords on a space
if (lastWord.length === 0) { return self; }
if (!forDataTypes) {
// Evaluate model.keywords
evaluateKeywords(self, lastWord, model);
}
else {
evaluateTypes(self, lastWord, model);
}
return self;
}
function getWord(elem) {
var location = getCaret(elem);
// If the next letter after the caret is alphanumeric, do not return the word so that autocomplete doesn't fire.
var nextLetter = elem.val().charAt(location + 1);
if (/[a-zA-Z0-9]/.test(nextLetter)) { return ''; }
// letters will be reversed
var temp = "";
var i = location;
while (i--) {
var val = elem.val();
var letter = (val.length >= 0) ? val.charAt(i) : 0;
if (letter.charCodeAt(0) === 32 || letter.charAt(0) === "\n" || letter.charAt(0) === "(") {
break;
}
if (letter.charAt(0) === "." || letter.charAt(0) === ")") { continue; }
temp += letter;
}
// reverse the letters
var word = reverseText(temp);
return word;
}
function getCaret(elem) {
var el = $(elem).get(0);
if (el.selectionStart) {
return el.selectionStart;
}
else if (document.selection) {
// Set focus on the element
el.focus ();
// To get cursor position, get empty selection range
var oSel = document.selection.createRange ();
// Move selection start to 0 position
oSel.moveStart ('character', -el.value.length);
// The caret position is selection length
return oSel.text.length;
}
return 0;
}
function reverseText(text) {
var word = "";
var i = text.length;
while (i--) {
word += text[i];
}
return word;
}
function insertAtCursor(myField, myValue) {
//IE support
if (document.selection) {
myField.focus();
sel = document.selection.createRange();
sel.text = myValue;
}
//MOZILLA/NETSCAPE support
else if (myField.selectionStart || myField.selectionStart == '0') {
var startPos = myField.selectionStart;
var endPos = myField.selectionEnd;
myField.value = myField.value.substring(0, startPos)
+ myValue
+ myField.value.substring(endPos, myField.value.length);
} else {
myField.value += myValue;
}
}
(function($) {
$.fn.codeComplete = function (model) {
model = $.extend({
//keywords: [ { name: 'if', type: 'syntax' }, { name: 'else', type: 'syntax' }, { name: 'firstname', type: 'string' }, { name: 'age', type: 'int32' } ],
//dataTypes: [ { type: 'string', methods: [ 'tostring', 'toupper', 'tolower' ] }, { type: 'int32', methods: [ 'tostring' ] } ]
}, model);
// Sort all the keyword names ascending
model.keywords.sort(function(a, b) {
if (a.name === b.name) { return 0; }
return (a.name < b.name) ? -1 : 1;
});
this.bind("keyup", function(e) {
var self = $(this);
log('Key ANSII Code: ' + e.keyCode);
switch (e.keyCode) {
case 8: // Backspace
case 13: // Enter
case 37: // Left Arrow
case 38: // Top Arrow
case 39: // Right Arrow
case 40: // Bottom Arrow
case 16: // SHIFT
case 17: // ALT
case 18: // CTRL
e.preventDefault();
break;
case 190: // Period
executeCodeCompletion(self, model, true);
break;
default:
executeCodeCompletion(self, model);
}
});
this.bind('keydown', function(e) {
var self = $(this);
// Removes dropdown if present
jQuery("#matches").remove();
switch (e.keyCode) {
case 13: // Enter
case 32: // Spacebar
case 190: // Period
self.moveToEndOfSelectedText();
break;
}
});
return this;
};
$.fn.appendText = function(text) {
insertAtCursor(this.get(0), text);
return this;
};
$.fn.valLength = function() {
return this.val().length;
};
$.fn.moveToEndOfSelectedText = function() {
var el = $(this).get(0);
if (el.selectionEnd) { // Chrome
el.selectionStart = el.selectionEnd;
}
else if (document.selection) { // IE
var range = document.selection.createRange();
range.collapse(false);
range.select();
}
return this;
};
})(jQuery);