Commit 8b079603 authored by Franco Pariani's avatar Franco Pariani
Browse files

Changing the way of show hints to be more smart.

parent 39da6a86
// English words
let EN_default_words = [
'squareroot', 'rgb', 'rect', 'circ', 'line', 'poli', 'join', 'color', 'move', 'rotate', 'scale', 'toFig', 'line3D', 'sphere', 'cylinder', 'cube', 'ring', 'scale3D', 'range', 'first', 'rest', 'A'
let EN_functions = [
'-', 'round', 'sin', 'cos', 'squareroot', 'rgb', 'rect', 'circ', 'line', 'poli', 'join', 'color', 'move', 'rotate', 'scale', 'toFig', 'line3D', 'sphere', 'cylinder', 'cube', 'ring', 'join3D', 'color3D', 'move3D', 'rotate3D', 'scale3D', 'range', 'first', 'rest'
]
var EN_keywords = [
"include"
,"set","in"
,"if", "or"
,"isEmpty"
];
let EN_types = [
"Red", "Green", "Blue", "Black","White","Gray","Yellow","FigEmpty", "[]"
,"R", "Fig", "Fig3D", "Color", "color3D", "join3D", "rotate3D", "move3D"
]
let EN_functions = [
"abs", "acos", "acosh", "all", "and", "any", "appendFile", "asTypeOf",
"asin", "asinh", "atan", "atan2", "atanh", "break", "catch", "ceiling",
"compare", "concat", "concatMap", "const", "cos", "cosh", "curry",
"cycle", "decodeFloat", "div", "divMod", "drop", "dropWhile", "either",
"elem", "encodeFloat", "enumFrom", "enumFromThen", "enumFromThenTo",
"enumFromTo", "error", "even", "exp", "exponent", "fail", "filter",
"flip", "floatDigits", "floatRadix", "floatRange", "floor", "fmap",
"foldl", "foldl1", "foldr", "foldr1", "fromEnum", "fromInteger",
"fromIntegral", "fromRational", "fst", "gcd", "getChar", "getContents",
"getLine", "head", "id", "init", "interact", "ioError", "isDenormalized",
"isIEEE", "isInfinite", "isNaN", "isNegativeZero", "iterate", "last",
"lcm", "length", "lex", "lines", "log", "logBase", "lookup", "map",
"mapM", "mapM_", "max", "maxBound", "maximum", "maybe", "min", "minBound",
"minimum", "mod", "negate", "not", "notElem", "null", "odd", "or",
"otherwise", "pi", "pred", "print", "product", "properFraction",
"putChar", "putStr", "putStrLn", "quot", "quotRem", "read", "readFile",
"readIO", "readList", "readLn", "readParen", "reads", "readsPrec",
"realToFrac", "recip", "rem", "repeat", "replicate", "return", "reverse",
"round", "scaleFloat", "scanl", "scanl1", "scanr", "scanr1", "seq",
"sequence", "sequence_", "show", "showChar", "showList", "showParen",
"showString", "shows", "showsPrec", "significand", "signum", "sin",
"sinh", "snd", "span", "splitAt", "sqrt", "subtract", "succ", "sum",
"tail", "take", "takeWhile", "tan", "tanh", "toEnum", "toInteger",
"toRational", "truncate", "uncurry", "undefined", "unlines", "until",
"unwords", "unzip", "unzip3", "userError", "words", "writeFile", "zip",
"zip3", "zipWith", "zipWith3"
let EN_default_sets = [
'R', 'Color', 'Fig', 'A', 'Fig3D'
]
// Spanish words
......@@ -93,8 +56,11 @@ let ES_functions = [
]
module.exports = {
EN_words: function() {
return EN_default_words.concat(EN_keywords).concat(EN_types).concat(EN_functions);
EN_default_sets: function() {
return EN_default_sets;
},
EN_functions: function() {
return EN_functions;
},
ES_words: function() {
return ES_default_words.concat(ES_keywords).concat(ES_types).concat(ES_functions);
......
......@@ -27,27 +27,136 @@
const hintWords = require('./hint_words');
var anyWord = CodeMirror.hint.anyword;
CodeMirror.hint.anyword = function(cm) {
var cur = cm.getCursor(), token = cm.getTokenAt(cur)
var term, from = CodeMirror.Pos(cur.line, token.start), to = cur
if (token.start < cur.ch && /[\w\-\_]/.test(token.string.charAt(cur.ch - token.start - 1))) {
term = token.string.substr(0, cur.ch - token.start)
} else {
term = ""
from = cur
}
var inner = anyWord(cm, { word: /[\w$\-\_]+/ }) || {from: from, to: to, list: []};
var filtered_words = hintWords.EN_words().filter(w => w.indexOf(term) === 0);
// var anyWord = CodeMirror.hint.anyword;
// CodeMirror.hint.anyword = function(cm) {
// var cur = cm.getCursor(), token = cm.getTokenAt(cur)
// var term, from = CodeMirror.Pos(cur.line, token.start), to = cur
// if (token.start < cur.ch && /[\w\_]/.test(token.string.charAt(cur.ch - token.start - 1))) {
// term = token.string.substr(0, cur.ch - token.start)
// } else {
// term = ""
// from = cur
// }
// var inner = anyWord(cm, { word: /[\w$\_]+/ }) || {from: from, to: to, list: []};
// var filtered_words = hintWords.EN_words().filter(w => w.indexOf(term) === 0);
// for(var i= 0; i < filtered_words.length; i++){
// if (inner.list.indexOf(filtered_words[i]) === -1)
// inner.list.push(filtered_words[i]);
// }
// return inner;
// };
var WORD = /[\w$]+/;
var SET = /set\s*([\w\_]*?)\s*\=/;
var DOM = /\:\:/;
var FUNS = /([\w\_]*)\s*\:\:/;
var FUN = /([\w\_]+)\s*\(([\,\w\_\s]*)\)\s*\=/;
var ENUMS = /set\s*[\w\_]*\s*\=\s*\{(.*?)\}/;
var RANGE = 500;
CodeMirror.registerHelper("hint", "anyword", function(editor, options) {
var word = options && options.word || WORD;
var range = options && options.range || RANGE;
var cur = editor.getCursor(), curLine = editor.getLine(cur.line);
var end = cur.ch, start = end;
while (start && word.test(curLine.charAt(start - 1))) --start;
var curWord = start != end && curLine.slice(start, end);
var list = options && options.list || [], seen = {};
// sets
var previous_part = curLine.slice(0, start);
var re_dom = new RegExp(DOM.source, "g");
if (re_dom.exec(previous_part)){
var re_sets = new RegExp(SET.source, "g");
for (var dir = -1; dir <= 1; dir += 2) {
var line = cur.line, endLine = Math.min(Math.max(line + dir * range, editor.firstLine()), editor.lastLine()) + dir;
for (; line != endLine; line += dir) {
var text = editor.getLine(line), m;
while (m = re_sets.exec(text)) {
if (line == cur.line && m[1] === curWord) continue;
if ((!curWord || m[1].lastIndexOf(curWord, 0) == 0) && !Object.prototype.hasOwnProperty.call(seen, m[1])) {
seen[m[1]] = true;
list.push(m[1]);
}
}
}
}
for(var i= 0; i < filtered_words.length; i++){
if (inner.list.indexOf(filtered_words[i]) === -1)
inner.list.push(filtered_words[i]);
// add default sets
var default_sets = hintWords.EN_default_sets();
for (var i = 0; i < default_sets.length; i++) {
if ((!curWord || default_sets[i].lastIndexOf(curWord, 0) == 0) && !Object.prototype.hasOwnProperty.call(seen, default_sets[i])) {
seen[default_sets[i]] = true;
list.push(default_sets[i]);
}
}
}else{
// get variables of function
var previous_block = "";
for (var i = 1; i < cur.line; i++){
previous_block += editor.getLine(i) + '\n';
}
previous_block += previous_part;
var re_fun = new RegExp(FUN.source, "g"), match, last_match;
while (match = re_fun.exec(previous_block)){ last_match = match };
if (last_match){
var vars = last_match[2].replace(/\s/g,'').split(',');
for (var i = 0; i < vars.length; i++) {
if ((!curWord || vars[i].lastIndexOf(curWord, 0) == 0) && !Object.prototype.hasOwnProperty.call(seen, vars[i])) {
seen[vars[i]] = true;
list.push(vars[i]);
}
}
}
// get enums
var re_enums = new RegExp(ENUMS.source, "g");
for (var dir = -1; dir <= 1; dir += 2) {
var line = cur.line, endLine = Math.min(Math.max(line + dir * range, editor.firstLine()), editor.lastLine()) + dir;
for (; line != endLine; line += dir) {
var text = editor.getLine(line), m;
while (m = re_enums.exec(text)) {
var enums = m[1].replace(/\s/g,'').split(',');
for (var i = 0; i < enums.length; i++) {
if ((!curWord || enums[i].lastIndexOf(curWord, 0) == 0) && !Object.prototype.hasOwnProperty.call(seen, enums[i])) {
seen[enums[i]] = true;
list.push(enums[i]);
}
}
}
}
}
// get functions
var re_funs = new RegExp(FUNS.source, "g");
for (var dir = -1; dir <= 1; dir += 2) {
var line = cur.line, endLine = Math.min(Math.max(line + dir * range, editor.firstLine()), editor.lastLine()) + dir;
for (; line != endLine; line += dir) {
var text = editor.getLine(line), m;
while (m = re_funs.exec(text)) {
if ((!curWord || m[1].lastIndexOf(curWord, 0) == 0) && !Object.prototype.hasOwnProperty.call(seen, m[1])) {
seen[m[1]] = true;
list.push(m[1]);
}
}
}
}
// add default functions
var default_functions = hintWords.EN_functions();
for (var i = 0; i < default_functions.length; i++) {
if ((!curWord || default_functions[i].lastIndexOf(curWord, 0) == 0) && !Object.prototype.hasOwnProperty.call(seen, default_functions[i])) {
seen[default_functions[i]] = true;
list.push(default_functions[i]);
}
}
}
return inner;
};
return {list: list, from: CodeMirror.Pos(cur.line, start), to: CodeMirror.Pos(cur.line, end)};
});
function normal(source, setState) {
if (source.eatWhile(whiteCharRE)) {
......
......@@ -418,7 +418,7 @@ export class MateFunComponent {
archivoModificado(event){
if (!event.ctrlKey && !event.shiftKey && !event.altKey){
if (/^[a-zA-Z\-\_]$/.test(event.key))
if (/^[a-zA-Z\_]$/.test(event.key))
this.codemirror.instance.showHint(event);
if(this.copiaNombreArchivo!=this.archivo.nombre || this.copiaContenidoArchivo != this.archivo.contenido){
this.modificado = true;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment