1
0
mirror of https://github.com/godotengine/godot.git synced 2025-11-18 14:21:41 +00:00

Add string placeholder syntax highlighting

This commit is contained in:
Cyril Bissey
2025-11-13 08:16:41 +01:00
parent bad1287b62
commit 3905392596
5 changed files with 62 additions and 4 deletions

View File

@@ -244,18 +244,66 @@ Dictionary GDScriptSyntaxHighlighter::_get_line_syntax_highlighting_impl(int p_l
int region_end_index = -1;
int end_key_length = color_regions[in_region].end_key.length();
const char32_t *end_key = color_regions[in_region].end_key.get_data();
int placeholder_end = from;
for (; from < line_length; from++) {
if (line_length - from < end_key_length) {
// Don't break if '\' to highlight esc chars.
if (str.find_char('\\', from) < 0) {
// Don't break if '\' to highlight escape sequences,
// and '%' and '{' to highlight placeholders.
if (str.find_char('\\', from) == -1 && str.find_char('%', from) == -1 && str.find_char('{', from) == -1) {
break;
}
}
if (!is_symbol(str[from])) {
if (!is_symbol(str[from]) && str[from] != '%') {
continue;
}
if (str[from] == '%' || str[from] == '{') {
int placeholder_start = from;
bool is_percent = str[from] == '%';
from++;
if (is_percent) {
if (str[from] == '%') {
placeholder_end = from + 1;
} else {
const String allowed_chars = "+.-*0123456789";
const String placeholder_types = "cdfosvxX";
for (int i = 0; i < line_length - from; i++) {
if (allowed_chars.contains_char(str[from + i]) &&
!placeholder_types.contains_char(str[from + i]) &&
(str[from + i] != end_key[0] || (str[from + i] == end_key[0] && str[from + i - 1] == '\\'))) {
continue;
}
if (placeholder_types.contains_char(str[from + i])) {
placeholder_end = from + i + 1;
}
break;
}
}
} else {
for (int i = 0; i < line_length - from; i++) {
if (str[from + i] != '}' && (str[from + i] != end_key[0] || (str[from + i] == end_key[0] && str[from + i - 1] == '\\'))) {
continue;
}
if (str[from + i] == '}') {
placeholder_end = from + i + 1;
}
break;
}
}
if (placeholder_end > placeholder_start) {
Dictionary placeholder_highlighter_info;
placeholder_highlighter_info["color"] = placeholder_color;
color_map[placeholder_start] = placeholder_highlighter_info;
Dictionary region_continue_highlighter_info;
region_continue_highlighter_info["color"] = region_color;
color_map[placeholder_end] = region_continue_highlighter_info;
}
}
if (str[from] == '\\') {
if (!color_regions[in_region].r_prefix) {
Dictionary escape_char_highlighter_info;
@@ -280,7 +328,7 @@ Dictionary GDScriptSyntaxHighlighter::_get_line_syntax_highlighting_impl(int p_l
}
Dictionary region_continue_highlighter_info;
region_continue_highlighter_info["color"] = region_color;
region_continue_highlighter_info["color"] = from < placeholder_end ? placeholder_color : region_color;
color_map[from + 1] = region_continue_highlighter_info;
}
@@ -315,6 +363,7 @@ 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.
}
}
@@ -813,6 +862,7 @@ void GDScriptSyntaxHighlighter::_update_cache() {
/* Strings */
string_color = EDITOR_GET("text_editor/theme/highlighting/string_color");
placeholder_color = EDITOR_GET("text_editor/theme/highlighting/string_placeholder_color");
add_color_region(ColorRegion::TYPE_STRING, "\"", "\"", string_color);
add_color_region(ColorRegion::TYPE_STRING, "'", "'", string_color);
add_color_region(ColorRegion::TYPE_MULTILINE_STRING, "\"\"\"", "\"\"\"", string_color);