Skip to content

Commit 5d72153

Browse files
committed
Merge pull request #112575 from Cykyrios/string-placeholder-highlight
Add string placeholder syntax highlighting
2 parents 6d060b0 + 3905392 commit 5d72153

File tree

5 files changed

+62
-4
lines changed

5 files changed

+62
-4
lines changed

doc/classes/EditorSettings.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1646,6 +1646,10 @@
16461646
<member name="text_editor/theme/highlighting/string_color" type="Color" setter="" getter="">
16471647
The script editor's color for strings (single-line and multi-line).
16481648
</member>
1649+
<member name="text_editor/theme/highlighting/string_placeholder_color" type="Color" setter="" getter="">
1650+
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.
1651+
[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.
1652+
</member>
16491653
<member name="text_editor/theme/highlighting/symbol_color" type="Color" setter="" getter="">
16501654
The script editor's color for operators ([code]( ) [ ] { } + - * /[/code], ...).
16511655
</member>

editor/settings/editor_settings.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -716,6 +716,7 @@ void EditorSettings::_load_defaults(Ref<ConfigFile> p_extra_config) {
716716
"text_editor/theme/highlighting/comment_color",
717717
"text_editor/theme/highlighting/doc_comment_color",
718718
"text_editor/theme/highlighting/string_color",
719+
"text_editor/theme/highlighting/string_placeholder_color",
719720
"text_editor/theme/highlighting/background_color",
720721
"text_editor/theme/highlighting/text_color",
721722
"text_editor/theme/highlighting/line_number_color",
@@ -1711,6 +1712,7 @@ HashMap<StringName, Color> EditorSettings::get_godot2_text_editor_theme() {
17111712
colors["text_editor/theme/highlighting/comment_color"] = Color(0.4, 0.4, 0.4);
17121713
colors["text_editor/theme/highlighting/doc_comment_color"] = Color(0.5, 0.6, 0.7);
17131714
colors["text_editor/theme/highlighting/string_color"] = Color(0.94, 0.43, 0.75);
1715+
colors["text_editor/theme/highlighting/string_placeholder_color"] = Color(1, 0.75, 0.4);
17141716
colors["text_editor/theme/highlighting/background_color"] = Color(0.13, 0.12, 0.15);
17151717
colors["text_editor/theme/highlighting/completion_background_color"] = Color(0.17, 0.16, 0.2);
17161718
colors["text_editor/theme/highlighting/completion_selected_color"] = Color(0.26, 0.26, 0.27);

editor/themes/editor_theme_manager.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,7 @@ void EditorThemeManager::_populate_text_editor_styles(const Ref<EditorTheme> &p_
506506
colors["text_editor/theme/highlighting/comment_color"] = p_config.dark_icon_and_font ? dim_color : Color(0.08, 0.08, 0.08, 0.5);
507507
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);
508508
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);
509+
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);
509510

510511
// Use the brightest background color on a light theme (which generally uses a negative contrast rate).
511512
colors["text_editor/theme/highlighting/background_color"] = p_config.dark_icon_and_font ? p_config.dark_color_2 : p_config.dark_color_3;

modules/gdscript/editor/gdscript_highlighter.cpp

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -244,18 +244,66 @@ Dictionary GDScriptSyntaxHighlighter::_get_line_syntax_highlighting_impl(int p_l
244244
int region_end_index = -1;
245245
int end_key_length = color_regions[in_region].end_key.length();
246246
const char32_t *end_key = color_regions[in_region].end_key.get_data();
247+
int placeholder_end = from;
247248
for (; from < line_length; from++) {
248249
if (line_length - from < end_key_length) {
249-
// Don't break if '\' to highlight esc chars.
250-
if (str.find_char('\\', from) < 0) {
250+
// Don't break if '\' to highlight escape sequences,
251+
// and '%' and '{' to highlight placeholders.
252+
if (str.find_char('\\', from) == -1 && str.find_char('%', from) == -1 && str.find_char('{', from) == -1) {
251253
break;
252254
}
253255
}
254256

255-
if (!is_symbol(str[from])) {
257+
if (!is_symbol(str[from]) && str[from] != '%') {
256258
continue;
257259
}
258260

261+
if (str[from] == '%' || str[from] == '{') {
262+
int placeholder_start = from;
263+
bool is_percent = str[from] == '%';
264+
265+
from++;
266+
267+
if (is_percent) {
268+
if (str[from] == '%') {
269+
placeholder_end = from + 1;
270+
} else {
271+
const String allowed_chars = "+.-*0123456789";
272+
const String placeholder_types = "cdfosvxX";
273+
for (int i = 0; i < line_length - from; i++) {
274+
if (allowed_chars.contains_char(str[from + i]) &&
275+
!placeholder_types.contains_char(str[from + i]) &&
276+
(str[from + i] != end_key[0] || (str[from + i] == end_key[0] && str[from + i - 1] == '\\'))) {
277+
continue;
278+
}
279+
if (placeholder_types.contains_char(str[from + i])) {
280+
placeholder_end = from + i + 1;
281+
}
282+
break;
283+
}
284+
}
285+
} else {
286+
for (int i = 0; i < line_length - from; i++) {
287+
if (str[from + i] != '}' && (str[from + i] != end_key[0] || (str[from + i] == end_key[0] && str[from + i - 1] == '\\'))) {
288+
continue;
289+
}
290+
if (str[from + i] == '}') {
291+
placeholder_end = from + i + 1;
292+
}
293+
break;
294+
}
295+
}
296+
297+
if (placeholder_end > placeholder_start) {
298+
Dictionary placeholder_highlighter_info;
299+
placeholder_highlighter_info["color"] = placeholder_color;
300+
color_map[placeholder_start] = placeholder_highlighter_info;
301+
Dictionary region_continue_highlighter_info;
302+
region_continue_highlighter_info["color"] = region_color;
303+
color_map[placeholder_end] = region_continue_highlighter_info;
304+
}
305+
}
306+
259307
if (str[from] == '\\') {
260308
if (!color_regions[in_region].r_prefix) {
261309
Dictionary escape_char_highlighter_info;
@@ -280,7 +328,7 @@ Dictionary GDScriptSyntaxHighlighter::_get_line_syntax_highlighting_impl(int p_l
280328
}
281329

282330
Dictionary region_continue_highlighter_info;
283-
region_continue_highlighter_info["color"] = region_color;
331+
region_continue_highlighter_info["color"] = from < placeholder_end ? placeholder_color : region_color;
284332
color_map[from + 1] = region_continue_highlighter_info;
285333
}
286334

@@ -315,6 +363,7 @@ Dictionary GDScriptSyntaxHighlighter::_get_line_syntax_highlighting_impl(int p_l
315363
prev_is_binary_op = false;
316364
continue;
317365
}
366+
color_map.sort(); // Prevents e.g. escape sequences from being overridden by string placeholders.
318367
}
319368
}
320369

@@ -813,6 +862,7 @@ void GDScriptSyntaxHighlighter::_update_cache() {
813862

814863
/* Strings */
815864
string_color = EDITOR_GET("text_editor/theme/highlighting/string_color");
865+
placeholder_color = EDITOR_GET("text_editor/theme/highlighting/string_placeholder_color");
816866
add_color_region(ColorRegion::TYPE_STRING, "\"", "\"", string_color);
817867
add_color_region(ColorRegion::TYPE_STRING, "'", "'", string_color);
818868
add_color_region(ColorRegion::TYPE_MULTILINE_STRING, "\"\"\"", "\"\"\"", string_color);

modules/gdscript/editor/gdscript_highlighter.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ class GDScriptSyntaxHighlighter : public EditorSyntaxHighlighter {
8989
Color number_color;
9090
Color member_variable_color;
9191
Color string_color;
92+
Color placeholder_color;
9293
Color node_path_color;
9394
Color node_ref_color;
9495
Color annotation_color;

0 commit comments

Comments
 (0)