diff options
author | RĂ©mi Verschelde <remi@verschelde.fr> | 2021-06-20 00:38:49 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-06-20 00:38:49 +0200 |
commit | 9b7c963d19cc0df53792d33b127f7a7356996030 (patch) | |
tree | eb6e15b1c69ed40cc7e6d1ea38fd7682a1abbc6b /editor | |
parent | de7293b6eb6f45b44b3467fe25e2a0b6f403ebef (diff) | |
parent | d0e78c86d72de1ec5d1c49de5c5ee2fff9589efc (diff) |
Merge pull request #48804 from EricEzaM/scripting-multi-error-support
Added support for scripts reporting multiple errors to ScriptTextEditor
Diffstat (limited to 'editor')
-rw-r--r-- | editor/code_editor.cpp | 65 | ||||
-rw-r--r-- | editor/code_editor.h | 9 | ||||
-rw-r--r-- | editor/plugins/script_text_editor.cpp | 79 | ||||
-rw-r--r-- | editor/plugins/script_text_editor.h | 3 | ||||
-rw-r--r-- | editor/plugins/shader_editor_plugin.cpp | 4 |
5 files changed, 120 insertions, 40 deletions
diff --git a/editor/code_editor.cpp b/editor/code_editor.cpp index 807a45eb32..e0e9c6e2d1 100644 --- a/editor/code_editor.cpp +++ b/editor/code_editor.cpp @@ -1612,15 +1612,19 @@ void CodeTextEditor::validate_script() { idle->start(); } -void CodeTextEditor::_warning_label_gui_input(const Ref<InputEvent> &p_event) { - Ref<InputEventMouseButton> mb = p_event; - if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == MOUSE_BUTTON_LEFT) { - _warning_button_pressed(); - } +void CodeTextEditor::_error_button_pressed() { + _set_show_errors_panel(!is_errors_panel_opened); + _set_show_warnings_panel(false); } void CodeTextEditor::_warning_button_pressed() { _set_show_warnings_panel(!is_warnings_panel_opened); + _set_show_errors_panel(false); +} + +void CodeTextEditor::_set_show_errors_panel(bool p_show) { + is_errors_panel_opened = p_show; + emit_signal("show_errors_panel", p_show); } void CodeTextEditor::_set_show_warnings_panel(bool p_show) { @@ -1653,6 +1657,7 @@ void CodeTextEditor::_notification(int p_what) { _update_font(); } break; case NOTIFICATION_ENTER_TREE: { + error_button->set_icon(get_theme_icon("StatusError", "EditorIcons")); warning_button->set_icon(get_theme_icon("NodeWarning", "EditorIcons")); add_theme_constant_override("separation", 4 * EDSCALE); } break; @@ -1667,11 +1672,18 @@ void CodeTextEditor::_notification(int p_what) { } } -void CodeTextEditor::set_warning_nb(int p_warning_nb) { - warning_count_label->set_text(itos(p_warning_nb)); - warning_count_label->set_visible(p_warning_nb > 0); - warning_button->set_visible(p_warning_nb > 0); - if (!p_warning_nb) { +void CodeTextEditor::set_error_count(int p_error_count) { + error_button->set_text(itos(p_error_count)); + error_button->set_visible(p_error_count > 0); + if (!p_error_count) { + _set_show_errors_panel(false); + } +} + +void CodeTextEditor::set_warning_count(int p_warning_count) { + warning_button->set_text(itos(p_warning_count)); + warning_button->set_visible(p_warning_count > 0); + if (!p_warning_count) { _set_show_warnings_panel(false); } } @@ -1738,6 +1750,7 @@ void CodeTextEditor::_bind_methods() { ADD_SIGNAL(MethodInfo("validate_script")); ADD_SIGNAL(MethodInfo("load_theme_settings")); + ADD_SIGNAL(MethodInfo("show_errors_panel")); ADD_SIGNAL(MethodInfo("show_warnings_panel")); } @@ -1835,6 +1848,22 @@ CodeTextEditor::CodeTextEditor() { error->set_mouse_filter(MOUSE_FILTER_STOP); error->connect("gui_input", callable_mp(this, &CodeTextEditor::_error_pressed)); + // Errors + error_button = memnew(Button); + error_button->set_flat(true); + status_bar->add_child(error_button); + error_button->set_v_size_flags(SIZE_EXPAND | SIZE_SHRINK_CENTER); + error_button->set_default_cursor_shape(CURSOR_POINTING_HAND); + error_button->connect("pressed", callable_mp(this, &CodeTextEditor::_error_button_pressed)); + error_button->set_tooltip(TTR("Errors")); + + error_button->add_theme_color_override("font_color", EditorNode::get_singleton()->get_gui_base()->get_theme_color("error_color", "Editor")); + error_button->add_theme_font_override("font", EditorNode::get_singleton()->get_gui_base()->get_theme_font("status_source", "EditorFonts")); + error_button->add_theme_font_size_override("font_size", EditorNode::get_singleton()->get_gui_base()->get_theme_font_size("status_source_size", "EditorFonts")); + + is_errors_panel_opened = false; + set_error_count(0); + // Warnings warning_button = memnew(Button); warning_button->set_flat(true); @@ -1844,20 +1873,12 @@ CodeTextEditor::CodeTextEditor() { warning_button->connect("pressed", callable_mp(this, &CodeTextEditor::_warning_button_pressed)); warning_button->set_tooltip(TTR("Warnings")); - warning_count_label = memnew(Label); - status_bar->add_child(warning_count_label); - warning_count_label->set_v_size_flags(SIZE_EXPAND | SIZE_SHRINK_CENTER); - warning_count_label->set_align(Label::ALIGN_RIGHT); - warning_count_label->set_default_cursor_shape(CURSOR_POINTING_HAND); - warning_count_label->set_mouse_filter(MOUSE_FILTER_STOP); - warning_count_label->set_tooltip(TTR("Warnings")); - warning_count_label->add_theme_color_override("font_color", EditorNode::get_singleton()->get_gui_base()->get_theme_color("warning_color", "Editor")); - warning_count_label->add_theme_font_override("font", EditorNode::get_singleton()->get_gui_base()->get_theme_font("status_source", "EditorFonts")); - warning_count_label->add_theme_font_size_override("font_size", EditorNode::get_singleton()->get_gui_base()->get_theme_font_size("status_source_size", "EditorFonts")); - warning_count_label->connect("gui_input", callable_mp(this, &CodeTextEditor::_warning_label_gui_input)); + warning_button->add_theme_color_override("font_color", EditorNode::get_singleton()->get_gui_base()->get_theme_color("warning_color", "Editor")); + warning_button->add_theme_font_override("font", EditorNode::get_singleton()->get_gui_base()->get_theme_font("status_source", "EditorFonts")); + warning_button->add_theme_font_size_override("font_size", EditorNode::get_singleton()->get_gui_base()->get_theme_font_size("status_source_size", "EditorFonts")); is_warnings_panel_opened = false; - set_warning_nb(0); + set_warning_count(0); // Line and column line_and_col_txt = memnew(Label); diff --git a/editor/code_editor.h b/editor/code_editor.h index f368305e85..28b09e0a5d 100644 --- a/editor/code_editor.h +++ b/editor/code_editor.h @@ -145,8 +145,8 @@ class CodeTextEditor : public VBoxContainer { HBoxContainer *status_bar; Button *toggle_scripts_button; + Button *error_button; Button *warning_button; - Label *warning_count_label; Label *line_and_col_txt; @@ -184,8 +184,9 @@ class CodeTextEditor : public VBoxContainer { CodeTextEditorCodeCompleteFunc code_complete_func; void *code_complete_ud; - void _warning_label_gui_input(const Ref<InputEvent> &p_event); + void _error_button_pressed(); void _warning_button_pressed(); + void _set_show_errors_panel(bool p_show); void _set_show_warnings_panel(bool p_show); void _error_pressed(const Ref<InputEvent> &p_event); @@ -205,6 +206,7 @@ protected: static void _bind_methods(); bool is_warnings_panel_opened; + bool is_errors_panel_opened; public: void trim_trailing_whitespace(); @@ -238,7 +240,8 @@ public: Variant get_edit_state(); void set_edit_state(const Variant &p_state); - void set_warning_nb(int p_warning_nb); + void set_error_count(int p_error_count); + void set_warning_count(int p_warning_count); void update_editor_settings(); void set_error(const String &p_error); diff --git a/editor/plugins/script_text_editor.cpp b/editor/plugins/script_text_editor.cpp index 3ec20ae68e..012011ef4a 100644 --- a/editor/plugins/script_text_editor.cpp +++ b/editor/plugins/script_text_editor.cpp @@ -109,13 +109,11 @@ ConnectionInfoDialog::ConnectionInfoDialog() { //////////////////////////////////////////////////////////////////////////////// Vector<String> ScriptTextEditor::get_functions() { - String errortxt; - int line = -1, col; CodeEdit *te = code_editor->get_text_editor(); String text = te->get_text(); List<String> fnc; - if (script->get_language()->validate(text, line, col, errortxt, script->get_path(), &fnc)) { + if (script->get_language()->validate(text, script->get_path(), &fnc)) { //if valid rewrite functions to latest functions.clear(); for (List<String>::Element *E = fnc.front(); E; E = E->next()) { @@ -265,6 +263,10 @@ void ScriptTextEditor::_set_theme_for_script() { } } +void ScriptTextEditor::_show_errors_panel(bool p_show) { + errors_panel->set_visible(p_show); +} + void ScriptTextEditor::_show_warnings_panel(bool p_show) { warnings_panel->set_visible(p_show); } @@ -279,6 +281,12 @@ void ScriptTextEditor::_warning_clicked(Variant p_line) { } } +void ScriptTextEditor::_error_clicked(Variant p_line) { + if (p_line.get_type() == Variant::INT) { + code_editor->get_text_editor()->cursor_set_line(p_line.operator int64_t()); + } +} + void ScriptTextEditor::reload_text() { ERR_FAIL_COND(script.is_null()); @@ -429,23 +437,21 @@ Ref<Texture2D> ScriptTextEditor::get_theme_icon() { } void ScriptTextEditor::_validate_script() { - String errortxt; - int line = -1, col; CodeEdit *te = code_editor->get_text_editor(); String text = te->get_text(); List<String> fnc; Set<int> safe_lines; List<ScriptLanguage::Warning> warnings; + List<ScriptLanguage::ScriptError> errors; - if (!script->get_language()->validate(text, line, col, errortxt, script->get_path(), &fnc, &warnings, &safe_lines)) { - String error_text = "error(" + itos(line) + "," + itos(col) + "): " + errortxt; + if (!script->get_language()->validate(text, script->get_path(), &fnc, &errors, &warnings, &safe_lines)) { + String error_text = TTR("Error at ") + "(" + itos(errors[0].line) + "," + itos(errors[0].column) + "): " + errors[0].message; code_editor->set_error(error_text); - code_editor->set_error_pos(line - 1, col - 1); + code_editor->set_error_pos(errors[0].line - 1, errors[0].column - 1); script_is_valid = false; } else { code_editor->set_error(""); - line = -1; if (!script->is_tool()) { script->set_source_code(text); script->update_exports(); @@ -487,7 +493,8 @@ void ScriptTextEditor::_validate_script() { } } - code_editor->set_warning_nb(warning_nb); + code_editor->set_error_count(errors.size()); + code_editor->set_warning_count(warning_nb); // Add script warnings. warnings_panel->push_table(3); @@ -521,11 +528,40 @@ void ScriptTextEditor::_validate_script() { } warnings_panel->pop(); // Table. - line--; + errors_panel->clear(); + errors_panel->push_table(2); + for (List<ScriptLanguage::ScriptError>::Element *E = errors.front(); E; E = E->next()) { + ScriptLanguage::ScriptError err = E->get(); + + errors_panel->push_cell(); + errors_panel->push_meta(err.line - 1); + errors_panel->push_color(warnings_panel->get_theme_color("error_color", "Editor")); + errors_panel->add_text(TTR("Line") + " " + itos(err.line) + ":"); + errors_panel->pop(); // Color. + errors_panel->pop(); // Meta goto. + errors_panel->pop(); // Cell. + + errors_panel->push_cell(); + errors_panel->add_text(err.message); + errors_panel->pop(); // Cell. + } + errors_panel->pop(); // Table + bool highlight_safe = EDITOR_DEF("text_editor/highlighting/highlight_type_safe_lines", true); bool last_is_safe = false; for (int i = 0; i < te->get_line_count(); i++) { - te->set_line_background_color(i, (line == i) ? marked_line_color : Color(0, 0, 0, 0)); + if (errors.is_empty()) { + te->set_line_background_color(i, Color(0, 0, 0, 0)); + } else { + for (List<ScriptLanguage::ScriptError>::Element *E = errors.front(); E; E = E->next()) { + bool error_line = i == E->get().line - 1; + te->set_line_background_color(i, error_line ? marked_line_color : Color(0, 0, 0, 0)); + if (error_line) { + break; + } + } + } + if (highlight_safe) { if (safe_lines.has(i + 1)) { te->set_line_gutter_item_color(i, line_number_gutter, safe_line_number_color); @@ -537,7 +573,7 @@ void ScriptTextEditor::_validate_script() { last_is_safe = false; } } else { - te->set_line_gutter_item_color(line, 1, default_line_number_color); + te->set_line_gutter_item_color(i, 1, default_line_number_color); } } @@ -1675,6 +1711,7 @@ void ScriptTextEditor::_enable_code_editor() { editor_box->set_v_size_flags(SIZE_EXPAND_FILL); editor_box->add_child(code_editor); + code_editor->connect("show_errors_panel", callable_mp(this, &ScriptTextEditor::_show_errors_panel)); code_editor->connect("show_warnings_panel", callable_mp(this, &ScriptTextEditor::_show_warnings_panel)); code_editor->connect("validate_script", callable_mp(this, &ScriptTextEditor::_validate_script)); code_editor->connect("load_theme_settings", callable_mp(this, &ScriptTextEditor::_load_theme_settings)); @@ -1695,6 +1732,13 @@ void ScriptTextEditor::_enable_code_editor() { "normal_font_size", EditorNode::get_singleton()->get_gui_base()->get_theme_font_size("main_size", "EditorFonts")); warnings_panel->connect("meta_clicked", callable_mp(this, &ScriptTextEditor::_warning_clicked)); + editor_box->add_child(errors_panel); + errors_panel->add_theme_font_override( + "normal_font", EditorNode::get_singleton()->get_gui_base()->get_theme_font("main", "EditorFonts")); + errors_panel->add_theme_font_size_override( + "normal_font_size", EditorNode::get_singleton()->get_gui_base()->get_theme_font_size("main_size", "EditorFonts")); + errors_panel->connect("meta_clicked", callable_mp(this, &ScriptTextEditor::_error_clicked)); + add_child(context_menu); context_menu->connect("id_pressed", callable_mp(this, &ScriptTextEditor::_edit_option)); @@ -1826,6 +1870,14 @@ ScriptTextEditor::ScriptTextEditor() { warnings_panel->set_focus_mode(FOCUS_CLICK); warnings_panel->hide(); + errors_panel = memnew(RichTextLabel); + errors_panel->set_custom_minimum_size(Size2(0, 100 * EDSCALE)); + errors_panel->set_h_size_flags(SIZE_EXPAND_FILL); + errors_panel->set_meta_underline(true); + errors_panel->set_selection_enabled(true); + errors_panel->set_focus_mode(FOCUS_CLICK); + errors_panel->hide(); + update_settings(); code_editor->get_text_editor()->set_code_hint_draw_below(EditorSettings::get_singleton()->get("text_editor/completion/put_callhint_tooltip_below_current_line")); @@ -1886,6 +1938,7 @@ ScriptTextEditor::~ScriptTextEditor() { if (!editor_enabled) { memdelete(code_editor); memdelete(warnings_panel); + memdelete(errors_panel); memdelete(context_menu); memdelete(color_panel); memdelete(edit_hb); diff --git a/editor/plugins/script_text_editor.h b/editor/plugins/script_text_editor.h index 7bb961bf19..8a8e9aa737 100644 --- a/editor/plugins/script_text_editor.h +++ b/editor/plugins/script_text_editor.h @@ -55,6 +55,7 @@ class ScriptTextEditor : public ScriptEditorBase { CodeTextEditor *code_editor = nullptr; RichTextLabel *warnings_panel = nullptr; + RichTextLabel *errors_panel = nullptr; Ref<Script> script; bool script_is_valid = false; @@ -161,7 +162,9 @@ protected: void _load_theme_settings(); void _set_theme_for_script(); + void _show_errors_panel(bool p_show); void _show_warnings_panel(bool p_show); + void _error_clicked(Variant p_line); void _warning_clicked(Variant p_line); void _notification(int p_what); diff --git a/editor/plugins/shader_editor_plugin.cpp b/editor/plugins/shader_editor_plugin.cpp index e4a5a3796e..22d598d180 100644 --- a/editor/plugins/shader_editor_plugin.cpp +++ b/editor/plugins/shader_editor_plugin.cpp @@ -240,7 +240,7 @@ void ShaderTextEditor::_validate_script() { warnings.sort_custom<WarningsComparator>(); _update_warning_panel(); } else { - set_warning_nb(0); + set_warning_count(0); } emit_signal("script_changed"); } @@ -280,7 +280,7 @@ void ShaderTextEditor::_update_warning_panel() { } warnings_panel->pop(); // Table. - set_warning_nb(warning_count); + set_warning_count(warning_count); } void ShaderTextEditor::_bind_methods() { |