Skip to content

Commit

Permalink
Scripting: Add script documentation cache to project
Browse files Browse the repository at this point in the history
This PR adds a script documentation cache in the project folder.
It is loaded at alongside native documentation caches. This makes
scripts fully accessible through Search Help, including their
members, etc, right from project start, without having to compile
every single script.
  • Loading branch information
anvilfolk committed Aug 20, 2024
1 parent da5f398 commit 5dca40d
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 0 deletions.
8 changes: 8 additions & 0 deletions editor/doc_tools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,14 @@ void DocTools::remove_doc(const String &p_class_name) {
class_list.erase(p_class_name);
}

void DocTools::remove_script_doc_by_path(const String &p_path) {
for (KeyValue<String, DocData::ClassDoc> &E : class_list)
if (E.value.is_script_doc && E.value.script_path == p_path) {
remove_doc(E.key);
return;
}
}

bool DocTools::has_doc(const String &p_class_name) {
if (p_class_name.is_empty()) {
return false;
Expand Down
1 change: 1 addition & 0 deletions editor/doc_tools.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class DocTools {
void merge_from(const DocTools &p_data);
void add_doc(const DocData::ClassDoc &p_class_doc);
void remove_doc(const String &p_class_name);
void remove_script_doc_by_path(const String &p_path);
bool has_doc(const String &p_class_name);
enum GenerateFlags {
GENERATE_FLAG_SKIP_BASIC_TYPES = (1 << 0),
Expand Down
6 changes: 6 additions & 0 deletions editor/editor_file_system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1864,6 +1864,12 @@ void EditorFileSystem::_update_script_documentation() {

if (!efd || index < 0) {
// The file was removed
DocTools *doc = EditorHelp::get_doc_data();
if (doc)
doc->remove_script_doc_by_path(path);
else {
ERR_PRINT("Cannot update documentation for deleted file: EditorHelp::DocTools not initialized.");
}
continue;
}

Expand Down
39 changes: 39 additions & 0 deletions editor/editor_help.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include "core/input/input.h"
#include "core/object/script_language.h"
#include "core/os/keyboard.h"
#include "core/os/time.h"
#include "core/string/string_builder.h"
#include "core/version_generated.gen.h"
#include "editor/doc_data_compressed.gen.h"
Expand Down Expand Up @@ -2859,6 +2860,10 @@ String EditorHelp::get_cache_full_path() {
return EditorPaths::get_singleton()->get_cache_dir().path_join(vformat("editor_doc_cache-%d.%d.res", VERSION_MAJOR, VERSION_MINOR));
}

String EditorHelp::get_script_doc_cache_full_path() {
return EditorPaths::get_singleton()->get_project_data_dir().path_join("editor_script_doc_cache.res");
}

void EditorHelp::load_xml_buffer(const uint8_t *p_buffer, int p_size) {
if (!ext_doc) {
ext_doc = memnew(DocTools);
Expand Down Expand Up @@ -2889,6 +2894,7 @@ void EditorHelp::_load_doc_thread(void *p_udata) {
doc->add_doc(DocData::ClassDoc::from_dict(classes[i]));
}

_load_script_doc_cache();
// Extensions' docs are not cached. Generate them now (on the main thread).
callable_mp_static(&EditorHelp::_gen_extensions_docs).call_deferred();
} else {
Expand Down Expand Up @@ -2923,6 +2929,9 @@ void EditorHelp::_gen_doc_thread(void *p_udata) {
ERR_PRINT("Cannot save editor help cache (" + get_cache_full_path() + ").");
}

// Load script docs after native ones are cached so native cache doesn't contain script docs.
_load_script_doc_cache();

OS::get_singleton()->benchmark_end_measure("EditorHelp", vformat("Generate Documentation (Run %d)", doc_generation_count));
}

Expand All @@ -2935,6 +2944,36 @@ void EditorHelp::_gen_extensions_docs() {
}
}

// Called in 1) _load_doc_thread() from worker thread after other caches have been read,
// or 2) in _gen_doc_thread() **only from main thread**. This avoids worker thread loading
// script cache and main thread generating native cache accessing DocTools simultaneously.
void EditorHelp::_load_script_doc_cache() {
Ref<Resource> script_doc_cache_res = ResourceLoader::load(get_script_doc_cache_full_path());
if (script_doc_cache_res.is_valid()) {
Array classes = script_doc_cache_res->get_meta("classes", Array());
for (int i = 0; i < classes.size(); i++) {
DocData::ClassDoc cd = DocData::ClassDoc::from_dict(classes[i]);
if (FileAccess::create(FileAccess::ACCESS_RESOURCES)->file_exists(cd.script_path))
doc->add_doc(cd);
}
}
}

void EditorHelp::save_script_doc_cache() {
Ref<Resource> cache_res;
cache_res.instantiate();
Array classes;
for (const KeyValue<String, DocData::ClassDoc> &E : doc->class_list)
if (E.value.is_script_doc)
classes.push_back(DocData::ClassDoc::to_dict(E.value));

cache_res->set_meta("classes", classes);
Error err = ResourceSaver::save(cache_res, get_script_doc_cache_full_path(), ResourceSaver::FLAG_COMPRESS);
if (err) {
ERR_PRINT("Cannot save editor help script documentation cache (" + get_script_doc_cache_full_path() + ").");
}
}

void EditorHelp::generate_doc(bool p_use_cache) {
doc_generation_count++;
OS::get_singleton()->benchmark_begin_measure("EditorHelp", vformat("Generate Documentation (Run %d)", doc_generation_count));
Expand Down
3 changes: 3 additions & 0 deletions editor/editor_help.h
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ class EditorHelp : public VBoxContainer {
static void _load_doc_thread(void *p_udata);
static void _gen_doc_thread(void *p_udata);
static void _gen_extensions_docs();
static void _load_script_doc_cache();
static void _compute_doc_version_hash();

struct PropertyCompare {
Expand All @@ -221,7 +222,9 @@ class EditorHelp : public VBoxContainer {
static void generate_doc(bool p_use_cache = true);
static DocTools *get_doc_data();
static void cleanup_doc();
static void save_script_doc_cache();
static String get_cache_full_path();
static String get_script_doc_cache_full_path();

static void load_xml_buffer(const uint8_t *p_buffer, int p_size);
static void remove_class(const String &p_class);
Expand Down
1 change: 1 addition & 0 deletions editor/editor_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7838,6 +7838,7 @@ EditorNode::~EditorNode() {
ResourceImporterScene::clean_up_importer_plugins();

remove_print_handler(&print_handler);
EditorHelp::save_script_doc_cache();
EditorHelp::cleanup_doc();
#if defined(MODULE_GDSCRIPT_ENABLED) || defined(MODULE_MONO_ENABLED)
EditorHelpHighlighter::free_singleton();
Expand Down

0 comments on commit 5dca40d

Please sign in to comment.