Skip to content

armgit / godot

Public Imported from GitHub
0

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

Improve GDScript Syntax Highlighting performance

kit188aaca643c3
diff --git a/modules/gdscript/editor/gdscript_highlighter.cpp b/modules/gdscript/editor/gdscript_highlighter.cpp
index 64fa3de9070..96d8d8d21c5 100644
--- a/modules/gdscript/editor/gdscript_highlighter.cpp
+++ b/modules/gdscript/editor/gdscript_highlighter.cpp
@@ -99,6 +99,9 @@ Dictionary GDScriptSyntaxHighlighter::_get_line_syntax_highlighting_impl(int p_l
const int line_length = str.length();
Color prev_color;
+ bool code_region_start_checked = false;
+ bool code_region_not_at_start = false;
+
if (in_region != -1 && line_length == 0) {
color_region_cache[p_line] = in_region;
}
@@ -129,22 +132,22 @@ Dictionary GDScriptSyntaxHighlighter::_get_line_syntax_highlighting_impl(int p_l
// Check if we are in entering a region.
if (in_region == -1) {
const bool r_prefix = from > 0 && str[from - 1] == 'r';
- for (int c = 0; c < color_regions.size(); c++) {
+ for (uint32_t c = 0; c < color_regions.size(); c++) {
+ const GDScriptSyntaxHighlighter::ColorRegion &c_color_region = color_regions[c];
// Check there is enough room.
int chars_left = line_length - from;
- int start_key_length = color_regions[c].start_key.length();
- int end_key_length = color_regions[c].end_key.length();
+ int start_key_length = c_color_region.start_key.length();
if (chars_left < start_key_length) {
continue;
}
- if (color_regions[c].is_string && color_regions[c].r_prefix != r_prefix) {
+ if (c_color_region.is_string && c_color_region.r_prefix != r_prefix) {
continue;
}
// Search the line.
bool match = true;
- const char32_t *start_key = color_regions[c].start_key.get_data();
+ const char32_t *start_key = c_color_region.start_key.get_data();
for (int k = 0; k < start_key_length; k++) {
if (start_key[k] != str[from + k]) {
match = false;
@@ -152,11 +155,15 @@ Dictionary GDScriptSyntaxHighlighter::_get_line_syntax_highlighting_impl(int p_l
}
}
// "#region" and "#endregion" only highlighted if they're the first region on the line.
- if (color_regions[c].type == ColorRegion::TYPE_CODE_REGION) {
- Vector<String> str_stripped_split = str.strip_edges().split_spaces(1);
- if (!str_stripped_split.is_empty() &&
- str_stripped_split[0] != "#region" &&
- str_stripped_split[0] != "#endregion") {
+ if (c_color_region.type == ColorRegion::TYPE_CODE_REGION) {
+ if (!code_region_start_checked) {
+ const Vector<String> str_stripped_split = str.strip_edges(true, false).split_spaces(1);
+ code_region_not_at_start = !str_stripped_split.is_empty() &&
+ str_stripped_split[0] != "#region" &&
+ str_stripped_split[0] != "#endregion";
+ code_region_start_checked = true;
+ }
+ if (code_region_not_at_start) {
match = false;
}
}
@@ -166,8 +173,9 @@ Dictionary GDScriptSyntaxHighlighter::_get_line_syntax_highlighting_impl(int p_l
in_region = c;
from += start_key_length;
+ int end_key_length = c_color_region.end_key.length();
// Check if it's the whole line.
- if (end_key_length == 0 || color_regions[c].line_only || from + end_key_length > line_length) {
+ if (end_key_length == 0 || c_color_region.line_only || from + end_key_length > line_length) {
// Don't skip comments, for highlighting markers.
if (color_regions[in_region].is_comment) {
break;
@@ -179,11 +187,11 @@ Dictionary GDScriptSyntaxHighlighter::_get_line_syntax_highlighting_impl(int p_l
}
}
prev_color = color_regions[in_region].color;
- highlighter_info["color"] = color_regions[c].color;
+ highlighter_info["color"] = c_color_region.color;
color_map[j] = highlighter_info;
j = line_length;
- if (!color_regions[c].line_only) {
+ if (!c_color_region.line_only) {
color_region_cache[p_line] = c;
}
}
@@ -363,7 +371,6 @@ Dictionary GDScriptSyntaxHighlighter::_get_line_syntax_highlighting_impl(int p_l
prev_is_binary_op = false;
continue;
}
- color_map.sort(); // Prevents e.g. escape sequences from being overridden by string placeholders.
}
}
@@ -573,7 +580,7 @@ Dictionary GDScriptSyntaxHighlighter::_get_line_syntax_highlighting_impl(int p_l
in_declaration_param_dicts -= 1;
break;
}
- } else if ((is_after_func_signal_declaration || prev_text == GDScriptTokenizer::get_token_name(GDScriptTokenizer::Token::FUNC)) && str[j] == '(') {
+ } else if (str[j] == '(' && (is_after_func_signal_declaration || prev_text == GDScriptTokenizer::get_token_name(GDScriptTokenizer::Token::FUNC))) {
in_declaration_params = 1;
in_declaration_param_dicts = 0;
}
@@ -737,6 +744,7 @@ Dictionary GDScriptSyntaxHighlighter::_get_line_syntax_highlighting_impl(int p_l
color_map[j] = highlighter_info;
}
}
+ color_map.sort(); // Prevents e.g. escape sequences from being overridden by string placeholders.
return color_map;
}
diff --git a/modules/gdscript/editor/gdscript_highlighter.h b/modules/gdscript/editor/gdscript_highlighter.h
index 0cec1a52ede..e1d3f6f48ac 100644
--- a/modules/gdscript/editor/gdscript_highlighter.h
+++ b/modules/gdscript/editor/gdscript_highlighter.h
@@ -54,7 +54,7 @@ private:
bool is_string = false; // `TYPE_STRING` or `TYPE_MULTILINE_STRING`.
bool is_comment = false; // `TYPE_COMMENT` or `TYPE_CODE_REGION`.
};
- Vector<ColorRegion> color_regions;
+ LocalVector<ColorRegion> color_regions;
HashMap<int, int> color_region_cache;
HashMap<StringName, Color> class_names;
diff --git a/scene/gui/text_edit.cpp b/scene/gui/text_edit.cpp
index bd320e330e6..118ae9b4af9 100644
--- a/scene/gui/text_edit.cpp
+++ b/scene/gui/text_edit.cpp
@@ -1265,7 +1265,7 @@ void TextEdit::_notification(int p_what) {
break;
}
- const Vector<Pair<int64_t, Color>> color_map = _get_line_syntax_highlighting(minimap_line);
+ const bool minimap_has_highlighting = _update_line_syntax_highlighting(minimap_line);
Color line_background_color = text.get_line_background_color(minimap_line);
@@ -1330,15 +1330,17 @@ void TextEdit::_notification(int p_what) {
for (characters = 0; j + characters < str.length(); characters++) {
int next_char_index = j + characters;
- for (const Pair<int64_t, Color> &color_data : color_map) {
- if (last_wrap_column + next_char_index >= color_data.first) {
- next_color = color_data.second;
- if (!editable) {
- next_color.a = theme_cache.font_readonly_color.a;
+ if (minimap_has_highlighting) {
+ for (const Pair<int64_t, Color> &color_data : syntax_highlighting_cache[minimap_line]) {
+ if (last_wrap_column + next_char_index >= color_data.first) {
+ next_color = color_data.second;
+ if (!editable) {
+ next_color.a = theme_cache.font_readonly_color.a;
+ }
+ next_color.a *= 0.6;
+ } else {
+ break;
}
- next_color.a *= 0.6;
- } else {
- break;
}
}
if (characters == 0) {
@@ -1426,7 +1428,7 @@ void TextEdit::_notification(int p_what) {
LineDrawingCache cache_entry;
- const Vector<Pair<int64_t, Color>> color_map = _get_line_syntax_highlighting(line);
+ const int has_highlighting = _update_line_syntax_highlighting(line);
// Ensure we at least use the font color.
Color current_color = !editable ? theme_cache.font_readonly_color : theme_cache.font_color;
@@ -1748,14 +1750,16 @@ void TextEdit::_notification(int p_what) {
}
for (int j = 0; j < gl_size; j++) {
- for (const Pair<int64_t, Color> &color_data : color_map) {
- if (color_data.first <= glyphs[j].start) {
- current_color = color_data.second;
- if (!editable && current_color.a > theme_cache.font_readonly_color.a) {
- current_color.a = theme_cache.font_readonly_color.a;
+ if (has_highlighting) {
+ for (const Pair<int64_t, Color> &color_data : syntax_highlighting_cache[line]) {
+ if (color_data.first <= glyphs[j].start) {
+ current_color = color_data.second;
+ if (!editable && current_color.a > theme_cache.font_readonly_color.a) {
+ current_color.a = theme_cache.font_readonly_color.a;
+ }
+ } else {
+ break;
}
- } else {
- break;
}
}
Color gl_color = current_color;
@@ -9719,18 +9723,17 @@ Vector2i TextEdit::_get_hovered_gutter(const Point2 &p_mouse_pos) const {
}
/* Syntax highlighting. */
-Vector<Pair<int64_t, Color>> TextEdit::_get_line_syntax_highlighting(int p_line) {
+bool TextEdit::_update_line_syntax_highlighting(int p_line) {
if (syntax_highlighter.is_null() || setting_text) {
- return Vector<Pair<int64_t, Color>>();
+ return false;
}
- HashMap<int, Vector<Pair<int64_t, Color>>>::Iterator E = syntax_highlighting_cache.find(p_line);
- if (E) {
- return E->value;
+ if (syntax_highlighting_cache.has(p_line)) {
+ return true;
}
Dictionary color_map = syntax_highlighter->get_line_syntax_highlighting(p_line);
- Vector<Pair<int64_t, Color>> result;
+ LocalVector<Pair<int64_t, Color>> result;
result.resize(color_map.size());
int i = 0;
for (const Variant *key = color_map.next(nullptr); key; key = color_map.next(key), i++) {
@@ -9740,11 +9743,11 @@ Vector<Pair<int64_t, Color>> TextEdit::_get_line_syntax_highlighting(int p_line)
if (color_data != nullptr) {
color_value = (color_data->operator Dictionary()).get("color", color_value);
}
- result.write[i] = Pair<int64_t, Color>(key_data, color_value);
+ result[i] = Pair<int64_t, Color>(key_data, color_value);
}
syntax_highlighting_cache.insert(p_line, result);
- return result;
+ return true;
}
void TextEdit::_clear_syntax_highlighting_cache() {
diff --git a/scene/gui/text_edit.h b/scene/gui/text_edit.h
index 3e6ce0de38c..ca8e5449633 100644
--- a/scene/gui/text_edit.h
+++ b/scene/gui/text_edit.h
@@ -648,9 +648,9 @@ private:
/* Syntax highlighting. */
Ref<SyntaxHighlighter> syntax_highlighter;
- HashMap<int, Vector<Pair<int64_t, Color>>> syntax_highlighting_cache;
+ HashMap<int, LocalVector<Pair<int64_t, Color>>> syntax_highlighting_cache;
- Vector<Pair<int64_t, Color>> _get_line_syntax_highlighting(int p_line);
+ bool _update_line_syntax_highlighting(int p_line);
void _clear_syntax_highlighting_cache();
void _syntax_highlighter_changed();