Skip to content

armgit / godot

Public Imported from GitHub
0

Godot Engine – Multi-platform 2D and 3D game engine

Move `ScriptLanguage::validate` to `EditorLanguage`

HolonProductionf3fd9519b918
diff --git a/core/object/editor_language.h b/core/object/editor_language.h
index 97e41ca8d43..f348e96934d 100644
--- a/core/object/editor_language.h
+++ b/core/object/editor_language.h
@@ -130,6 +130,44 @@ public:
*/
virtual void format_code(String &r_code, uint32_t p_from_line, uint32_t p_to_line) const {}
+ struct Warning {
+ /// One-based.
+ int start_line = 0;
+ int start_column = -1;
+
+ /// One-based.
+ int end_line = 0;
+ int end_column = -1;
+
+ String string_code;
+ String message;
+ };
+
+ struct ScriptError {
+ String path;
+ /// All one-based.
+ int start_line = -1;
+ int start_column = -1;
+ int end_line = -1;
+ int end_column = -1;
+ String message;
+ };
+
+ /**
+ * Called by the editor to check a source fragment for diagnostics (warnings and errors).
+ *
+ * The language should also return a list of functions for the editor to show in the outline. It can also specify safe lines for highlighting. In the future those features might be separated.
+ *
+ * @param p_code The current content of the source fragment.
+ * @param p_path The path which identifies the source fragment. Implementations MAY support paths to builtin resources, or return an error for those.
+ * @param r_errors The returned errors of the script. Might be `nullptr` if the caller does not care.
+ * @param r_warnings The returned warnings of the script. Might be `nullptr` if the caller does not care.
+ * @param r_functions The returned functions for the outline. Might be `nullptr` if the caller does not care.
+ * @param r_safe_lines The returned safe-lines, this is a GDScript specific concept. Might be `nullptr` if the caller does not care.
+ * @return `true` if the script has no errors, otherwise `false`.
+ */
+ virtual bool validate(const String &p_code, const String &p_path, List<ScriptError> *r_errors, List<Warning> *r_warnings, List<String> *r_functions, HashSet<int> *r_safe_lines) const { return true; }
+
virtual ~EditorLanguage() = default;
};
diff --git a/core/object/script_language.h b/core/object/script_language.h
index 9cdd79e160f..e22e2485e31 100644
--- a/core/object/script_language.h
+++ b/core/object/script_language.h
@@ -230,30 +230,6 @@ public:
virtual EditorLanguage *get_editor_language() = 0;
#endif // TOOLS_ENABLED
- struct Warning {
- /// One-based.
- int start_line = 0;
- int start_column = -1;
-
- /// One-based.
- int end_line = 0;
- int end_column = -1;
-
- int code;
- String string_code;
- String message;
- };
-
- struct ScriptError {
- String path;
- /// All one-based.
- int start_line = -1;
- int start_column = -1;
- int end_line = -1;
- int end_column = -1;
- String message;
- };
-
enum TemplateLocation {
TEMPLATE_BUILT_IN,
TEMPLATE_EDITOR,
@@ -289,7 +265,6 @@ public:
virtual Ref<Script> make_template(const String &p_template, const String &p_class_name, const String &p_base_class_name) const { return Ref<Script>(); }
virtual Vector<ScriptTemplate> get_built_in_templates(const StringName &p_object) { return Vector<ScriptTemplate>(); }
virtual bool is_using_templates() { return false; }
- virtual bool validate(const String &p_script, const String &p_path = "", List<String> *r_functions = nullptr, List<ScriptError> *r_errors = nullptr, List<Warning> *r_warnings = nullptr, HashSet<int> *r_safe_lines = nullptr) const = 0;
virtual String validate_path(const String &p_path) const { return ""; }
virtual bool supports_builtin_mode() const = 0;
virtual bool supports_documentation() const { return false; }
diff --git a/core/object/script_language_extension.h b/core/object/script_language_extension.h
index e2a5b975f7d..e3c67f2f124 100644
--- a/core/object/script_language_extension.h
+++ b/core/object/script_language_extension.h
@@ -287,6 +287,10 @@ private:
return script_language->auto_indent_code(r_code, p_from_line, p_to_line);
}
+ virtual bool validate(const String &p_code, const String &p_path, List<ScriptError> *r_errors, List<Warning> *r_warnings, List<String> *r_functions, HashSet<int> *r_safe_lines) const override {
+ return script_language->validate(p_code, p_path, r_functions, r_errors, r_warnings, r_safe_lines);
+ }
+
EditorAdapter(ScriptLanguageExtension *p_script_language) {
script_language = p_script_language;
}
@@ -363,7 +367,9 @@ public:
EXBIND0R(bool, is_using_templates)
GDVIRTUAL6RC_REQUIRED(Dictionary, _validate, const String &, const String &, bool, bool, bool, bool)
- virtual bool validate(const String &p_script, const String &p_path = "", List<String> *r_functions = nullptr, List<ScriptError> *r_errors = nullptr, List<Warning> *r_warnings = nullptr, HashSet<int> *r_safe_lines = nullptr) const override {
+
+#ifdef TOOLS_ENABLED
+ bool validate(const String &p_script, const String &p_path = "", List<String> *r_functions = nullptr, List<EditorLanguage::ScriptError> *r_errors = nullptr, List<EditorLanguage::Warning> *r_warnings = nullptr, HashSet<int> *r_safe_lines = nullptr) const {
Dictionary ret;
GDVIRTUAL_CALL(_validate, p_script, p_path, r_functions != nullptr, r_errors != nullptr, r_warnings != nullptr, r_safe_lines != nullptr, ret);
if (!ret.has("valid")) {
@@ -383,7 +389,7 @@ public:
ERR_CONTINUE(!err.has("column"));
ERR_CONTINUE(!err.has("message"));
- ScriptError serr;
+ EditorLanguage::ScriptError serr;
if (err.has("path")) {
serr.path = err["path"];
}
@@ -403,14 +409,12 @@ public:
Dictionary warn = warning;
ERR_CONTINUE(!warn.has("start_line"));
ERR_CONTINUE(!warn.has("end_line"));
- ERR_CONTINUE(!warn.has("code"));
ERR_CONTINUE(!warn.has("string_code"));
ERR_CONTINUE(!warn.has("message"));
- Warning swarn;
+ EditorLanguage::Warning swarn;
swarn.start_line = warn["start_line"];
swarn.end_line = warn["end_line"];
- swarn.code = warn["code"];
swarn.string_code = warn["string_code"];
swarn.message = warn["message"];
@@ -425,6 +429,7 @@ public:
}
return ret["valid"];
}
+#endif
EXBIND1RC(String, validate_path, const String &)
#ifndef DISABLE_DEPRECATED
diff --git a/editor/script/script_editor_plugin.cpp b/editor/script/script_editor_plugin.cpp
index a1f49c58e3d..b25687c70c5 100644
--- a/editor/script/script_editor_plugin.cpp
+++ b/editor/script/script_editor_plugin.cpp
@@ -38,6 +38,7 @@
#include "core/io/resource_saver.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 "core/os/os.h"
#include "core/string/fuzzy_search.h"
@@ -1505,7 +1506,7 @@ void ScriptEditor::trigger_live_script_reload(const String &p_script_path) {
reloaded_script = ResourceLoader::load(p_script_path);
}
if (reloaded_script.is_valid()) {
- if (!reloaded_script->get_language()->validate(reloaded_script->get_source_code(), p_script_path)) {
+ if (!reloaded_script->get_language()->get_editor_language()->validate(reloaded_script->get_source_code(), p_script_path, nullptr, nullptr, nullptr, nullptr)) {
// Script has errors, don't live reload.
return;
}
diff --git a/editor/script/script_text_editor.cpp b/editor/script/script_text_editor.cpp
index 420f58836ff..78c90057213 100644
--- a/editor/script/script_text_editor.cpp
+++ b/editor/script/script_text_editor.cpp
@@ -173,7 +173,7 @@ Vector<String> ScriptTextEditor::get_functions() {
List<String> fnc;
Ref<Script> script = edited_res;
- if (script.is_valid() && script->get_language()->validate(text, script->get_path(), &fnc)) {
+ if (script.is_valid() && script->get_language()->get_editor_language()->validate(text, script->get_path(), nullptr, nullptr, &fnc, nullptr)) {
//if valid rewrite functions to latest
functions.clear();
for (const String &E : fnc) {
@@ -745,7 +745,7 @@ void ScriptTextEditor::_update_background_color() {
// Set the warning background.
if (warning_line_color.a != 0.0 || warning_underline_color.a != 0.0) {
- for (const ScriptLanguage::Warning &warning : warnings) {
+ for (const EditorLanguage::Warning &warning : warnings) {
int warning_start_line = CLAMP(warning.start_line - 1, 0, te->get_line_count() - 1);
int warning_start_column = warning.start_column - 1;
int warning_end_line = CLAMP(warning.end_line - 1, 0, te->get_line_count() - 1);
@@ -772,7 +772,7 @@ void ScriptTextEditor::_update_background_color() {
// Set the error background.
if (marked_line_color.a != 0.0 || error_underline_color.a != 0.0) {
- for (const ScriptLanguage::ScriptError &error : errors) {
+ for (const EditorLanguage::ScriptError &error : errors) {
int error_start_line = CLAMP(error.start_line - 1, 0, te->get_line_count() - 1);
int error_start_column = error.start_column - 1;
int error_end_line = CLAMP(error.end_line - 1, 0, te->get_line_count() - 1);
@@ -889,7 +889,7 @@ Ref<Texture2D> ScriptTextEditor::get_theme_icon() {
}
struct ScriptErrorLineComparator {
- bool operator()(const ScriptLanguage::ScriptError &p_a, const ScriptLanguage::ScriptError &p_b) const {
+ bool operator()(const EditorLanguage::ScriptError &p_a, const EditorLanguage::ScriptError &p_b) const {
if (p_a.start_line != p_b.start_line) {
return p_a.start_line < p_b.start_line;
}
@@ -909,12 +909,12 @@ void ScriptTextEditor::_validate_script() {
safe_lines.clear();
Ref<Script> script = edited_res;
- if (!script->get_language()->validate(text, script->get_path(), &fnc, &errors, &warnings, &safe_lines)) {
+ if (!script->get_language()->get_editor_language()->validate(text, script->get_path(), &errors, &warnings, &fnc, &safe_lines)) {
errors.sort_custom<ScriptErrorLineComparator>();
- List<ScriptLanguage::ScriptError>::Element *E = errors.front();
+ List<EditorLanguage::ScriptError>::Element *E = errors.front();
while (E) {
- List<ScriptLanguage::ScriptError>::Element *next_E = E->next();
+ List<EditorLanguage::ScriptError>::Element *next_E = E->next();
if ((E->get().path.is_empty() && !script->get_path().is_empty()) || E->get().path != script->get_path()) {
depended_errors[E->get().path].push_back(E->get());
E->erase();
@@ -992,7 +992,7 @@ void ScriptTextEditor::_update_warnings() {
// Add script warnings.
warnings_panel->push_table(3);
- for (const ScriptLanguage::Warning &w : warnings) {
+ for (const EditorLanguage::Warning &w : warnings) {
Dictionary ignore_meta;
ignore_meta["line"] = w.start_line - 1;
ignore_meta["code"] = w.string_code.to_lower();
@@ -1026,7 +1026,7 @@ void ScriptTextEditor::_update_errors() {
errors_panel->clear();
errors_panel->push_table(2);
- for (const ScriptLanguage::ScriptError &err : errors) {
+ for (const EditorLanguage::ScriptError &err : errors) {
errors_panel->push_cell();
errors_panel->push_meta(err.start_line - 1);
errors_panel->push_color(warnings_panel->get_theme_color(SNAME("error_color"), EditorStringName(Editor)));
@@ -1042,7 +1042,7 @@ void ScriptTextEditor::_update_errors() {
}
errors_panel->pop(); // Table
- for (const KeyValue<String, List<ScriptLanguage::ScriptError>> &KV : depended_errors) {
+ for (const KeyValue<String, List<EditorLanguage::ScriptError>> &KV : depended_errors) {
Dictionary click_meta_script;
click_meta_script["path"] = KV.key;
click_meta_script["line"] = 0;
@@ -1056,7 +1056,7 @@ void ScriptTextEditor::_update_errors() {
errors_panel->push_indent(1);
errors_panel->push_table(2);
- for (const ScriptLanguage::ScriptError &err : KV.value) {
+ for (const EditorLanguage::ScriptError &err : KV.value) {
Dictionary click_meta;
click_meta["path"] = KV.key;
click_meta["line"] = err.start_line - 1;
@@ -2386,7 +2386,7 @@ void ScriptTextEditor::_assign_dragged_export_variables() {
bool script_has_errors = false;
String scr_path = si->get_script()->get_path();
- for (const ScriptLanguage::ScriptError &error : errors) {
+ for (const EditorLanguage::ScriptError &error : errors) {
if (error.path == scr_path) {
script_has_errors = true;
break;
diff --git a/editor/script/script_text_editor.h b/editor/script/script_text_editor.h
index 2e7749aa7fd..bf4f93aaaf0 100644
--- a/editor/script/script_text_editor.h
+++ b/editor/script/script_text_editor.h
@@ -30,6 +30,7 @@
#pragma once
+#include "core/object/editor_language.h"
#include "editor/gui/code_editor.h"
#include "editor/script/script_editor_base.h"
#include "editor/script/script_editor_plugin.h"
@@ -63,9 +64,9 @@ class ScriptTextEditor : public CodeEditorBase {
Label *drag_info_label = nullptr;
Vector<String> functions;
- List<ScriptLanguage::Warning> warnings;
- List<ScriptLanguage::ScriptError> errors;
- HashMap<String, List<ScriptLanguage::ScriptError>> depended_errors;
+ List<EditorLanguage::Warning> warnings;
+ List<EditorLanguage::ScriptError> errors;
+ HashMap<String, List<EditorLanguage::ScriptError>> depended_errors;
HashSet<int> safe_lines;
List<Connection> missing_connections;
diff --git a/modules/gdscript/editor/gdscript_editor_language.h b/modules/gdscript/editor/gdscript_editor_language.h
index e39eec865f7..730ee44189a 100644
--- a/modules/gdscript/editor/gdscript_editor_language.h
+++ b/modules/gdscript/editor/gdscript_editor_language.h
@@ -46,6 +46,8 @@ public:
virtual void format_code(String &r_code, uint32_t p_from_line, uint32_t p_to_line) const override;
+ virtual bool validate(const String &p_code, const String &p_path, List<ScriptError> *r_errors, List<Warning> *r_warnings, List<String> *r_functions, HashSet<int> *r_safe_lines) const override;
+
GDScriptEditorLanguage() {
ERR_FAIL_COND(singleton != nullptr);
singleton = this;
diff --git a/modules/gdscript/gdscript.h b/modules/gdscript/gdscript.h
index 4ab7a2a6db4..a255c10e7da 100644
--- a/modules/gdscript/gdscript.h
+++ b/modules/gdscript/gdscript.h
@@ -596,7 +596,6 @@ public:
virtual bool is_using_templates() override;
virtual Ref<Script> make_template(const String &p_template, const String &p_class_name, const String &p_base_class_name) const override;
virtual Vector<ScriptTemplate> get_built_in_templates(const StringName &p_object) override;
- virtual bool validate(const String &p_script, const String &p_path = "", List<String> *r_functions = nullptr, List<ScriptLanguage::ScriptError> *r_errors = nullptr, List<ScriptLanguage::Warning> *r_warnings = nullptr, HashSet<int> *r_safe_lines = nullptr) const override;
virtual bool supports_builtin_mode() const override;
virtual bool supports_documentation() const override;
virtual bool can_inherit_from_file() const override { return true; }
diff --git a/modules/gdscript/gdscript_editor.cpp b/modules/gdscript/gdscript_editor.cpp
index cf2c49846a3..3425369243a 100644
--- a/modules/gdscript/gdscript_editor.cpp
+++ b/modules/gdscript/gdscript_editor.cpp
@@ -154,6 +154,8 @@ Vector<ScriptLanguage::ScriptTemplate> GDScriptLanguage::get_built_in_templates(
return templates;
}
+#ifdef TOOLS_ENABLED
+
static void get_function_names_recursively(const GDScriptParser::ClassNode *p_class, const String &p_prefix, HashMap<int, String> &r_funcs) {
for (int i = 0; i < p_class->members.size(); i++) {
if (p_class->members[i].type == GDScriptParser::ClassNode::Member::FUNCTION) {
@@ -166,7 +168,7 @@ static void get_function_names_recursively(const GDScriptParser::ClassNode *p_cl
}
}
-bool GDScriptLanguage::validate(const String &p_script, const String &p_path, List<String> *r_functions, List<ScriptLanguage::ScriptError> *r_errors, List<ScriptLanguage::Warning> *r_warnings, HashSet<int> *r_safe_lines) const {
+bool GDScriptEditorLanguage::validate(const String &p_script, const String &p_path, List<ScriptError> *r_errors, List<Warning> *r_warnings, List<String> *r_functions, HashSet<int> *r_safe_lines) const {
GDScriptParser parser;
GDScriptAnalyzer analyzer(&parser);
@@ -178,12 +180,11 @@ bool GDScriptLanguage::validate(const String &p_script, const String &p_path, Li
if (r_warnings) {
for (const GDScriptWarning &E : parser.get_warnings()) {
const GDScriptWarning &warn = E;
- ScriptLanguage::Warning w;
+ Warning w;
w.start_line = warn.start_line;
w.start_column = warn.start_column;
w.end_line = warn.end_line;
w.end_column = warn.end_column;
- w.code = (int)warn.code;
w.string_code = GDScriptWarning::get_name_from_code(warn.code);
w.message = warn.get_message();
r_warnings->push_back(w);
@@ -193,7 +194,7 @@ bool GDScriptLanguage::validate(const String &p_script, const String &p_path, Li
if (err) {
if (r_errors) {
for (const GDScriptParser::ParserError &pe : parser.get_errors()) {
- ScriptLanguage::ScriptError e;
+ ScriptError e;
e.path = p_path;
e.start_line = pe.start_line;
e.start_column = pe.start_column;
@@ -206,7 +207,7 @@ bool GDScriptLanguage::validate(const String &p_script, const String &p_path, Li
for (KeyValue<String, Ref<GDScriptParserRef>> E : parser.get_depended_parsers()) {
GDScriptParser *depended_parser = E.value->get_parser();
for (const GDScriptParser::ParserError &pe : depended_parser->get_errors()) {
- ScriptLanguage::ScriptError e;
+ ScriptError e;
e.path = E.key;
e.start_line = pe.start_line;
e.start_column = pe.start_column;
@@ -243,6 +244,8 @@ bool GDScriptLanguage::validate(const String &p_script, const String &p_path, Li
return true;
}
+#endif // TOOLS_ENABLED
+
bool GDScriptLanguage::supports_builtin_mode() const {
return true;
}
diff --git a/modules/mono/csharp_script.h b/modules/mono/csharp_script.h
index a15e908f04e..336f2e19661 100644
--- a/modules/mono/csharp_script.h
+++ b/modules/mono/csharp_script.h
@@ -511,10 +511,6 @@ public:
bool is_using_templates() override;
virtual Ref<Script> make_template(const String &p_template, const String &p_class_name, const String &p_base_class_name) const override;
virtual Vector<ScriptTemplate> get_built_in_templates(const StringName &p_object) override;
- /* TODO */ bool validate(const String &p_script, const String &p_path, List<String> *r_functions,
- List<ScriptLanguage::ScriptError> *r_errors = nullptr, List<ScriptLanguage::Warning> *r_warnings = nullptr, HashSet<int> *r_safe_lines = nullptr) const override {
- return true;
- }
String validate_path(const String &p_path) const override;
bool supports_builtin_mode() const override;
String make_function(const String &p_class, const String &p_name, const PackedStringArray &p_args) const override;