Skip to content

Commit 25dd400

Browse files
committed
Improved overall textcomplete performance and more cases and UX handling
1 parent 88e6e85 commit 25dd400

2 files changed

Lines changed: 49 additions & 23 deletions

File tree

public/js/index.js

Lines changed: 47 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2504,31 +2504,38 @@ function checkCursorMenuInner() {
25042504
dropdown[0].style.top = top + offsetTop + 'px';
25052505
}
25062506

2507+
function checkInIndentCode() {
2508+
// if line starts with tab or four spaces is a code block
2509+
var line = editor.getLine(editor.getCursor().line);
2510+
var isIndentCode = ((line.substr(0, 4) === ' ') || (line.substr(0, 1) === '\t'));
2511+
return isIndentCode;
2512+
}
2513+
25072514
var isInCode = false;
25082515

25092516
function checkInCode() {
2510-
isInCode = checkAbove();
2517+
isInCode = checkAbove(matchInCode) || checkInIndentCode();
25112518
}
25122519

2513-
function checkAbove() {
2520+
function checkAbove(method) {
25142521
var cursor = editor.getCursor();
25152522
var text = [];
25162523
for (var i = 0; i < cursor.line; i++) //contain current line
25172524
text.push(editor.getLine(i));
25182525
text = text.join('\n') + '\n' + editor.getLine(cursor.line).slice(0, cursor.ch);
25192526
//console.log(text);
2520-
return matchInCode(text);
2527+
return method(text);
25212528
}
25222529

2523-
function checkBelow() {
2530+
function checkBelow(method) {
25242531
var cursor = editor.getCursor();
25252532
var count = editor.lineCount();
25262533
var text = [];
25272534
for (var i = cursor.line + 1; i < count; i++) //contain current line
25282535
text.push(editor.getLine(i));
25292536
text = editor.getLine(cursor.line).slice(cursor.ch) + '\n' + text.join('\n');
25302537
//console.log(text);
2531-
return matchInCode(text);
2538+
return method(text);
25322539
}
25332540

25342541
function matchInCode(text) {
@@ -2546,6 +2553,29 @@ function matchInCode(text) {
25462553
}
25472554
}
25482555

2556+
var isInContainer = false;
2557+
var isInContainerSyntax = false;
2558+
2559+
function checkInContainer() {
2560+
isInContainer = checkAbove(matchInContainer) && !checkInIndentCode();
2561+
}
2562+
2563+
function checkInContainerSyntax() {
2564+
// if line starts with :::, it's in container syntax
2565+
var line = editor.getLine(editor.getCursor().line);
2566+
isInContainerSyntax = (line.substr(0, 3) === ':::');
2567+
}
2568+
2569+
function matchInContainer(text) {
2570+
var match;
2571+
match = text.match(/:{3,}/g);
2572+
if (match && match.length % 2) {
2573+
return true;
2574+
} else {
2575+
return false;
2576+
}
2577+
}
2578+
25492579
$(editor.getInputField())
25502580
.textcomplete([
25512581
{ // emoji strategy
@@ -2561,7 +2591,6 @@ $(editor.getInputField())
25612591
list.push(emoji);
25622592
});
25632593
callback(list);
2564-
checkCursorMenu();
25652594
},
25662595
template: function (value) {
25672596
return '<img class="emoji" src="' + serverurl + '/vendor/emojify/images/' + value + '.png"></img> ' + value;
@@ -2571,16 +2600,19 @@ $(editor.getInputField())
25712600
},
25722601
index: 1,
25732602
context: function (text) {
2574-
checkCursorMenu();
25752603
checkInCode();
2576-
return !isInCode;
2604+
checkInContainer();
2605+
checkInContainerSyntax();
2606+
return !isInCode && !isInContainerSyntax;
25772607
}
25782608
},
25792609
{ // Code block language strategy
25802610
langs: supportCodeModes,
25812611
charts: supportCharts,
25822612
match: /(^|\n)```(\w+)$/,
25832613
search: function (term, callback) {
2614+
var line = editor.getLine(editor.getCursor().line);
2615+
term = line.match(this.match)[2];
25842616
var list = [];
25852617
$.map(this.langs, function (lang) {
25862618
if (lang.indexOf(term) === 0 && lang !== term)
@@ -2591,11 +2623,10 @@ $(editor.getInputField())
25912623
list.push(chart);
25922624
});
25932625
callback(list);
2594-
checkCursorMenu();
25952626
},
25962627
replace: function (lang) {
25972628
var ending = '';
2598-
if (!checkBelow()) {
2629+
if (!checkBelow(matchInCode)) {
25992630
ending = '\n\n```';
26002631
}
26012632
if (this.langs.indexOf(lang) !== -1)
@@ -2614,7 +2645,6 @@ $(editor.getInputField())
26142645
editor.doc.cm.execCommand("goLineUp");
26152646
},
26162647
context: function (text) {
2617-
checkCursorMenu();
26182648
return isInCode;
26192649
}
26202650
},
@@ -2660,13 +2690,11 @@ $(editor.getInputField())
26602690
callback($.map(supportHeaders, function (header) {
26612691
return header.search.indexOf(term) === 0 ? header.text : null;
26622692
}));
2663-
checkCursorMenu();
26642693
},
26652694
replace: function (value) {
26662695
return '$1' + value;
26672696
},
26682697
context: function (text) {
2669-
checkCursorMenu();
26702698
return !isInCode;
26712699
}
26722700
},
@@ -2683,13 +2711,11 @@ $(editor.getInputField())
26832711
list.push(referral.text);
26842712
})
26852713
callback(list);
2686-
checkCursorMenu();
26872714
},
26882715
replace: function (value) {
26892716
return '$1' + value;
26902717
},
26912718
context: function (text) {
2692-
checkCursorMenu();
26932719
return !isInCode;
26942720
}
26952721
},
@@ -2706,13 +2732,11 @@ $(editor.getInputField())
27062732
list.push(referral.text);
27072733
})
27082734
callback(list);
2709-
checkCursorMenu();
27102735
},
27112736
replace: function (value) {
27122737
return '$1' + value;
27132738
},
27142739
context: function (text) {
2715-
checkCursorMenu();
27162740
return !isInCode;
27172741
}
27182742
},
@@ -2722,13 +2746,11 @@ $(editor.getInputField())
27222746
callback($.map(supportReferrals, function (referral) {
27232747
return referral.search.indexOf(term) === 0 ? referral.text : null;
27242748
}));
2725-
checkCursorMenu();
27262749
},
27272750
replace: function (value) {
27282751
return '$1' + value;
27292752
},
27302753
context: function (text) {
2731-
checkCursorMenu();
27322754
return !isInCode;
27332755
}
27342756
},
@@ -2738,25 +2760,28 @@ $(editor.getInputField())
27382760
callback($.map(supportExternals, function (external) {
27392761
return external.search.indexOf(term) === 0 ? external.text : null;
27402762
}));
2741-
checkCursorMenu();
27422763
},
27432764
replace: function (value) {
27442765
return '$1' + value;
27452766
},
27462767
context: function (text) {
2747-
checkCursorMenu();
27482768
return !isInCode;
27492769
}
27502770
}
27512771
], {
27522772
appendTo: $('.cursor-menu')
27532773
})
27542774
.on({
2775+
'textComplete:beforeSearch': function (e) {
2776+
//NA
2777+
},
2778+
'textComplete:afterSearch': function (e) {
2779+
checkCursorMenu();
2780+
},
27552781
'textComplete:select': function (e, value, strategy) {
27562782
//NA
27572783
},
27582784
'textComplete:show': function (e) {
2759-
checkCursorMenu();
27602785
$(this).data('autocompleting', true);
27612786
editor.setOption("extraKeys", {
27622787
"Up": function () {
@@ -2776,7 +2801,6 @@ $(editor.getInputField())
27762801
},
27772802
"Backspace": function () {
27782803
editor.doc.cm.execCommand("delCharBefore");
2779-
checkCursorMenu();
27802804
}
27812805
});
27822806
},

public/vendor/jquery-textcomplete/jquery.textcomplete.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,9 @@ if (typeof jQuery === 'undefined') {
202202
// Ignore shift-key, ctrl-key and so on.
203203
if (skipUnchangedTerm && this._term === term) { return; }
204204
this._term = term;
205+
this.fire('textComplete:beforeSearch');
205206
this._search.apply(this, searchQuery);
207+
this.fire('textComplete:afterSearch');
206208
} else {
207209
this._term = null;
208210
this.dropdown.deactivate();

0 commit comments

Comments
 (0)