فهرست منبع

automatic reload/recheck of scripts when api file changed

Nicolas Cannasse 6 سال پیش
والد
کامیت
38255568aa
1فایلهای تغییر یافته به همراه27 افزوده شده و 9 حذف شده
  1. 27 9
      hide/comp/ScriptEditor.hx

+ 27 - 9
hide/comp/ScriptEditor.hx

@@ -12,24 +12,33 @@ class ScriptChecker {
 	static var TYPES_SAVE = new Map();
 	static var TYPES_SAVE = new Map();
 	static var ERROR_SAVE = new Map();
 	static var ERROR_SAVE = new Map();
 	var ide : hide.Ide;
 	var ide : hide.Ide;
+	var apiFiles : Array<String>;
+	var config : hide.Config;
+	var documentName : String;
+	var constants : Map<String,Dynamic>;
 	public var checker : hscript.Checker;
 	public var checker : hscript.Checker;
 
 
 	public function new( config : hide.Config, documentName : String, ?constants : Map<String,Dynamic> ) {
 	public function new( config : hide.Config, documentName : String, ?constants : Map<String,Dynamic> ) {
+		this.config = config;
+		this.documentName = documentName;
+		this.constants = constants == null ? new Map() : constants;
+		ide = hide.Ide.inst;
+		apiFiles = config.get("script.api.files");
+		reload();
+	}
 
 
+	public function reload() {
 		checker = new hscript.Checker();
 		checker = new hscript.Checker();
-		ide = hide.Ide.inst;
 
 
-		var files : Array<String> = config.get("script.api.files");
-		if( files != null && files.length >= 0 ) {
-			var types = TYPES_SAVE.get(files.join(";"));
+		if( apiFiles != null && apiFiles.length >= 0 ) {
+			var types = TYPES_SAVE.get(apiFiles.join(";"));
 			if( types == null ) {
 			if( types == null ) {
 				types = new hscript.Checker.CheckerTypes();
 				types = new hscript.Checker.CheckerTypes();
-				for( f in files ) {
-					// TODO : reload + recheck script when modified
+				for( f in apiFiles ) {
 					var content = try sys.io.File.getContent(ide.getPath(f)) catch( e : Dynamic ) { error(""+e); continue; };
 					var content = try sys.io.File.getContent(ide.getPath(f)) catch( e : Dynamic ) { error(""+e); continue; };
 					types.addXmlApi(Xml.parse(content).firstElement());
 					types.addXmlApi(Xml.parse(content).firstElement());
 				}
 				}
-				TYPES_SAVE.set(files.join(";"), types);
+				TYPES_SAVE.set(apiFiles.join(";"), types);
 			}
 			}
 			checker.types = types;
 			checker.types = types;
 		}
 		}
@@ -52,7 +61,7 @@ class ScriptChecker {
 					var fields = [];
 					var fields = [];
 					while( path.length > 0 ) {
 					while( path.length > 0 ) {
 						var name = path.join(".");
 						var name = path.join(".");
-						if( constants != null && constants.exists(name) ) {
+						if( constants.exists(name) ) {
 							var value : Dynamic = constants.get(name);
 							var value : Dynamic = constants.get(name);
 							for( f in fields )
 							for( f in fields )
 								value = Reflect.field(value, f);
 								value = Reflect.field(value, f);
@@ -63,7 +72,7 @@ class ScriptChecker {
 					}
 					}
 				}
 				}
 				if( t == null ) {
 				if( t == null ) {
-					error('Global type $tname not found in $files ($f)');
+					error('Global type $tname not found in $apiFiles ($f)');
 					continue;
 					continue;
 				}
 				}
 				checker.setGlobal(f, t);
 				checker.setGlobal(f, t);
@@ -192,6 +201,15 @@ class ScriptEditor extends CodeEditor {
 		if( checker == null ) {
 		if( checker == null ) {
 			checker = new ScriptChecker(new hide.Config(),"");
 			checker = new ScriptChecker(new hide.Config(),"");
 			checkTypes = false;
 			checkTypes = false;
+		} else {
+			var files = @:privateAccess checker.apiFiles;
+			if( files != null ) {
+				for( f in files )
+					ide.fileWatcher.register(f, function() {
+						@:privateAccess ScriptChecker.TYPES_SAVE = [];
+						haxe.Timer.delay(function() { checker.reload(); doCheckScript(); }, 100);
+					}, root);
+			}
 		}
 		}
 		this.checker = checker;
 		this.checker = checker;
 		onChanged = doCheckScript;
 		onChanged = doCheckScript;