diff --git a/include/openspace/documentation/documentationengine.h b/include/openspace/documentation/documentationengine.h index 2e7ffc1e7d..980af4ee3c 100644 --- a/include/openspace/documentation/documentationengine.h +++ b/include/openspace/documentation/documentationengine.h @@ -90,6 +90,7 @@ public: void writeDocumentation() const; + nlohmann::json generateScriptEngineJson() const; private: /// The list of all Documentation%s that are stored by the DocumentationEngine std::vector _documentations; diff --git a/include/openspace/scripting/scriptengine.h b/include/openspace/scripting/scriptengine.h index 85f9224896..559db5a5b1 100644 --- a/include/openspace/scripting/scriptengine.h +++ b/include/openspace/scripting/scriptengine.h @@ -96,8 +96,7 @@ public: ShouldSendToRemote shouldSendToRemote, ScriptCallback cb = ScriptCallback()); std::vector allLuaFunctions() const; - - nlohmann::json generateJson() const; + const std::vector& allLuaLibraries() const; private: BooleanType(Replace); diff --git a/modules/server/src/topics/documentationtopic.cpp b/modules/server/src/topics/documentationtopic.cpp index 247736e67c..17632d2338 100644 --- a/modules/server/src/topics/documentationtopic.cpp +++ b/modules/server/src/topics/documentationtopic.cpp @@ -28,7 +28,7 @@ #include #include -#include +#include #include #include #include @@ -47,7 +47,7 @@ void DocumentationTopic::handleJson(const nlohmann::json& json) { // Do not parse generated json. Instead implement ability to get // ghoul::Dictionary objects from ScriptEngine, FactoryManager, and KeybindingManager. if (requestedType == "lua") { - response = global::scriptEngine->generateJson(); + response = DocEng.generateScriptEngineJson(); } else if (requestedType == "factories") { response = FactoryManager::ref().generateJson(); diff --git a/src/documentation/documentationengine.cpp b/src/documentation/documentationengine.cpp index 9bcd02f767..e76b021d24 100644 --- a/src/documentation/documentationengine.cpp +++ b/src/documentation/documentationengine.cpp @@ -49,6 +49,37 @@ namespace openspace::documentation { +nlohmann::json LuaFunctionToJson(const openspace::scripting::LuaLibrary::Function& f, + bool includeSourceLocation) +{ + using namespace openspace; + using namespace openspace::scripting; + nlohmann::json function; + function["name"] = f.name; + nlohmann::json arguments = nlohmann::json::array(); + + for (const LuaLibrary::Function::Argument& arg : f.arguments) { + nlohmann::json argument; + argument["name"] = arg.name; + argument["type"] = arg.type; + argument["defaultValue"] = arg.defaultValue.value_or(""); + arguments.push_back(argument); + } + + function["arguments"] = arguments; + function["returnType"] = f.returnType; + function["help"] = f.helpText; + + if (includeSourceLocation) { + nlohmann::json sourceLocation; + sourceLocation["file"] = f.sourceLocation.file; + sourceLocation["line"] = f.sourceLocation.line; + function["sourceLocation"] = sourceLocation; + } + + return function; +} + DocumentationEngine* DocumentationEngine::_instance = nullptr; DocumentationEngine::DuplicateDocumentationException::DuplicateDocumentationException( @@ -147,6 +178,42 @@ std::string DocumentationEngine::generateJson() const { return json.dump(); } +nlohmann::json DocumentationEngine::generateScriptEngineJson() const { + ZoneScoped; + + using namespace openspace; + using namespace scripting; + const std::vector libraries = global::scriptEngine->allLuaLibraries(); + nlohmann::json json; + + for (const LuaLibrary& l : libraries) { + + nlohmann::json library; + std::string libraryName = l.name; + // Keep the library key for backwards compatability + library["library"] = libraryName; + library["name"] = libraryName; + std::string os = "openspace"; + library["fullName"] = libraryName.empty() ? os : os + "." + libraryName; + + for (const LuaLibrary::Function& f : l.functions) { + bool hasSourceLocation = true; + library["functions"].push_back(LuaFunctionToJson(f, hasSourceLocation)); + } + + for (const LuaLibrary::Function& f : l.documentations) { + bool hasSourceLocation = false; + library["functions"].push_back(LuaFunctionToJson(f, hasSourceLocation)); + } + sortJson(library["functions"], "name"); + json.push_back(library); + + sortJson(json, "library"); + } + return json; +} + + void DocumentationEngine::writeDocumentation() const { ZoneScoped; @@ -171,7 +238,7 @@ void DocumentationEngine::writeDocumentation() const { SceneLicenseWriter writer; - nlohmann::json scripting = global::scriptEngine->generateJson(); + nlohmann::json scripting = generateScriptEngineJson(); nlohmann::json factory = FactoryManager::ref().generateJson(); nlohmann::json keybindings = global::keybindingManager->generateJson(); nlohmann::json license = writer.generateJsonGroupedByLicense(); diff --git a/src/scripting/scriptengine.cpp b/src/scripting/scriptengine.cpp index 42345a9afa..cd4ec3154c 100644 --- a/src/scripting/scriptengine.cpp +++ b/src/scripting/scriptengine.cpp @@ -81,36 +81,7 @@ namespace { return result; } - nlohmann::json toJson(const openspace::scripting::LuaLibrary::Function& f, - bool includeSourceLocation) - { - using namespace openspace; - using namespace openspace::scripting; - nlohmann::json function; - function["name"] = f.name; - nlohmann::json arguments = nlohmann::json::array(); - for (const LuaLibrary::Function::Argument& arg : f.arguments) { - nlohmann::json argument; - argument["name"] = arg.name; - argument["type"] = arg.type; - argument["defaultValue"] = arg.defaultValue.value_or(""); - arguments.push_back(argument); - } - - function["arguments"] = arguments; - function["returnType"] = f.returnType; - function["help"] = f.helpText; - - if (includeSourceLocation) { - nlohmann::json sourceLocation; - sourceLocation["file"] = f.sourceLocation.file; - sourceLocation["line"] = f.sourceLocation.line; - function["sourceLocation"] = sourceLocation; - } - - return function; - } #include "scriptengine_codegen.cpp" } // namespace @@ -448,38 +419,8 @@ std::vector ScriptEngine::allLuaFunctions() const { return result; } -nlohmann::json ScriptEngine::generateJson() const { - ZoneScoped; - - nlohmann::json json; - - for (const LuaLibrary& l : _registeredLibraries) { - using namespace openspace; - using namespace openspace::scripting; - - nlohmann::json library; - std::string libraryName = l.name; - // Keep the library key for backwards compatability - library["library"] = libraryName; - library["name"] = libraryName; - std::string os = "openspace"; - library["fullName"] = libraryName.empty() ? os : os + "." + libraryName; - - for (const LuaLibrary::Function& f : l.functions) { - bool hasSourceLocation = true; - library["functions"].push_back(toJson(f, hasSourceLocation)); - } - - for (const LuaLibrary::Function& f : l.documentations) { - bool hasSourceLocation = false; - library["functions"].push_back(toJson(f, hasSourceLocation)); - } - sortJson(library["functions"], "name"); - json.push_back(library); - - sortJson(json, "library"); - } - return json; +const std::vector& ScriptEngine::allLuaLibraries() const { + return _registeredLibraries; } void ScriptEngine::writeLog(const std::string& script) {