This repository has been archived by the owner on Apr 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
init.js
executable file
·1141 lines (1063 loc) · 43 KB
/
init.js
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
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) Codiad & Andr3as, distributed
* as-is and without warranty under the MIT License.
* See [root]/license.md for more information. This information must remain intact.
*/
(function(global, $){
var codiad = global.codiad,
scripts = document.getElementsByTagName('script'),
path = scripts[scripts.length-1].src.split('?')[0],
curpath = path.split('/').slice(0, -1).join('/')+'/';
var EventEmitter = ace.require('ace/lib/event_emitter').EventEmitter;
var Range = ace.require('ace/range').Range;
$(function() {
codiad.Complete.init();
});
codiad.Complete = {
path : curpath,
comAdded : false,
isVisible : false,
prefix : "",
api : 1.1,
wordRegex : /[^a-zA-Z_0-9\$\-]+/,
extensions : {}, //Autocomplete extensions
snippetManager: null,
//Caches
suggestionCache : null, //Cache displayed suggestions
suggestionTextCache : null, //Cache suggestions of the text
pluginInitCache : null, //Cache suggestions of plugins at start of init
pluginCache : null, //Cache suggestions of plugins on each change
pluginSessionCache : null, //Cache suggestions of plugins at start of session
pluginMajCache : null, //Cache suggestions of plugins to display them matching or not
//Default commands
defaultEscExec : null,
defaultUpExec : null,
defaultDownExec : null,
defaultLeftExec : null,
defaultRightExec : null,
defaultEnterExec : null,
defaultIndentExec : null,
init: function() {
var _this = this;
//Load library
$.getScript(_this.path + 'Snippets.js').complete(function(){
var id = setInterval(function(){
if (typeof(ace.require) != 'undefined') {
if (typeof(ace.require("ace/snippets")) != 'undefined') {
if (typeof(ace.require("ace/snippets").snippetManager) != 'undefined') {
clearInterval(id);
_this.snippetManager = ace.require("ace/snippets").snippetManager;
}
}
}
},250);
});
//Load extensions
$.getJSON(this.path + 'controller.php?action=getExtensions', function(result) {
$.each(result.extensions, function(i, ext){
$.getScript(_this.path + 'extensions/' + ext);
});
});
this.$comUp = this.goUp.bind(this);
this.$comDown = this.goDown.bind(this);
this.$comLeft = this.goLeft.bind(this);
this.$comRight = this.goRight.bind(this);
this.$comReturn = this.enter.bind(this);
this.$comIndent = this.indent.bind(this);
this.$comComplete = this.complete.bind(this);
this.$onDocumentChange = this.onDocumentChange.bind(this);
//Overwrite existing click command
codiad.autocomplete.complete = this.$comComplete;
//Register complete command
amplify.subscribe('active.onOpen', function(path){
setTimeout(_this.setKeyBindings(), 50);
});
//Hide dialog on file close
amplify.subscribe('active.onClose', function(){
_this.hide();
});
amplify.subscribe('active.onRemoveAll', function(){
_this.hide();
});
//Get init data
setTimeout(function(){
//Publish plugin listener
amplify.publish("Complete.Init");
}, 1000);
},
//////////////////////////////////////////////////////////
//
// Set key bindings and publish Complete.Init
//
//////////////////////////////////////////////////////////
setKeyBindings: function() {
var _this = this;
if (codiad.editor.getActive() !== null) {
//Add initial keyboard command
var _commandManager = codiad.editor.getActive().commands;
_commandManager.addCommand({
name: 'CompletePlus',
bindKey: {
"win": "Ctrl-Space",
"mac": "Ctrl-Space"
},
exec: function () {
_this.suggest();
}
});
if (this.__onKeyUpEnabled()) {
$('.editor').keyup(function(e){
if (e.which >= 48 && e.which <= 90) {
_this.suggest();
}
});
}
}
},
//////////////////////////////////////////////////////////
//
// Add keyboard command to handle user interactions
//
//////////////////////////////////////////////////////////
addKeyboardCommands: function() {
if (codiad.editor.getActive() === null) {
return false;
}
var _this = this;
var _commandManager = codiad.editor.getActive().commands;
//Save default commands
this.defaultUpExec = _commandManager.commands.golineup.exec;
this.defaultDownExec = _commandManager.commands.golinedown.exec;
this.defaultLeftExec = _commandManager.commands.gotoleft.exec;
this.defaultRightExec = _commandManager.commands.gotoright.exec;
this.defaultIndentExec = _commandManager.commands.indent.exec;
//Register new commands
_commandManager.commands.golineup.exec = this.$comUp;
_commandManager.commands.golinedown.exec = this.$comDown;
_commandManager.commands.gotoleft.exec = this.$comLeft;
_commandManager.commands.gotoright.exec = this.$comRight;
_commandManager.commands.indent.exec = this.$comIndent;
//Overwrite multiselection rule
_commandManager.commands.golineup.multiSelectAction = "";
_commandManager.commands.golinedown.multiSelectAction = "";
_commandManager.commands.gotoleft.multiSelectAction = "";
_commandManager.commands.gotoright.multiSelectAction = "";
_commandManager.commands.indent.multiSelectAction = "";
_commandManager.addCommand({
name: 'hidecomplete',
bindKey: 'Esc',
exec: function () {
_this.hide();
}
});
_commandManager.addCommand({
name: 'ReturnToComplete',
bindKey: 'Return',
exec: this.$comReturn
});
var session = codiad.editor.getActive().getSession();
session.addEventListener('change', this.$onDocumentChange);
// handle click-out autoclosing.
var fn = function () {
_this.hide();
$(window).off('click', fn);
};
$(window).on('click', fn);
this.comAdded = true;
},
//////////////////////////////////////////////////////////
//
// Remove keyboard commands and restore default commands
//
//////////////////////////////////////////////////////////
removeKeyboardCommands: function() {
if (codiad.editor.getActive() === null) {
return false;
}
//Restore default commands
var _commandManager = codiad.editor.getActive().commands;
//Make sure default exits
if (this.defaultUpExec !== null) {
_commandManager.commands.golineup.exec = this.defaultUpExec;
}
if (this.defaultDownExec !== null) {
_commandManager.commands.golinedown.exec = this.defaultDownExec;
}
if (this.defaultLeftExec !== null) {
_commandManager.commands.gotoleft.exec = this.defaultLeftExec;
}
if (this.defaultRightExec !== null) {
_commandManager.commands.gotoright.exec = this.defaultRightExec;
}
if (this.defaultIndentExec !== null) {
_commandManager.commands.indent.exec = this.defaultIndentExec;
}
//Restore multiselection rule
_commandManager.commands.golineup.multiSelectAction = "forEach";
_commandManager.commands.golinedown.multiSelectAction = "forEach";
_commandManager.commands.gotoleft.multiSelectAction = "forEach";
_commandManager.commands.gotoright.multiSelectAction = "forEach";
_commandManager.commands.indent.multiSelectAction = "forEach";
_commandManager.removeCommand('hidecomplete');
_commandManager.removeCommand('ReturnToComplete');
var session = codiad.editor.getActive().getSession();
session.removeEventListener('change', this.$onDocumentChange);
this.comAdded = false;
},
//////////////////////////////////////////////////////////
//
// Suggest
//
// Parameter:
//
// position - (Optional) - {Object} - Position of the cursor
//
//////////////////////////////////////////////////////////
suggest: function(position) {
if (codiad.editor.getActive() === null) {
this.hide();
return false;
}
//Get prefix
if (typeof(position) == 'undefined') {
position = codiad.editor.getActive().getCursorPosition();
}
var token = codiad.editor.getActive().getSession().getTokenAt(position.row,position.column);
if (token === null) {
this.hide();
return false;
}
//Get text before cursor
var prefix = token.value.substr(0, position.column - token.start);
prefix = prefix.split(this.wordRegex).slice(-1)[0];
//Get the current line
var text = codiad.editor.getActive().getSession().getValue();
var line = text.split("\n")[position.row];
//Get content before prefix
var range = new Range(position.row,0,position.row,position.column);
var value = codiad.editor.getActive().getSession().getTextRange(range);
var syntax = $('#current-mode').text();
//Get current file
var file = codiad.editor.getActive().getSession().path;
var publishObj = {"prefix": prefix, "before": value, "syntax": syntax, "line": line, "token": token, "position": position, "file": file };
this.prefix = prefix;
this.suggestionTextCache = this.getTextSuggestions();
//Remove old plugin data
this.pluginMajCache = [];
this.pluginCache = [];
//Publish for Plugins
amplify.publish( "Complete.Normal", publishObj);
amplify.publish( "Complete.Major", publishObj);
//Suggestion for current session
if (!this.isVisible) {
amplify.publish( "Complete.Session", publishObj);
}
var matches, fuzzilies;
if (prefix !== "") {
matches = this.getMatches(prefix, this.suggestionTextCache);
fuzzilies = this.getFuzzilies(prefix, this.suggestionTextCache);
} else {
matches = [];
fuzzilies = [];
}
var pluginSugs = this.pluginInitCache;
if (pluginSugs === null || pluginSugs.length === 0) {
pluginSugs = this.pluginCache;
} else {
pluginSugs = this.push(pluginSugs, this.pluginCache);
}
//Add session suggestions
if (pluginSugs === null || pluginSugs.length === 0) {
pluginSugs = this.pluginSessionCache;
} else {
pluginSugs = this.push(pluginSugs, this.pluginSessionCache);
}
var pluginMatches = this.getMatches(prefix, pluginSugs);
var pluginFuzzilies = this.getFuzzilies(prefix, pluginSugs);
//Add type info
matches = this.addExtraInfo(matches, "type", "text");
fuzzilies = this.addExtraInfo(fuzzilies, "type", "text");
pluginMatches = this.addExtraInfo(pluginMatches, "type", "plugin");
pluginFuzzilies = this.addExtraInfo(pluginFuzzilies, "type", "plugin");
this.pluginMajCache = this.addExtraInfo(this.pluginMajCache, "type", "plugin");
//Remove double entries
fuzzilies = this.removeDoubleEntries(matches, fuzzilies);
pluginFuzzilies = this.removeDoubleEntries(pluginMatches, pluginFuzzilies);
//Remove entries in textArray, which are defined in pluginArray
matches = this.removeDoubleEntries(pluginMatches, matches);
fuzzilies = this.removeDoubleEntries(pluginFuzzilies, fuzzilies);
//Combine plugin und text matches
matches = this.push(matches, pluginMatches);
fuzzilies = this.push(fuzzilies, pluginFuzzilies);
//Remove empty arrays
matches = this.removeEmptyArrays(matches);
fuzzilies = this.removeEmptyArrays(fuzzilies);
//Rank suggestions
matches = this.rankSuggestions(prefix, matches);
fuzzilies = this.rankSuggestions(prefix, fuzzilies);
//Combine matches and fuzzilies
var pluginMaj = this.pluginMajCache;
var sugs = pluginMaj;
sugs = this.push(sugs, matches);
sugs = this.push(sugs, fuzzilies);
//Remove empty arrays
sugs = this.removeEmptyArrays(sugs);
if (sugs.length === 0) {
this.hide();
return false;
}
//Limit suggestions on 30
sugs = sugs.slice(0,30);
this.suggestionCache = sugs;
//Smart complete enabled?
if (sugs.length == 1 && this.__onSmartCompleteEnabled() && !this.__onKeyUpEnabled()) {
//Complete without dialog
this.complete(sugs[0]);
} else {
this.show();
}
return true;
},
/*
Array - [{
title - Title of the suggestions to display
suggestion - Suggestions to insert
ranking - Suggestion to rank
callback - "Amplify Topic" -> amplify.publish(callback, {Array of the Suggestion})
wenn callback == "" -> macht CompletePlus die ersetzung aufgrundlage von suggestion
},{...}]
*/
//////////////////////////////////////////////////////////
//
// Function for plugins to add their suggestions
//
// Parameter:
//
// sug - {Object/Array} - Suggestion(s) to add
//
//////////////////////////////////////////////////////////
pluginInit: function(sug) {
return this.pluginSuggest("pluginInitCache", sug);
},
pluginNormal: function(sug) {
return this.pluginSuggest("pluginCache", sug);
},
pluginSession: function(sug) {
return this.pluginSuggest("pluginSessionCache", sug);
},
pluginMajor: function(sug) {
return this.pluginSuggest("pluginMajCache", sug);
},
//////////////////////////////////////////////////////////
//
// Function to add suggestions of plugins
//
// PRIVATE - ! DO NOT USE !
//
// Parameter:
//
// sugArray - {String} - Array to add the suggestion
// sug - {Object/Array} - Suggestion(s) to add
//
//////////////////////////////////////////////////////////
pluginSuggest: function(sugArray, sug) {
if (typeof(sug) == 'undefined' || sug === null || sug.length === 0) {
return false;
}
if (this[sugArray] === null) {
this[sugArray] = [];
}
this[sugArray] = this.push(this[sugArray], sug);
return true;
},
//////////////////////////////////////////////////////////
//
// Complete the current active suggestin or publish it
// for a plugin
//
// Parameters:
//
// suggestion - {Object} - Object to complete current suggestion
//
//////////////////////////////////////////////////////////
complete: function(object) {
if (typeof(object) === 'undefined') {
var sug = this.extensions.phpjs.htmlspecialchars_decode($('.active-suggestion').attr("data-suggestion"));
object = this.getObjectOutArray(sug, this.suggestionCache);
}
this.hide();
if (object === false) {
return false;
}
if (object.type == "plugin") {
if (object.callback !== "") {
//Workaround for return command
setTimeout(function(){
amplify.publish(object.callback, object);
},0);
return true;
}
}
//Insert suggestion
this.replacePrefix(object.suggestion, object.isSnippet);
return true;
},
//////////////////////////////////////////////////////////
//
// Activate suggestion over the current active
//
//////////////////////////////////////////////////////////
goUp: function() {
var entries = $('.suggestion');
if (entries.length == 1) {
return false;
}
for (var i = 0; i < entries.length; i++) {
if ($(entries[i]).hasClass('active-suggestion')) {
if ($('.suggestion:first').hasClass('active-suggestion')) {
$('.suggestion:last').addClass('active-suggestion');
} else {
$(entries[i-1]).addClass('active-suggestion');
}
$(entries[i]).removeClass('active-suggestion');
//Scroll dialog
this._computeScrolling();
return true;
}
}
return false;
},
//////////////////////////////////////////////////////////
//
// Activate suggestion under the current active
//
//////////////////////////////////////////////////////////
goDown: function() {
var entries = $('.suggestion');
if (entries.length == 1) {
return false;
}
for (var i = 0; i < entries.length; i++) {
if ($(entries[i]).hasClass('active-suggestion')) {
if ($('.suggestion:last').hasClass('active-suggestion')) {
$('.suggestion:first').addClass('active-suggestion');
} else {
$(entries[i+1]).addClass('active-suggestion');
}
$(entries[i]).removeClass('active-suggestion');
//Scroll dialog
this._computeScrolling();
return true;
}
}
return false;
},
//////////////////////////////////////////////////////////
//
// Handle left key
//
//////////////////////////////////////////////////////////
goLeft: function() {
this.hide();
},
//////////////////////////////////////////////////////////
//
// Handle right key
//
//////////////////////////////////////////////////////////
goRight: function() {
this.complete();
},
//////////////////////////////////////////////////////////
//
// Handle tab key
//
//////////////////////////////////////////////////////////
indent: function() {
this.complete();
},
//////////////////////////////////////////////////////////
//
// Handle return key
//
//////////////////////////////////////////////////////////
enter: function() {
this.complete();
},
//////////////////////////////////////////////////////////
//
// Handle document change
//
//////////////////////////////////////////////////////////
onDocumentChange: function (e) {
var _this = this, text, action, range;
if (typeof(e.data) == 'undefined') {
text = e.text || e.lines[0] || "";
action = e.action;
if (typeof(e.range) == 'undefined') {
range = {start: e.start, end: e.end};
} else {
range = e.range;
}
} else {
text = e.data.text;
action = e.data.action;
range = e.data.range;
}
if (text.search(/^\s+$/) !== -1) {
this.hide();
return;
}
var position = null;
if (action === 'insertText' || action === 'insert') {
position = range.end;
} else if (action === 'removeText' || action == 'remove') {
position = range.start;
} else {
alert('Unkown document change action.');
}
setTimeout(function(){
if (!_this.suggest(position)){
_this.hide();
}
}, 0);
},
//////////////////////////////////////////////////////////
//
// Show autocomplete dialog
//
//////////////////////////////////////////////////////////
show: function() {
if (!this.isVisible) {
this.isVisible = true;
var popup = $('#autocomplete');
popup.css({
'top': this._computeTopOffset(),
'left': this._computeLeftOffset(),
'font-family': $('.ace_editor').css('font-family'),
'font-size': $('.ace_editor').css('font-size')
});
popup.slideToggle('fast', function(){
$(this).css('overflow', '');
});
this.addKeyboardCommands();
}
//Remove old suggestions
$('#suggestions').html("");
for (var i = 0; i < this.suggestionCache.length; i++) {
this.insertSuggestion(this.suggestionCache[i]);
}
//Select first suggestion
$('.suggestion:first').addClass('active-suggestion');
//Scroll to top
this._computeScrolling();
},
//////////////////////////////////////////////////////////
//
// Hide autocomplete dialog
//
//////////////////////////////////////////////////////////
hide: function() {
if (this.isVisible) {
this.isVisible = false;
$('#autocomplete').hide();
this.removeKeyboardCommands();
}
this.suggestionCache = null;
this.suggestionTextCache = null;
this.pluginCache = null;
this.pluginSessionCache = null;
this.pluginMajCache = null;
},
//////////////////////////////////////////////////////////
//
// Insert suggestion to autocomplete dialog
//
// Parameter:
//
// sug - {Object} - Suggestion to insert
//
//////////////////////////////////////////////////////////
insertSuggestion: function(sug) {
if (sug === null || sug == [] || typeof(sug) == 'undefined') {
return false;
}
var _this = this;
var color = function(title) {
var indexes = _this.removeNegativeIndexes(codiad.autocomplete.getMatchIndexes(_this.prefix, title));
var colored = "";
var char = "";
for (var i = 0; i < title.length; i++) {
char = $('<div/>').text(title[i]).html();
if (indexes.indexOf(i) != -1) {
colored += '<span class="matched">'+char+'</span>';
} else {
colored += char;
}
}
return colored;
};
var insText = '<li class="suggestion" data-suggestion="'+_this.extensions.phpjs.htmlentities(sug.suggestion)+'" ';
if (sug.type == "plugin") {
insText += 'data-type="plugin" data-callback="'+sug.callback+'" >';
insText += color(sug.title) + '</li>';
} else {
insText += 'data-type="text">';
insText += color(sug.suggestion) + '</li>';
}
$('#suggestions').append(insText);
},
//////////////////////////////////////////////////////////
//
// Get every suggestion of the text
//
//////////////////////////////////////////////////////////
getTextSuggestions: function() {
if (this.suggestionTextCache !== null) {
return this.suggestionTextCache;
}
var buf = "";
var marker = "__complete_markerword__";
var position= codiad.editor.getActive().getCursorPosition();
var text = codiad.editor.getActive().getSession().getValue();
text = text.split("\n");
for (var i = 0; i < position.row; i++) {
buf += "\n"+text[i];
}
buf += text[position.row].substring(0, position.column) + marker + text[position.row].substring(position.column);
for (i++; i < text.length; i++) {
buf += "\n"+text[i];
}
var sugBuf = buf.split(this.wordRegex);
var sugs = [];
for (i = 0; i < sugBuf.length; i++) {
if ((sugBuf[i] !== "") && (sugBuf[i].search(marker) == -1)) {
//Get each value just one time
if (sugs.indexOf(sugBuf[i]) == -1) {
sugs.push(sugBuf[i]);
}
}
}
//Formatting array
var result = [];
for (i = 0; i < sugs.length; i++) {
result.push({"suggestion":sugs[i], "ranking": sugs[i]});
}
return result;
},
//////////////////////////////////////////////////////////
//
// Get matching suggestions
//
// Parameter:
//
// prefix - {String} - Prefix
// sugCache - {Array} - Suggestion to check
//
//////////////////////////////////////////////////////////
getMatches: function(prefix, sugCache) {
var score;
var buf = [];
if (sugCache === null || typeof(sugCache) == 'undefined' || sugCache.length === 0) {
return [];
}
for (var i = 0; i < sugCache.length; i++) {
score = codiad.autocomplete.computeSimpleMatchScore(prefix, sugCache[i].ranking);
if (score == prefix.length) {
buf.push(sugCache[i]);
}
}
return buf;
},
//////////////////////////////////////////////////////////
//
// Get fuzzilies
//
// Parameter:
//
// prefix - {String} - Prefix
// sugCache - {Array} - Suggestion to check
//
//////////////////////////////////////////////////////////
getFuzzilies: function(prefix, sugCache) {
var buf = [];
if (sugCache === null || typeof(sugCache) == 'undefined' || sugCache.length === 0) {
return [];
}
for (var i = 0; i < sugCache.length; i++) {
if (codiad.autocomplete.isMatchingFuzzily(prefix, sugCache[i].ranking)) {
buf.push(sugCache[i]);
}
}
return buf;
},
//////////////////////////////////////////////////////////
//
// Add an extra information to each object of an array
//
// Parameter:
//
// cache - {Array} - Array of objects
// type - {String} - Object element to add information
// kind - {various} - Extra information to add
//
//////////////////////////////////////////////////////////
addExtraInfo: function(cache, type, kind) {
if (cache === null || typeof(cache) == 'undefined' || cache.length === 0) {
return [];
}
for (var i = 0; i < cache.length; i++) {
cache[i][type] = kind;
}
return cache;
},
//////////////////////////////////////////////////////////
//
// Rank suggestions
//
// Parameter:
//
// prefix - {String} - Prefix
// sugCache - {Array} - Suggestion to rank
//
//////////////////////////////////////////////////////////
rankSuggestions: function(prefix, sugCache) {
var _this = this;
if (sugCache === null || typeof(sugCache) == 'undefined' || sugCache.length === 0) {
return [];
}
return sugCache.sort(function(a, b) {
if (a == b) {
return 0;
}
var aScore = codiad.autocomplete.getMatchIndexes(prefix, a.ranking);
var bScore = codiad.autocomplete.getMatchIndexes(prefix, b.ranking);
aScore = _this.removeNegativeIndexes(aScore);
bScore = _this.removeNegativeIndexes(bScore);
var result = aScore.length-bScore.length;
if (result === 0) {
var buf = [a.ranking, b.ranking];
buf.sort();
if (buf[0] === a.ranking) {
return -1;
} else {
return 1;
}
} else {
return result;
}
});
},
//////////////////////////////////////////////////////////
//
// Remove negative integer elements of an array
//
// Parameter:
//
// buf - {Array} - Array to remove from
//
//////////////////////////////////////////////////////////
removeNegativeIndexes: function(buf) {
var result = [];
for (var i = 0; i < buf.length; i++) {
if (buf[i] != -1) {
result.push(buf[i]);
}
}
return result;
},
//////////////////////////////////////////////////////////
//
// Remove double entries of the second array
//
// Parameter:
//
// one - {Array} - Array with entries to keep
// two - {Array} - Array with entries to discard
//
//////////////////////////////////////////////////////////
removeDoubleEntries: function(one, two) {
var buf = [];
for (var i = 0; i < two.length; i++) {
if (!this.isInArraySpecial(two[i].ranking, one)) {
buf.push(two[i]);
}
}
return buf;
},
//////////////////////////////////////////////////////////
//
// Search in array for element (MyOne)
//
// Parameter:
//
// element - {various} - Element to search for
// buf - {Array} - Array to search in
//
//////////////////////////////////////////////////////////
isInArraySpecial: function(element, buf) {
for (var i = 0; i < buf.length; i++) {
if (buf[i].ranking === element) {
return true;
}
}
return false;
},
//////////////////////////////////////////////////////////
//
// Return element out of buf defined by needle
//
// Parameter:
//
// needle - {String} - Suggestion text as needle
// buf - {Array} - Array to search in
//
//////////////////////////////////////////////////////////
getObjectOutArray: function(needle, buf) {
for (var i = 0; i < buf.length; i++) {
if (buf[i].suggestion === needle) {
return buf[i];
}
}
return false;
},
//////////////////////////////////////////////////////////
//
// Push an array or an element to an array
//
// Parameter:
//
// cache - {Array} - Array push element in
// buf - {Array/Element} - Element or Elements to push
//
//////////////////////////////////////////////////////////
push: function(cache, buf) {
if (cache === null) {
cache = [];
}
if (buf === null) {
return cache;
}
if (cache.length === 0) {
return buf;
}
if ($.isArray(buf)) {
for (var i = 0; i < buf.length; i++) {
cache.push(buf[i]);
}
} else {
cache.push(buf);
}
return cache;
},
//////////////////////////////////////////////////////////
//
// Remove empty arrays of an array
//
// Parameter:
//
// cache - {Array}
//
//////////////////////////////////////////////////////////
removeEmptyArrays: function(cache) {
var buf = [];
for (var i = 0; i < cache.length; i++) {
if (cache[i].length !== 0) {
buf.push(cache[i]);
}
}
return buf;
},
//////////////////////////////////////////////////////////
//
// Compute top offset for autocomplete dialog
//
//////////////////////////////////////////////////////////
_computeTopOffset: function() {
var position = codiad.editor.getActive().getCursorPosition();
var cursor = $('.ace_gutter-cell:contains("'+ (position.row+1) +'")');
if (cursor.length > 0) {
var fontSize = codiad.editor.getActive().container.style.fontSize.replace('px', '');
var interLine = 1.7;
cursor = $(cursor[0]);
var top = cursor.offset().top + fontSize * interLine;
return top;
}
},
//////////////////////////////////////////////////////////
//
// Compute left offset for autocomplete dialog
//
//////////////////////////////////////////////////////////
_computeLeftOffset: function() {
var cursor = $('.ace_cursor');
if (cursor.length > 0) {
var position = codiad.editor.getActive().getCursorPosition();
var line = $('.ace_gutter-cell:contains("'+ (position.row+1) +'")');
for (var i = 0; i < cursor.length; i++) {
if ($(cursor[i]).offset().top == $(line).offset().top) {
return $(cursor[i]).offset().left;
}
}
cursor = $(cursor[0]);
var left = cursor.offset().left;
return left;
}
},
//////////////////////////////////////////////////////////
//
// Compute scrolling for autocomplete dialog
//
//////////////////////////////////////////////////////////
_computeScrolling: function() {
var pos = $('.suggestion').index($('.active-suggestion')) - 4;
if (pos < 0) {
pos = 0;
}
$('#autocomplete').scrollTop(pos * 16);
},
/*{
title - Title of the suggestions to display
suggestion - Suggestions to insert
ranking - Suggestion to rank
callback - "Amplify Topic" -> amplify.publish(callback, {Array of the Suggestion})
wenn callback == "" -> macht CompletePlus die ersetzung aufgrundlage von suggestion
}*/
//////////////////////////////////////////////////////////
//
// Parse an suggestion for a plugin
//
// Parameter:
//
// sug - (Required) - {String} - Suggestion