Improved code block and emojiy textcomplete and match rules

This commit is contained in:
Cheng-Han, Wu 2016-01-31 15:32:35 -06:00
parent eb4cb3f52f
commit 2eec0ae35d

View file

@ -2186,23 +2186,42 @@ function checkCursorMenu() {
var isInCode = false; var isInCode = false;
function check(text) { function checkInCode() {
isInCode = checkAbove() && checkBelow();
}
function checkAbove() {
var cursor = editor.getCursor(); var cursor = editor.getCursor();
text = []; var text = [];
for (var i = 0; i < cursor.line; i++) 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);
}
function checkBelow() {
var cursor = editor.getCursor();
var count = editor.lineCount();
var text = [];
for (var i = cursor.line + 1; i < count; i++) //not contain current line
text.push(editor.getLine(i));
text = text.join('\n') + '\n' + editor.getLine(cursor.line).slice(0, cursor.ch);
//console.log(text);
return matchInCode(text);
}
function matchInCode(text) {
var match; var match;
match = text.match(/`{3,}/g); match = text.match(/`{3,}/g);
if (match && match.length % 2) { if (match && match.length % 2) {
isInCode = true; return true;
} else { } else {
match = text.match(/`/g); match = text.match(/`/g);
if (match && match.length % 2) { if (match && match.length % 2) {
isInCode = true; return true;
} else { } else {
isInCode = false; return false;
} }
} }
} }
@ -2212,9 +2231,16 @@ $(editor.getInputField())
{ // emoji strategy { // emoji strategy
match: /(?:^|\n|)\B:([\-+\w]*)$/, match: /(?:^|\n|)\B:([\-+\w]*)$/,
search: function (term, callback) { search: function (term, callback) {
callback($.map(emojify.emojiNames, function (emoji) { var list = [];
return emoji.indexOf(term) === 0 ? emoji : null; $.map(emojify.emojiNames, function (emoji) {
})); if (emoji.indexOf(term) === 0) //match at first character
list.push(emoji);
});
$.map(emojify.emojiNames, function (emoji) {
if (emoji.indexOf(term) !== -1) //match inside the word
list.push(emoji);
});
callback(list);
checkCursorMenu(); checkCursorMenu();
}, },
template: function (value) { template: function (value) {
@ -2225,7 +2251,8 @@ $(editor.getInputField())
}, },
index: 1, index: 1,
context: function (text) { context: function (text) {
check(text); checkCursorMenu();
checkInCode();
return !isInCode; return !isInCode;
} }
}, },
@ -2236,27 +2263,39 @@ $(editor.getInputField())
search: function (term, callback) { search: function (term, callback) {
var list = []; var list = [];
$.map(this.langs, function (lang) { $.map(this.langs, function (lang) {
if (lang.indexOf(term) === 0) if (lang.indexOf(term) === 0 && lang !== term)
list.push(lang); list.push(lang);
}); });
$.map(this.charts, function (chart) { $.map(this.charts, function (chart) {
if (chart.indexOf(term) === 0) if (chart.indexOf(term) === 0 && chart !== term)
list.push(chart); list.push(chart);
}); });
checkCursorMenu();
callback(list); callback(list);
checkCursorMenu();
}, },
replace: function (lang) { replace: function (lang) {
var ending = '';
if (isInCode) {
ending = '\n\n```';
}
if (this.langs.indexOf(lang) !== -1) if (this.langs.indexOf(lang) !== -1)
return '$1```' + lang + '=\n\n```'; return '$1```' + lang + '=' + ending;
else if (this.charts.indexOf(lang) !== -1) else if (this.charts.indexOf(lang) !== -1)
return '$1```' + lang + '\n\n```'; return '$1```' + lang + ending;
}, },
done: function () { done: function () {
var cursor = editor.getCursor();
var text = [];
text.push(editor.getLine(cursor.line - 1));
text.push(editor.getLine(cursor.line));
text = text.join('\n');
//console.log(text);
if (text == '\n```')
editor.doc.cm.execCommand("goLineUp"); editor.doc.cm.execCommand("goLineUp");
}, },
context: function () { context: function (text) {
return isInCode; checkCursorMenu();
return true;
} }
}, },
{ //header { //header
@ -2271,6 +2310,7 @@ $(editor.getInputField())
return '$1' + value; return '$1' + value;
}, },
context: function (text) { context: function (text) {
checkCursorMenu();
return !isInCode; return !isInCode;
} }
}, },
@ -2293,6 +2333,7 @@ $(editor.getInputField())
return '$1' + value; return '$1' + value;
}, },
context: function (text) { context: function (text) {
checkCursorMenu();
return !isInCode; return !isInCode;
} }
}, },
@ -2315,6 +2356,7 @@ $(editor.getInputField())
return '$1' + value; return '$1' + value;
}, },
context: function (text) { context: function (text) {
checkCursorMenu();
return !isInCode; return !isInCode;
} }
}, },
@ -2330,6 +2372,7 @@ $(editor.getInputField())
return '$1' + value; return '$1' + value;
}, },
context: function (text) { context: function (text) {
checkCursorMenu();
return !isInCode; return !isInCode;
} }
}, },
@ -2345,6 +2388,7 @@ $(editor.getInputField())
return '$1' + value; return '$1' + value;
}, },
context: function (text) { context: function (text) {
checkCursorMenu();
return !isInCode; return !isInCode;
} }
} }