Path: blob/master/modules/gdscript/editor/gdscript_highlighter.cpp
20843 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();246int placeholder_end = from;247for (; from < line_length; from++) {248if (line_length - from < end_key_length) {249// Don't break if '\' to highlight escape sequences,250// and '%' and '{' to highlight placeholders.251if (str.find_char('\\', from) == -1 && str.find_char('%', from) == -1 && str.find_char('{', from) == -1) {252break;253}254}255256if (!is_symbol(str[from]) && str[from] != '%') {257continue;258}259260if (str[from] == '%' || str[from] == '{') {261int placeholder_start = from;262bool is_percent = str[from] == '%';263264from++;265266if (is_percent) {267if (str[from] == '%') {268placeholder_end = from + 1;269} else {270const String allowed_chars = "+.-*0123456789";271const String placeholder_types = "cdfosvxX";272for (int i = 0; i < line_length - from; i++) {273if (allowed_chars.contains_char(str[from + i]) &&274!placeholder_types.contains_char(str[from + i]) &&275(str[from + i] != end_key[0] || (str[from + i] == end_key[0] && str[from + i - 1] == '\\'))) {276continue;277}278if (placeholder_types.contains_char(str[from + i])) {279placeholder_end = from + i + 1;280}281break;282}283}284} else {285for (int i = 0; i < line_length - from; i++) {286if (str[from + i] != '}' && (str[from + i] != end_key[0] || (str[from + i] == end_key[0] && str[from + i - 1] == '\\'))) {287continue;288}289if (str[from + i] == '}') {290placeholder_end = from + i + 1;291}292break;293}294}295296if (placeholder_end > placeholder_start) {297Dictionary placeholder_highlighter_info;298placeholder_highlighter_info["color"] = placeholder_color;299color_map[placeholder_start] = placeholder_highlighter_info;300Dictionary region_continue_highlighter_info;301region_continue_highlighter_info["color"] = region_color;302color_map[placeholder_end] = region_continue_highlighter_info;303}304}305306if (str[from] == '\\') {307if (!color_regions[in_region].r_prefix) {308Dictionary escape_char_highlighter_info;309escape_char_highlighter_info["color"] = symbol_color;310color_map[from] = escape_char_highlighter_info;311}312313from++;314315if (!color_regions[in_region].r_prefix) {316int esc_len = 0;317if (str[from] == 'u') {318esc_len = 4;319} else if (str[from] == 'U') {320esc_len = 6;321}322for (int k = 0; k < esc_len && from < line_length - 1; k++) {323if (!is_hex_digit(str[from + 1])) {324break;325}326from++;327}328329Dictionary region_continue_highlighter_info;330region_continue_highlighter_info["color"] = from < placeholder_end ? placeholder_color : region_color;331color_map[from + 1] = region_continue_highlighter_info;332}333334continue;335}336337region_end_index = from;338for (int k = 0; k < end_key_length; k++) {339if (end_key[k] != str[from + k]) {340region_end_index = -1;341break;342}343}344345if (region_end_index != -1) {346break;347}348}349j = from + (end_key_length - 1);350if (region_end_index == -1) {351color_region_cache[p_line] = in_region;352}353}354355prev_type = REGION;356prev_text = "";357prev_column = j;358359in_region = -1;360prev_is_char = false;361prev_is_digit = false;362prev_is_binary_op = false;363continue;364}365color_map.sort(); // Prevents e.g. escape sequences from being overridden by string placeholders.366}367}368369// VERY hacky... but couldn't come up with anything better.370if (j > 0 && (str[j] == '&' || str[j] == '^' || str[j] == '%' || str[j] == '+' || str[j] == '-' || str[j] == '~' || str[j] == '.')) {371int to = j - 1;372// Find what the last text was (prev_text won't work if there's no whitespace, so we need to do it manually).373while (to > 0 && is_whitespace(str[to])) {374to--;375}376int from = to;377while (from > 0 && !is_symbol(str[from])) {378from--;379}380String word = str.substr(from + 1, to - from);381// Keywords need to be exceptions, except for keywords that represent a value.382if (word == "true" || word == "false" || word == "null" || word == "PI" || word == "TAU" || word == "INF" || word == "NAN" || word == "self" || word == "super" || !reserved_keywords.has(word)) {383if (!is_symbol(str[to]) || str[to] == '"' || str[to] == '\'' || str[to] == ')' || str[to] == ']' || str[to] == '}') {384is_binary_op = true;385}386}387}388389if (!is_char) {390in_keyword = false;391}392393// Allow ABCDEF in hex notation.394if (is_hex_notation && (is_hex_digit(str[j]) || is_a_digit)) {395is_a_digit = true;396} else if (str[j] != '_') {397is_hex_notation = false;398}399400// Disallow anything not a 0 or 1 in binary notation.401if (is_bin_notation && !is_binary_digit(str[j])) {402is_a_digit = false;403is_bin_notation = false;404}405406if (!in_number && !in_word && is_a_digit) {407in_number = true;408}409410// Special cases for numbers.411if (in_number && !is_a_digit) {412if ((str[j] == 'b' || str[j] == 'B') && str[j - 1] == '0') {413is_bin_notation = true;414} else if ((str[j] == 'x' || str[j] == 'X') && str[j - 1] == '0') {415is_hex_notation = true;416} else if (!((str[j] == '-' || str[j] == '+') && (str[j - 1] == 'e' || str[j - 1] == 'E') && !prev_is_digit) &&417!(str[j] == '_' && (prev_is_digit || str[j - 1] == 'b' || str[j - 1] == 'B' || str[j - 1] == 'x' || str[j - 1] == 'X' || str[j - 1] == '.')) &&418!((str[j] == 'e' || str[j] == 'E') && (prev_is_digit || str[j - 1] == '_')) &&419!(str[j] == '.' && (prev_is_digit || (!prev_is_binary_op && (j > 0 && (str[j - 1] == '_' || str[j - 1] == '-' || str[j - 1] == '+' || str[j - 1] == '~'))))) &&420!((str[j] == '-' || str[j] == '+' || str[j] == '~') && !is_binary_op && !prev_is_binary_op && str[j - 1] != 'e' && str[j - 1] != 'E')) {421/* This condition continues number highlighting in special cases.4221st row: '+' or '-' after scientific notation (like 3e-4);4232nd row: '_' as a numeric separator;4243rd row: Scientific notation 'e' and floating points;4254th row: Floating points inside the number, or leading if after a unary mathematical operator;4265th row: Multiple unary mathematical operators (like ~-7) */427in_number = false;428}429} else if (str[j] == '.' && !is_binary_op && is_digit(str[j + 1]) && (j == 0 || (j > 0 && str[j - 1] != '.'))) {430// Start number highlighting from leading decimal points (like .42)431in_number = true;432} else if ((str[j] == '-' || str[j] == '+' || str[j] == '~') && !is_binary_op) {433// Only start number highlighting on unary operators if a digit follows them.434int non_op = j + 1;435while (str[non_op] == '-' || str[non_op] == '+' || str[non_op] == '~') {436non_op++;437}438if (is_digit(str[non_op]) || (str[non_op] == '.' && non_op < line_length && is_digit(str[non_op + 1]))) {439in_number = true;440}441}442443if (!in_word && is_unicode_identifier_start(str[j]) && !in_number) {444in_word = true;445}446447if (is_a_symbol && str[j] != '.' && in_word) {448in_word = false;449}450451if (!in_keyword && is_char && !prev_is_char) {452int to = j;453while (to < line_length && !is_symbol(str[to])) {454to++;455}456457String word = str.substr(j, to - j);458Color col;459if (global_functions.has(word)) {460// "assert" and "preload" are reserved, so highlight even if not followed by a bracket.461if (word == GDScriptTokenizer::get_token_name(GDScriptTokenizer::Token::ASSERT) || word == GDScriptTokenizer::get_token_name(GDScriptTokenizer::Token::PRELOAD)) {462col = global_function_color;463} else {464// For other global functions, check if followed by bracket.465int k = to;466while (k < line_length && is_whitespace(str[k])) {467k++;468}469470if (str[k] == '(') {471col = global_function_color;472}473}474} else if (class_names.has(word)) {475col = class_names[word];476} else if (reserved_keywords.has(word)) {477col = reserved_keywords[word];478// Don't highlight `list` as a type in `for elem: Type in list`.479expect_type = false;480} else if (member_keywords.has(word)) {481col = member_keywords[word];482in_member_variable = true;483}484485if (col != Color()) {486for (int k = j - 1; k >= 0; k--) {487if (str[k] == '.') {488// Keyword, member, & global func indexing not allowed,489// but don't reset color if prev was a number like "5."490col = (prev_type == NUMBER) ? col : Color();491break;492} else if (str[k] > 32) {493break;494}495}496497if (!in_member_variable && col != Color()) {498in_keyword = true;499keyword_color = col;500}501}502}503504if (!in_function_name && in_word && !in_keyword) {505if (prev_text == GDScriptTokenizer::get_token_name(GDScriptTokenizer::Token::SIGNAL)) {506in_signal_declaration = true;507} else {508int k = j;509while (k < line_length && !is_symbol(str[k]) && !is_whitespace(str[k])) {510k++;511}512513// Check for space between name and bracket.514while (k < line_length && is_whitespace(str[k])) {515k++;516}517518if (str[k] == '(') {519in_function_name = true;520if (prev_text == GDScriptTokenizer::get_token_name(GDScriptTokenizer::Token::FUNC)) {521in_function_declaration = true;522}523} 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)) {524in_var_const_declaration = true;525}526527// Check for lambda.528if (in_function_declaration) {529k = j - 1;530while (k > 0 && is_whitespace(str[k])) {531k--;532}533534if (str[k] == ':') {535in_lambda = true;536}537}538}539}540541if (!in_function_name && !in_member_variable && !in_keyword && !in_number && in_word) {542int k = j;543while (k > 0 && !is_symbol(str[k]) && !is_whitespace(str[k])) {544k--;545}546547if (str[k] == '.' && (k < 1 || str[k - 1] != '.')) {548in_member_variable = true;549}550}551552if (is_a_symbol) {553if (in_function_declaration || in_signal_declaration) {554is_after_func_signal_declaration = true;555}556if (in_var_const_declaration) {557is_after_var_const_declaration = true;558}559560if (in_declaration_params > 0) {561switch (str[j]) {562case '(':563in_declaration_params += 1;564break;565case ')':566in_declaration_params -= 1;567break;568case '{':569in_declaration_param_dicts += 1;570break;571case '}':572in_declaration_param_dicts -= 1;573break;574}575} else if ((is_after_func_signal_declaration || prev_text == GDScriptTokenizer::get_token_name(GDScriptTokenizer::Token::FUNC)) && str[j] == '(') {576in_declaration_params = 1;577in_declaration_param_dicts = 0;578}579580if (expect_type) {581switch (str[j]) {582case '[':583in_type_params += 1;584break;585case ']':586in_type_params -= 1;587break;588case ',':589if (in_type_params <= 0) {590expect_type = false;591}592break;593case ' ':594case '\t':595case '.':596break;597default:598expect_type = false;599break;600}601} else {602if (j > 0 && str[j - 1] == '-' && str[j] == '>') {603expect_type = true;604in_type_params = 0;605}606if ((is_after_var_const_declaration || (in_declaration_params == 1 && in_declaration_param_dicts == 0)) && str[j] == ':') {607expect_type = true;608in_type_params = 0;609}610}611612in_function_name = false;613in_function_declaration = false;614in_signal_declaration = false;615in_var_const_declaration = false;616in_lambda = false;617in_member_variable = false;618619if (!is_whitespace(str[j])) {620is_after_func_signal_declaration = false;621is_after_var_const_declaration = false;622}623}624625// Set color of StringName, keeping symbol color for binary '&&' and '&'.626if (!in_string_name && in_region == -1 && str[j] == '&' && !is_binary_op) {627if (j + 1 <= line_length - 1 && (str[j + 1] == '\'' || str[j + 1] == '"')) {628in_string_name = true;629// Cover edge cases of i.e. '+&""' and '&&&""', so the StringName is properly colored.630if (prev_is_binary_op && j >= 2 && str[j - 1] == '&' && str[j - 2] != '&') {631in_string_name = false;632is_binary_op = true;633}634} else {635is_binary_op = true;636}637} else if (in_region != -1 || is_a_symbol) {638in_string_name = false;639}640641// '^^' has no special meaning, so unlike StringName, when binary, use NodePath color for the last caret.642if (!in_node_path && in_region == -1 && str[j] == '^' && !is_binary_op && (j == 0 || (j > 0 && str[j - 1] != '^') || prev_is_binary_op)) {643in_node_path = true;644} else if (in_region != -1 || is_a_symbol) {645in_node_path = false;646}647648if (!in_node_ref && in_region == -1 && (str[j] == '$' || (str[j] == '%' && !is_binary_op))) {649in_node_ref = true;650} 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] == '%'))) {651// NodeRefs can't start with digits, so point out wrong syntax immediately.652in_node_ref = false;653}654655if (!in_annotation && in_region == -1 && str[j] == '@') {656in_annotation = true;657} else if (in_region != -1 || is_a_symbol) {658in_annotation = false;659}660661const bool in_raw_string_prefix = in_region == -1 && str[j] == 'r' && j + 1 < line_length && (str[j + 1] == '"' || str[j + 1] == '\'');662663if (in_raw_string_prefix) {664color = string_color;665} else if (in_node_ref) {666next_type = NODE_REF;667color = node_ref_color;668} else if (in_annotation) {669next_type = ANNOTATION;670color = annotation_color;671} else if (in_string_name) {672next_type = STRING_NAME;673color = string_name_color;674} else if (in_node_path) {675next_type = NODE_PATH;676color = node_path_color;677} else if (in_keyword) {678next_type = KEYWORD;679color = keyword_color;680} else if (in_signal_declaration) {681next_type = SIGNAL;682color = member_variable_color;683} else if (in_function_name) {684next_type = FUNCTION;685if (!in_lambda && in_function_declaration) {686color = function_definition_color;687} else {688color = function_color;689}690} else if (in_number) {691next_type = NUMBER;692color = number_color;693} else if (is_a_symbol) {694next_type = SYMBOL;695color = symbol_color;696} else if (expect_type) {697next_type = TYPE;698color = type_color;699} else if (in_member_variable) {700next_type = MEMBER;701color = member_variable_color;702} else {703next_type = IDENTIFIER;704}705706if (next_type != current_type) {707if (current_type == NONE) {708current_type = next_type;709} else {710prev_type = current_type;711current_type = next_type;712713// No need to store regions...714if (prev_type == REGION) {715prev_text = "";716prev_column = j;717} else {718String text = str.substr(prev_column, j - prev_column).strip_edges();719prev_column = j;720721// Ignore if just whitespace.722if (!text.is_empty()) {723prev_text = text;724}725}726}727}728729prev_is_char = is_char;730prev_is_digit = is_a_digit;731prev_is_binary_op = is_binary_op;732733if (color != prev_color) {734prev_color = color;735highlighter_info["color"] = color;736color_map[j] = highlighter_info;737}738}739return color_map;740}741742String GDScriptSyntaxHighlighter::_get_name() const {743return "GDScript";744}745746PackedStringArray GDScriptSyntaxHighlighter::_get_supported_languages() const {747PackedStringArray languages;748languages.push_back("GDScript");749return languages;750}751752void GDScriptSyntaxHighlighter::_update_cache() {753class_names.clear();754reserved_keywords.clear();755member_keywords.clear();756global_functions.clear();757color_regions.clear();758color_region_cache.clear();759760font_color = text_edit->get_theme_color(SceneStringName(font_color));761symbol_color = EDITOR_GET("text_editor/theme/highlighting/symbol_color");762function_color = EDITOR_GET("text_editor/theme/highlighting/function_color");763number_color = EDITOR_GET("text_editor/theme/highlighting/number_color");764member_variable_color = EDITOR_GET("text_editor/theme/highlighting/member_variable_color");765766/* Engine types. */767const Color types_color = EDITOR_GET("text_editor/theme/highlighting/engine_type_color");768LocalVector<StringName> types;769ClassDB::get_class_list(types);770for (const StringName &type : types) {771if (ClassDB::is_class_exposed(type)) {772class_names[type] = types_color;773}774}775776/* Global enums. */777List<StringName> global_enums;778CoreConstants::get_global_enums(&global_enums);779for (const StringName &enum_name : global_enums) {780class_names[enum_name] = types_color;781}782783/* User types. */784const Color usertype_color = EDITOR_GET("text_editor/theme/highlighting/user_type_color");785LocalVector<StringName> global_classes;786ScriptServer::get_global_class_list(global_classes);787for (const StringName &class_name : global_classes) {788class_names[class_name] = usertype_color;789}790791/* Autoloads. */792for (const KeyValue<StringName, ProjectSettings::AutoloadInfo> &E : ProjectSettings::get_singleton()->get_autoload_list()) {793const ProjectSettings::AutoloadInfo &info = E.value;794if (info.is_singleton) {795class_names[info.name] = usertype_color;796}797}798799const GDScriptLanguage *gdscript = GDScriptLanguage::get_singleton();800801/* Core types. */802const Color basetype_color = EDITOR_GET("text_editor/theme/highlighting/base_type_color");803List<String> core_types;804gdscript->get_core_type_words(&core_types);805for (const String &E : core_types) {806class_names[StringName(E)] = basetype_color;807}808class_names[SNAME("Variant")] = basetype_color;809class_names[SNAME("void")] = basetype_color;810// `get_core_type_words()` doesn't return primitive types.811class_names[SNAME("bool")] = basetype_color;812class_names[SNAME("int")] = basetype_color;813class_names[SNAME("float")] = basetype_color;814815/* Reserved words. */816const Color keyword_color = EDITOR_GET("text_editor/theme/highlighting/keyword_color");817const Color control_flow_keyword_color = EDITOR_GET("text_editor/theme/highlighting/control_flow_keyword_color");818for (const String &keyword : gdscript->get_reserved_words()) {819if (gdscript->is_control_flow_keyword(keyword)) {820reserved_keywords[StringName(keyword)] = control_flow_keyword_color;821} else {822reserved_keywords[StringName(keyword)] = keyword_color;823}824}825826// Highlight `set` and `get` as "keywords" with the function color to avoid conflicts with method calls.827reserved_keywords[SNAME("set")] = function_color;828reserved_keywords[SNAME("get")] = function_color;829830/* Global functions. */831List<StringName> global_function_list;832GDScriptUtilityFunctions::get_function_list(&global_function_list);833Variant::get_utility_function_list(&global_function_list);834// "assert" and "preload" are not utility functions, but are global nonetheless, so insert them.835global_functions.insert(SNAME("assert"));836global_functions.insert(SNAME("preload"));837for (const StringName &E : global_function_list) {838global_functions.insert(E);839}840841/* Comments. */842const Color comment_color = EDITOR_GET("text_editor/theme/highlighting/comment_color");843for (const String &comment : gdscript->get_comment_delimiters()) {844String beg = comment.get_slicec(' ', 0);845String end = comment.get_slice_count(" ") > 1 ? comment.get_slicec(' ', 1) : String();846add_color_region(ColorRegion::TYPE_COMMENT, beg, end, comment_color, end.is_empty());847}848849/* Doc comments */850const Color doc_comment_color = EDITOR_GET("text_editor/theme/highlighting/doc_comment_color");851for (const String &doc_comment : gdscript->get_doc_comment_delimiters()) {852String beg = doc_comment.get_slicec(' ', 0);853String end = doc_comment.get_slice_count(" ") > 1 ? doc_comment.get_slicec(' ', 1) : String();854add_color_region(ColorRegion::TYPE_COMMENT, beg, end, doc_comment_color, end.is_empty());855}856857/* Code regions */858const Color code_region_color = Color(EDITOR_GET("text_editor/theme/highlighting/folded_code_region_color").operator Color(), 1.0);859add_color_region(ColorRegion::TYPE_CODE_REGION, "#region", "", code_region_color, true);860add_color_region(ColorRegion::TYPE_CODE_REGION, "#endregion", "", code_region_color, true);861862/* Strings */863string_color = EDITOR_GET("text_editor/theme/highlighting/string_color");864placeholder_color = EDITOR_GET("text_editor/theme/highlighting/string_placeholder_color");865add_color_region(ColorRegion::TYPE_STRING, "\"", "\"", string_color);866add_color_region(ColorRegion::TYPE_STRING, "'", "'", string_color);867add_color_region(ColorRegion::TYPE_MULTILINE_STRING, "\"\"\"", "\"\"\"", string_color);868add_color_region(ColorRegion::TYPE_MULTILINE_STRING, "'''", "'''", string_color);869add_color_region(ColorRegion::TYPE_STRING, "\"", "\"", string_color, false, true);870add_color_region(ColorRegion::TYPE_STRING, "'", "'", string_color, false, true);871add_color_region(ColorRegion::TYPE_MULTILINE_STRING, "\"\"\"", "\"\"\"", string_color, false, true);872add_color_region(ColorRegion::TYPE_MULTILINE_STRING, "'''", "'''", string_color, false, true);873874/* Members. */875Ref<Script> scr = _get_edited_resource();876if (scr.is_valid()) {877StringName instance_base = scr->get_instance_base_type();878if (instance_base != StringName()) {879List<PropertyInfo> property_list;880ClassDB::get_property_list(instance_base, &property_list);881for (const PropertyInfo &E : property_list) {882String prop_name = E.name;883if (E.usage & PROPERTY_USAGE_CATEGORY || E.usage & PROPERTY_USAGE_GROUP || E.usage & PROPERTY_USAGE_SUBGROUP) {884continue;885}886if (prop_name.contains_char('/')) {887continue;888}889member_keywords[prop_name] = member_variable_color;890}891892List<MethodInfo> signal_list;893ClassDB::get_signal_list(instance_base, &signal_list);894for (const MethodInfo &E : signal_list) {895member_keywords[E.name] = member_variable_color;896}897898// For callables.899List<MethodInfo> method_list;900ClassDB::get_method_list(instance_base, &method_list);901for (const MethodInfo &E : method_list) {902member_keywords[E.name] = member_variable_color;903}904905List<String> constant_list;906ClassDB::get_integer_constant_list(instance_base, &constant_list);907for (const String &E : constant_list) {908member_keywords[E] = member_variable_color;909}910911List<StringName> builtin_enums;912ClassDB::get_enum_list(instance_base, &builtin_enums);913for (const StringName &E : builtin_enums) {914member_keywords[E] = types_color;915}916}917918List<PropertyInfo> scr_property_list;919scr->get_script_property_list(&scr_property_list);920for (const PropertyInfo &E : scr_property_list) {921String prop_name = E.name;922if (E.usage & PROPERTY_USAGE_CATEGORY || E.usage & PROPERTY_USAGE_GROUP || E.usage & PROPERTY_USAGE_SUBGROUP) {923continue;924}925if (prop_name.contains_char('/')) {926continue;927}928member_keywords[prop_name] = member_variable_color;929}930931List<MethodInfo> scr_signal_list;932scr->get_script_signal_list(&scr_signal_list);933for (const MethodInfo &E : scr_signal_list) {934member_keywords[E.name] = member_variable_color;935}936937// For callables.938List<MethodInfo> scr_method_list;939scr->get_script_method_list(&scr_method_list);940for (const MethodInfo &E : scr_method_list) {941member_keywords[E.name] = member_variable_color;942}943944Ref<Script> scr_class = scr;945while (scr_class.is_valid()) {946HashMap<StringName, Variant> scr_constant_list;947scr_class->get_constants(&scr_constant_list);948for (const KeyValue<StringName, Variant> &E : scr_constant_list) {949member_keywords[E.key.operator String()] = member_variable_color;950}951scr_class = scr_class->get_base_script();952}953}954955function_definition_color = EDITOR_GET("text_editor/theme/highlighting/gdscript/function_definition_color");956global_function_color = EDITOR_GET("text_editor/theme/highlighting/gdscript/global_function_color");957node_path_color = EDITOR_GET("text_editor/theme/highlighting/gdscript/node_path_color");958node_ref_color = EDITOR_GET("text_editor/theme/highlighting/gdscript/node_reference_color");959annotation_color = EDITOR_GET("text_editor/theme/highlighting/gdscript/annotation_color");960string_name_color = EDITOR_GET("text_editor/theme/highlighting/gdscript/string_name_color");961type_color = EDITOR_GET("text_editor/theme/highlighting/base_type_color");962comment_marker_colors[COMMENT_MARKER_CRITICAL] = EDITOR_GET("text_editor/theme/highlighting/comment_markers/critical_color");963comment_marker_colors[COMMENT_MARKER_WARNING] = EDITOR_GET("text_editor/theme/highlighting/comment_markers/warning_color");964comment_marker_colors[COMMENT_MARKER_NOTICE] = EDITOR_GET("text_editor/theme/highlighting/comment_markers/notice_color");965966comment_markers.clear();967Vector<String> critical_list = EDITOR_GET("text_editor/theme/highlighting/comment_markers/critical_list").operator String().split(",", false);968for (int i = 0; i < critical_list.size(); i++) {969comment_markers[critical_list[i]] = COMMENT_MARKER_CRITICAL;970}971Vector<String> warning_list = EDITOR_GET("text_editor/theme/highlighting/comment_markers/warning_list").operator String().split(",", false);972for (int i = 0; i < warning_list.size(); i++) {973comment_markers[warning_list[i]] = COMMENT_MARKER_WARNING;974}975Vector<String> notice_list = EDITOR_GET("text_editor/theme/highlighting/comment_markers/notice_list").operator String().split(",", false);976for (int i = 0; i < notice_list.size(); i++) {977comment_markers[notice_list[i]] = COMMENT_MARKER_NOTICE;978}979}980981void 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) {982ERR_FAIL_COND_MSG(p_start_key.is_empty(), "Color region start key cannot be empty.");983ERR_FAIL_COND_MSG(!is_symbol(p_start_key[0]), "Color region start key must start with a symbol.");984985if (!p_end_key.is_empty()) {986ERR_FAIL_COND_MSG(!is_symbol(p_end_key[0]), "Color region end key must start with a symbol.");987}988989int at = 0;990for (const ColorRegion ®ion : color_regions) {991ERR_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.");992if (p_start_key.length() < region.start_key.length()) {993at++;994} else {995break;996}997}998999ColorRegion color_region;1000color_region.type = p_type;1001color_region.color = p_color;1002color_region.start_key = p_start_key;1003color_region.end_key = p_end_key;1004color_region.line_only = p_line_only;1005color_region.r_prefix = p_r_prefix;1006color_region.is_string = p_type == ColorRegion::TYPE_STRING || p_type == ColorRegion::TYPE_MULTILINE_STRING;1007color_region.is_comment = p_type == ColorRegion::TYPE_COMMENT || p_type == ColorRegion::TYPE_CODE_REGION;1008color_regions.insert(at, color_region);1009clear_highlighting_cache();1010}10111012Ref<EditorSyntaxHighlighter> GDScriptSyntaxHighlighter::_create() const {1013Ref<GDScriptSyntaxHighlighter> syntax_highlighter;1014syntax_highlighter.instantiate();1015return syntax_highlighter;1016}101710181019