Skip to content

Commit

Permalink
#5977: Introduce changed signal on IDeclaration
Browse files Browse the repository at this point in the history
  • Loading branch information
codereader committed Jul 22, 2022
1 parent d0c668f commit 8f94f81
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
5 changes: 5 additions & 0 deletions include/ideclmanager.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <map>
#include <sigc++/signal.h>
#include "imodule.h"
#include "ifilesystem.h"
#include "ModResource.h"
Expand Down Expand Up @@ -69,6 +70,10 @@ class IDeclaration :

// Sets the internally used parse epoch counter
virtual void setParseStamp(std::size_t parseStamp) = 0;

// Fired when this declaration changed (i.e. as result of a reloadDecls
// operation or a change in an editor).
virtual sigc::signal<void>& signal_DeclarationChanged() = 0;
};

// Factory interface being able to create a single declaration type
Expand Down
9 changes: 9 additions & 0 deletions libs/decl/DeclarationBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ class DeclarationBase :

bool _parsed;

sigc::signal<void> _changedSignal;

protected:
DeclarationBase(decl::Type type, const std::string& name) :
_name(name),
Expand Down Expand Up @@ -71,6 +73,8 @@ class DeclarationBase :
_parsed = false;

onSyntaxBlockAssigned(_declBlock);

_changedSignal.emit();
}

std::string getModName() const final
Expand Down Expand Up @@ -98,6 +102,11 @@ class DeclarationBase :
_parseStamp = parseStamp;
}

sigc::signal<void>& signal_DeclarationChanged() final
{
return _changedSignal;
}

protected:
// Defines the whitespace characters used by the DefTokeniser to separate tokens
virtual const char* getWhitespaceDelimiters() const
Expand Down
18 changes: 18 additions & 0 deletions test/DeclManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1079,4 +1079,22 @@ TEST_F(DeclManagerTest, SetDeclFileInfo)
EXPECT_EQ(decl->getBlockSyntax().fileInfo.visibility, vfs::Visibility::HIDDEN);
}

TEST_F(DeclManagerTest, ChangedSignal)
{
GlobalDeclarationManager().registerDeclType("testdecl", std::make_shared<TestDeclarationCreator>());
GlobalDeclarationManager().registerDeclFolder(decl::Type::TestDecl, TEST_DECL_FOLDER, ".decl");

auto decl = GlobalDeclarationManager().findDeclaration(decl::Type::TestDecl, "decl/numbers/3");

std::size_t changedSignalReceiveCount = 0;
decl->signal_DeclarationChanged().connect([&] { ++changedSignalReceiveCount; });

// Assign a new syntax block, this should emit the signal
auto syntax = decl->getBlockSyntax();
syntax.contents += "\n";
decl->setBlockSyntax(syntax);

EXPECT_EQ(changedSignalReceiveCount, 1) << "Changed signal should have fired once after assigning the syntax block";
}

}

0 comments on commit 8f94f81

Please sign in to comment.