From f4ecc13a290ec6afafee4edd9a59680f9bb8d28a Mon Sep 17 00:00:00 2001
From: Franco Pariani <franco@rootstrap.com>
Date: Fri, 22 Feb 2019 12:16:48 -0300
Subject: [PATCH] Jump to file preview when it's included

---
 .../app/layout/archivos/archivos.component.ts | 17 +++++++-
 .../matefun/codemirror/matefun-mode-EN.js     | 42 +++++++++++++++++--
 .../matefun/codemirror/matefun-mode-ES.js     | 42 +++++++++++++++++--
 .../app/layout/matefun/matefun.component.ts   | 15 ++++++-
 4 files changed, 106 insertions(+), 10 deletions(-)

diff --git a/Frontend Angular 4/src/app/layout/archivos/archivos.component.ts b/Frontend Angular 4/src/app/layout/archivos/archivos.component.ts
index f0418ad5..deb80abd 100755
--- a/Frontend Angular 4/src/app/layout/archivos/archivos.component.ts	
+++ b/Frontend Angular 4/src/app/layout/archivos/archivos.component.ts	
@@ -4,7 +4,7 @@ import { AuthenticationService } from '../../shared/services/authentication.serv
 import { HaskellService } from '../../shared/services/haskell.service';
 import { SessionService } from '../../shared/services/session.service';
 import { NotificacionService } from '../../shared/services/notificacion.service';
-import { Router } from '@angular/router';
+import { Router, ActivatedRoute } from '@angular/router';
 import { NuevoArchivo } from './nuevoArchivo.component';
 import { VerCalificacionComponent } from './verCalificacion.component';
 import { CompartirArchivoComponent } from './compartirArchivo.component';
@@ -16,6 +16,7 @@ import { CodemirrorComponent } from 'ng2-codemirror';
 import { NgbPopoverConfig, NgbPopover} from '@ng-bootstrap/ng-bootstrap';
 import { TranslateService } from '@ngx-translate/core';
 import { TitleCasePipe } from '../../shared/pipes/titlecase.pipe';
+import { map }                from 'rxjs/operators';
 
 import 'codemirror/mode/haskell/haskell';
 import 'codemirror/addon/display/panel';
@@ -30,6 +31,8 @@ import 'codemirror/mode/markdown/markdown';
 })
 
 export class ArchivosComponent {
+	file_loaded = null;
+	function_to_find = null;
 	translateService : any;
 	titlecasePipe: any;
 	archivos : Archivo[] = [];
@@ -54,7 +57,8 @@ export class ArchivosComponent {
 		private haskellService: HaskellService,
 		private sessionService: SessionService,
 		private dialogService:DialogService,
-		public translate: TranslateService
+		public translate: TranslateService,
+		private route: ActivatedRoute
 		){
 		this.translateService = translate;
 		this.titlecasePipe = new TitleCasePipe();
@@ -67,6 +71,10 @@ export class ArchivosComponent {
 	@ViewChild(CodemirrorComponent) codemirror: CodemirrorComponent;
 
 	ngOnInit(){
+		this.route.queryParams.subscribe(params => {
+			this.file_loaded = params['file'] || null;
+    });
+			
 		this.sortFunction = 'tipo'
 		let cedula = this.authService.getUser().cedula;
 		this.loading = true;
@@ -77,6 +85,11 @@ export class ArchivosComponent {
 				this.loading = false;
 				this.buildTreeFromList();
 
+				if (this.file_loaded !== null){
+					var file_id = this.file_loaded;
+					var found_file = this.archivos.filter(a => a.id == parseInt(file_id))[0];
+					this.seleccionarArchivo(found_file);
+				}
 			}, 
 			error => console.log(error) 
 			);
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 138d9758..e4838e89 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	
@@ -25,11 +25,13 @@
     var specialRE = /[(),;[\]`{}]/;
   var whiteCharRE = /[ \t\v\f]/; // newlines are handled in tokenizer
 
+  var INCLUIR = /include\s+([\w\_\d]+)/;
+
   // jump to definition
 
   var COMPLETE_FUNS = /([\w\_\d\-]+)(\s+\:\:.*)/;
 
-  CodeMirror.defineExtension("jumpToDefinition", function(editor, options) {
+  CodeMirror.defineExtension("jumpToDefinition", function(all_files, editor, options) {
     var word = options && options.word || WORD;
     var range = options && options.range || RANGE;
     var cur = editor.getCursor(), curLine = editor.getLine(cur.line);
@@ -64,6 +66,42 @@
           }
         }
       }
+
+      if (!found){
+        var file_found = null;
+
+        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)) && !found) {
+          var files = all_files.archivos.filter(
+            function(a){
+                return a.nombre === file[1];
+            });
+          for (var f = 0; f < files.length && !found; 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)) && !found) {
+              if (!re_digits.exec(m[1]) && (!curWord || m[1].lastIndexOf(curWord, 0) == 0) && !Object.prototype.hasOwnProperty.call(seen, m[1])) {
+                seen[m[1]] = true;
+                found = true;
+                file_found = files[f];
+              }
+            }
+          }
+        }
+
+        if (file_found !== null){
+          editor.cm.closeFunctionDefinition();          
+          window.matefunComponent.component.goToFilesPreview(file_found);
+        }
+      }
     }
   });
 
@@ -234,8 +272,6 @@
     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");
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 2fbe7f74..ae0fbec6 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	
@@ -25,11 +25,13 @@
     var specialRE = /[(),;[\]`{}]/;
   var whiteCharRE = /[ \t\v\f]/; // newlines are handled in tokenizer
 
+  var INCLUIR = /incluir\s+([\w\_\d]+)/;
+  
   // jump to definition
 
   var COMPLETE_FUNS = /([\w\_\d\-]+)(\s+\:\:.*)/;
   
-  CodeMirror.defineExtension("jumpToDefinition", function(editor, options) {
+  CodeMirror.defineExtension("jumpToDefinition", function(all_files, editor, options) {
     var word = options && options.word || WORD;
     var range = options && options.range || RANGE;
     var cur = editor.getCursor(), curLine = editor.getLine(cur.line);
@@ -65,6 +67,42 @@
         }
       }
     }
+
+    if (!found){ 
+      var file_found = null; 
+
+      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)) && !found) { 
+        var files = all_files.archivos.filter( 
+          function(a){ 
+              return a.nombre === file[1]; 
+          }); 
+        for (var f = 0; f < files.length && !found; 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)) && !found) { 
+            if (!re_digits.exec(m[1]) && (!curWord || m[1].lastIndexOf(curWord, 0) == 0) && !Object.prototype.hasOwnProperty.call(seen, m[1])) { 
+              seen[m[1]] = true; 
+              found = true; 
+              file_found = files[f]; 
+            } 
+          } 
+        } 
+      } 
+
+      if (file_found !== null){ 
+        editor.cm.closeFunctionDefinition();           
+        window.matefunComponent.component.goToFilesPreview(file_found); 
+      } 
+    } 
   });
 
   const hintWords = require('./addons/hint_words');
@@ -231,8 +269,6 @@
     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");
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 cb59844a..84e01f33 100755
--- a/Frontend Angular 4/src/app/layout/matefun/matefun.component.ts	
+++ b/Frontend Angular 4/src/app/layout/matefun/matefun.component.ts	
@@ -24,6 +24,7 @@ import { Graph2DComponent } from '../plotter/graph2D/graph2D.component';
 import { Graph3DComponent } from '../plotter/graph3D/graph3D.component';
 import { TranslateService } from '@ngx-translate/core';
 import { TitleCasePipe } from '../../shared/pipes/titlecase.pipe';
+import { Router, NavigationExtras } from '@angular/router';
 
 import 'rxjs/add/operator/catch';
 import 'rxjs/add/operator/map';
@@ -118,7 +119,8 @@ export class MateFunComponent {
         private sessionService: SessionService,
         private dialogService:DialogService,
         private usuarioService: UsuarioService,
-        public translate: TranslateService) {
+        public translate: TranslateService,
+        private router: Router) {
 
         // i18n
         this.translateService = translate;
@@ -290,6 +292,7 @@ export class MateFunComponent {
     }
 
     ngOnInit() {
+        window['matefunComponent'] = { component: this };
 
         this.ghciService.rendered(); 
 
@@ -458,7 +461,7 @@ export class MateFunComponent {
                 this.modificado = false;
             }
         }else if (event.ctrlKey && event.shiftKey && event.key == 'K') this.codemirror.instance.toggleComment();
-        else if (event.altKey && event.key == '.') this.codemirror.instance.jumpToDefinition(this.codemirror.instance.doc, event);
+        else if (event.altKey && event.key == '.') this.codemirror.instance.jumpToDefinition(this.archivosTree, this.codemirror.instance.doc, event);
     }
 
     clickEnEditor(event){
@@ -472,6 +475,14 @@ export class MateFunComponent {
         }
     }
 
+    goToFilesPreview(file_found){
+        let navigationExtras: NavigationExtras = {
+            queryParams: { file: file_found.id },
+            skipLocationChange: true
+          };
+        this.router.navigate(['/archivos'], navigationExtras);
+    }
+
     guardarArchivo(){
         var regex = /^[A-Z]/
         if(this.archivo.nombre.trim() == ""){
-- 
GitLab