Skip to content

Commit ce3e006

Browse files
committed
👼Script: 'script_editor.as' fixed text sliding in folded regions
Problem: The character offsets weren't updated, so when you edited text above a folded region, it's content would come out corrupted after unfolding. This also factors out helper function `mergeCollectedFoldingRegionsWithExisting()` with extra commentary.
1 parent a7b0820 commit ce3e006

File tree

1 file changed

+30
-18
lines changed

1 file changed

+30
-18
lines changed

resources/scripts/script_editor.as

+30-18
Original file line numberDiff line numberDiff line change
@@ -1452,14 +1452,24 @@ class ScriptEditorTab
14521452
this.totalChars++;
14531453
}
14541454

1455+
this.matchCollectedFoldingRegionsWithExisting(collectedRegions);
1456+
}
1457+
1458+
private void mergeCollectedFoldingRegionsWithExisting(dictionary&in collectedRegions) // helper for `analyzeBuffer()`
1459+
{
1460+
// The `ImGui::TextInputMultiline()` doesn't provide info on where new characters were inserted/removed (or does it? TODO research ImGuiInputTextCallbackData)
1461+
// Either way, we simply scan the buffer again, collect the regions and then match them with those already existing.
1462+
// This relies on the #region/#endregion tags always being in the buffer, even if folded.
1463+
// ---------------------------------------------------------------------------------------------------------------
1464+
14551465
// prune broken regions (missing '#endregion' -> RegionInfo is null)
14561466
array<string>@ collectedRegionNames = collectedRegions.getKeys();
14571467
for (uint i = 0; i< collectedRegionNames.length(); i++)
14581468
{
14591469
RegionInfo@ regionInfo = findRegion(collectedRegions, collectedRegionNames[i]);
14601470
if (@regionInfo == null)
14611471
{
1462-
game.log ("DBG pruning broken region '" + collectedRegionNames[i] + "'");
1472+
//game.log ("DBG mergeCollectedFoldingRegionsWithExisting(): pruning broken region '" + collectedRegionNames[i] + "'");
14631473
collectedRegions.delete(collectedRegionNames[i]);
14641474
}
14651475
}
@@ -1472,7 +1482,7 @@ class ScriptEditorTab
14721482
RegionInfo@ oldRegionInfo = findRegion(this.workBufferRegions, oldRegionNames[i]);
14731483
if (isGone && oldRegionInfo.isFolded)
14741484
{
1475-
//game.log ("DBG analyzeBuffer(): region '" + oldRegionNames[i] + "' has gone orphan.");
1485+
//game.log ("DBG mergeCollectedFoldingRegionsWithExisting(): region '" + oldRegionNames[i] + "' has gone orphan.");
14761486
oldRegionInfo.isOrphan = true;
14771487
}
14781488
}
@@ -1482,32 +1492,34 @@ class ScriptEditorTab
14821492
for (uint i = 0; i < newRegionNames.length(); i++)
14831493
{
14841494
RegionInfo@ newRegionInfo = findRegion(collectedRegions, newRegionNames[i]);
1485-
RegionInfo@ oldRegionInfo = findRegion(this.workBufferRegions, newRegionNames[i]);
1486-
if (@oldRegionInfo == null)
1495+
RegionInfo@ existingRegionInfo = findRegion(this.workBufferRegions, newRegionNames[i]);
1496+
if (@existingRegionInfo == null)
14871497
{
1488-
//game.log("DBG analyzeBuffer(): A brand new region '"+newRegionNames[i]+"' was created");
1498+
//game.log("DBG mergeCollectedFoldingRegionsWithExisting(): A brand new region '"+newRegionNames[i]+"' was created");
14891499
this.workBufferRegions[newRegionNames[i]] = newRegionInfo;
14901500
}
14911501
else
14921502
{
1493-
/*game.log("DBG analyzeBuffer(): Region '"+newRegionNames[i]+"' already exists:"
1494-
+" lineCount="+oldRegionInfo.regionLineCount+" (new:"+newRegionInfo.regionLineCount+")"
1495-
+" regionBodyStartOffset="+oldRegionInfo.regionBodyStartOffset+" (new:"+newRegionInfo.regionBodyStartOffset+")"
1496-
+" regionBodyNumChars="+oldRegionInfo.regionBodyNumChars+" (new:"+newRegionInfo.regionBodyNumChars+")"
1497-
+" isOrphan="+oldRegionInfo.isOrphan+" isFolded="+newRegionInfo.isFolded);
1503+
/*game.log("DBG mergeCollectedFoldingRegionsWithExisting(): Region '"+newRegionNames[i]+"' already exists:"
1504+
+" lineCount="+existingRegionInfo.regionLineCount+" (new:"+newRegionInfo.regionLineCount+")"
1505+
+" regionBodyStartOffset="+existingRegionInfo.regionBodyStartOffset+" (new:"+newRegionInfo.regionBodyStartOffset+")"
1506+
+" regionBodyNumChars="+existingRegionInfo.regionBodyNumChars+" (new:"+newRegionInfo.regionBodyNumChars+")"
1507+
+" isOrphan="+existingRegionInfo.isOrphan+" isFolded="+newRegionInfo.isFolded);
14981508
*/
14991509

1500-
if (oldRegionInfo.isOrphan && newRegionInfo.regionLineCount == 0)
1510+
existingRegionInfo.regionBodyStartOffset = newRegionInfo.regionBodyStartOffset;
1511+
1512+
if (!existingRegionInfo.isFolded)
15011513
{
1502-
//game.log("DBG analyzeBuffer(): An orphan region '"+newRegionNames[i]+"' has resurfaced");
1503-
oldRegionInfo.regionBodyStartOffset = newRegionInfo.regionBodyStartOffset;
1504-
oldRegionInfo.isOrphan = false;
1514+
//game.log("DBG mergeCollectedFoldingRegionsWithExisting(): An existing UNFOLDED region '"+newRegionNames[i]+"' was updated ~ text may have changed");
1515+
existingRegionInfo.regionLineCount = newRegionInfo.regionLineCount;
1516+
existingRegionInfo.regionBodyNumChars = newRegionInfo.regionBodyNumChars;
1517+
existingRegionInfo.regionStartsAtLineIndex = newRegionInfo.regionStartsAtLineIndex;
15051518
}
1506-
else if (!oldRegionInfo.isFolded)
1519+
else if (existingRegionInfo.isOrphan && newRegionInfo.regionLineCount == 0)
15071520
{
1508-
//game.log("DBG analyzeBuffer(): An existing region '"+newRegionNames[i]+"' was updated");
1509-
newRegionInfo.isFoldedBackup = oldRegionInfo.isFoldedBackup; // don't lose backups!
1510-
this.workBufferRegions[newRegionNames[i]] = newRegionInfo;
1521+
//game.log("DBG mergeCollectedFoldingRegionsWithExisting(): An orphan (so logically FOLDED) region '"+newRegionNames[i]+"' has resurfaced");
1522+
existingRegionInfo.isOrphan = false;
15111523
}
15121524
}
15131525
}

0 commit comments

Comments
 (0)