Path: blob/master/modules/gdscript/editor/gdscript_highlighter.cpp
10278 views
/**************************************************************************/1/* gdscript_highlighter.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "gdscript_highlighter.h"3132#include "../gdscript.h"33#include "../gdscript_tokenizer.h"3435#include "core/config/project_settings.h"36#include "core/core_constants.h"37#include "editor/settings/editor_settings.h"38#include "editor/themes/editor_theme_manager.h"39#include "scene/gui/text_edit.h"4041Dictionary GDScriptSyntaxHighlighter::_get_line_syntax_highlighting_impl(int p_line) {42Dictionary color_map;4344Type next_type = NONE;45Type current_type = NONE;46Type prev_type = NONE;4748String prev_text = "";49int prev_column = 0;50bool prev_is_char = false;51bool prev_is_digit = false;52bool prev_is_binary_op = false;5354bool in_keyword = false;55bool in_word = false;56bool in_number = false;57bool in_node_path = false;58bool in_node_ref = false;59bool in_annotation = false;60bool in_string_name = false;61bool is_hex_notation = false;62bool is_bin_notation = false;63bool in_member_variable = false;64bool in_lambda = false;6566bool in_function_name = false; // Any call.67bool in_function_declaration = false; // Only declaration.68bool in_signal_declaration = false;69bool is_after_func_signal_declaration = false;70bool in_var_const_declaration = false;71bool is_after_var_const_declaration = false;72bool expect_type = false;7374int in_declaration_params = 0; // The number of opened `(` after func/signal name.75int in_declaration_param_dicts = 0; // The number of opened `{` inside func params.76int in_type_params = 0; // The number of opened `[` after type name.7778Color keyword_color;79Color color;8081color_region_cache[p_line] = -1;82int in_region = -1;83if (p_line != 0) {84int prev_region_line = p_line - 1;85while (prev_region_line > 0 && !color_region_cache.has(prev_region_line)) {86prev_region_line--;87}88for (int i = prev_region_line; i < p_line - 1; i++) {89get_line_syntax_highlighting(i);90}91if (!color_region_cache.has(p_line - 1)) {92get_line_syntax_highlighting(p_line - 1);93}94in_region = color_region_cache[p_line - 1];95}9697const String &str = text_edit->get_line_with_ime(p_line);98const int line_length = str.length();99Color prev_color;100101if (in_region != -1 && line_length == 0) {102color_region_cache[p_line] = in_region;103}104for (int j = 0; j < line_length; j++) {105Dictionary highlighter_info;106107color = font_color;108bool is_char = !is_symbol(str[j]);109bool is_a_symbol = is_symbol(str[j]);110bool is_a_digit = is_digit(str[j]);111bool is_binary_op = false;112113/* color regions */114if (is_a_symbol || in_region != -1) {115int from = j;116117if (in_region == -1) {118for (; from < line_length; from++) {119if (str[from] == '\\') {120from++;121continue;122}123break;124}125}126127if (from != line_length) {128// Check if we are in entering a region.129if (in_region == -1) {130const bool r_prefix = from > 0 && str[from - 1] == 'r';131for (int c = 0; c < color_regions.size(); c++) {132// Check there is enough room.133int chars_left = line_length - from;134int start_key_length = color_regions[c].start_key.length();135int end_key_length = color_regions[c].end_key.length();136if (chars_left < start_key_length) {137continue;138}139140if (color_regions[c].is_string && color_regions[c].r_prefix != r_prefix) {141continue;142}143144// Search the line.145bool match = true;146const char32_t *start_key = color_regions[c].start_key.get_data();147for (int k = 0; k < start_key_length; k++) {148if (start_key[k] != str[from + k]) {149match = false;150break;151}152}153// "#region" and "#endregion" only highlighted if they're the first region on the line.154if (color_regions[c].type == ColorRegion::TYPE_CODE_REGION) {155Vector<String> str_stripped_split = str.strip_edges().split_spaces(1);156if (!str_stripped_split.is_empty() &&157str_stripped_split[0] != "#region" &&158str_stripped_split[0] != "#endregion") {159match = false;160}161}162if (!match) {163continue;164}165in_region = c;166from += start_key_length;167168// Check if it's the whole line.169if (end_key_length == 0 || color_regions[c].line_only || from + end_key_length > line_length) {170// Don't skip comments, for highlighting markers.171if (color_regions[in_region].is_comment) {172break;173}174if (from + end_key_length > line_length) {175// If it's key length and there is a '\', dont skip to highlight esc chars.176if (str.find_char('\\', from) >= 0) {177break;178}179}180prev_color = color_regions[in_region].color;181highlighter_info["color"] = color_regions[c].color;182color_map[j] = highlighter_info;183184j = line_length;185if (!color_regions[c].line_only) {186color_region_cache[p_line] = c;187}188}189break;190}191192// Don't skip comments, for highlighting markers.193if (j == line_length && !color_regions[in_region].is_comment) {194continue;195}196}197198// If we are in one, find the end key.199if (in_region != -1) {200Color region_color = color_regions[in_region].color;201if (in_node_path && color_regions[in_region].type == ColorRegion::TYPE_STRING) {202region_color = node_path_color;203}204if (in_node_ref && color_regions[in_region].type == ColorRegion::TYPE_STRING) {205region_color = node_ref_color;206}207if (in_string_name && color_regions[in_region].type == ColorRegion::TYPE_STRING) {208region_color = string_name_color;209}210211prev_color = region_color;212highlighter_info["color"] = region_color;213color_map[j] = highlighter_info;214215if (color_regions[in_region].is_comment) {216int marker_start_pos = from;217int marker_len = 0;218while (from <= line_length) {219if (from < line_length && is_unicode_identifier_continue(str[from])) {220marker_len++;221} else {222if (marker_len > 0) {223HashMap<String, CommentMarkerLevel>::ConstIterator E = comment_markers.find(str.substr(marker_start_pos, marker_len));224if (E) {225Dictionary marker_highlighter_info;226marker_highlighter_info["color"] = comment_marker_colors[E->value];227color_map[marker_start_pos] = marker_highlighter_info;228229Dictionary marker_continue_highlighter_info;230marker_continue_highlighter_info["color"] = region_color;231color_map[from] = marker_continue_highlighter_info;232}233}234marker_start_pos = from + 1;235marker_len = 0;236}237from++;238}239from = line_length - 1;240j = from;241} else {242// Search the line.243int region_end_index = -1;244int end_key_length = color_regions[in_region].end_key.length();245const char32_t *end_key = color_regions[in_region].end_key.get_data();246for (; from < line_length; from++) {247if (line_length - from < end_key_length) {248// Don't break if '\' to highlight esc chars.249if (str.find_char('\\', from) < 0) {250break;251}252}253254if (!is_symbol(str[from])) {255continue;256}257258if (str[from] == '\\') {259if (!color_regions[in_region].r_prefix) {260Dictionary escape_char_highlighter_info;261escape_char_highlighter_info["color"] = symbol_color;262color_map[from] = escape_char_highlighter_info;263}264265from++;266267if (!color_regions[in_region].r_prefix) {268int esc_len = 0;269if (str[from] == 'u') {270esc_len = 4;271} else if (str[from] == 'U') {272esc_len = 6;273}274for (int k = 0; k < esc_len && from < line_length - 1; k++) {275if (!is_hex_digit(str[from + 1])) {276break;277}278from++;279}280281Dictionary region_continue_highlighter_info;282region_continue_highlighter_info["color"] = region_color;283color_map[from + 1] = region_continue_highlighter_info;284}285286continue;287}288289region_end_index = from;290for (int k = 0; k < end_key_length; k++) {291if (end_key[k] != str[from + k]) {292region_end_index = -1;293break;294}295}296297if (region_end_index != -1) {298break;299}300}301j = from + (end_key_length - 1);302if (region_end_index == -1) {303color_region_cache[p_line] = in_region;304}305}306307prev_type = REGION;308prev_text = "";309prev_column = j;310311in_region = -1;312prev_is_char = false;313prev_is_digit = false;314prev_is_binary_op = false;315continue;316}317}318}319320// VERY hacky... but couldn't come up with anything better.321if (j > 0 && (str[j] == '&' || str[j] == '^' || str[j] == '%' || str[j] == '+' || str[j] == '-' || str[j] == '~' || str[j] == '.')) {322int to = j - 1;323// Find what the last text was (prev_text won't work if there's no whitespace, so we need to do it manually).324while (to > 0 && is_whitespace(str[to])) {325to--;326}327int from = to;328while (from > 0 && !is_symbol(str[from])) {329from--;330}331String word = str.substr(from + 1, to - from);332// Keywords need to be exceptions, except for keywords that represent a value.333if (word == "true" || word == "false" || word == "null" || word == "PI" || word == "TAU" || word == "INF" || word == "NAN" || word == "self" || word == "super" || !reserved_keywords.has(word)) {334if (!is_symbol(str[to]) || str[to] == '"' || str[to] == '\'' || str[to] == ')' || str[to] == ']' || str[to] == '}') {335is_binary_op = true;336}337}338}339340if (!is_char) {341in_keyword = false;342}343344// Allow ABCDEF in hex notation.345if (is_hex_notation && (is_hex_digit(str[j]) || is_a_digit)) {346is_a_digit = true;347} else if (str[j] != '_') {348is_hex_notation = false;349}350351// Disallow anything not a 0 or 1 in binary notation.352if (is_bin_notation && !is_binary_digit(str[j])) {353is_a_digit = false;354is_bin_notation = false;355}356357if (!in_number && !in_word && is_a_digit) {358in_number = true;359}360361// Special cases for numbers.362if (in_number && !is_a_digit) {363if ((str[j] == 'b' || str[j] == 'B') && str[j - 1] == '0') {364is_bin_notation = true;365} else if ((str[j] == 'x' || str[j] == 'X') && str[j - 1] == '0') {366is_hex_notation = true;367} else if (!((str[j] == '-' || str[j] == '+') && (str[j - 1] == 'e' || str[j - 1] == 'E') && !prev_is_digit) &&368!(str[j] == '_' && (prev_is_digit || str[j - 1] == 'b' || str[j - 1] == 'B' || str[j - 1] == 'x' || str[j - 1] == 'X' || str[j - 1] == '.')) &&369!((str[j] == 'e' || str[j] == 'E') && (prev_is_digit || str[j - 1] == '_')) &&370!(str[j] == '.' && (prev_is_digit || (!prev_is_binary_op && (j > 0 && (str[j - 1] == '_' || str[j - 1] == '-' || str[j - 1] == '+' || str[j - 1] == '~'))))) &&371!((str[j] == '-' || str[j] == '+' || str[j] == '~') && !is_binary_op && !prev_is_binary_op && str[j - 1] != 'e' && str[j - 1] != 'E')) {372/* This condition continues number highlighting in special cases.3731st row: '+' or '-' after scientific notation (like 3e-4);3742nd row: '_' as a numeric separator;3753rd row: Scientific notation 'e' and floating points;3764th row: Floating points inside the number, or leading if after a unary mathematical operator;3775th row: Multiple unary mathematical operators (like ~-7) */378in_number = false;379}380} else if (str[j] == '.' && !is_binary_op && is_digit(str[j + 1]) && (j == 0 || (j > 0 && str[j - 1] != '.'))) {381// Start number highlighting from leading decimal points (like .42)382in_number = true;383} else if ((str[j] == '-' || str[j] == '+' || str[j] == '~') && !is_binary_op) {384// Only start number highlighting on unary operators if a digit follows them.385int non_op = j + 1;386while (str[non_op] == '-' || str[non_op] == '+' || str[non_op] == '~') {387non_op++;388}389if (is_digit(str[non_op]) || (str[non_op] == '.' && non_op < line_length && is_digit(str[non_op + 1]))) {390in_number = true;391}392}393394if (!in_word && is_unicode_identifier_start(str[j]) && !in_number) {395in_word = true;396}397398if (is_a_symbol && str[j] != '.' && in_word) {399in_word = false;400}401402if (!in_keyword && is_char && !prev_is_char) {403int to = j;404while (to < line_length && !is_symbol(str[to])) {405to++;406}407408String word = str.substr(j, to - j);409Color col;410if (global_functions.has(word)) {411// "assert" and "preload" are reserved, so highlight even if not followed by a bracket.412if (word == GDScriptTokenizer::get_token_name(GDScriptTokenizer::Token::ASSERT) || word == GDScriptTokenizer::get_token_name(GDScriptTokenizer::Token::PRELOAD)) {413col = global_function_color;414} else {415// For other global functions, check if followed by bracket.416int k = to;417while (k < line_length && is_whitespace(str[k])) {418k++;419}420421if (str[k] == '(') {422col = global_function_color;423}424}425} else if (class_names.has(word)) {426col = class_names[word];427} else if (reserved_keywords.has(word)) {428col = reserved_keywords[word];429// Don't highlight `list` as a type in `for elem: Type in list`.430expect_type = false;431} else if (member_keywords.has(word)) {432col = member_keywords[word];433in_member_variable = true;434}435436if (col != Color()) {437for (int k = j - 1; k >= 0; k--) {438if (str[k] == '.') {439col = Color(); // Keyword, member & global func indexing not allowed.440break;441} else if (str[k] > 32) {442break;443}444}445446if (!in_member_variable && col != Color()) {447in_keyword = true;448keyword_color = col;449}450}451}452453if (!in_function_name && in_word && !in_keyword) {454if (prev_text == GDScriptTokenizer::get_token_name(GDScriptTokenizer::Token::SIGNAL)) {455in_signal_declaration = true;456} else {457int k = j;458while (k < line_length && !is_symbol(str[k]) && !is_whitespace(str[k])) {459k++;460}461462// Check for space between name and bracket.463while (k < line_length && is_whitespace(str[k])) {464k++;465}466467if (str[k] == '(') {468in_function_name = true;469if (prev_text == GDScriptTokenizer::get_token_name(GDScriptTokenizer::Token::FUNC)) {470in_function_declaration = true;471}472} else if (prev_text == GDScriptTokenizer::get_token_name(GDScriptTokenizer::Token::VAR) || prev_text == GDScriptTokenizer::get_token_name(GDScriptTokenizer::Token::FOR) || prev_text == GDScriptTokenizer::get_token_name(GDScriptTokenizer::Token::TK_CONST)) {473in_var_const_declaration = true;474}475476// Check for lambda.477if (in_function_declaration) {478k = j - 1;479while (k > 0 && is_whitespace(str[k])) {480k--;481}482483if (str[k] == ':') {484in_lambda = true;485}486}487}488}489490if (!in_function_name && !in_member_variable && !in_keyword && !in_number && in_word) {491int k = j;492while (k > 0 && !is_symbol(str[k]) && !is_whitespace(str[k])) {493k--;494}495496if (str[k] == '.' && (k < 1 || str[k - 1] != '.')) {497in_member_variable = true;498}499}500501if (is_a_symbol) {502if (in_function_declaration || in_signal_declaration) {503is_after_func_signal_declaration = true;504}505if (in_var_const_declaration) {506is_after_var_const_declaration = true;507}508509if (in_declaration_params > 0) {510switch (str[j]) {511case '(':512in_declaration_params += 1;513break;514case ')':515in_declaration_params -= 1;516break;517case '{':518in_declaration_param_dicts += 1;519break;520case '}':521in_declaration_param_dicts -= 1;522break;523}524} else if ((is_after_func_signal_declaration || prev_text == GDScriptTokenizer::get_token_name(GDScriptTokenizer::Token::FUNC)) && str[j] == '(') {525in_declaration_params = 1;526in_declaration_param_dicts = 0;527}528529if (expect_type) {530switch (str[j]) {531case '[':532in_type_params += 1;533break;534case ']':535in_type_params -= 1;536break;537case ',':538if (in_type_params <= 0) {539expect_type = false;540}541break;542case ' ':543case '\t':544case '.':545break;546default:547expect_type = false;548break;549}550} else {551if (j > 0 && str[j - 1] == '-' && str[j] == '>') {552expect_type = true;553in_type_params = 0;554}555if ((is_after_var_const_declaration || (in_declaration_params == 1 && in_declaration_param_dicts == 0)) && str[j] == ':') {556expect_type = true;557in_type_params = 0;558}559}560561in_function_name = false;562in_function_declaration = false;563in_signal_declaration = false;564in_var_const_declaration = false;565in_lambda = false;566in_member_variable = false;567568if (!is_whitespace(str[j])) {569is_after_func_signal_declaration = false;570is_after_var_const_declaration = false;571}572}573574// Set color of StringName, keeping symbol color for binary '&&' and '&'.575if (!in_string_name && in_region == -1 && str[j] == '&' && !is_binary_op) {576if (j + 1 <= line_length - 1 && (str[j + 1] == '\'' || str[j + 1] == '"')) {577in_string_name = true;578// Cover edge cases of i.e. '+&""' and '&&&""', so the StringName is properly colored.579if (prev_is_binary_op && j >= 2 && str[j - 1] == '&' && str[j - 2] != '&') {580in_string_name = false;581is_binary_op = true;582}583} else {584is_binary_op = true;585}586} else if (in_region != -1 || is_a_symbol) {587in_string_name = false;588}589590// '^^' has no special meaning, so unlike StringName, when binary, use NodePath color for the last caret.591if (!in_node_path && in_region == -1 && str[j] == '^' && !is_binary_op && (j == 0 || (j > 0 && str[j - 1] != '^') || prev_is_binary_op)) {592in_node_path = true;593} else if (in_region != -1 || is_a_symbol) {594in_node_path = false;595}596597if (!in_node_ref && in_region == -1 && (str[j] == '$' || (str[j] == '%' && !is_binary_op))) {598in_node_ref = true;599} else if (in_region != -1 || (is_a_symbol && str[j] != '/' && str[j] != '%') || (is_a_digit && j > 0 && (str[j - 1] == '$' || str[j - 1] == '/' || str[j - 1] == '%'))) {600// NodeRefs can't start with digits, so point out wrong syntax immediately.601in_node_ref = false;602}603604if (!in_annotation && in_region == -1 && str[j] == '@') {605in_annotation = true;606} else if (in_region != -1 || is_a_symbol) {607in_annotation = false;608}609610const bool in_raw_string_prefix = in_region == -1 && str[j] == 'r' && j + 1 < line_length && (str[j + 1] == '"' || str[j + 1] == '\'');611612if (in_raw_string_prefix) {613color = string_color;614} else if (in_node_ref) {615next_type = NODE_REF;616color = node_ref_color;617} else if (in_annotation) {618next_type = ANNOTATION;619color = annotation_color;620} else if (in_string_name) {621next_type = STRING_NAME;622color = string_name_color;623} else if (in_node_path) {624next_type = NODE_PATH;625color = node_path_color;626} else if (in_keyword) {627next_type = KEYWORD;628color = keyword_color;629} else if (in_signal_declaration) {630next_type = SIGNAL;631color = member_variable_color;632} else if (in_function_name) {633next_type = FUNCTION;634if (!in_lambda && in_function_declaration) {635color = function_definition_color;636} else {637color = function_color;638}639} else if (in_number) {640next_type = NUMBER;641color = number_color;642} else if (is_a_symbol) {643next_type = SYMBOL;644color = symbol_color;645} else if (expect_type) {646next_type = TYPE;647color = type_color;648} else if (in_member_variable) {649next_type = MEMBER;650color = member_variable_color;651} else {652next_type = IDENTIFIER;653}654655if (next_type != current_type) {656if (current_type == NONE) {657current_type = next_type;658} else {659prev_type = current_type;660current_type = next_type;661662// No need to store regions...663if (prev_type == REGION) {664prev_text = "";665prev_column = j;666} else {667String text = str.substr(prev_column, j - prev_column).strip_edges();668prev_column = j;669670// Ignore if just whitespace.671if (!text.is_empty()) {672prev_text = text;673}674}675}676}677678prev_is_char = is_char;679prev_is_digit = is_a_digit;680prev_is_binary_op = is_binary_op;681682if (color != prev_color) {683prev_color = color;684highlighter_info["color"] = color;685color_map[j] = highlighter_info;686}687}688return color_map;689}690691String GDScriptSyntaxHighlighter::_get_name() const {692return "GDScript";693}694695PackedStringArray GDScriptSyntaxHighlighter::_get_supported_languages() const {696PackedStringArray languages;697languages.push_back("GDScript");698return languages;699}700701void GDScriptSyntaxHighlighter::_update_cache() {702class_names.clear();703reserved_keywords.clear();704member_keywords.clear();705global_functions.clear();706color_regions.clear();707color_region_cache.clear();708709font_color = text_edit->get_theme_color(SceneStringName(font_color));710symbol_color = EDITOR_GET("text_editor/theme/highlighting/symbol_color");711function_color = EDITOR_GET("text_editor/theme/highlighting/function_color");712number_color = EDITOR_GET("text_editor/theme/highlighting/number_color");713member_variable_color = EDITOR_GET("text_editor/theme/highlighting/member_variable_color");714715/* Engine types. */716const Color types_color = EDITOR_GET("text_editor/theme/highlighting/engine_type_color");717List<StringName> types;718ClassDB::get_class_list(&types);719for (const StringName &E : types) {720if (ClassDB::is_class_exposed(E)) {721class_names[E] = types_color;722}723}724725/* Global enums. */726List<StringName> global_enums;727CoreConstants::get_global_enums(&global_enums);728for (const StringName &enum_name : global_enums) {729class_names[enum_name] = types_color;730}731732/* User types. */733const Color usertype_color = EDITOR_GET("text_editor/theme/highlighting/user_type_color");734List<StringName> global_classes;735ScriptServer::get_global_class_list(&global_classes);736for (const StringName &E : global_classes) {737class_names[E] = usertype_color;738}739740/* Autoloads. */741for (const KeyValue<StringName, ProjectSettings::AutoloadInfo> &E : ProjectSettings::get_singleton()->get_autoload_list()) {742const ProjectSettings::AutoloadInfo &info = E.value;743if (info.is_singleton) {744class_names[info.name] = usertype_color;745}746}747748const GDScriptLanguage *gdscript = GDScriptLanguage::get_singleton();749750/* Core types. */751const Color basetype_color = EDITOR_GET("text_editor/theme/highlighting/base_type_color");752List<String> core_types;753gdscript->get_core_type_words(&core_types);754for (const String &E : core_types) {755class_names[StringName(E)] = basetype_color;756}757class_names[SNAME("Variant")] = basetype_color;758class_names[SNAME("void")] = basetype_color;759// `get_core_type_words()` doesn't return primitive types.760class_names[SNAME("bool")] = basetype_color;761class_names[SNAME("int")] = basetype_color;762class_names[SNAME("float")] = basetype_color;763764/* Reserved words. */765const Color keyword_color = EDITOR_GET("text_editor/theme/highlighting/keyword_color");766const Color control_flow_keyword_color = EDITOR_GET("text_editor/theme/highlighting/control_flow_keyword_color");767for (const String &keyword : gdscript->get_reserved_words()) {768if (gdscript->is_control_flow_keyword(keyword)) {769reserved_keywords[StringName(keyword)] = control_flow_keyword_color;770} else {771reserved_keywords[StringName(keyword)] = keyword_color;772}773}774775// Highlight `set` and `get` as "keywords" with the function color to avoid conflicts with method calls.776reserved_keywords[SNAME("set")] = function_color;777reserved_keywords[SNAME("get")] = function_color;778779/* Global functions. */780List<StringName> global_function_list;781GDScriptUtilityFunctions::get_function_list(&global_function_list);782Variant::get_utility_function_list(&global_function_list);783// "assert" and "preload" are not utility functions, but are global nonetheless, so insert them.784global_functions.insert(SNAME("assert"));785global_functions.insert(SNAME("preload"));786for (const StringName &E : global_function_list) {787global_functions.insert(E);788}789790/* Comments. */791const Color comment_color = EDITOR_GET("text_editor/theme/highlighting/comment_color");792for (const String &comment : gdscript->get_comment_delimiters()) {793String beg = comment.get_slicec(' ', 0);794String end = comment.get_slice_count(" ") > 1 ? comment.get_slicec(' ', 1) : String();795add_color_region(ColorRegion::TYPE_COMMENT, beg, end, comment_color, end.is_empty());796}797798/* Doc comments */799const Color doc_comment_color = EDITOR_GET("text_editor/theme/highlighting/doc_comment_color");800for (const String &doc_comment : gdscript->get_doc_comment_delimiters()) {801String beg = doc_comment.get_slicec(' ', 0);802String end = doc_comment.get_slice_count(" ") > 1 ? doc_comment.get_slicec(' ', 1) : String();803add_color_region(ColorRegion::TYPE_COMMENT, beg, end, doc_comment_color, end.is_empty());804}805806/* Code regions */807const Color code_region_color = Color(EDITOR_GET("text_editor/theme/highlighting/folded_code_region_color").operator Color(), 1.0);808add_color_region(ColorRegion::TYPE_CODE_REGION, "#region", "", code_region_color, true);809add_color_region(ColorRegion::TYPE_CODE_REGION, "#endregion", "", code_region_color, true);810811/* Strings */812string_color = EDITOR_GET("text_editor/theme/highlighting/string_color");813add_color_region(ColorRegion::TYPE_STRING, "\"", "\"", string_color);814add_color_region(ColorRegion::TYPE_STRING, "'", "'", string_color);815add_color_region(ColorRegion::TYPE_MULTILINE_STRING, "\"\"\"", "\"\"\"", string_color);816add_color_region(ColorRegion::TYPE_MULTILINE_STRING, "'''", "'''", string_color);817add_color_region(ColorRegion::TYPE_STRING, "\"", "\"", string_color, false, true);818add_color_region(ColorRegion::TYPE_STRING, "'", "'", string_color, false, true);819add_color_region(ColorRegion::TYPE_MULTILINE_STRING, "\"\"\"", "\"\"\"", string_color, false, true);820add_color_region(ColorRegion::TYPE_MULTILINE_STRING, "'''", "'''", string_color, false, true);821822/* Members. */823Ref<Script> scr = _get_edited_resource();824if (scr.is_valid()) {825StringName instance_base = scr->get_instance_base_type();826if (instance_base != StringName()) {827List<PropertyInfo> property_list;828ClassDB::get_property_list(instance_base, &property_list);829for (const PropertyInfo &E : property_list) {830String prop_name = E.name;831if (E.usage & PROPERTY_USAGE_CATEGORY || E.usage & PROPERTY_USAGE_GROUP || E.usage & PROPERTY_USAGE_SUBGROUP) {832continue;833}834if (prop_name.contains_char('/')) {835continue;836}837member_keywords[prop_name] = member_variable_color;838}839840List<MethodInfo> signal_list;841ClassDB::get_signal_list(instance_base, &signal_list);842for (const MethodInfo &E : signal_list) {843member_keywords[E.name] = member_variable_color;844}845846// For callables.847List<MethodInfo> method_list;848ClassDB::get_method_list(instance_base, &method_list);849for (const MethodInfo &E : method_list) {850member_keywords[E.name] = member_variable_color;851}852853List<String> constant_list;854ClassDB::get_integer_constant_list(instance_base, &constant_list);855for (const String &E : constant_list) {856member_keywords[E] = member_variable_color;857}858859List<StringName> builtin_enums;860ClassDB::get_enum_list(instance_base, &builtin_enums);861for (const StringName &E : builtin_enums) {862member_keywords[E] = types_color;863}864}865866List<PropertyInfo> scr_property_list;867scr->get_script_property_list(&scr_property_list);868for (const PropertyInfo &E : scr_property_list) {869String prop_name = E.name;870if (E.usage & PROPERTY_USAGE_CATEGORY || E.usage & PROPERTY_USAGE_GROUP || E.usage & PROPERTY_USAGE_SUBGROUP) {871continue;872}873if (prop_name.contains_char('/')) {874continue;875}876member_keywords[prop_name] = member_variable_color;877}878879List<MethodInfo> scr_signal_list;880scr->get_script_signal_list(&scr_signal_list);881for (const MethodInfo &E : scr_signal_list) {882member_keywords[E.name] = member_variable_color;883}884885// For callables.886List<MethodInfo> scr_method_list;887scr->get_script_method_list(&scr_method_list);888for (const MethodInfo &E : scr_method_list) {889member_keywords[E.name] = member_variable_color;890}891892Ref<Script> scr_class = scr;893while (scr_class.is_valid()) {894HashMap<StringName, Variant> scr_constant_list;895scr_class->get_constants(&scr_constant_list);896for (const KeyValue<StringName, Variant> &E : scr_constant_list) {897member_keywords[E.key.operator String()] = member_variable_color;898}899scr_class = scr_class->get_base_script();900}901}902903function_definition_color = EDITOR_GET("text_editor/theme/highlighting/gdscript/function_definition_color");904global_function_color = EDITOR_GET("text_editor/theme/highlighting/gdscript/global_function_color");905node_path_color = EDITOR_GET("text_editor/theme/highlighting/gdscript/node_path_color");906node_ref_color = EDITOR_GET("text_editor/theme/highlighting/gdscript/node_reference_color");907annotation_color = EDITOR_GET("text_editor/theme/highlighting/gdscript/annotation_color");908string_name_color = EDITOR_GET("text_editor/theme/highlighting/gdscript/string_name_color");909type_color = EDITOR_GET("text_editor/theme/highlighting/base_type_color");910comment_marker_colors[COMMENT_MARKER_CRITICAL] = EDITOR_GET("text_editor/theme/highlighting/comment_markers/critical_color");911comment_marker_colors[COMMENT_MARKER_WARNING] = EDITOR_GET("text_editor/theme/highlighting/comment_markers/warning_color");912comment_marker_colors[COMMENT_MARKER_NOTICE] = EDITOR_GET("text_editor/theme/highlighting/comment_markers/notice_color");913914comment_markers.clear();915Vector<String> critical_list = EDITOR_GET("text_editor/theme/highlighting/comment_markers/critical_list").operator String().split(",", false);916for (int i = 0; i < critical_list.size(); i++) {917comment_markers[critical_list[i]] = COMMENT_MARKER_CRITICAL;918}919Vector<String> warning_list = EDITOR_GET("text_editor/theme/highlighting/comment_markers/warning_list").operator String().split(",", false);920for (int i = 0; i < warning_list.size(); i++) {921comment_markers[warning_list[i]] = COMMENT_MARKER_WARNING;922}923Vector<String> notice_list = EDITOR_GET("text_editor/theme/highlighting/comment_markers/notice_list").operator String().split(",", false);924for (int i = 0; i < notice_list.size(); i++) {925comment_markers[notice_list[i]] = COMMENT_MARKER_NOTICE;926}927}928929void GDScriptSyntaxHighlighter::add_color_region(ColorRegion::Type p_type, const String &p_start_key, const String &p_end_key, const Color &p_color, bool p_line_only, bool p_r_prefix) {930ERR_FAIL_COND_MSG(p_start_key.is_empty(), "Color region start key cannot be empty.");931ERR_FAIL_COND_MSG(!is_symbol(p_start_key[0]), "Color region start key must start with a symbol.");932933if (!p_end_key.is_empty()) {934ERR_FAIL_COND_MSG(!is_symbol(p_end_key[0]), "Color region end key must start with a symbol.");935}936937int at = 0;938for (const ColorRegion ®ion : color_regions) {939ERR_FAIL_COND_MSG(region.start_key == p_start_key && region.r_prefix == p_r_prefix, "Color region with start key '" + p_start_key + "' already exists.");940if (p_start_key.length() < region.start_key.length()) {941at++;942} else {943break;944}945}946947ColorRegion color_region;948color_region.type = p_type;949color_region.color = p_color;950color_region.start_key = p_start_key;951color_region.end_key = p_end_key;952color_region.line_only = p_line_only;953color_region.r_prefix = p_r_prefix;954color_region.is_string = p_type == ColorRegion::TYPE_STRING || p_type == ColorRegion::TYPE_MULTILINE_STRING;955color_region.is_comment = p_type == ColorRegion::TYPE_COMMENT || p_type == ColorRegion::TYPE_CODE_REGION;956color_regions.insert(at, color_region);957clear_highlighting_cache();958}959960Ref<EditorSyntaxHighlighter> GDScriptSyntaxHighlighter::_create() const {961Ref<GDScriptSyntaxHighlighter> syntax_highlighter;962syntax_highlighter.instantiate();963return syntax_highlighter;964}965966967