From cd05690d8ad8beade6deea94ac948064ea82f092 Mon Sep 17 00:00:00 2001 From: Joe Date: Tue, 20 Jan 2026 23:05:16 -0600 Subject: [PATCH] Refactor PatchTree and applyPatch for improved readability and logic consistency - Restructured property change checks in PatchTree.build to enhance clarity and maintainability. - Updated applyPatch to improve the handling of instance retrieval logic for removed instances. - Simplified the findDuplicateNames function in diff.lua for better readability and consistency in handling duplicate names. --- plugin/src/PatchTree.lua | 60 ++++++++++++++-------------- plugin/src/Reconciler/applyPatch.lua | 4 +- plugin/src/Reconciler/diff.lua | 10 ++--- 3 files changed, 38 insertions(+), 36 deletions(-) diff --git a/plugin/src/PatchTree.lua b/plugin/src/PatchTree.lua index 6b9cb310..72c4ff9f 100644 --- a/plugin/src/PatchTree.lua +++ b/plugin/src/PatchTree.lua @@ -257,40 +257,40 @@ function PatchTree.build(patch, instanceMap, changeListHeaders) addProp("Name", instance.Name, change.changedName) end - for prop, incoming in change.changedProperties do - local incomingSuccess, incomingValue = decodeValue(incoming, instanceMap) - local currentSuccess, currentValue = getProperty(instance, prop) + for prop, incoming in change.changedProperties do + local incomingSuccess, incomingValue = decodeValue(incoming, instanceMap) + local currentSuccess, currentValue = getProperty(instance, prop) - -- If incoming is a raw number and current is an EnumItem, convert incoming - -- to the proper EnumItem for better display (server sends Enum as raw numbers) - if - incomingSuccess - and currentSuccess - and typeof(incomingValue) == "number" - and typeof(currentValue) == "EnumItem" - then - local enumType = currentValue.EnumType - local ok, converted = pcall(function() - return enumType:FromValue(incomingValue) - end) - if ok and converted then - incomingValue = converted + -- If incoming is a raw number and current is an EnumItem, convert incoming + -- to the proper EnumItem for better display (server sends Enum as raw numbers) + if + incomingSuccess + and currentSuccess + and typeof(incomingValue) == "number" + and typeof(currentValue) == "EnumItem" + then + local enumType = currentValue.EnumType + local ok, converted = pcall(function() + return enumType:FromValue(incomingValue) + end) + if ok and converted then + incomingValue = converted + end end - end - -- Check if this is a Source property - count line differences - if prop == "Source" and currentSuccess and incomingSuccess then - hasSourceChange = true - totalLineChanges, whitespaceLineChanges = - WhitespaceUtil.CountLineDifferences(currentValue, incomingValue) - end + -- Check if this is a Source property - count line differences + if prop == "Source" and currentSuccess and incomingSuccess then + hasSourceChange = true + totalLineChanges, whitespaceLineChanges = + WhitespaceUtil.CountLineDifferences(currentValue, incomingValue) + end - addProp( - prop, - if currentSuccess then currentValue else "[Error]", - if incomingSuccess then incomingValue else select(2, next(incoming)) - ) - end + addProp( + prop, + if currentSuccess then currentValue else "[Error]", + if incomingSuccess then incomingValue else select(2, next(incoming)) + ) + end -- Count non-Source property changes local propCount = changeIndex - (if hasSourceChange then 1 else 0) diff --git a/plugin/src/Reconciler/applyPatch.lua b/plugin/src/Reconciler/applyPatch.lua index a04f1ff7..3940a1b6 100644 --- a/plugin/src/Reconciler/applyPatch.lua +++ b/plugin/src/Reconciler/applyPatch.lua @@ -29,7 +29,9 @@ local function applyPatch(instanceMap, patch) for _, removedIdOrInstance in ipairs(patch.removed) do -- Skip auto-created instances that shouldn't be synced - local instance = if Types.RbxId(removedIdOrInstance) then instanceMap.fromIds[removedIdOrInstance] else removedIdOrInstance + local instance = if Types.RbxId(removedIdOrInstance) + then instanceMap.fromIds[removedIdOrInstance] + else removedIdOrInstance if instance and Config.ignoredClassNames[instance.ClassName] then continue end diff --git a/plugin/src/Reconciler/diff.lua b/plugin/src/Reconciler/diff.lua index d16efecd..f9d89372 100644 --- a/plugin/src/Reconciler/diff.lua +++ b/plugin/src/Reconciler/diff.lua @@ -17,7 +17,10 @@ local function isEmpty(table) end -- Finds duplicate names among children and returns a set of names that are duplicated -local function findDuplicateNames(children: { Instance } | { string }, virtualInstances: { [string]: any }?): { [string]: boolean } +local function findDuplicateNames( + children: { Instance } | { string }, + virtualInstances: { [string]: any }? +): { [string]: boolean } local nameCounts: { [string]: number } = {} local duplicates: { [string]: boolean } = {} @@ -411,10 +414,7 @@ local function diff(instanceMap, virtualInstances, rootId) -- Log count of skipped duplicates if skippedDuplicateCount > 0 then - Log.warn( - "Skipped {} location(s) with duplicate-named siblings (cannot reliably sync)", - skippedDuplicateCount - ) + Log.warn("Skipped {} location(s) with duplicate-named siblings (cannot reliably sync)", skippedDuplicateCount) end if not diffSuccess then