Merge pull request #82516 from dalexeev/gds-improve-hl-types

GDScript: Improve highlighting of types
This commit is contained in:
Rémi Verschelde 2023-10-03 17:24:32 +02:00
commit 2af3229ce7
No known key found for this signature in database
GPG Key ID: C3336907360768E1
3 changed files with 68 additions and 62 deletions

View File

@ -442,7 +442,7 @@ Dictionary GDScriptSyntaxHighlighter::_get_line_syntax_highlighting_impl(int p_l
if (str[k] == '(') { if (str[k] == '(') {
in_function_name = true; in_function_name = true;
} else if (prev_text == GDScriptTokenizer::get_token_name(GDScriptTokenizer::Token::VAR)) { } else if (prev_text == GDScriptTokenizer::get_token_name(GDScriptTokenizer::Token::VAR) || prev_text == GDScriptTokenizer::get_token_name(GDScriptTokenizer::Token::FOR)) {
in_variable_declaration = true; in_variable_declaration = true;
} }
@ -480,7 +480,7 @@ Dictionary GDScriptSyntaxHighlighter::_get_line_syntax_highlighting_impl(int p_l
in_function_args = false; in_function_args = false;
} }
if (expect_type && (prev_is_char || str[j] == '=') && str[j] != '[') { if (expect_type && (prev_is_char || str[j] == '=') && str[j] != '[' && str[j] != '.') {
expect_type = false; expect_type = false;
} }
@ -562,16 +562,11 @@ Dictionary GDScriptSyntaxHighlighter::_get_line_syntax_highlighting_impl(int p_l
} else if (in_keyword) { } else if (in_keyword) {
next_type = KEYWORD; next_type = KEYWORD;
color = keyword_color; color = keyword_color;
} else if (in_member_variable) {
next_type = MEMBER;
color = member_color;
} else if (in_signal_declaration) { } else if (in_signal_declaration) {
next_type = SIGNAL; next_type = SIGNAL;
color = member_color; color = member_color;
} else if (in_function_name) { } else if (in_function_name) {
next_type = FUNCTION; next_type = FUNCTION;
if (!in_lambda && prev_text == GDScriptTokenizer::get_token_name(GDScriptTokenizer::Token::FUNC)) { if (!in_lambda && prev_text == GDScriptTokenizer::get_token_name(GDScriptTokenizer::Token::FUNC)) {
color = function_definition_color; color = function_definition_color;
} else { } else {
@ -586,6 +581,9 @@ Dictionary GDScriptSyntaxHighlighter::_get_line_syntax_highlighting_impl(int p_l
} else if (expect_type) { } else if (expect_type) {
next_type = TYPE; next_type = TYPE;
color = type_color; color = type_color;
} else if (in_member_variable) {
next_type = MEMBER;
color = member_color;
} else { } else {
next_type = IDENTIFIER; next_type = IDENTIFIER;
} }
@ -683,6 +681,12 @@ void GDScriptSyntaxHighlighter::_update_cache() {
for (const String &E : core_types) { for (const String &E : core_types) {
class_names[StringName(E)] = basetype_color; class_names[StringName(E)] = basetype_color;
} }
class_names[SNAME("Variant")] = basetype_color;
class_names[SNAME("void")] = basetype_color;
// `get_core_type_words()` doesn't return primitive types.
class_names[SNAME("bool")] = basetype_color;
class_names[SNAME("int")] = basetype_color;
class_names[SNAME("float")] = basetype_color;
/* Reserved words. */ /* Reserved words. */
const Color keyword_color = EDITOR_GET("text_editor/theme/highlighting/keyword_color"); const Color keyword_color = EDITOR_GET("text_editor/theme/highlighting/keyword_color");
@ -697,6 +701,10 @@ void GDScriptSyntaxHighlighter::_update_cache() {
} }
} }
// Highlight `set` and `get` as "keywords" with the function color to avoid conflicts with method calls.
reserved_keywords[SNAME("set")] = function_color;
reserved_keywords[SNAME("get")] = function_color;
/* Global functions. */ /* Global functions. */
List<StringName> global_function_list; List<StringName> global_function_list;
GDScriptUtilityFunctions::get_function_list(&global_function_list); GDScriptUtilityFunctions::get_function_list(&global_function_list);

View File

@ -2368,62 +2368,60 @@ void GDScriptLanguage::frame() {
/* EDITOR FUNCTIONS */ /* EDITOR FUNCTIONS */
void GDScriptLanguage::get_reserved_words(List<String> *p_words) const { void GDScriptLanguage::get_reserved_words(List<String> *p_words) const {
// TODO: Add annotations here? // Please keep alphabetical order within categories.
static const char *_reserved_words[] = { static const char *_reserved_words[] = {
// operators // Control flow.
"and",
"in",
"not",
"or",
// types and values
"false",
"float",
"int",
"bool",
"null",
"PI",
"TAU",
"INF",
"NAN",
"self",
"true",
"void",
// functions
"as",
"assert",
"await",
"breakpoint",
"class",
"class_name",
"extends",
"is",
"func",
"preload",
"signal",
"super",
// var
"const",
"enum",
"static",
"var",
// control flow
"break", "break",
"continue", "continue",
"if",
"elif", "elif",
"else", "else",
"for", "for",
"if",
"match",
"pass", "pass",
"return", "return",
"match",
"while",
"when", "when",
// These keywords are not implemented currently, but reserved for (potential) future use. "while",
// We highlight them as keywords to make errors easier to understand. // Declarations.
"trait", "class",
"namespace", "class_name",
"yield", "const",
nullptr "enum",
"extends",
"func",
"namespace", // Reserved for potential future use.
"signal",
"static",
"trait", // Reserved for potential future use.
"var",
// Other keywords.
"await",
"breakpoint",
"self",
"super",
"yield", // Reserved for potential future use.
// Operators.
"and",
"as",
"in",
"is",
"not",
"or",
// Special values (tokenizer treats them as literals, not as tokens).
"false",
"null",
"true",
// Constants.
"INF",
"NAN",
"PI",
"TAU",
// Functions (highlighter uses global function color instead).
"assert",
"preload",
// Types (highlighter uses type color instead).
"void",
nullptr,
}; };
const char **w = _reserved_words; const char **w = _reserved_words;
@ -2432,22 +2430,16 @@ void GDScriptLanguage::get_reserved_words(List<String> *p_words) const {
p_words->push_back(*w); p_words->push_back(*w);
w++; w++;
} }
List<StringName> functions;
GDScriptUtilityFunctions::get_function_list(&functions);
for (const StringName &E : functions) {
p_words->push_back(String(E));
}
} }
bool GDScriptLanguage::is_control_flow_keyword(String p_keyword) const { bool GDScriptLanguage::is_control_flow_keyword(String p_keyword) const {
// Please keep alphabetical order.
return p_keyword == "break" || return p_keyword == "break" ||
p_keyword == "continue" || p_keyword == "continue" ||
p_keyword == "elif" || p_keyword == "elif" ||
p_keyword == "else" || p_keyword == "else" ||
p_keyword == "if" ||
p_keyword == "for" || p_keyword == "for" ||
p_keyword == "if" ||
p_keyword == "match" || p_keyword == "match" ||
p_keyword == "pass" || p_keyword == "pass" ||
p_keyword == "return" || p_keyword == "return" ||

View File

@ -3396,6 +3396,12 @@ static Error _lookup_symbol_from_base(const GDScriptParser::DataType &p_base, co
} }
} }
if ("Variant" == p_symbol) {
r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS;
r_result.class_name = "Variant";
return OK;
}
if ("PI" == p_symbol || "TAU" == p_symbol || "INF" == p_symbol || "NAN" == p_symbol) { if ("PI" == p_symbol || "TAU" == p_symbol || "INF" == p_symbol || "NAN" == p_symbol) {
r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_CONSTANT; r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_CONSTANT;
r_result.class_name = "@GDScript"; r_result.class_name = "@GDScript";