Improved overall textcomplete performance and more cases and UX handling

This commit is contained in:
Cheng-Han, Wu 2016-03-15 11:04:45 +08:00
parent 88e6e8522f
commit 25dd400708
2 changed files with 49 additions and 23 deletions

View file

@ -2504,23 +2504,30 @@ function checkCursorMenuInner() {
dropdown[0].style.top = top + offsetTop + 'px'; dropdown[0].style.top = top + offsetTop + 'px';
} }
function checkInIndentCode() {
// if line starts with tab or four spaces is a code block
var line = editor.getLine(editor.getCursor().line);
var isIndentCode = ((line.substr(0, 4) === ' ') || (line.substr(0, 1) === '\t'));
return isIndentCode;
}
var isInCode = false; var isInCode = false;
function checkInCode() { function checkInCode() {
isInCode = checkAbove(); isInCode = checkAbove(matchInCode) || checkInIndentCode();
} }
function checkAbove() { function checkAbove(method) {
var cursor = editor.getCursor(); var cursor = editor.getCursor();
var text = []; var text = [];
for (var i = 0; i < cursor.line; i++) //contain current line for (var i = 0; i < cursor.line; i++) //contain current line
text.push(editor.getLine(i)); text.push(editor.getLine(i));
text = text.join('\n') + '\n' + editor.getLine(cursor.line).slice(0, cursor.ch); text = text.join('\n') + '\n' + editor.getLine(cursor.line).slice(0, cursor.ch);
//console.log(text); //console.log(text);
return matchInCode(text); return method(text);
} }
function checkBelow() { function checkBelow(method) {
var cursor = editor.getCursor(); var cursor = editor.getCursor();
var count = editor.lineCount(); var count = editor.lineCount();
var text = []; var text = [];
@ -2528,7 +2535,7 @@ function checkBelow() {
text.push(editor.getLine(i)); text.push(editor.getLine(i));
text = editor.getLine(cursor.line).slice(cursor.ch) + '\n' + text.join('\n'); text = editor.getLine(cursor.line).slice(cursor.ch) + '\n' + text.join('\n');
//console.log(text); //console.log(text);
return matchInCode(text); return method(text);
} }
function matchInCode(text) { function matchInCode(text) {
@ -2546,6 +2553,29 @@ function matchInCode(text) {
} }
} }
var isInContainer = false;
var isInContainerSyntax = false;
function checkInContainer() {
isInContainer = checkAbove(matchInContainer) && !checkInIndentCode();
}
function checkInContainerSyntax() {
// if line starts with :::, it's in container syntax
var line = editor.getLine(editor.getCursor().line);
isInContainerSyntax = (line.substr(0, 3) === ':::');
}
function matchInContainer(text) {
var match;
match = text.match(/:{3,}/g);
if (match && match.length % 2) {
return true;
} else {
return false;
}
}
$(editor.getInputField()) $(editor.getInputField())
.textcomplete([ .textcomplete([
{ // emoji strategy { // emoji strategy
@ -2561,7 +2591,6 @@ $(editor.getInputField())
list.push(emoji); list.push(emoji);
}); });
callback(list); callback(list);
checkCursorMenu();
}, },
template: function (value) { template: function (value) {
return '<img class="emoji" src="' + serverurl + '/vendor/emojify/images/' + value + '.png"></img> ' + value; return '<img class="emoji" src="' + serverurl + '/vendor/emojify/images/' + value + '.png"></img> ' + value;
@ -2571,9 +2600,10 @@ $(editor.getInputField())
}, },
index: 1, index: 1,
context: function (text) { context: function (text) {
checkCursorMenu();
checkInCode(); checkInCode();
return !isInCode; checkInContainer();
checkInContainerSyntax();
return !isInCode && !isInContainerSyntax;
} }
}, },
{ // Code block language strategy { // Code block language strategy
@ -2581,6 +2611,8 @@ $(editor.getInputField())
charts: supportCharts, charts: supportCharts,
match: /(^|\n)```(\w+)$/, match: /(^|\n)```(\w+)$/,
search: function (term, callback) { search: function (term, callback) {
var line = editor.getLine(editor.getCursor().line);
term = line.match(this.match)[2];
var list = []; var list = [];
$.map(this.langs, function (lang) { $.map(this.langs, function (lang) {
if (lang.indexOf(term) === 0 && lang !== term) if (lang.indexOf(term) === 0 && lang !== term)
@ -2591,11 +2623,10 @@ $(editor.getInputField())
list.push(chart); list.push(chart);
}); });
callback(list); callback(list);
checkCursorMenu();
}, },
replace: function (lang) { replace: function (lang) {
var ending = ''; var ending = '';
if (!checkBelow()) { if (!checkBelow(matchInCode)) {
ending = '\n\n```'; ending = '\n\n```';
} }
if (this.langs.indexOf(lang) !== -1) if (this.langs.indexOf(lang) !== -1)
@ -2614,7 +2645,6 @@ $(editor.getInputField())
editor.doc.cm.execCommand("goLineUp"); editor.doc.cm.execCommand("goLineUp");
}, },
context: function (text) { context: function (text) {
checkCursorMenu();
return isInCode; return isInCode;
} }
}, },
@ -2660,13 +2690,11 @@ $(editor.getInputField())
callback($.map(supportHeaders, function (header) { callback($.map(supportHeaders, function (header) {
return header.search.indexOf(term) === 0 ? header.text : null; return header.search.indexOf(term) === 0 ? header.text : null;
})); }));
checkCursorMenu();
}, },
replace: function (value) { replace: function (value) {
return '$1' + value; return '$1' + value;
}, },
context: function (text) { context: function (text) {
checkCursorMenu();
return !isInCode; return !isInCode;
} }
}, },
@ -2683,13 +2711,11 @@ $(editor.getInputField())
list.push(referral.text); list.push(referral.text);
}) })
callback(list); callback(list);
checkCursorMenu();
}, },
replace: function (value) { replace: function (value) {
return '$1' + value; return '$1' + value;
}, },
context: function (text) { context: function (text) {
checkCursorMenu();
return !isInCode; return !isInCode;
} }
}, },
@ -2706,13 +2732,11 @@ $(editor.getInputField())
list.push(referral.text); list.push(referral.text);
}) })
callback(list); callback(list);
checkCursorMenu();
}, },
replace: function (value) { replace: function (value) {
return '$1' + value; return '$1' + value;
}, },
context: function (text) { context: function (text) {
checkCursorMenu();
return !isInCode; return !isInCode;
} }
}, },
@ -2722,13 +2746,11 @@ $(editor.getInputField())
callback($.map(supportReferrals, function (referral) { callback($.map(supportReferrals, function (referral) {
return referral.search.indexOf(term) === 0 ? referral.text : null; return referral.search.indexOf(term) === 0 ? referral.text : null;
})); }));
checkCursorMenu();
}, },
replace: function (value) { replace: function (value) {
return '$1' + value; return '$1' + value;
}, },
context: function (text) { context: function (text) {
checkCursorMenu();
return !isInCode; return !isInCode;
} }
}, },
@ -2738,13 +2760,11 @@ $(editor.getInputField())
callback($.map(supportExternals, function (external) { callback($.map(supportExternals, function (external) {
return external.search.indexOf(term) === 0 ? external.text : null; return external.search.indexOf(term) === 0 ? external.text : null;
})); }));
checkCursorMenu();
}, },
replace: function (value) { replace: function (value) {
return '$1' + value; return '$1' + value;
}, },
context: function (text) { context: function (text) {
checkCursorMenu();
return !isInCode; return !isInCode;
} }
} }
@ -2752,11 +2772,16 @@ $(editor.getInputField())
appendTo: $('.cursor-menu') appendTo: $('.cursor-menu')
}) })
.on({ .on({
'textComplete:beforeSearch': function (e) {
//NA
},
'textComplete:afterSearch': function (e) {
checkCursorMenu();
},
'textComplete:select': function (e, value, strategy) { 'textComplete:select': function (e, value, strategy) {
//NA //NA
}, },
'textComplete:show': function (e) { 'textComplete:show': function (e) {
checkCursorMenu();
$(this).data('autocompleting', true); $(this).data('autocompleting', true);
editor.setOption("extraKeys", { editor.setOption("extraKeys", {
"Up": function () { "Up": function () {
@ -2776,7 +2801,6 @@ $(editor.getInputField())
}, },
"Backspace": function () { "Backspace": function () {
editor.doc.cm.execCommand("delCharBefore"); editor.doc.cm.execCommand("delCharBefore");
checkCursorMenu();
} }
}); });
}, },

View file

@ -202,7 +202,9 @@ if (typeof jQuery === 'undefined') {
// Ignore shift-key, ctrl-key and so on. // Ignore shift-key, ctrl-key and so on.
if (skipUnchangedTerm && this._term === term) { return; } if (skipUnchangedTerm && this._term === term) { return; }
this._term = term; this._term = term;
this.fire('textComplete:beforeSearch');
this._search.apply(this, searchQuery); this._search.apply(this, searchQuery);
this.fire('textComplete:afterSearch');
} else { } else {
this._term = null; this._term = null;
this.dropdown.deactivate(); this.dropdown.deactivate();