Skip to content

Commit

Permalink
GDScript: Add raw string literals (r-strings)
Browse files Browse the repository at this point in the history
  • Loading branch information
dalexeev committed Sep 11, 2023
1 parent 221884e commit 2964c7d
Show file tree
Hide file tree
Showing 13 changed files with 250 additions and 132 deletions.
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

0 comments on commit 2964c7d

Please sign in to comment.