Browse Source

Refactored onRenameRec to isolate the rename logic

Clément Espeute 11 months ago
parent
commit
e706b2bb05
4 changed files with 143 additions and 92 deletions
  1. 129 4
      hide/Ide.hx
  2. 12 86
      hide/view/FileTree.hx
  3. 1 1
      hide/view/Prefab.hx
  4. 1 1
      hide/view/RefViewer.hx

+ 129 - 4
hide/Ide.hx

@@ -807,7 +807,104 @@ class Ide extends hide.tools.IdeData {
 
 	}
 
-	public function filterPrefabs( callb : hrt.prefab.Prefab -> Bool ) {
+	/**
+		Iterate throught all the strings in the project that could contain a path, replacing
+		the value by what `callb` returns. The callb must call `changed()` if it changed the path.
+	**/
+	public function filterPaths(callb: (ctx : FilterPathContext) -> Void) {
+		var context = new FilterPathContext(callb);
+
+		var adaptedFilter = function(obj: String) {
+			return context.filter(obj);
+		}
+
+		function filterContent(content:Dynamic) {
+			var visited = new Map<Dynamic, Bool>();
+			function browseRec(obj:Dynamic) : Dynamic {
+				switch( Type.typeof(obj) ) {
+				case TObject:
+					if( visited.exists(obj)) return null;
+					visited.set(obj, true);
+					for( f in Reflect.fields(obj) ) {
+						var v : Dynamic = Reflect.field(obj, f);
+						v = browseRec(v);
+						if( v != null ) Reflect.setField(obj, f, v);
+					}
+				case TClass(Array):
+					if( visited.exists(obj)) return null;
+					visited.set(obj, true);
+					var arr : Array<Dynamic> = obj;
+					for( i in 0...arr.length ) {
+						var v : Dynamic = arr[i];
+						v = browseRec(v);
+						if( v != null ) arr[i] = v;
+					}
+				case TClass(String):
+					return context.filter(content);
+				default:
+				}
+				return null;
+			}
+			for( f in Reflect.fields(content) ) {
+				if (f == "children")
+					continue;
+				var v = browseRec(Reflect.field(content,f));
+				if( v != null ) Reflect.setField(content,f,v);
+			}
+		}
+
+		filterPrefabs(function(p:hrt.prefab.Prefab, path: String) {
+			context.changed = false;
+			context.contextPath = path;
+			context.openFunc = () -> openFile(context.contextPath);
+			p.source = context.filter(p.source);
+			var h = p.getHideProps();
+			if( h.onResourceRenamed != null )
+				h.onResourceRenamed(adaptedFilter);
+			else {
+				filterContent(p);
+			}
+			return context.changed;
+		});
+
+		filterProps(function(content:Dynamic, path: String) {
+			context.changed = false;
+			context.contextPath = path;
+			context.openFunc = Ide.showFileInExplorer.bind(path);
+			filterContent(content);
+			return context.changed;
+		});
+
+		context.changed = false;
+		var tmpSheets = [];
+		for( sheet in database.sheets ) {
+			if( sheet.props.dataFiles != null && sheet.lines == null ) {
+				// we already updated prefabs, no need to load data files
+				tmpSheets.push(sheet);
+				@:privateAccess sheet.sheet.lines = [];
+			}
+			context.contextPath = 'cdb:${sheet.getPath()}';
+			context.openFunc = () -> {throw "Todo";};
+			for( c in sheet.columns ) {
+				switch( c.type ) {
+				case TFile:
+					for( o in sheet.getLines() ) {
+						var v : Dynamic = context.filter(Reflect.field(o, c.name));
+						if( v != null ) Reflect.setField(o, c.name, v);
+					}
+				default:
+				}
+			}
+		}
+		if( context.changed ) {
+			saveDatabase();
+			hide.comp.cdb.Editor.refreshAll(true);
+		}
+		for( sheet in tmpSheets )
+			@:privateAccess sheet.sheet.lines = null;
+	}
+
+	public function filterPrefabs( callb : (hrt.prefab.Prefab, path: String) -> Bool) {
 		var exts = Lambda.array({iterator : @:privateAccess hrt.prefab.Prefab.extensionRegistry.keys });
 		exts.push("prefab");
 		var todo = [];
@@ -817,7 +914,7 @@ class Ide extends hide.tools.IdeData {
 			var prefab = loadPrefab(path);
 			var changed = false;
 			function filterRec(p) {
-				if( callb(p) ) changed = true;
+				if( callb(p, path) ) changed = true;
 				for( ps in p.children )
 					filterRec(ps);
 			}
@@ -829,14 +926,14 @@ class Ide extends hide.tools.IdeData {
 			t();
 	}
 
-	public function filterProps( callb : Dynamic -> Bool ) {
+	public function filterProps( callb : (data: Dynamic, path: String) -> Bool ) {
 		var exts = ["props", "json"];
 		var todo = [];
 		browseFiles(function(path) {
 			var ext = path.split(".").pop();
 			if( exts.indexOf(ext) < 0 ) return;
 			var content = parseJSON(sys.io.File.getContent(getPath(path)));
-			var changed = callb(content);
+			var changed = callb(content, path);
 			if( !changed ) return;
 			todo.push(function() sys.io.File.saveContent(getPath(path), toJSON(content)));
 		});
@@ -1362,3 +1459,31 @@ class CustomLoader extends hxd.res.Loader {
 	}
 
 }
+
+@:allow(hide.Ide)
+class FilterPathContext {
+	public var valueCurrent: String;
+	var valueChanged: String;
+
+	public var contextPath: String;
+	public var openFunc: () -> Void;
+
+	public var filterFn: (FilterPathContext) -> Void;
+
+	var changed = false;
+	public function new(filterFn: (FilterPathContext) -> Void) {
+		this.filterFn = filterFn;
+	};
+
+	public function change(newValue) : Void {
+		changed = true;
+		valueChanged = newValue;
+	}
+
+	public function filter(valueCurrent: String) {
+		this.valueCurrent = valueCurrent;
+		valueChanged = null;
+		filterFn(this);
+		return valueChanged;
+	}
+}

+ 12 - 86
hide/view/FileTree.hx

@@ -255,105 +255,31 @@ class FileTree extends FileView {
 			sys.FileSystem.rename(ide.getPath(path), ide.getPath(newPath));
 
 		var changed = false;
-		function filter(p:String) {
+		function filter(ctx: hide.Ide.FilterPathContext) {
+			var p = ctx.valueCurrent;
 			if( p == null )
-				return null;
+				return;
 			if( p == path ) {
-				changed = true;
-				return newPath;
+				ctx.change(newPath);
+				return;
 			}
 			if( p == "/"+path ) {
-				changed = true;
-				return "/"+newPath;
+				ctx.change(newPath);
+				return;
 			}
 			if( isDir ) {
 				if( StringTools.startsWith(p,path+"/") ) {
-					changed = true;
-					return newPath + p.substr(path.length);
+					ctx.change(newPath + p.substr(path.length));
+					return;
 				}
 				if( StringTools.startsWith(p,"/"+path+"/") ) {
-					changed = true;
-					return "/"+newPath + p.substr(path.length+1);
-				}
-			}
-			return p;
-		}
-
-		function filterContent(content:Dynamic) {
-			var visited = new Array<Dynamic>();
-			function browseRec(obj:Dynamic) : Dynamic {
-				switch( Type.typeof(obj) ) {
-				case TObject:
-					if( visited.indexOf(obj) >= 0 ) return null;
-					visited.push(obj);
-					for( f in Reflect.fields(obj) ) {
-						var v : Dynamic = Reflect.field(obj, f);
-						v = browseRec(v);
-						if( v != null ) Reflect.setField(obj, f, v);
-					}
-				case TClass(Array):
-					if( visited.indexOf(obj) >= 0 ) return null;
-					visited.push(obj);
-					var arr : Array<Dynamic> = obj;
-					for( i in 0...arr.length ) {
-						var v : Dynamic = arr[i];
-						v = browseRec(v);
-						if( v != null ) arr[i] = v;
-					}
-				case TClass(String):
-					return filter(obj);
-				default:
+					ctx.change("/"+newPath + p.substr(path.length+1));
+					return;
 				}
-				return null;
-			}
-			for( f in Reflect.fields(content) ) {
-				var v = browseRec(Reflect.field(content,f));
-				if( v != null ) Reflect.setField(content,f,v);
 			}
 		}
-		ide.filterPrefabs(function(p:hrt.prefab.Prefab) {
-			changed = false;
-			p.source = filter(p.source);
-			var h = p.getHideProps();
-			if( h.onResourceRenamed != null )
-				h.onResourceRenamed(filter);
-			else {
-				filterContent(p);
-			}
-			return changed;
-		});
 
-		ide.filterProps(function(content:Dynamic) {
-			changed = false;
-			filterContent(content);
-			return changed;
-		});
-
-		changed = false;
-		var tmpSheets = [];
-		for( sheet in ide.database.sheets ) {
-			if( sheet.props.dataFiles != null && sheet.lines == null ) {
-				// we already updated prefabs, no need to load data files
-				tmpSheets.push(sheet);
-				@:privateAccess sheet.sheet.lines = [];
-			}
-			for( c in sheet.columns ) {
-				switch( c.type ) {
-				case TFile:
-					for( o in sheet.getLines() ) {
-						var v : Dynamic = filter(Reflect.field(o, c.name));
-						if( v != null ) Reflect.setField(o, c.name, v);
-					}
-				default:
-				}
-			}
-		}
-		if( changed ) {
-			ide.saveDatabase();
-			hide.comp.cdb.Editor.refreshAll(true);
-		}
-		for( sheet in tmpSheets )
-			@:privateAccess sheet.sheet.lines = null;
+		ide.filterPaths(filter);
 
 		var dataDir = new haxe.io.Path(path);
 		if( dataDir.ext != "dat" ) {

+ 1 - 1
hide/view/Prefab.hx

@@ -661,7 +661,7 @@ class Prefab extends hide.view.FileView {
 			}
 		}
 
-		ide.filterProps(function(content:Dynamic) {
+		ide.filterProps(function(content:Dynamic, path: String) {
 			renameContent(content);
 			return true;
 		});

+ 1 - 1
hide/view/RefViewer.hx

@@ -13,7 +13,7 @@ class RefViewer extends hide.ui.View<Data> {
 		// onDisplay would clear the results
 		if (initialized == false)
 			showRefs([]);
-	}
+		}
 
 	public function showRefs(refs: Data, description: String = "Number of references") {
 		element.html("");