Path: blob/master/modules/gdscript/editor/gdscript_highlighter.cpp
11353 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] == '.') {439// Keyword, member, & global func indexing not allowed,440// but don't reset color if prev was a number like "5."441col = (prev_type == NUMBER) ? col : Color();442break;443} else if (str[k] > 32) {444break;445}446}447448if (!in_member_variable && col != Color()) {449in_keyword = true;450keyword_color = col;451}452}453}454455if (!in_function_name && in_word && !in_keyword) {456if (prev_text == GDScriptTokenizer::get_token_name(GDScriptTokenizer::Token::SIGNAL)) {457in_signal_declaration = true;458} else {459int k = j;460while (k < line_length && !is_symbol(str[k]) && !is_whitespace(str[k])) {461k++;462}463464// Check for space between name and bracket.465while (k < line_length && is_whitespace(str[k])) {466k++;467}468469if (str[k] == '(') {470in_function_name = true;471if (prev_text == GDScriptTokenizer::get_token_name(GDScriptTokenizer::Token::FUNC)) {472in_function_declaration = true;473}474} 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)) {475in_var_const_declaration = true;476}477478// Check for lambda.479if (in_function_declaration) {480k = j - 1;481while (k > 0 && is_whitespace(str[k])) {482k--;483}484485if (str[k] == ':') {486in_lambda = true;487}488}489}490}491492if (!in_function_name && !in_member_variable && !in_keyword && !in_number && in_word) {493int k = j;494while (k > 0 && !is_symbol(str[k]) && !is_whitespace(str[k])) {495k--;496}497498if (str[k] == '.' && (k < 1 || str[k - 1] != '.')) {499in_member_variable = true;500}501}502503if (is_a_symbol) {504if (in_function_declaration || in_signal_declaration) {505is_after_func_signal_declaration = true;506}507if (in_var_const_declaration) {508is_after_var_const_declaration = true;509}510511if (in_declaration_params > 0) {512switch (str[j]) {513case '(':514in_declaration_params += 1;515break;516case ')':517in_declaration_params -= 1;518break;519case '{':520in_declaration_param_dicts += 1;521break;522case '}':523in_declaration_param_dicts -= 1;524break;525}526} else if ((is_after_func_signal_declaration || prev_text == GDScriptTokenizer::get_token_name(GDScriptTokenizer::Token::FUNC)) && str[j] == '(') {527in_declaration_params = 1;528in_declaration_param_dicts = 0;529}530531if (expect_type) {532switch (str[j]) {533case '[':534in_type_params += 1;535break;536case ']':537in_type_params -= 1;538break;539case ',':540if (in_type_params <= 0) {541expect_type = false;542}543break;544case ' ':545case '\t':546case '.':547break;548default:549expect_type = false;550break;551}552} else {553if (j > 0 && str[j - 1] == '-' && str[j] == '>') {554expect_type = true;555in_type_params = 0;556}557if ((is_after_var_const_declaration || (in_declaration_params == 1 && in_declaration_param_dicts == 0)) && str[j] == ':') {558expect_type = true;559in_type_params = 0;560}561}562563in_function_name = false;564in_function_declaration = false;565in_signal_declaration = false;566in_var_const_declaration = false;567in_lambda = false;568in_member_variable = false;569570if (!is_whitespace(str[j])) {571is_after_func_signal_declaration = false;572is_after_var_const_declaration = false;573}574}575576// Set color of StringName, keeping symbol color for binary '&&' and '&'.577if (!in_string_name && in_region == -1 && str[j] == '&' && !is_binary_op) {578if (j + 1 <= line_length - 1 && (str[j + 1] == '\'' || str[j + 1] == '"')) {579in_string_name = true;580// Cover edge cases of i.e. '+&""' and '&&&""', so the StringName is properly colored.581if (prev_is_binary_op && j >= 2 && str[j - 1] == '&' && str[j - 2] != '&') {582in_string_name = false;583is_binary_op = true;584}585} else {586is_binary_op = true;587}588} else if (in_region != -1 || is_a_symbol) {589in_string_name = false;590}591592// '^^' has no special meaning, so unlike StringName, when binary, use NodePath color for the last caret.593if (!in_node_path && in_region == -1 && str[j] == '^' && !is_binary_op && (j == 0 || (j > 0 && str[j - 1] != '^') || prev_is_binary_op)) {594in_node_path = true;595} else if (in_region != -1 || is_a_symbol) {596in_node_path = false;597}598599if (!in_node_ref && in_region == -1 && (str[j] == '$' || (str[j] == '%' && !is_binary_op))) {600in_node_ref = true;601} 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] == '%'))) {602// NodeRefs can't start with digits, so point out wrong syntax immediately.603in_node_ref = false;604}605606if (!in_annotation && in_region == -1 && str[j] == '@') {607in_annotation = true;608} else if (in_region != -1 || is_a_symbol) {609in_annotation = false;610}611612const bool in_raw_string_prefix = in_region == -1 && str[j] == 'r' && j + 1 < line_length && (str[j + 1] == '"' || str[j + 1] == '\'');613614if (in_raw_string_prefix) {615color = string_color;616} else if (in_node_ref) {617next_type = NODE_REF;618color = node_ref_color;619} else if (in_annotation) {620next_type = ANNOTATION;621color = annotation_color;622} else if (in_string_name) {623next_type = STRING_NAME;624color = string_name_color;625} else if (in_node_path) {626next_type = NODE_PATH;627color = node_path_color;628} else if (in_keyword) {629next_type = KEYWORD;630color = keyword_color;631} else if (in_signal_declaration) {632next_type = SIGNAL;633color = member_variable_color;634} else if (in_function_name) {635next_type = FUNCTION;636if (!in_lambda && in_function_declaration) {637color = function_definition_color;638} else {639color = function_color;640}641} else if (in_number) {642next_type = NUMBER;643color = number_color;644} else if (is_a_symbol) {645next_type = SYMBOL;646color = symbol_color;647} else if (expect_type) {648next_type = TYPE;649color = type_color;650} else if (in_member_variable) {651next_type = MEMBER;652color = member_variable_color;653} else {654next_type = IDENTIFIER;655}656657if (next_type != current_type) {658if (current_type == NONE) {659current_type = next_type;660} else {661prev_type = current_type;662current_type = next_type;663664// No need to store regions...665if (prev_type == REGION) {666prev_text = "";667prev_column = j;668} else {669String text = str.substr(prev_column, j - prev_column).strip_edges();670prev_column = j;671672// Ignore if just whitespace.673if (!text.is_empty()) {674prev_text = text;675}676}677}678}679680prev_is_char = is_char;681prev_is_digit = is_a_digit;682prev_is_binary_op = is_binary_op;683684if (color != prev_color) {685prev_color = color;686highlighter_info["color"] = color;687color_map[j] = highlighter_info;688}689}690return color_map;691}692693String GDScriptSyntaxHighlighter::_get_name() const {694return "GDScript";695}696697PackedStringArray GDScriptSyntaxHighlighter::_get_supported_languages() const {698PackedStringArray languages;699languages.push_back("GDScript");700return languages;701}702703void GDScriptSyntaxHighlighter::_update_cache() {704class_names.clear();705reserved_keywords.clear();706member_keywords.clear();707global_functions.clear();708color_regions.clear();709color_region_cache.clear();710711font_color = text_edit->get_theme_color(SceneStringName(font_color));712symbol_color = EDITOR_GET("text_editor/theme/highlighting/symbol_color");713function_color = EDITOR_GET("text_editor/theme/highlighting/function_color");714number_color = EDITOR_GET("text_editor/theme/highlighting/number_color");715member_variable_color = EDITOR_GET("text_editor/theme/highlighting/member_variable_color");716717/* Engine types. */718const Color types_color = EDITOR_GET("text_editor/theme/highlighting/engine_type_color");719LocalVector<StringName> types;720ClassDB::get_class_list(types);721for (const StringName &type : types) {722if (ClassDB::is_class_exposed(type)) {723class_names[type] = types_color;724}725}726727/* Global enums. */728List<StringName> global_enums;729CoreConstants::get_global_enums(&global_enums);730for (const StringName &enum_name : global_enums) {731class_names[enum_name] = types_color;732}733734/* User types. */735const Color usertype_color = EDITOR_GET("text_editor/theme/highlighting/user_type_color");736LocalVector<StringName> global_classes;737ScriptServer::get_global_class_list(global_classes);738for (const StringName &class_name : global_classes) {739class_names[class_name] = usertype_color;740}741742/* Autoloads. */743for (const KeyValue<StringName, ProjectSettings::AutoloadInfo> &E : ProjectSettings::get_singleton()->get_autoload_list()) {744const ProjectSettings::AutoloadInfo &info = E.value;745if (info.is_singleton) {746class_names[info.name] = usertype_color;747}748}749750const GDScriptLanguage *gdscript = GDScriptLanguage::get_singleton();751752/* Core types. */753const Color basetype_color = EDITOR_GET("text_editor/theme/highlighting/base_type_color");754List<String> core_types;755gdscript->get_core_type_words(&core_types);756for (const String &E : core_types) {757class_names[StringName(E)] = basetype_color;758}759class_names[SNAME("Variant")] = basetype_color;760class_names[SNAME("void")] = basetype_color;761// `get_core_type_words()` doesn't return primitive types.762class_names[SNAME("bool")] = basetype_color;763class_names[SNAME("int")] = basetype_color;764class_names[SNAME("float")] = basetype_color;765766/* Reserved words. */767const Color keyword_color = EDITOR_GET("text_editor/theme/highlighting/keyword_color");768const Color control_flow_keyword_color = EDITOR_GET("text_editor/theme/highlighting/control_flow_keyword_color");769for (const String &keyword : gdscript->get_reserved_words()) {770if (gdscript->is_control_flow_keyword(keyword)) {771reserved_keywords[StringName(keyword)] = control_flow_keyword_color;772} else {773reserved_keywords[StringName(keyword)] = keyword_color;774}775}776777// Highlight `set` and `get` as "keywords" with the function color to avoid conflicts with method calls.778reserved_keywords[SNAME("set")] = function_color;779reserved_keywords[SNAME("get")] = function_color;780781/* Global functions. */782List<StringName> global_function_list;783GDScriptUtilityFunctions::get_function_list(&global_function_list);784Variant::get_utility_function_list(&global_function_list);785// "assert" and "preload" are not utility functions, but are global nonetheless, so insert them.786global_functions.insert(SNAME("assert"));787global_functions.insert(SNAME("preload"));788for (const StringName &E : global_function_list) {789global_functions.insert(E);790}791792/* Comments. */793const Color comment_color = EDITOR_GET("text_editor/theme/highlighting/comment_color");794for (const String &comment : gdscript->get_comment_delimiters()) {795String beg = comment.get_slicec(' ', 0);796String end = comment.get_slice_count(" ") > 1 ? comment.get_slicec(' ', 1) : String();797add_color_region(ColorRegion::TYPE_COMMENT, beg, end, comment_color, end.is_empty());798}799800/* Doc comments */801const Color doc_comment_color = EDITOR_GET("text_editor/theme/highlighting/doc_comment_color");802for (const String &doc_comment : gdscript->get_doc_comment_delimiters()) {803String beg = doc_comment.get_slicec(' ', 0);804String end = doc_comment.get_slice_count(" ") > 1 ? doc_comment.get_slicec(' ', 1) : String();805add_color_region(ColorRegion::TYPE_COMMENT, beg, end, doc_comment_color, end.is_empty());806}807808/* Code regions */809const Color code_region_color = Color(EDITOR_GET("text_editor/theme/highlighting/folded_code_region_color").operator Color(), 1.0);810add_color_region(ColorRegion::TYPE_CODE_REGION, "#region", "", code_region_color, true);811add_color_region(ColorRegion::TYPE_CODE_REGION, "#endregion", "", code_region_color, true);812813/* Strings */814string_color = EDITOR_GET("text_editor/theme/highlighting/string_color");815add_color_region(ColorRegion::TYPE_STRING, "\"", "\"", string_color);816add_color_region(ColorRegion::TYPE_STRING, "'", "'", string_color);817add_color_region(ColorRegion::TYPE_MULTILINE_STRING, "\"\"\"", "\"\"\"", string_color);818add_color_region(ColorRegion::TYPE_MULTILINE_STRING, "'''", "'''", string_color);819add_color_region(ColorRegion::TYPE_STRING, "\"", "\"", string_color, false, true);820add_color_region(ColorRegion::TYPE_STRING, "'", "'", string_color, false, true);821add_color_region(ColorRegion::TYPE_MULTILINE_STRING, "\"\"\"", "\"\"\"", string_color, false, true);822add_color_region(ColorRegion::TYPE_MULTILINE_STRING, "'''", "'''", string_color, false, true);823824/* Members. */825Ref<Script> scr = _get_edited_resource();826if (scr.is_valid()) {827StringName instance_base = scr->get_instance_base_type();828if (instance_base != StringName()) {829List<PropertyInfo> property_list;830ClassDB::get_property_list(instance_base, &property_list);831for (const PropertyInfo &E : property_list) {832String prop_name = E.name;833if (E.usage & PROPERTY_USAGE_CATEGORY || E.usage & PROPERTY_USAGE_GROUP || E.usage & PROPERTY_USAGE_SUBGROUP) {834continue;835}836if (prop_name.contains_char('/')) {837continue;838}839member_keywords[prop_name] = member_variable_color;840}841842List<MethodInfo> signal_list;843ClassDB::get_signal_list(instance_base, &signal_list);844for (const MethodInfo &E : signal_list) {845member_keywords[E.name] = member_variable_color;846}847848// For callables.849List<MethodInfo> method_list;850ClassDB::get_method_list(instance_base, &method_list);851for (const MethodInfo &E : method_list) {852member_keywords[E.name] = member_variable_color;853}854855List<String> constant_list;856ClassDB::get_integer_constant_list(instance_base, &constant_list);857for (const String &E : constant_list) {858member_keywords[E] = member_variable_color;859}860861List<StringName> builtin_enums;862ClassDB::get_enum_list(instance_base, &builtin_enums);863for (const StringName &E : builtin_enums) {864member_keywords[E] = types_color;865}866}867868List<PropertyInfo> scr_property_list;869scr->get_script_property_list(&scr_property_list);870for (const PropertyInfo &E : scr_property_list) {871String prop_name = E.name;872if (E.usage & PROPERTY_USAGE_CATEGORY || E.usage & PROPERTY_USAGE_GROUP || E.usage & PROPERTY_USAGE_SUBGROUP) {873continue;874}875if (prop_name.contains_char('/')) {876continue;877}878member_keywords[prop_name] = member_variable_color;879}880881List<MethodInfo> scr_signal_list;882scr->get_script_signal_list(&scr_signal_list);883for (const MethodInfo &E : scr_signal_list) {884member_keywords[E.name] = member_variable_color;885}886887// For callables.888List<MethodInfo> scr_method_list;889scr->get_script_method_list(&scr_method_list);890for (const MethodInfo &E : scr_method_list) {891member_keywords[E.name] = member_variable_color;892}893894Ref<Script> scr_class = scr;895while (scr_class.is_valid()) {896HashMap<StringName, Variant> scr_constant_list;897scr_class->get_constants(&scr_constant_list);898for (const KeyValue<StringName, Variant> &E : scr_constant_list) {899member_keywords[E.key.operator String()] = member_variable_color;900}901scr_class = scr_class->get_base_script();902}903}904905function_definition_color = EDITOR_GET("text_editor/theme/highlighting/gdscript/function_definition_color");906global_function_color = EDITOR_GET("text_editor/theme/highlighting/gdscript/global_function_color");907node_path_color = EDITOR_GET("text_editor/theme/highlighting/gdscript/node_path_color");908node_ref_color = EDITOR_GET("text_editor/theme/highlighting/gdscript/node_reference_color");909annotation_color = EDITOR_GET("text_editor/theme/highlighting/gdscript/annotation_color");910string_name_color = EDITOR_GET("text_editor/theme/highlighting/gdscript/string_name_color");911type_color = EDITOR_GET("text_editor/theme/highlighting/base_type_color");912comment_marker_colors[COMMENT_MARKER_CRITICAL] = EDITOR_GET("text_editor/theme/highlighting/comment_markers/critical_color");913comment_marker_colors[COMMENT_MARKER_WARNING] = EDITOR_GET("text_editor/theme/highlighting/comment_markers/warning_color");914comment_marker_colors[COMMENT_MARKER_NOTICE] = EDITOR_GET("text_editor/theme/highlighting/comment_markers/notice_color");915916comment_markers.clear();917Vector<String> critical_list = EDITOR_GET("text_editor/theme/highlighting/comment_markers/critical_list").operator String().split(",", false);918for (int i = 0; i < critical_list.size(); i++) {919comment_markers[critical_list[i]] = COMMENT_MARKER_CRITICAL;920}921Vector<String> warning_list = EDITOR_GET("text_editor/theme/highlighting/comment_markers/warning_list").operator String().split(",", false);922for (int i = 0; i < warning_list.size(); i++) {923comment_markers[warning_list[i]] = COMMENT_MARKER_WARNING;924}925Vector<String> notice_list = EDITOR_GET("text_editor/theme/highlighting/comment_markers/notice_list").operator String().split(",", false);926for (int i = 0; i < notice_list.size(); i++) {927comment_markers[notice_list[i]] = COMMENT_MARKER_NOTICE;928}929}930931void 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) {932ERR_FAIL_COND_MSG(p_start_key.is_empty(), "Color region start key cannot be empty.");933ERR_FAIL_COND_MSG(!is_symbol(p_start_key[0]), "Color region start key must start with a symbol.");934935if (!p_end_key.is_empty()) {936ERR_FAIL_COND_MSG(!is_symbol(p_end_key[0]), "Color region end key must start with a symbol.");937}938939int at = 0;940for (const ColorRegion ®ion : color_regions) {941ERR_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.");942if (p_start_key.length() < region.start_key.length()) {943at++;944} else {945break;946}947}948949ColorRegion color_region;950color_region.type = p_type;951color_region.color = p_color;952color_region.start_key = p_start_key;953color_region.end_key = p_end_key;954color_region.line_only = p_line_only;955color_region.r_prefix = p_r_prefix;956color_region.is_string = p_type == ColorRegion::TYPE_STRING || p_type == ColorRegion::TYPE_MULTILINE_STRING;957color_region.is_comment = p_type == ColorRegion::TYPE_COMMENT || p_type == ColorRegion::TYPE_CODE_REGION;958color_regions.insert(at, color_region);959clear_highlighting_cache();960}961962Ref<EditorSyntaxHighlighter> GDScriptSyntaxHighlighter::_create() const {963Ref<GDScriptSyntaxHighlighter> syntax_highlighter;964syntax_highlighter.instantiate();965return syntax_highlighter;966}967968969