@@ -76,6 +76,10 @@ Dictionary GDScriptSyntaxHighlighter::_get_line_syntax_highlighting_impl(int p_l
7676 int in_declaration_param_dicts = 0 ; // The number of opened `{` inside func params.
7777 int in_type_params = 0 ; // The number of opened `[` after type name.
7878
79+ int bracket_level_round = 0 ; // for ().
80+ int bracket_level_curly = 0 ; // for {}.
81+ int bracket_level_square = 0 ; // for [].
82+
7983 Color keyword_color;
8084 Color color;
8185
@@ -93,6 +97,24 @@ Dictionary GDScriptSyntaxHighlighter::_get_line_syntax_highlighting_impl(int p_l
9397 get_line_syntax_highlighting (p_line - 1 );
9498 }
9599 in_region = color_region_cache[p_line - 1 ];
100+
101+ if (bracket_pair_colors.size () > 0 ) {
102+ if (!bracket_level_cache.has (p_line - 1 )) {
103+ int prev_line = p_line - 1 ;
104+ while (prev_line > 0 && !bracket_level_cache.has (prev_line)) {
105+ prev_line--;
106+ }
107+
108+ for (int i = prev_line; i < p_line; i++) {
109+ get_line_syntax_highlighting (i);
110+ }
111+ }
112+
113+ Vector<int > prev_levels = bracket_level_cache[p_line - 1 ];
114+ bracket_level_square = prev_levels[SQUARE];
115+ bracket_level_round = prev_levels[ROUND];
116+ bracket_level_curly = prev_levels[CURLY];
117+ }
96118 }
97119
98120 const String &str = text_edit->get_line_with_ime (p_line);
@@ -655,6 +677,59 @@ Dictionary GDScriptSyntaxHighlighter::_get_line_syntax_highlighting_impl(int p_l
655677 next_type = IDENTIFIER;
656678 }
657679
680+ // Bracket pair colorization.
681+ if (bracket_pair_colors.size () > 0 ) {
682+ bool is_bracket = false ;
683+ Color bracket_color;
684+
685+ int *level_ptr = nullptr ;
686+ bool is_opening_bracket = false ;
687+
688+ switch (str[j]) {
689+ case ' (' :
690+ level_ptr = &bracket_level_round;
691+ is_opening_bracket = true ;
692+ break ;
693+ case ' )' :
694+ level_ptr = &bracket_level_round;
695+ is_opening_bracket = false ;
696+ break ;
697+
698+ case ' [' :
699+ level_ptr = &bracket_level_square;
700+ is_opening_bracket = true ;
701+ break ;
702+ case ' ]' :
703+ level_ptr = &bracket_level_square;
704+ is_opening_bracket = false ;
705+ break ;
706+
707+ case ' {' :
708+ level_ptr = &bracket_level_curly;
709+ is_opening_bracket = true ;
710+ break ;
711+ case ' }' :
712+ level_ptr = &bracket_level_curly;
713+ is_opening_bracket = false ;
714+ break ;
715+ }
716+
717+ if (level_ptr != nullptr ) {
718+ if (is_opening_bracket) {
719+ bracket_color = bracket_pair_colors[*level_ptr % bracket_pair_colors.size ()];
720+ (*level_ptr)++;
721+ } else {
722+ *level_ptr = MAX (0 , *level_ptr - 1 );
723+ bracket_color = bracket_pair_colors[*level_ptr % bracket_pair_colors.size ()];
724+ }
725+ is_bracket = true ;
726+ }
727+
728+ if (is_bracket) {
729+ color = bracket_color;
730+ }
731+ }
732+
658733 if (next_type != current_type) {
659734 if (current_type == NONE) {
660735 current_type = next_type;
@@ -688,6 +763,14 @@ Dictionary GDScriptSyntaxHighlighter::_get_line_syntax_highlighting_impl(int p_l
688763 color_map[j] = highlighter_info;
689764 }
690765 }
766+
767+ Vector<int > final_levels;
768+ final_levels.resize (3 );
769+ final_levels.set (SQUARE, bracket_level_square);
770+ final_levels.set (ROUND, bracket_level_round);
771+ final_levels.set (CURLY, bracket_level_curly);
772+ bracket_level_cache[p_line] = final_levels;
773+
691774 return color_map;
692775}
693776
@@ -910,6 +993,7 @@ void GDScriptSyntaxHighlighter::_update_cache() {
910993 annotation_color = EDITOR_GET (" text_editor/theme/highlighting/gdscript/annotation_color" );
911994 string_name_color = EDITOR_GET (" text_editor/theme/highlighting/gdscript/string_name_color" );
912995 type_color = EDITOR_GET (" text_editor/theme/highlighting/base_type_color" );
996+ bracket_pair_colors = EDITOR_GET (" text_editor/theme/highlighting/gdscript/bracket_pair_colors" );
913997 comment_marker_colors[COMMENT_MARKER_CRITICAL] = EDITOR_GET (" text_editor/theme/highlighting/comment_markers/critical_color" );
914998 comment_marker_colors[COMMENT_MARKER_WARNING] = EDITOR_GET (" text_editor/theme/highlighting/comment_markers/warning_color" );
915999 comment_marker_colors[COMMENT_MARKER_NOTICE] = EDITOR_GET (" text_editor/theme/highlighting/comment_markers/notice_color" );
@@ -960,6 +1044,10 @@ void GDScriptSyntaxHighlighter::add_color_region(ColorRegion::Type p_type, const
9601044 clear_highlighting_cache ();
9611045}
9621046
1047+ void GDScriptSyntaxHighlighter::_clear_highlighting_cache () {
1048+ bracket_level_cache.clear ();
1049+ }
1050+
9631051Ref<EditorSyntaxHighlighter> GDScriptSyntaxHighlighter::_create () const {
9641052 Ref<GDScriptSyntaxHighlighter> syntax_highlighter;
9651053 syntax_highlighter.instantiate ();
0 commit comments