| 1 |
#pragma once |
| 2 |
|
| 3 |
#include "iscriptinterface.h" |
| 4 |
#include "ideclmanager.h" |
| 5 |
#include "os/path.h" |
| 6 |
|
| 7 |
namespace script |
| 8 |
{ |
| 9 |
|
| 10 |
// Wrapper class to represent an IDeclaration object in Python |
| 11 |
class ScriptDeclaration |
| 12 |
{ |
| 13 |
private: |
| 14 |
decl::IDeclaration::Ptr _decl; |
| 15 |
|
| 16 |
public: |
| 17 |
ScriptDeclaration(const decl::IDeclaration::Ptr& decl) : |
| 18 |
_decl(decl) |
| 19 |
{} |
| 20 |
|
| 21 |
const decl::IDeclaration::Ptr& get() const |
| 22 |
{ |
| 23 |
return _decl; |
| 24 |
} |
| 25 |
|
| 26 |
bool isNull() const |
| 27 |
{ |
| 28 |
return !_decl; |
| 29 |
} |
| 30 |
|
| 31 |
const std::string& getDeclName() const |
| 32 |
{ |
| 33 |
static std::string _emptyName; |
| 34 |
return _decl ? _decl->getDeclName() : _emptyName; |
| 35 |
} |
| 36 |
|
| 37 |
decl::Type getDeclType() const |
| 38 |
{ |
| 39 |
return _decl ? _decl->getDeclType() : decl::Type::None; |
| 40 |
} |
| 41 |
|
| 42 |
const decl::DeclarationBlockSyntax& getBlockSyntax() |
| 43 |
{ |
| 44 |
static decl::DeclarationBlockSyntax _emptySyntax; |
| 45 |
return _decl ? _decl->getBlockSyntax() : _emptySyntax; |
| 46 |
} |
| 47 |
|
| 48 |
void setBlockSyntax(const decl::DeclarationBlockSyntax& block) |
| 49 |
{ |
| 50 |
if (_decl) |
| 51 |
{ |
| 52 |
_decl->setBlockSyntax(block); |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
std::string getDeclFilePath() const |
| 57 |
{ |
| 58 |
return _decl ? _decl->getDeclFilePath() : ""; |
| 59 |
} |
| 60 |
|
| 61 |
void setDeclFilePath(const std::string& folder, const std::string& filename) |
| 62 |
{ |
| 63 |
if (!_decl) return; |
| 64 |
|
| 65 |
_decl->setFileInfo(vfs::FileInfo(os::standardPathWithSlash(folder), filename, vfs::Visibility::NORMAL)); |
| 66 |
} |
| 67 |
}; |
| 68 |
|
| 69 |
class DeclarationVisitor |
| 70 |
{ |
| 71 |
public: |
| 72 |
virtual ~DeclarationVisitor() {} |
| 73 |
virtual void visit(const decl::IDeclaration::Ptr& declaration) = 0; |
| 74 |
}; |
| 75 |
|
| 76 |
/** |
| 77 |
* Exposes the GlobalDeclarationManager interface to scripts |
| 78 |
*/ |
| 79 |
class DeclarationManagerInterface : |
| 80 |
public IScriptInterface |
| 81 |
{ |
| 82 |
public: |
| 83 |
// Mapped methods |
| 84 |
ScriptDeclaration findDeclaration(decl::Type type, const std::string& name); |
| 85 |
ScriptDeclaration findOrCreateDeclaration(decl::Type type, const std::string& name); |
| 86 |
void foreachDeclaration(decl::Type type, DeclarationVisitor& visitor); |
| 87 |
bool renameDeclaration(decl::Type type, const std::string& oldName, const std::string& newName); |
| 88 |
void removeDeclaration(decl::Type type, const std::string& name); |
| 89 |
void reloadDeclarations(); |
| 90 |
void saveDeclaration(const ScriptDeclaration& decl); |
| 91 |
|
| 92 |
// IScriptInterface implementation |
| 93 |
void registerInterface(py::module& scope, py::dict& globals) override; |
| 94 |
}; |
| 95 |
|
| 96 |
} |