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

@ -1646,6 +1646,10 @@
<member name="text_editor/theme/highlighting/string_color" type="Color" setter="" getter="">
The script editor's color for strings (single-line and multi-line).
</member>
<member name="text_editor/theme/highlighting/string_placeholder_color" type="Color" setter="" getter="">
The script editor's color for string placeholders, such as [code]%s[/code] and [code]{_}[/code]. Refer to the [url=$DOCS_URL/tutorials/scripting/gdscript/gdscript_format_string.html]GDScript format strings documentation[/url] for more details.
[b]Note:[/b] Only the default [code]{_}[/code] placeholder patterns are highlighted for the [method String.format] method. Custom patterns still appear as plain strings.
</member>
<member name="text_editor/theme/highlighting/symbol_color" type="Color" setter="" getter="">
The script editor's color for operators ([code]( ) [ ] { } + - * /[/code], ...).
</member>

View file

@ -716,6 +716,7 @@ void EditorSettings::_load_defaults(Ref<ConfigFile> p_extra_config) {
"text_editor/theme/highlighting/comment_color",
"text_editor/theme/highlighting/doc_comment_color",
"text_editor/theme/highlighting/string_color",
"text_editor/theme/highlighting/string_placeholder_color",
"text_editor/theme/highlighting/background_color",
"text_editor/theme/highlighting/text_color",
"text_editor/theme/highlighting/line_number_color",
@ -1711,6 +1712,7 @@ HashMap<StringName, Color> EditorSettings::get_godot2_text_editor_theme() {
colors["text_editor/theme/highlighting/comment_color"] = Color(0.4, 0.4, 0.4);
colors["text_editor/theme/highlighting/doc_comment_color"] = Color(0.5, 0.6, 0.7);
colors["text_editor/theme/highlighting/string_color"] = Color(0.94, 0.43, 0.75);
colors["text_editor/theme/highlighting/string_placeholder_color"] = Color(1, 0.75, 0.4);
colors["text_editor/theme/highlighting/background_color"] = Color(0.13, 0.12, 0.15);
colors["text_editor/theme/highlighting/completion_background_color"] = Color(0.17, 0.16, 0.2);
colors["text_editor/theme/highlighting/completion_selected_color"] = Color(0.26, 0.26, 0.27);

View file

@ -506,6 +506,7 @@ void EditorThemeManager::_populate_text_editor_styles(const Ref<EditorTheme> &p_
colors["text_editor/theme/highlighting/comment_color"] = p_config.dark_icon_and_font ? dim_color : Color(0.08, 0.08, 0.08, 0.5);
colors["text_editor/theme/highlighting/doc_comment_color"] = p_config.dark_icon_and_font ? Color(0.6, 0.7, 0.8, 0.8) : Color(0.15, 0.15, 0.4, 0.7);
colors["text_editor/theme/highlighting/string_color"] = p_config.dark_icon_and_font ? Color(1, 0.93, 0.63) : Color(0.6, 0.42, 0);
colors["text_editor/theme/highlighting/string_placeholder_color"] = p_config.dark_icon_and_font ? Color(1, 0.75, 0.4) : Color(0.93, 0.6, 0.33);
// Use the brightest background color on a light theme (which generally uses a negative contrast rate).
colors["text_editor/theme/highlighting/background_color"] = p_config.dark_icon_and_font ? p_config.dark_color_2 : p_config.dark_color_3;

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);

View file

@ -89,6 +89,7 @@ private:
Color number_color;
Color member_variable_color;
Color string_color;
Color placeholder_color;
Color node_path_color;
Color node_ref_color;
Color annotation_color;