From d1a1ad127e03053a484fa71e1c36590d7e39ed95 Mon Sep 17 00:00:00 2001 From: Paulb23 Date: Wed, 30 Jun 2021 11:58:13 +0100 Subject: [PATCH] Move brace matching into CodeEdit --- doc/classes/CodeEdit.xml | 3 +++ doc/classes/TextEdit.xml | 2 -- editor/code_editor.cpp | 2 +- scene/gui/code_edit.cpp | 13 +++++++++++++ scene/gui/code_edit.h | 3 +++ scene/gui/text_edit.cpp | 6 +++--- scene/gui/text_edit.h | 7 ++----- scene/resources/default_theme/default_theme.cpp | 1 - 8 files changed, 25 insertions(+), 12 deletions(-) diff --git a/doc/classes/CodeEdit.xml b/doc/classes/CodeEdit.xml index 1a6311c9050..249334f4164 100644 --- a/doc/classes/CodeEdit.xml +++ b/doc/classes/CodeEdit.xml @@ -420,6 +420,9 @@ Sets whether brace pairs should be autocompleted. + + Highlight mismatching brace pairs. + Sets the brace pairs to be autocompleted. diff --git a/doc/classes/TextEdit.xml b/doc/classes/TextEdit.xml index cc5c36bbd3f..0b0d3d14a39 100644 --- a/doc/classes/TextEdit.xml +++ b/doc/classes/TextEdit.xml @@ -798,8 +798,6 @@ Sets the background [Color] of this [TextEdit]. - - diff --git a/editor/code_editor.cpp b/editor/code_editor.cpp index 668d364f3ac..6e1ac3e8379 100644 --- a/editor/code_editor.cpp +++ b/editor/code_editor.cpp @@ -1825,7 +1825,7 @@ CodeTextEditor::CodeTextEditor() { } text_editor->set_draw_line_numbers(true); - text_editor->set_brace_matching(true); + text_editor->set_highlight_matching_braces_enabled(true); text_editor->set_auto_indent_enabled(true); status_bar = memnew(HBoxContainer); diff --git a/scene/gui/code_edit.cpp b/scene/gui/code_edit.cpp index e21f05c9796..47ab3f82c18 100644 --- a/scene/gui/code_edit.cpp +++ b/scene/gui/code_edit.cpp @@ -937,6 +937,15 @@ bool CodeEdit::is_auto_brace_completion_enabled() const { return auto_brace_completion_enabled; } +void CodeEdit::set_highlight_matching_braces_enabled(bool p_enabled) { + highlight_matching_braces_enabled = p_enabled; + update(); +} + +bool CodeEdit::is_highlight_matching_braces_enabled() const { + return highlight_matching_braces_enabled; +} + void CodeEdit::add_auto_brace_completion_pair(const String &p_open_key, const String &p_close_key) { ERR_FAIL_COND_MSG(p_open_key.is_empty(), "auto brace completion open key cannot be empty"); ERR_FAIL_COND_MSG(p_close_key.is_empty(), "auto brace completion close key cannot be empty"); @@ -1881,6 +1890,9 @@ void CodeEdit::_bind_methods() { ClassDB::bind_method(D_METHOD("set_auto_brace_completion_enabled", "enable"), &CodeEdit::set_auto_brace_completion_enabled); ClassDB::bind_method(D_METHOD("is_auto_brace_completion_enabled"), &CodeEdit::is_auto_brace_completion_enabled); + ClassDB::bind_method(D_METHOD("set_highlight_matching_braces_enabled", "enable"), &CodeEdit::set_highlight_matching_braces_enabled); + ClassDB::bind_method(D_METHOD("is_highlight_matching_braces_enabled"), &CodeEdit::is_highlight_matching_braces_enabled); + ClassDB::bind_method(D_METHOD("add_auto_brace_completion_pair", "start_key", "end_key"), &CodeEdit::add_auto_brace_completion_pair); ClassDB::bind_method(D_METHOD("set_auto_brace_completion_pairs", "pairs"), &CodeEdit::set_auto_brace_completion_pairs); ClassDB::bind_method(D_METHOD("get_auto_brace_completion_pairs"), &CodeEdit::get_auto_brace_completion_pairs); @@ -2048,6 +2060,7 @@ void CodeEdit::_bind_methods() { ADD_GROUP("Auto brace completion", "auto_brace_completion_"); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "auto_brace_completion_enabled"), "set_auto_brace_completion_enabled", "is_auto_brace_completion_enabled"); + ADD_PROPERTY(PropertyInfo(Variant::BOOL, "auto_brace_completion_highlight_matching"), "set_highlight_matching_braces_enabled", "is_highlight_matching_braces_enabled"); ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY, "auto_brace_completion_pairs"), "set_auto_brace_completion_pairs", "get_auto_brace_completion_pairs"); /* Signals */ diff --git a/scene/gui/code_edit.h b/scene/gui/code_edit.h index 1795364b82c..00958f2d064 100644 --- a/scene/gui/code_edit.h +++ b/scene/gui/code_edit.h @@ -259,6 +259,9 @@ public: void set_auto_brace_completion_enabled(bool p_enabled); bool is_auto_brace_completion_enabled() const; + void set_highlight_matching_braces_enabled(bool p_enabled); + bool is_highlight_matching_braces_enabled() const; + void add_auto_brace_completion_pair(const String &p_open_key, const String &p_close_key); void set_auto_brace_completion_pairs(const Dictionary &p_auto_brace_completion_pairs); Dictionary get_auto_brace_completion_pairs() const; diff --git a/scene/gui/text_edit.cpp b/scene/gui/text_edit.cpp index 1c92916b33a..f6711461ed6 100644 --- a/scene/gui/text_edit.cpp +++ b/scene/gui/text_edit.cpp @@ -626,7 +626,7 @@ void TextEdit::_notification(int p_what) { bool brace_close_matching = false; bool brace_close_mismatch = false; - if (brace_matching_enabled && cursor.line >= 0 && cursor.line < text.size() && cursor.column >= 0) { + if (highlight_matching_braces_enabled && cursor.line >= 0 && cursor.line < text.size() && cursor.column >= 0) { if (cursor.column < text[cursor.line].length()) { // Check for open. char32_t c = text[cursor.line][cursor.column]; @@ -1269,7 +1269,7 @@ void TextEdit::_notification(int p_what) { int char_pos = char_ofs + char_margin + ofs_x; if (char_pos >= xmargin_beg) { - if (brace_matching_enabled) { + if (highlight_matching_braces_enabled) { if ((brace_open_match_line == line && brace_open_match_column == glyphs[j].start) || (cursor.column == glyphs[j].start && cursor.line == line && cursor_wrap_index == line_wrap_index && (brace_open_matching || brace_open_mismatch))) { if (brace_open_mismatch) { @@ -3838,7 +3838,7 @@ void TextEdit::_update_caches() { cache.current_line_color = get_theme_color(SNAME("current_line_color")); cache.line_length_guideline_color = get_theme_color(SNAME("line_length_guideline_color")); cache.code_folding_color = get_theme_color(SNAME("code_folding_color"), SNAME("CodeEdit")); - cache.brace_mismatch_color = get_theme_color(SNAME("brace_mismatch_color")); + cache.brace_mismatch_color = get_theme_color(SNAME("brace_mismatch_color"), SNAME("CodeEdit")); cache.word_highlighted_color = get_theme_color(SNAME("word_highlighted_color")); cache.search_result_color = get_theme_color(SNAME("search_result_color")); cache.search_result_border_color = get_theme_color(SNAME("search_result_border_color")); diff --git a/scene/gui/text_edit.h b/scene/gui/text_edit.h index 0ac5e0917b2..c494b6399cb 100644 --- a/scene/gui/text_edit.h +++ b/scene/gui/text_edit.h @@ -291,7 +291,6 @@ private: bool highlight_all_occurrences = false; bool scroll_past_end_of_file_enabled = false; - bool brace_matching_enabled = false; bool highlight_current_line = false; String cut_copy_line; @@ -433,6 +432,8 @@ private: void _move_cursor_document_end(bool p_select); protected: + bool highlight_matching_braces_enabled = false; + struct Cache { Ref tab_icon; Ref space_icon; @@ -628,10 +629,6 @@ public: scroll_past_end_of_file_enabled = p_enabled; update(); } - inline void set_brace_matching(bool p_enabled) { - brace_matching_enabled = p_enabled; - update(); - } void center_viewport_to_cursor(); diff --git a/scene/resources/default_theme/default_theme.cpp b/scene/resources/default_theme/default_theme.cpp index a4e126fcc6a..9f14e5e276d 100644 --- a/scene/resources/default_theme/default_theme.cpp +++ b/scene/resources/default_theme/default_theme.cpp @@ -457,7 +457,6 @@ void fill_default_theme(Ref &theme, const Ref &default_font, const theme->set_color("current_line_color", "TextEdit", Color(0.25, 0.25, 0.26, 0.8)); theme->set_color("caret_color", "TextEdit", control_font_color); theme->set_color("caret_background_color", "TextEdit", Color(0, 0, 0)); - theme->set_color("brace_mismatch_color", "TextEdit", Color(1, 0.2, 0.2)); theme->set_color("word_highlighted_color", "TextEdit", Color(0.8, 0.9, 0.9, 0.15)); theme->set_constant("line_spacing", "TextEdit", 4 * scale);