From 450d595cc2d0aacfcad962072f8755dc50b983b8 Mon Sep 17 00:00:00 2001
From: Franco Pariani <franco@rootstrap.com>
Date: Mon, 25 Feb 2019 10:03:20 -0300
Subject: [PATCH] Go to line in file preview with external files

---
 .../app/layout/archivos/archivos.component.ts | 13 ++++++++-
 .../matefun/codemirror/matefun-mode-EN.js     | 26 +++++++++++++++++-
 .../matefun/codemirror/matefun-mode-ES.js     | 27 +++++++++++++++++--
 .../app/layout/matefun/matefun.component.ts   |  4 +--
 4 files changed, 64 insertions(+), 6 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 deb80abd..30b8bc9b 100755
--- a/Frontend Angular 4/src/app/layout/archivos/archivos.component.ts	
+++ b/Frontend Angular 4/src/app/layout/archivos/archivos.component.ts	
@@ -48,6 +48,7 @@ export class ArchivosComponent {
 	preview: string = '';
 	directorioActual:any;
 	sortFunction:any;
+	checked: boolean;
 	configCodeMirror = JSON.parse(sessionStorage.getItem('codeMirrorConfig')) ;
 
 	constructor(
@@ -71,9 +72,12 @@ export class ArchivosComponent {
 	@ViewChild(CodemirrorComponent) codemirror: CodemirrorComponent;
 
 	ngOnInit(){
+		this.checked = false;
+
 		this.route.queryParams.subscribe(params => {
 			this.file_loaded = params['file'] || null;
-    });
+			this.function_to_find = params['word'] || null;
+    	});
 			
 		this.sortFunction = 'tipo'
 		let cedula = this.authService.getUser().cedula;
@@ -556,4 +560,11 @@ export class ArchivosComponent {
 			}
 		});
 	}
+
+	ngAfterViewChecked(){
+		if(this.codemirror !== undefined && this.codemirror.instance.doc.getValue() !== "" && this.function_to_find !== null && !this.checked){
+			this.checked = true;
+			this.codemirror.instance.jumpToDefinitionByWord(this.codemirror.instance.doc, this.function_to_find);
+		}
+	}
 }
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 e4838e89..3fc45a46 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	
@@ -99,7 +99,7 @@
 
         if (file_found !== null){
           editor.cm.closeFunctionDefinition();          
-          window.matefunComponent.component.goToFilesPreview(file_found);
+          window.matefunComponent.component.goToFilesPreview(file_found, curWord);
         }
       }
     }
@@ -119,6 +119,30 @@
   var WRITING_SET = /set\s/;
   var WRITING_COMMENT = /\{\-/;
 
+  CodeMirror.defineExtension("jumpToDefinitionByWord", function(editor, word){
+    var re_digits = new RegExp(/^\d+$/, "g");
+    var re_funs = new RegExp(FUNS.source, "g");
+    var seen = {};
+    var found = false;
+    for (var dir = -1; dir <= 1 && !found; dir += 2) {
+      var line = editor.firstLine(), endLine = Math.min(Math.max(line + dir * RANGE, editor.firstLine()), editor.lastLine()) + dir;
+      for (; line != endLine && !found; line += dir) {
+        var text = editor.getLine(line), m;
+        while ((m = re_funs.exec(text)) && !found) {
+          if (!re_digits.exec(m[1]) && (!word || m[1].lastIndexOf(word, 0) == 0) && !Object.prototype.hasOwnProperty.call(seen, m[1])) {
+            seen[m[1]] = true;
+            found = true;
+            
+            var newCursor = editor.getCursor();
+            newCursor.line = line;
+            newCursor.ch = text.indexOf(word);
+            editor.setCursor(newCursor);
+          }
+        }
+      }
+    }
+  });
+  
   // hints
 
   CodeMirror.registerHelper("hint", "anyword", function(editor, options) {
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 ae0fbec6..2d1d7a85 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	
@@ -100,7 +100,7 @@
 
       if (file_found !== null){ 
         editor.cm.closeFunctionDefinition();           
-        window.matefunComponent.component.goToFilesPreview(file_found); 
+        window.matefunComponent.component.goToFilesPreview(file_found, curWord); 
       } 
     } 
   });
@@ -119,6 +119,30 @@
 
   var RANGE = 500;
 
+  CodeMirror.defineExtension("jumpToDefinitionByWord", function(editor, word){
+    var re_digits = new RegExp(/^\d+$/, "g");
+    var re_funs = new RegExp(FUNS.source, "g");
+    var seen = {};
+    var found = false;
+    for (var dir = -1; dir <= 1 && !found; dir += 2) {
+      var line = editor.firstLine(), endLine = Math.min(Math.max(line + dir * RANGE, editor.firstLine()), editor.lastLine()) + dir;
+      for (; line != endLine && !found; line += dir) {
+        var text = editor.getLine(line), m;
+        while ((m = re_funs.exec(text)) && !found) {
+          if (!re_digits.exec(m[1]) && (!word || m[1].lastIndexOf(word, 0) == 0) && !Object.prototype.hasOwnProperty.call(seen, m[1])) {
+            seen[m[1]] = true;
+            found = true;
+            
+            var newCursor = editor.getCursor();
+            newCursor.line = line;
+            newCursor.ch = text.indexOf(word);
+            editor.setCursor(newCursor);
+          }
+        }
+      }
+    }
+  });
+
   CodeMirror.registerHelper("hint", "anyword", function(editor, options) {
     var word = options && options.word || WORD;
     var range = options && options.range || RANGE;
@@ -584,7 +608,6 @@
     blockCommentEnd: "-}",
     lineComment: "--"
   };
-
 });
 
 CodeMirror.defineMIME("text/x-matefun", "matefun-ES");
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 84e01f33..758b50b7 100755
--- a/Frontend Angular 4/src/app/layout/matefun/matefun.component.ts	
+++ b/Frontend Angular 4/src/app/layout/matefun/matefun.component.ts	
@@ -475,9 +475,9 @@ export class MateFunComponent {
         }
     }
 
-    goToFilesPreview(file_found){
+    goToFilesPreview(file_found, word){
         let navigationExtras: NavigationExtras = {
-            queryParams: { file: file_found.id },
+            queryParams: { file: file_found.id, word: word },
             skipLocationChange: true
           };
         this.router.navigate(['/archivos'], navigationExtras);
-- 
GitLab