-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomplete.js
More file actions
553 lines (486 loc) · 19.8 KB
/
complete.js
File metadata and controls
553 lines (486 loc) · 19.8 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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
/**
* Cloud9 Language Foundation
*
* @copyright 2011, Ajax.org B.V.
* @license GPLv3 <http://www.gnu.org/licenses/gpl.txt>
*/
define(function(require, exports, module) {
/*global txtCompleter txtCompleterDoc txtCompleterHolder barCompleterCont
ceEditor sbCompleter*/
var ide = require("core/ide");
var editors = require("ext/editors/editors");
var dom = require("ace/lib/dom");
var keyhandler = require("ext/language/keyhandler");
var lang = require("ace/lib/lang");
var language;
var complete;
var oldCommandKey, oldOnTextInput;
var isDocShown;
var ID_REGEX = /[a-zA-Z_0-9\$\_]/;
var CLASS_SELECTED = "cc_complete_option selected";
var CLASS_UNSELECTED = "cc_complete_option";
var SHOW_DOC_DELAY = 1500;
var SHOW_DOC_DELAY_MOUSE_OVER = 100;
var HIDE_DOC_DELAY = 1000;
var AUTO_OPEN_DELAY = 200;
var AUTO_UPDATE_DELAY = 200;
var CONCORDE_DELAY = 70;
var CRASHED_COMPLETION_TIMEOUT = 6000;
var MENU_WIDTH = 330;
var MENU_SHOWN_ITEMS = 9;
var EXTRA_LINE_HEIGHT = 3;
var deferredInvoke = lang.deferredCall(function() {
var editor = editors.currentEditor.ceEditor.$editor;
var pos = editor.getCursorPosition();
var line = editor.getSession().getDocument().getLine(pos.row);
if (keyhandler.preceededByIdentifier(line, pos.column) ||
(line[pos.column - 1] === '.' && (!line[pos.column] || !line[pos.column].match(ID_REGEX))) ||
keyhandler.isRequireJSCall(line, pos.column)) {
module.exports.invoke(true);
}
else {
module.exports.closeCompletionBox();
}
isInvokeScheduled = false;
});
var isInvokeScheduled = false;
var ignoreMouseOnce = false;
var drawDocInvoke = lang.deferredCall(function() {
if (isPopupVisible() && complete.matches[complete.selectedIdx].doc) {
isDocShown = true;
txtCompleterDoc.parentNode.show();
}
isDrawDocInvokeScheduled = false;
});
var isDrawDocInvokeScheduled = false;
var undrawDocInvoke = lang.deferredCall(function() {
if (!isPopupVisible()) {
isDocShown = false;
txtCompleterDoc.parentNode.hide();
}
});
var killCrashedCompletionInvoke = lang.deferredCall(function() {
complete.closeCompletionBox();
});
function isPopupVisible() {
return barCompleterCont.$ext.style.display !== "none";
}
function retrievePreceedingIdentifier(text, pos) {
var buf = [];
for(var i = pos-1; i >= 0; i--) {
if(ID_REGEX.test(text[i]))
buf.push(text[i]);
else
break;
}
return buf.reverse().join("");
}
function retrieveFollowingIdentifier(text, pos) {
var buf = [];
for (var i = pos; i < text.length; i++) {
if (ID_REGEX.test(text[i]))
buf.push(text[i]);
else
break;
}
return buf;
}
function isJavaScript() {
return editors.currentEditor.amlEditor.syntax === "javascript";
}
/**
* Replaces the preceeding identifier (`prefix`) with `newText`, where ^^
* indicates the cursor position after the replacement.
* If the prefix is already followed by an identifier substring, that string
* is deleted.
*/
function replaceText(editor, prefix, match) {
// Replace text asynchronously in case Concorde didn't update the editor yet
setTimeout(function() {
asyncReplaceText(editor, prefix, match);
}, CONCORDE_DELAY);
}
function asyncReplaceText(editor, prefix, match) {
var newText = match.replaceText;
var pos = editor.getCursorPosition();
var line = editor.getSession().getLine(pos.row);
var doc = editor.getSession().getDocument();
if (match.replaceText === "require(^^)" && isJavaScript()) {
newText = "require(\"^^\")";
if (!isInvokeScheduled)
setTimeout(deferredInvoke, AUTO_OPEN_DELAY);
isInvokeScheduled = true;
}
// Ensure cursor marker
if (newText.indexOf("^^") === -1)
newText += "^^";
// Find prefix whitespace of current line
for (var i = 0; i < line.length && (line[i] === ' ' || line[i] === "\t");)
i++;
var prefixWhitespace = line.substring(0, i);
var postfix = retrieveFollowingIdentifier(line, pos.column) || "";
// Pad the text to be inserted
var paddedLines = newText.split("\n").join("\n" + prefixWhitespace);
var splitPaddedLines = paddedLines.split("\n");
var colOffset;
for (var rowOffset = 0; rowOffset < splitPaddedLines.length; rowOffset++) {
colOffset = splitPaddedLines[rowOffset].indexOf("^^");
if (colOffset !== -1)
break;
}
// Remove cursor marker
paddedLines = paddedLines.replace("^^", "");
doc.removeInLine(pos.row, pos.column - prefix.length, pos.column + postfix.length);
doc.insert({row: pos.row, column: pos.column - prefix.length}, paddedLines);
var cursorCol = pos.column + colOffset - prefix.length;
if (line.substring(0, pos.column).match(/require\("[^\"]+$/) && isJavaScript()) {
if (line.substr(pos.column + postfix.length, 1).match(/['"]/) || paddedLines.substr(0, 1) === '"')
cursorCol++;
if (line.substr(pos.column + postfix.length + 1, 1) === ')')
cursorCol++;
}
var cursorPos = { row: pos.row + rowOffset, column: cursorCol };
editor.selection.setSelectionRange({ start: cursorPos, end: cursorPos });
}
var menus = require("ext/menus/menus");
var commands = require("ext/commands/commands");
module.exports = {
hook: function(ext, worker) {
var _self = complete = this;
language = ext;
worker.on("complete", function(event) {
if(ext.disabled) return;
_self.onComplete(event);
});
this.$onChange = this.onChange.bind(this);
ext.nodes.push(
menus.addItemByPath("Edit/~", new apf.divider(), 2000),
menus.addItemByPath("Edit/Show Autocomplete", new apf.item({
command : "complete"
}), 2100)
);
commands.addCommand({
name: "complete",
hint: "code complete",
bindKey: {mac: "Ctrl-Space|Alt-Space", win: "Ctrl-Space|Alt-Space"},
isAvailable : function(editor){
return apf.activeElement.localName == "codeeditor";
},
exec: function(editor) {
_self.invoke();
}
});
this.ext = ext;
},
showCompletionBox: function(matches, prefix) {
var _self = this;
this.editor = editors.currentEditor;
var ace = this.editor.amlEditor.$editor;
this.selectedIdx = 0;
this.scrollIdx = 0;
this.matchEls = [];
this.prefix = prefix;
this.matches = matches;
this.completionElement = txtCompleter.$ext;
this.docElement = txtCompleterDoc.$ext;
this.cursorConfig = ace.renderer.$cursorLayer.config;
this.lineHeight = this.cursorConfig.lineHeight + EXTRA_LINE_HEIGHT;
var style = dom.computedStyle(this.editor.amlEditor.$ext);
this.completionElement.style.fontSize = style.fontSize;
barCompleterCont.setAttribute('visible', true);
// Monkey patch
if(!oldCommandKey) {
oldCommandKey = ace.keyBinding.onCommandKey;
ace.keyBinding.onCommandKey = this.onKeyPress.bind(this);
oldOnTextInput = ace.keyBinding.onTextInput;
ace.keyBinding.onTextInput = this.onTextInput.bind(this);
}
this.populateCompletionBox(matches);
document.addEventListener("click", this.closeCompletionBox);
ace.container.addEventListener("DOMMouseScroll", this.closeCompletionBox);
ace.container.addEventListener("mousewheel", this.closeCompletionBox);
apf.popup.setContent("completionBox", barCompleterCont.$ext);
var boxLength = this.matches.length || 1;
var completionBoxHeight = 11 + Math.min(10 * this.lineHeight, boxLength * (this.lineHeight));
var cursorLayer = ace.renderer.$cursorLayer;
var innerBoxLength = this.matches.length || 1;
var innerCompletionBoxHeight = Math.min(10 * this.lineHeight, innerBoxLength * (this.lineHeight));
txtCompleterHolder.$ext.style.height = innerCompletionBoxHeight + "px";
ignoreMouseOnce = !isPopupVisible();
apf.popup.show("completionBox", {
x : (prefix.length * -_self.cursorConfig.characterWidth) - 11,
y : _self.cursorConfig.lineHeight,
height : completionBoxHeight,
width : MENU_WIDTH,
animate : false,
ref : cursorLayer.cursor,
callback : function() {
barCompleterCont.setHeight(completionBoxHeight);
barCompleterCont.$ext.style.height = completionBoxHeight + "px";
sbCompleter.$resize();
// HACK: Need to set with non-falsy value first
_self.completionElement.scrollTop = 1;
_self.completionElement.scrollTop = 0;
}
});
},
closeCompletionBox : function(event) {
barCompleterCont.$ext.style.display = "none";
if (!editors.currentEditor.amlEditor) // no editor, try again later
return;
var ace = editors.currentEditor.amlEditor.$editor;
document.removeEventListener("click", this.closeCompletionBox);
ace.container.removeEventListener("DOMMouseScroll", this.closeCompletionBox);
ace.container.removeEventListener("mousewheel", this.closeCompletionBox);
if(oldCommandKey) {
ace.keyBinding.onCommandKey = oldCommandKey;
ace.keyBinding.onTextInput = oldOnTextInput;
}
oldCommandKey = oldOnTextInput = null;
undrawDocInvoke.schedule(HIDE_DOC_DELAY);
},
populateCompletionBox: function (matches) {
var _self = this;
_self.completionElement.innerHTML = "";
var cursorConfig = ceEditor.$editor.renderer.$cursorLayer.config;
var hasIcons = false;
matches.forEach(function(match) {
if (match.icon)
hasIcons = true;
});
var isInferAvailable = language.isInferAvailable();
matches.forEach(function(match, idx) {
var matchEl = dom.createElement("div");
matchEl.className = idx === _self.selectedIdx ? CLASS_SELECTED : CLASS_UNSELECTED;
var html = "";
if (match.icon)
html = "<img src='" + ide.staticPrefix + "/ext/language/img/" + match.icon + ".png'/>";
var docHead;
if (match.type) {
var shortType = _self.$guidToShortString(match.type)
if (shortType) {
match.meta = shortType;
docHead = match.name + " : " + _self.$guidToLongString(match.type) + "</div>";
}
}
var trim = match.meta ? " maintrim" : "";
if (!isInferAvailable || match.icon) {
html += '<span class="main' + trim + '"><u>' + _self.prefix + "</u>" + match.name.substring(_self.prefix.length) + '</span>';
}
else if (hasIcons) {
html += '<span class="main' + trim + '"><span class="deferred">' + match.name + '</span></span>';
}
else {
html += '<span class="main' + trim + '"><span class="deferred"><u>' + _self.prefix + "</u>" + match.name.substring(_self.prefix.length) + '</span></span>';
}
if (match.meta)
html += '<span class="meta">' + match.meta + '</span>';
if (match.doc)
match.doc = '<p>' + match.doc + '</p>';
if (match.icon || match.type)
match.doc = '<div class="code_complete_doc_head">' + (docHead || match.name) + '</div>' + (match.doc || "")
matchEl.innerHTML = html;
matchEl.addEventListener("mouseover", function() {
if (ignoreMouseOnce) {
ignoreMouseOnce = false;
return;
}
_self.matchEls[_self.selectedIdx].className = CLASS_UNSELECTED;
_self.selectedIdx = idx;
_self.matchEls[_self.selectedIdx].className = CLASS_SELECTED;
_self.updateDoc();
if (!isDrawDocInvokeScheduled)
drawDocInvoke.schedule(SHOW_DOC_DELAY_MOUSE_OVER);
});
matchEl.addEventListener("click", function() {
var amlEditor = editors.currentEditor.amlEditor;
replaceText(amlEditor.$editor, _self.prefix, match);
amlEditor.focus();
});
matchEl.style.height = cursorConfig.lineHeight + EXTRA_LINE_HEIGHT + "px";
matchEl.style.width = (MENU_WIDTH - 10) + "px";
_self.completionElement.appendChild(matchEl);
_self.matchEls.push(matchEl);
});
_self.updateDoc(true);
},
$guidToShortString : function(guid) {
var result = guid && guid.replace(/^[^:]+:(([^\/]+)\/)*?([^\/]*?)(\[\d+[^\]]*\])?(\/prototype)?$|.*/, "$3");
return result && result !== "Object" ? result : "";
},
$guidToLongString : function(guid, name) {
if (guid.substr(0, 6) === "local:")
return this.$guidToShortString(guid);
var result = guid && guid.replace(/^[^:]+:(([^\/]+\/)*)*?([^\/]*?)$|.*/, "$1$3");
if (!result || result === "Object")
return "";
result = result.replace(/\//g, ".").replace(/\[\d+[^\]]*\]/g, "");
if (name !== "prototype")
result = result.replace(/\.prototype$/, "");
return result;
},
updateDoc : function(delayPopup) {
this.docElement.innerHTML = '<span class="code_complete_doc_body">';
var selected = this.matches[this.selectedIdx];
if (selected && selected.doc) {
if (isDocShown) {
txtCompleterDoc.parentNode.show();
}
else {
txtCompleterDoc.parentNode.hide();
if (!isDrawDocInvokeScheduled || delayPopup)
drawDocInvoke.schedule(SHOW_DOC_DELAY);
}
this.docElement.innerHTML += selected.doc + '</span>';
}
else {
txtCompleterDoc.parentNode.hide();
}
if (selected && selected.docUrl)
this.docElement.innerHTML += '<p><a href="' + selected.docUrl + '" target="c9doc">(more)</a></p>';
this.docElement.innerHTML += '</span>';
},
onTextInput : function(text, pasted) {
var keyBinding = editors.currentEditor.ceEditor.$editor.keyBinding;
oldOnTextInput.apply(keyBinding, arguments);
if (!pasted) {
if (!text.match(ID_REGEX))
this.closeCompletionBox();
else
this.deferredInvoke();
}
},
onKeyPress : function(e, hashKey, keyCode) {
var _self = this;
if(e.metaKey || e.ctrlKey || e.altKey) {
this.closeCompletionBox();
return;
}
var keyBinding = editors.currentEditor.amlEditor.$editor.keyBinding;
switch(keyCode) {
case 0: break;
case 32: // Space
this.closeCompletionBox();
break;
case 27: // Esc
this.closeCompletionBox();
e.preventDefault();
break;
case 8: // Backspace
oldCommandKey.apply(keyBinding, arguments);
deferredInvoke();
e.preventDefault();
break;
case 37:
case 39:
oldCommandKey.apply(keyBinding, arguments);
this.closeCompletionBox();
e.preventDefault();
break;
case 13: // Enter
case 9: // Tab
var editor = editors.currentEditor.amlEditor.$editor;
replaceText(editor, this.prefix, this.matches[this.selectedIdx]);
this.closeCompletionBox();
e.preventDefault();
break;
case 40: // Down
if (this.matchEls.length === 1) {
this.closeCompletionBox();
break;
}
e.stopPropagation();
e.preventDefault();
this.matchEls[this.selectedIdx].className = CLASS_UNSELECTED;
if(this.selectedIdx < this.matches.length-1)
this.selectedIdx++;
this.matchEls[this.selectedIdx].className = CLASS_SELECTED;
if(this.selectedIdx - this.scrollIdx > MENU_SHOWN_ITEMS) {
this.scrollIdx++;
this.matchEls[this.scrollIdx].scrollIntoView(true);
}
this.updateDoc();
break;
case 38: // Up
if (this.matchEls.length === 1) {
this.closeCompletionBox();
break;
}
e.stopPropagation();
e.preventDefault();
if (this.selectedIdx <= 0)
return;
this.matchEls[this.selectedIdx].className = CLASS_UNSELECTED;
this.selectedIdx--;
this.matchEls[this.selectedIdx].className = CLASS_SELECTED;
if(this.selectedIdx < this.scrollIdx) {
this.scrollIdx--;
this.matchEls[this.scrollIdx].scrollIntoView(true);
}
this.updateDoc();
break;
}
},
setWorker: function(worker) {
this.worker = worker;
},
deferredInvoke: function() {
if (isInvokeScheduled)
return;
isInvokeScheduled = true;
deferredInvoke.schedule(isPopupVisible() ? AUTO_UPDATE_DELAY : AUTO_OPEN_DELAY);
},
onChange: function() {
this.deferredInvoke();
},
invoke: function(forceBox) {
var editor = editors.currentEditor.amlEditor.$editor;
if (editor.inMultiSelectMode) {
_self.closeCompletionBox();
return;
}
this.forceBox = forceBox;
editor.addEventListener("change", this.$onChange);
// This is required to ensure the updated document text has been sent to the worker before the 'complete' message
var worker = this.worker;
setTimeout(function() {
worker.emit("complete", {data: { pos: editor.getCursorPosition(), staticPrefix: ide.staticPrefix}});
});
var _self = this;
if(forceBox)
killCrashedCompletionInvoke(CRASHED_COMPLETION_TIMEOUT);
},
onComplete: function(event) {
var editor = editors.currentEditor.amlEditor.$editor;
var pos = editor.getCursorPosition();
var eventPos = event.data.pos;
var line = editor.getSession().getLine(pos.row);
var identifier = retrievePreceedingIdentifier(line, pos.column);
editor.removeEventListener("change", this.$onChange);
killCrashedCompletionInvoke.cancel();
if (pos.column !== eventPos.column || pos.row !== eventPos.row)
return;
var matches = event.data.matches;
// Remove out-of-date matches
for (var i = 0; i < matches.length; i++) {
if(matches[i].name.indexOf(identifier) !== 0) {
matches.splice(i, 1);
i--;
}
}
if (matches.length === 1 && !this.forceBox) {
replaceText(editor, identifier, matches[0]);
}
else if (matches.length > 0) {
this.showCompletionBox(matches, identifier);
}
else {
if(typeof barCompleterCont !== 'undefined')
this.closeCompletionBox();
}
},
destroy : function(){
commands.removeCommandByName("complete");
}
};
});