From 9c8e3b7570af9ec4caf686829be82c6fd5933ad0 Mon Sep 17 00:00:00 2001
From: Franco Pariani <franco19ps@gmail.com>
Date: Thu, 21 Feb 2019 20:50:10 -0300
Subject: [PATCH] Autocomplete hints from included files

---
 .../matefun/codemirror/matefun-mode-EN.js     | 96 +++++++++++++++++++
 .../matefun/codemirror/matefun-mode-ES.js     | 96 +++++++++++++++++++
 .../app/layout/matefun/matefun.component.ts   |  7 +-
 3 files changed, 197 insertions(+), 2 deletions(-)

diff --git a/Frontend Angular 4/src/app/layout/matefun/codemirror/matefun-mode-EN.js b/Frontend Angular 4/src/app/layout/matefun/codemirror/matefun-mode-EN.js
index 765b5b11..138d9758 100644
--- a/Frontend Angular 4/src/app/layout/matefun/codemirror/matefun-mode-EN.js	
+++ b/Frontend Angular 4/src/app/layout/matefun/codemirror/matefun-mode-EN.js	
@@ -152,6 +152,9 @@
               list.push(default_sets[i]);
             }
           }
+
+          // included sets
+          hintsSetsFromIncludedFiles(editor.options.files, editor, curWord, list);
         }else{
           // get variables of function
           var previous_block = "";
@@ -197,6 +200,8 @@
                   }
                 }
               }
+              // included enums
+              hintsEnumsFromIncludedFiles(editor.options.files, editor, curWord, list);
               // get functions
               var re_funs = new RegExp(FUNS.source, "g");
               for (var dir = -1; dir <= 1; dir += 2) {
@@ -211,6 +216,8 @@
                   }
                 }
               }
+              // included functions
+              hintsFunctionsFromIncludedFiles(editor.options.files, editor, curWord, list);
               // add default functions
               var default_functions = hintWords.EN_functions();
               for (var i = 0; i < default_functions.length; i++) {
@@ -227,6 +234,95 @@
     return {list: list, from: CodeMirror.Pos(cur.line, start), to: CodeMirror.Pos(cur.line, end)};
   });
 
+  var INCLUIR = /include\s+([\w\_\d]+)/;
+
+  function hintsFunctionsFromIncludedFiles(all_files, editor, curWord, list){
+    var re_digits = new RegExp(/^\d+$/, "g");
+    var includes = new RegExp(INCLUIR.source, "g");
+    var content = editor.getValue();
+
+    var file;
+    while (file = includes.exec(content)) {
+      var files = all_files.archivos.filter(
+        function(a){
+            return a.nombre === file[1];
+        });
+      for (var f = 0; f < files.length; f++){
+        var seen = {};
+        var m;
+        var re_funs = new RegExp(FUNS.source, "g");
+
+        var file_content = files[f].contenido;
+
+        while (m = re_funs.exec(file_content)) {
+          if (!re_digits.exec(m[1]) && (!curWord || m[1].lastIndexOf(curWord, 0) == 0) && !Object.prototype.hasOwnProperty.call(seen, m[1])) {
+            seen[m[1]] = true;
+            list.push(m[1]);
+          }
+        }
+      }
+    }
+  }
+
+  function hintsSetsFromIncludedFiles(all_files, editor, curWord, list){
+    var re_digits = new RegExp(/^\d+$/, "g");
+    var includes = new RegExp(INCLUIR.source, "g");
+    var content = editor.getValue();
+
+    var file;
+    while (file = includes.exec(content)) {
+      var files = all_files.archivos.filter(
+        function(a){
+            return a.nombre === file[1];
+        });
+      for (var f = 0; f < files.length; f++){
+        var seen = {};
+        var m;
+        var re_sets = new RegExp(SET.source, "g");
+
+        var file_content = files[f].contenido;
+
+        while (m = re_sets.exec(file_content)) {
+          if (!re_digits.exec(m[1]) && (!curWord || m[1].lastIndexOf(curWord, 0) == 0) && !Object.prototype.hasOwnProperty.call(seen, m[1])) {
+            seen[m[1]] = true;
+            list.push(m[1]);
+          }
+        }
+      }
+    }
+  }
+
+  function hintsEnumsFromIncludedFiles(all_files, editor, curWord, list){
+    var re_digits = new RegExp(/^\d+$/, "g");
+    var includes = new RegExp(INCLUIR.source, "g");
+    var content = editor.getValue();
+
+    var file;
+    while (file = includes.exec(content)) {
+      var files = all_files.archivos.filter(
+        function(a){
+            return a.nombre === file[1];
+        });
+      for (var f = 0; f < files.length; f++){
+        var seen = {};
+        var m;
+        var re_enums = new RegExp(ENUMS.source, "g");
+
+        var file_content = files[f].contenido;
+
+        while (m = re_enums.exec(file_content)) {
+          var enums = m[1].replace(/\s/g,'').split(',');
+          for (var i = 0; i < enums.length; i++) {
+            if (!re_digits.exec(enums[i]) && (!curWord || enums[i].lastIndexOf(curWord, 0) == 0) && !Object.prototype.hasOwnProperty.call(seen, enums[i])) {
+              seen[enums[i]] = true;
+              list.push(enums[i]);
+            }
+          }
+        }
+      }
+    }
+  }
+
   function normal(source, setState) {
     if (source.eatWhile(whiteCharRE)) {
       return null;
diff --git a/Frontend Angular 4/src/app/layout/matefun/codemirror/matefun-mode-ES.js b/Frontend Angular 4/src/app/layout/matefun/codemirror/matefun-mode-ES.js
index 567e7e95..2fbe7f74 100755
--- a/Frontend Angular 4/src/app/layout/matefun/codemirror/matefun-mode-ES.js	
+++ b/Frontend Angular 4/src/app/layout/matefun/codemirror/matefun-mode-ES.js	
@@ -149,6 +149,9 @@
               list.push(default_sets[i]);
             }
           }
+          
+          // included sets 
+          hintsSetsFromIncludedFiles(editor.options.files, editor, curWord, list); 
         }else{
           // get variables of function
           var previous_block = "";
@@ -194,6 +197,8 @@
                   }
                 }
               }
+              // included enums 
+              hintsEnumsFromIncludedFiles(editor.options.files, editor, curWord, list); 
               // get functions
               var re_funs = new RegExp(FUNS.source, "g");
               for (var dir = -1; dir <= 1; dir += 2) {
@@ -208,6 +213,8 @@
                   }
                 }
               }
+              // included functions 
+              hintsFunctionsFromIncludedFiles(editor.options.files, editor, curWord, list); 
               // add default functions
               var default_functions = hintWords.ES_functions();
               for (var i = 0; i < default_functions.length; i++) {
@@ -224,6 +231,95 @@
     return {list: list, from: CodeMirror.Pos(cur.line, start), to: CodeMirror.Pos(cur.line, end)};
   });
 
+  var INCLUIR = /incluir\s+([\w\_\d]+)/;
+
+  function hintsFunctionsFromIncludedFiles(all_files, editor, curWord, list){
+    var re_digits = new RegExp(/^\d+$/, "g");
+    var includes = new RegExp(INCLUIR.source, "g");
+    var content = editor.getValue();
+
+    var file;
+    while (file = includes.exec(content)) {
+      var files = all_files.archivos.filter(
+        function(a){
+            return a.nombre === file[1];
+        });
+      for (var f = 0; f < files.length; f++){
+        var seen = {};
+        var m;
+        var re_funs = new RegExp(FUNS.source, "g");
+
+        var file_content = files[f].contenido;
+
+        while (m = re_funs.exec(file_content)) {
+          if (!re_digits.exec(m[1]) && (!curWord || m[1].lastIndexOf(curWord, 0) == 0) && !Object.prototype.hasOwnProperty.call(seen, m[1])) {
+            seen[m[1]] = true;
+            list.push(m[1]);
+          }
+        }
+      }
+    }
+  }
+
+  function hintsSetsFromIncludedFiles(all_files, editor, curWord, list){
+    var re_digits = new RegExp(/^\d+$/, "g");
+    var includes = new RegExp(INCLUIR.source, "g");
+    var content = editor.getValue();
+
+    var file;
+    while (file = includes.exec(content)) {
+      var files = all_files.archivos.filter(
+        function(a){
+            return a.nombre === file[1];
+        });
+      for (var f = 0; f < files.length; f++){
+        var seen = {};
+        var m;
+        var re_sets = new RegExp(SET.source, "g");
+
+        var file_content = files[f].contenido;
+
+        while (m = re_sets.exec(file_content)) {
+          if (!re_digits.exec(m[1]) && (!curWord || m[1].lastIndexOf(curWord, 0) == 0) && !Object.prototype.hasOwnProperty.call(seen, m[1])) {
+            seen[m[1]] = true;
+            list.push(m[1]);
+          }
+        }
+      }
+    }
+  }
+
+  function hintsEnumsFromIncludedFiles(all_files, editor, curWord, list){
+    var re_digits = new RegExp(/^\d+$/, "g");
+    var includes = new RegExp(INCLUIR.source, "g");
+    var content = editor.getValue();
+
+    var file;
+    while (file = includes.exec(content)) {
+      var files = all_files.archivos.filter(
+        function(a){
+            return a.nombre === file[1];
+        });
+      for (var f = 0; f < files.length; f++){
+        var seen = {};
+        var m;
+        var re_enums = new RegExp(ENUMS.source, "g");
+
+        var file_content = files[f].contenido;
+
+        while (m = re_enums.exec(file_content)) {
+          var enums = m[1].replace(/\s/g,'').split(',');
+          for (var i = 0; i < enums.length; i++) {
+            if (!re_digits.exec(enums[i]) && (!curWord || enums[i].lastIndexOf(curWord, 0) == 0) && !Object.prototype.hasOwnProperty.call(seen, enums[i])) {
+              seen[enums[i]] = true;
+              list.push(enums[i]);
+            }
+          }
+        }
+      }
+    }
+  }
+
   function normal(source, setState) {
     if (source.eatWhile(whiteCharRE)) {
       return null;
diff --git a/Frontend Angular 4/src/app/layout/matefun/matefun.component.ts b/Frontend Angular 4/src/app/layout/matefun/matefun.component.ts
index 11361b7e..d7ff4324 100755
--- a/Frontend Angular 4/src/app/layout/matefun/matefun.component.ts	
+++ b/Frontend Angular 4/src/app/layout/matefun/matefun.component.ts	
@@ -103,7 +103,8 @@ export class MateFunComponent {
         hintOptions: {
             completeSingle: false,
             closeCharacters: /[^\d\w\_]/
-        }
+        },
+        files: null
     };
     themes = ['3024-day', '3024-night', 'abcdef', 'ambiance-mobile', 'ambiance', 'base16-dark', 'base16-light', 'bespin', 'blackboard', 'cobalt', 'colorforth', 'dracula', 'duotone-dark', 'duotone-light', 'eclipse', 'elegant', 'erlang-dark', 'hopscotch', 'icecoder', 'isotope', 'lesser-dark', 'liquibyte', 'material', 'mbo', 'mdn-like', 'midnight', 'monokai', 'neat', 'neo', 'night', 'panda-syntax', 'paraiso-dark', 'paraiso-light', 'pastel-on-dark', 'railscasts', 'rubyblue', 'seti', 'solarized', 'the-matrix', 'tomorrow-night-bright', 'tomorrow-night-eighties', 'ttcn', 'twilight', 'vibrant-ink', 'xq-dark', 'xq-light', 'yeti', 'zenburn']
     version: string = npm.version;
@@ -447,8 +448,10 @@ export class MateFunComponent {
 
     archivoModificado(event){
         if (this.hintsCheck && !event.ctrlKey && !event.altKey){
-            if (/^[\w\_\d]$/.test(event.key) || event.key == 'Enter')
+            if (/^[\w\_\d]$/.test(event.key) || event.key == 'Enter'){
+                this.codemirror.instance.options.files = this.archivosTree;
                 this.codemirror.instance.showHint(event);
+            }
             if(this.copiaNombreArchivo!=this.archivo.nombre || this.copiaContenidoArchivo != this.archivo.contenido){
                 this.modificado = true;
             }else{
-- 
GitLab