Skip to content

Commit

Permalink
Fix renaming of local variables in sub-events (#6582)
Browse files Browse the repository at this point in the history
- Don't show in changelog
  • Loading branch information
D8H committed May 21, 2024
1 parent 9b448f1 commit 7d95e44
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 3 deletions.
7 changes: 4 additions & 3 deletions Core/GDCore/IDE/Events/ArbitraryEventsWorker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "GDCore/Events/Expression.h"
#include "GDCore/Extensions/Metadata/ParameterMetadata.h"
#include "GDCore/String.h"
#include "GDCore/Tools/Log.h"

using namespace std;

Expand All @@ -28,9 +29,6 @@ void AbstractArbitraryEventsWorker::VisitEventList(gd::EventsList& events) {
if (events[i].AcceptVisitor(*this))
events.RemoveEvent(i);
else {
if (events[i].CanHaveSubEvents())
VisitEventList(events[i].GetSubEvents());

++i;
}
}
Expand All @@ -55,6 +53,9 @@ bool AbstractArbitraryEventsWorker::VisitEvent(gd::BaseEvent& event) {
*expressionAndMetadata.first, expressionAndMetadata.second);
}

if (!shouldDelete && event.CanHaveSubEvents()) {
VisitEventList(event.GetSubEvents());
}
return shouldDelete;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -770,6 +770,92 @@ TEST_CASE("WholeProjectRefactorer::ApplyRefactoringForVariablesContainer",
REQUIRE(event.GetActions()[0].GetType() == "SetStringVariable");
}

SECTION("Can rename a local variable") {
gd::Project project;
gd::Platform platform;
SetupProjectWithDummyPlatform(project, platform);

auto &scene = project.InsertNewLayout("Scene", 0);

gd::StandardEvent &event =
dynamic_cast<gd::StandardEvent &>(scene.GetEvents().InsertNewEvent(
project, "BuiltinCommonInstructions::Standard"));
event.GetVariables().InsertNew("MyLocalVariable").SetValue(123);

{
gd::Instruction action;
action.SetType("SetNumberVariable");
action.SetParametersCount(3);
action.SetParameter(0, gd::Expression("MyLocalVariable"));
action.SetParameter(1, gd::Expression("="));
action.SetParameter(2, gd::Expression("123"));
event.GetActions().Insert(action);
}

// Do the changes and launch the refactoring.
event.GetVariables().ResetPersistentUuid();
gd::SerializerElement originalSerializedVariables;
event.GetVariables().SerializeTo(originalSerializedVariables);

event.GetVariables().Rename("MyLocalVariable", "MyRenamedLocalVariable");
auto changeset =
gd::WholeProjectRefactorer::ComputeChangesetForVariablesContainer(
originalSerializedVariables, event.GetVariables());

REQUIRE(changeset.oldToNewVariableNames.find("MyLocalVariable")->second ==
"MyRenamedLocalVariable");

gd::WholeProjectRefactorer::ApplyRefactoringForVariablesContainer(
project, event.GetVariables(), changeset);

REQUIRE(event.GetActions()[0].GetParameter(0).GetPlainString() == "MyRenamedLocalVariable");
}

SECTION("Can rename a local variable in sub-events") {
gd::Project project;
gd::Platform platform;
SetupProjectWithDummyPlatform(project, platform);

auto &scene = project.InsertNewLayout("Scene", 0);

gd::StandardEvent &event =
dynamic_cast<gd::StandardEvent &>(scene.GetEvents().InsertNewEvent(
project, "BuiltinCommonInstructions::Standard"));
event.GetVariables().InsertNew("MyLocalVariable").SetValue(123);

gd::StandardEvent &subEvent =
dynamic_cast<gd::StandardEvent &>(event.GetSubEvents().InsertNewEvent(
project, "BuiltinCommonInstructions::Standard"));

{
gd::Instruction action;
action.SetType("SetNumberVariable");
action.SetParametersCount(3);
action.SetParameter(0, gd::Expression("MyLocalVariable"));
action.SetParameter(1, gd::Expression("="));
action.SetParameter(2, gd::Expression("123"));
subEvent.GetActions().Insert(action);
}

// Do the changes and launch the refactoring.
event.GetVariables().ResetPersistentUuid();
gd::SerializerElement originalSerializedVariables;
event.GetVariables().SerializeTo(originalSerializedVariables);

event.GetVariables().Rename("MyLocalVariable", "MyRenamedLocalVariable");
auto changeset =
gd::WholeProjectRefactorer::ComputeChangesetForVariablesContainer(
originalSerializedVariables, event.GetVariables());

REQUIRE(changeset.oldToNewVariableNames.find("MyLocalVariable")->second ==
"MyRenamedLocalVariable");

gd::WholeProjectRefactorer::ApplyRefactoringForVariablesContainer(
project, event.GetVariables(), changeset);

REQUIRE(subEvent.GetActions()[0].GetParameter(0).GetPlainString() == "MyRenamedLocalVariable");
}

SECTION("Can change the instruction type of variable occurrences (function)") {
gd::Project project;
gd::Platform platform;
Expand Down

0 comments on commit 7d95e44

Please sign in to comment.