diff --git a/core/object/editor_language.h b/core/object/editor_language.h new file mode 100644 index 0000000000..6bbd68df21 --- /dev/null +++ b/core/object/editor_language.h @@ -0,0 +1,65 @@ +/**************************************************************************/ +/* editor_language.h */ +/**************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/**************************************************************************/ +/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ +/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/**************************************************************************/ + +#pragma once + +// TODO: This file resides in `core/` while migrating `ScriptLanguage` features to prevent `editor/` includes in core. It should be moved to `editor/` when those includes are not needed anymore. +#ifdef TOOLS_ENABLED + +#include "core/object/script_language.h" + +/** + * Interface for supporting semantic language features in the builtin script editor. + * + * API design should keep the LSP spec in mind: https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/ since the GDScript language server is also using this API. + * + * Note: This class is unstable and work-in-progress, stability is only ensured for the things currently exposed through `ScriptLanguageExtension`. + * The goal is to move all editor functionality out of `ScriptLanguage` over time. + */ +class EditorLanguage { +public: + /** + * Called by the editor to request a list of `CodeCompletionOptions` from the language. + * + * The language MAY also calculate a hint for the call signature at the given position. See `r_call_hint` and `r_force` for more details. + * + * @param p_code The current content of the source fragment. Will always contain the special sentinel char 0xFFFF at the cursor position. The content might differ from the state on disk, the exact behavior in that case is up to the implementation. + * @param p_path The path which identifies the source fragment. Implementations MAY support paths to builtin resources, or return an error for those. + * @param p_owner If the source fragment is somehow tied to an object (e.g. the currently open scene) the editor will pass it to the language. Implementations MAY respect this in their analysis, e.g. for GDScript get node literals. + * @param r_options The returned options. Can be empty. + * @param r_force If `false` a non-empty signature hint will take priority over completion. If `true` completion will take priority. Might be removed in the future in favor of showing both. + * @param r_call_hint The returned signature hint. Can be empty. + */ + virtual Error complete_code(const String &p_code, const String &p_path, Object *p_owner, List *r_options, bool &r_force, String &r_call_hint) { return ERR_UNAVAILABLE; } + + virtual ~EditorLanguage() = default; +}; + +#endif // TOOLS_ENABLED diff --git a/core/object/script_language.h b/core/object/script_language.h index 374d31da97..4fd8c6c6de 100644 --- a/core/object/script_language.h +++ b/core/object/script_language.h @@ -41,6 +41,10 @@ class ScriptLanguage; template class TypedArray; +#ifdef TOOLS_ENABLED +class EditorLanguage; +#endif // TOOLS_ENABLED + typedef void (*ScriptEditRequestFunction)(const String &p_path); class ScriptServer { @@ -221,6 +225,11 @@ public: virtual void finish() = 0; /* EDITOR FUNCTIONS */ +#ifdef TOOLS_ENABLED + // Must not return `nullptr`. `EditorLanguage` can be used as default implementation for languages without editor support. + virtual EditorLanguage *get_editor_language() = 0; +#endif // TOOLS_ENABLED + struct Warning { /// One-based. int start_line = 0; @@ -360,8 +369,6 @@ public: TypedArray charac; }; - virtual Error complete_code(const String &p_code, const String &p_path, Object *p_owner, List *r_options, bool &r_force, String &r_call_hint) { return ERR_UNAVAILABLE; } - enum LookupResultType { LOOKUP_RESULT_SCRIPT_LOCATION, // Use if none of the options below apply. LOOKUP_RESULT_CLASS, diff --git a/core/object/script_language_extension.cpp b/core/object/script_language_extension.cpp index 73953eed91..02f2eb86d1 100644 --- a/core/object/script_language_extension.cpp +++ b/core/object/script_language_extension.cpp @@ -32,6 +32,18 @@ #include "core/object/class_db.h" +ScriptLanguageExtension::ScriptLanguageExtension() { +#ifdef TOOLS_ENABLED + editor_adapter = memnew(EditorAdapter(this)); +#endif // TOOLS_ENABLED +} + +ScriptLanguageExtension::~ScriptLanguageExtension() { +#ifdef TOOLS_ENABLED + memdelete(editor_adapter); +#endif // TOOLS_ENABLED +} + void ScriptExtension::_bind_methods() { GDVIRTUAL_BIND(_editor_can_reload_from_file); GDVIRTUAL_BIND(_placeholder_erased, "placeholder"); diff --git a/core/object/script_language_extension.h b/core/object/script_language_extension.h index 86f1a63084..565a880715 100644 --- a/core/object/script_language_extension.h +++ b/core/object/script_language_extension.h @@ -36,6 +36,10 @@ #include "core/variant/native_ptr.h" #include "core/variant/typed_array.h" +#ifdef TOOLS_ENABLED +#include "core/object/editor_language.h" +#endif // TOOLS_ENABLED + class ScriptExtension : public Script { GDCLASS(ScriptExtension, Script) @@ -238,6 +242,28 @@ public: /* EDITOR FUNCTIONS */ +#ifdef TOOLS_ENABLED +private: + class EditorAdapter final : public EditorLanguage { + ScriptLanguageExtension *script_language = nullptr; + + public: + virtual Error complete_code(const String &p_code, const String &p_path, Object *p_owner, List *r_options, bool &r_force, String &r_call_hint) override { + return script_language->complete_code(p_code, p_path, p_owner, r_options, r_force, r_call_hint); + } + + EditorAdapter(ScriptLanguageExtension *p_script_language) { + script_language = p_script_language; + } + }; + EditorAdapter *editor_adapter; + +public: + virtual EditorLanguage *get_editor_language() override { + return editor_adapter; + } +#endif // TOOLS_ENABLED + GDVIRTUAL0RC_REQUIRED(Vector, _get_reserved_words) virtual Vector get_reserved_words() const override { @@ -390,7 +416,7 @@ public: GDVIRTUAL3RC_REQUIRED(Dictionary, _complete_code, const String &, const String &, Object *) - virtual Error complete_code(const String &p_code, const String &p_path, Object *p_owner, List *r_options, bool &r_force, String &r_call_hint) override { + virtual Error complete_code(const String &p_code, const String &p_path, Object *p_owner, List *r_options, bool &r_force, String &r_call_hint) { Dictionary ret; GDVIRTUAL_CALL(_complete_code, p_code, p_path, p_owner, ret); if (!ret.has("result")) { @@ -674,6 +700,9 @@ public: } return ret["name"]; } + + ScriptLanguageExtension(); + virtual ~ScriptLanguageExtension(); }; VARIANT_ENUM_CAST(ScriptLanguageExtension::LookupResultType) diff --git a/editor/script/script_text_editor.cpp b/editor/script/script_text_editor.cpp index 1639f33f36..fac7ad792a 100644 --- a/editor/script/script_text_editor.cpp +++ b/editor/script/script_text_editor.cpp @@ -38,6 +38,7 @@ #include "core/math/expression.h" #include "core/object/callable_mp.h" #include "core/object/class_db.h" +#include "core/object/editor_language.h" #include "core/os/keyboard.h" #include "editor/debugger/editor_debugger_node.h" #include "editor/doc/editor_help.h" @@ -1159,7 +1160,7 @@ void ScriptTextEditor::_code_complete_script(const String &p_code, Listget_language()->complete_code(p_code, script->get_path(), base, r_options, r_force, hint); + Error err = script->get_language()->get_editor_language()->complete_code(p_code, script->get_path(), base, r_options, r_force, hint); if (err == OK) { code_editor->get_text_editor()->set_code_hint(hint); diff --git a/modules/gdscript/editor/gdscript_editor_language.h b/modules/gdscript/editor/gdscript_editor_language.h new file mode 100644 index 0000000000..5182d5ab7e --- /dev/null +++ b/modules/gdscript/editor/gdscript_editor_language.h @@ -0,0 +1,52 @@ +/**************************************************************************/ +/* gdscript_editor_language.h */ +/**************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/**************************************************************************/ +/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ +/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/**************************************************************************/ + +#pragma once + +#include "core/object/editor_language.h" + +class GDScriptEditorLanguage final : public EditorLanguage { + static GDScriptEditorLanguage *singleton; + +public: + _FORCE_INLINE_ static GDScriptEditorLanguage *get_singleton() { return singleton; } + + virtual Error complete_code(const String &p_code, const String &p_path, Object *p_owner, List *r_options, bool &r_force, String &r_call_hint) override; + + GDScriptEditorLanguage() { + ERR_FAIL_COND(singleton != nullptr); + singleton = this; + } + ~GDScriptEditorLanguage() { + if (singleton == this) { + singleton = nullptr; + } + } +}; diff --git a/modules/gdscript/gdscript.h b/modules/gdscript/gdscript.h index b88596b6fe..6a5577d538 100644 --- a/modules/gdscript/gdscript.h +++ b/modules/gdscript/gdscript.h @@ -590,6 +590,10 @@ public: virtual void finish() override; /* EDITOR FUNCTIONS */ +#ifdef TOOLS_ENABLED + virtual EditorLanguage *get_editor_language() override; +#endif // TOOLS_ENABLED + virtual Vector get_reserved_words() const override; virtual bool is_control_flow_keyword(const String &p_keywords) const override; virtual Vector get_comment_delimiters() const override; @@ -604,7 +608,6 @@ public: virtual bool can_inherit_from_file() const override { return true; } virtual int find_function(const String &p_function, const String &p_code) const override; virtual String make_function(const String &p_class, const String &p_name, const PackedStringArray &p_args) const override; - virtual Error complete_code(const String &p_code, const String &p_path, Object *p_owner, List *r_options, bool &r_forced, String &r_call_hint) override; #ifdef TOOLS_ENABLED virtual Error lookup_code(const String &p_code, const String &p_symbol, const String &p_path, Object *p_owner, LookupResult &r_result) override; #endif diff --git a/modules/gdscript/gdscript_editor.cpp b/modules/gdscript/gdscript_editor.cpp index f41cb4ef09..4024a513a8 100644 --- a/modules/gdscript/gdscript_editor.cpp +++ b/modules/gdscript/gdscript_editor.cpp @@ -36,6 +36,7 @@ #ifdef TOOLS_ENABLED #include "editor/gdscript_docgen.h" +#include "editor/gdscript_editor_language.h" #include "editor/script_templates/templates.gen.h" #endif @@ -55,6 +56,14 @@ #include "editor/settings/editor_settings.h" #endif +#ifdef TOOLS_ENABLED +GDScriptEditorLanguage *GDScriptEditorLanguage::singleton = nullptr; + +EditorLanguage *GDScriptLanguage::get_editor_language() { + return GDScriptEditorLanguage::get_singleton(); +} +#endif // TOOLS_ENABLED + Vector GDScriptLanguage::get_comment_delimiters() const { static const Vector delimiters = { "#" }; return delimiters; @@ -3430,7 +3439,7 @@ static void _find_call_arguments(GDScriptParser::CompletionContext &p_context, c r_forced = r_result.size() > 0; } -::Error GDScriptLanguage::complete_code(const String &p_code, const String &p_path, Object *p_owner, List *r_options, bool &r_forced, String &r_call_hint) { +::Error GDScriptEditorLanguage::complete_code(const String &p_code, const String &p_path, Object *p_owner, List *r_options, bool &r_forced, String &r_call_hint) { const String quote_style = EDITOR_GET("text_editor/completion/use_single_quotes") ? "'" : "\""; GDScriptParser parser; @@ -3621,7 +3630,7 @@ static void _find_call_arguments(GDScriptParser::CompletionContext &p_context, c HashMap opt; _find_identifiers_in_base(base, false, false, false, opt, 0); - for (const KeyValue &E : opt) { + for (const KeyValue &E : opt) { ScriptLanguage::CodeCompletionOption option = _calculate_string_insertion(existing_index, E.value.insert_text); option.kind = E.value.kind; option.location = E.value.location; @@ -3856,13 +3865,6 @@ static void _find_call_arguments(GDScriptParser::CompletionContext &p_context, c return OK; } - -#else // !TOOLS_ENABLED - -Error GDScriptLanguage::complete_code(const String &p_code, const String &p_path, Object *p_owner, List *r_options, bool &r_forced, String &r_call_hint) { - return OK; -} - #endif // TOOLS_ENABLED //////// END COMPLETION ////////// diff --git a/modules/gdscript/language_server/gdscript_workspace.cpp b/modules/gdscript/language_server/gdscript_workspace.cpp index 9be68897f2..04e5cf67ba 100644 --- a/modules/gdscript/language_server/gdscript_workspace.cpp +++ b/modules/gdscript/language_server/gdscript_workspace.cpp @@ -30,6 +30,7 @@ #include "gdscript_workspace.h" +#include "../editor/gdscript_editor_language.h" #include "../gdscript.h" #include "../gdscript_parser.h" #include "gdscript_language_protocol.h" @@ -637,7 +638,7 @@ void GDScriptWorkspace::completion(const LSP::CompletionParams &p_params, Listget_text_for_completion(p_params.position); - GDScriptLanguage::get_singleton()->complete_code(code, path, current, r_options, forced, call_hint); + GDScriptEditorLanguage::get_singleton()->complete_code(code, path, current, r_options, forced, call_hint); } } diff --git a/modules/gdscript/register_types.cpp b/modules/gdscript/register_types.cpp index 4d86e22d8f..8e6927d944 100644 --- a/modules/gdscript/register_types.cpp +++ b/modules/gdscript/register_types.cpp @@ -38,6 +38,7 @@ #include "gdscript_utility_functions.h" #ifdef TOOLS_ENABLED +#include "editor/gdscript_editor_language.h" #include "editor/gdscript_highlighter.h" #include "editor/gdscript_translation_parser_plugin.h" #include "editor/script/script_editor_plugin.h" @@ -160,6 +161,8 @@ void initialize_gdscript_module(ModuleInitializationLevel p_level) { gdscript_translation_parser_plugin.instantiate(); EditorTranslationParser::get_singleton()->add_parser(gdscript_translation_parser_plugin, EditorTranslationParser::STANDARD); } else if (p_level == MODULE_INITIALIZATION_LEVEL_EDITOR) { + memnew(GDScriptEditorLanguage); + GDREGISTER_CLASS(GDScriptSyntaxHighlighter); #ifndef GDSCRIPT_NO_LSP register_lsp_types(); @@ -203,6 +206,7 @@ void uninitialize_gdscript_module(ModuleInitializationLevel p_level) { #ifndef GDSCRIPT_NO_LSP memdelete(GDScriptLanguageProtocol::get_singleton()); #endif // GDSCRIPT_NO_LSP + memdelete(GDScriptEditorLanguage::get_singleton()); } #endif // TOOLS_ENABLED } diff --git a/modules/gdscript/tests/test_completion.h b/modules/gdscript/tests/test_completion.h index bfa27e0ed9..3394e23dea 100644 --- a/modules/gdscript/tests/test_completion.h +++ b/modules/gdscript/tests/test_completion.h @@ -32,6 +32,7 @@ #ifdef TOOLS_ENABLED +#include "../editor/gdscript_editor_language.h" #include "../gdscript.h" #include "gdscript_test_runner.h" @@ -184,7 +185,7 @@ static void test_directory(const String &p_dir) { owner->set_script(scr); } - GDScriptLanguage::get_singleton()->complete_code(code, res_path, owner, &options, forced, call_hint); + GDScriptEditorLanguage::get_singleton()->complete_code(code, res_path, owner, &options, forced, call_hint); ERR_PRINT_ON; String contains_excluded; diff --git a/modules/mono/csharp_script.h b/modules/mono/csharp_script.h index d5899bbe0c..77f3080123 100644 --- a/modules/mono/csharp_script.h +++ b/modules/mono/csharp_script.h @@ -39,6 +39,7 @@ #include "core/templates/self_list.h" #ifdef TOOLS_ENABLED +#include "core/object/editor_language.h" #include "editor/plugins/editor_plugin.h" #endif @@ -432,6 +433,7 @@ class CSharpLanguage : public ScriptLanguage { #ifdef TOOLS_ENABLED EditorPlugin *godotsharp_editor = nullptr; + EditorLanguage editor_language; static void _editor_init_callback(); #endif @@ -497,6 +499,10 @@ public: void finalize(); /* EDITOR FUNCTIONS */ +#ifdef TOOLS_ENABLED + virtual EditorLanguage *get_editor_language() override { return &editor_language; } +#endif + Vector get_reserved_words() const override; bool is_control_flow_keyword(const String &p_keyword) const override; Vector get_comment_delimiters() const override;