Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GDScript: Add raw string literals (r-strings) #74995

Merged
merged 1 commit into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 35 additions & 8 deletions modules/gdscript/editor/gdscript_highlighter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ Dictionary GDScriptSyntaxHighlighter::_get_line_syntax_highlighting_impl(int p_l
bool in_keyword = false;
bool in_word = false;
bool in_number = false;
bool in_raw_string = false;
bool in_node_path = false;
bool in_node_ref = false;
bool in_annotation = false;
Expand Down Expand Up @@ -234,15 +235,33 @@ Dictionary GDScriptSyntaxHighlighter::_get_line_syntax_highlighting_impl(int p_l
}

if (str[from] == '\\') {
Dictionary escape_char_highlighter_info;
escape_char_highlighter_info["color"] = symbol_color;
color_map[from] = escape_char_highlighter_info;
if (!in_raw_string) {
Dictionary escape_char_highlighter_info;
escape_char_highlighter_info["color"] = symbol_color;
color_map[from] = escape_char_highlighter_info;
}

from++;

Dictionary region_continue_highlighter_info;
region_continue_highlighter_info["color"] = region_color;
color_map[from + 1] = region_continue_highlighter_info;
if (!in_raw_string) {
int esc_len = 0;
if (str[from] == 'u') {
esc_len = 4;
} else if (str[from] == 'U') {
esc_len = 6;
}
for (int k = 0; k < esc_len && from < line_length - 1; k++) {
if (!is_hex_digit(str[from + 1])) {
break;
}
from++;
}

Dictionary region_continue_highlighter_info;
region_continue_highlighter_info["color"] = region_color;
color_map[from + 1] = region_continue_highlighter_info;
}

continue;
}

Expand Down Expand Up @@ -489,6 +508,12 @@ Dictionary GDScriptSyntaxHighlighter::_get_line_syntax_highlighting_impl(int p_l
in_member_variable = false;
}

if (!in_raw_string && in_region == -1 && str[j] == 'r' && j < line_length - 1 && (str[j + 1] == '"' || str[j + 1] == '\'')) {
in_raw_string = true;
} else if (in_raw_string && in_region == -1) {
in_raw_string = false;
}

// Keep symbol color for binary '&&'. In the case of '&&&' use StringName color for the last ampersand.
if (!in_string_name && in_region == -1 && str[j] == '&' && !is_binary_op) {
if (j >= 2 && str[j - 1] == '&' && str[j - 2] != '&' && prev_is_binary_op) {
Expand Down Expand Up @@ -520,7 +545,9 @@ Dictionary GDScriptSyntaxHighlighter::_get_line_syntax_highlighting_impl(int p_l
in_annotation = false;
}

if (in_node_ref) {
if (in_raw_string) {
color = string_color;
} else if (in_node_ref) {
next_type = NODE_REF;
color = node_ref_color;
} else if (in_annotation) {
Expand Down Expand Up @@ -692,7 +719,7 @@ void GDScriptSyntaxHighlighter::_update_cache() {
}

/* Strings */
const Color string_color = EDITOR_GET("text_editor/theme/highlighting/string_color");
string_color = EDITOR_GET("text_editor/theme/highlighting/string_color");
List<String> strings;
gdscript->get_string_delimiters(&strings);
for (const String &string : strings) {
Expand Down
1 change: 1 addition & 0 deletions modules/gdscript/editor/gdscript_highlighter.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ class GDScriptSyntaxHighlighter : public EditorSyntaxHighlighter {
Color built_in_type_color;
Color number_color;
Color member_color;
Color string_color;
Color node_path_color;
Color node_ref_color;
Color annotation_color;
Expand Down
1 change: 1 addition & 0 deletions modules/gdscript/gdscript_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ void GDScriptLanguage::get_string_delimiters(List<String> *p_delimiters) const {
p_delimiters->push_back("' '");
p_delimiters->push_back("\"\"\" \"\"\"");
p_delimiters->push_back("''' '''");
// NOTE: StringName, NodePath and r-strings are not listed here.
}

bool GDScriptLanguage::is_using_templates() {
Expand Down
Loading