Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/modules/gdscript/gdscript_editor.cpp
11351 views
1
/**************************************************************************/
2
/* gdscript_editor.cpp */
3
/**************************************************************************/
4
/* This file is part of: */
5
/* GODOT ENGINE */
6
/* https://godotengine.org */
7
/**************************************************************************/
8
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10
/* */
11
/* Permission is hereby granted, free of charge, to any person obtaining */
12
/* a copy of this software and associated documentation files (the */
13
/* "Software"), to deal in the Software without restriction, including */
14
/* without limitation the rights to use, copy, modify, merge, publish, */
15
/* distribute, sublicense, and/or sell copies of the Software, and to */
16
/* permit persons to whom the Software is furnished to do so, subject to */
17
/* the following conditions: */
18
/* */
19
/* The above copyright notice and this permission notice shall be */
20
/* included in all copies or substantial portions of the Software. */
21
/* */
22
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29
/**************************************************************************/
30
31
#include "gdscript.h"
32
33
#include "gdscript_analyzer.h"
34
#include "gdscript_parser.h"
35
#include "gdscript_tokenizer.h"
36
#include "gdscript_utility_functions.h"
37
38
#ifdef TOOLS_ENABLED
39
#include "editor/gdscript_docgen.h"
40
#include "editor/script_templates/templates.gen.h"
41
#endif
42
43
#include "core/config/engine.h"
44
#include "core/core_constants.h"
45
#include "core/io/file_access.h"
46
#include "core/math/expression.h"
47
#include "core/variant/container_type_validate.h"
48
49
#ifdef TOOLS_ENABLED
50
#include "core/config/project_settings.h"
51
#include "editor/editor_node.h"
52
#include "editor/editor_string_names.h"
53
#include "editor/file_system/editor_file_system.h"
54
#include "editor/settings/editor_settings.h"
55
#endif
56
57
Vector<String> GDScriptLanguage::get_comment_delimiters() const {
58
static const Vector<String> delimiters = { "#" };
59
return delimiters;
60
}
61
62
Vector<String> GDScriptLanguage::get_doc_comment_delimiters() const {
63
static const Vector<String> delimiters = { "##" };
64
return delimiters;
65
}
66
67
Vector<String> GDScriptLanguage::get_string_delimiters() const {
68
static const Vector<String> delimiters = {
69
"\" \"",
70
"' '",
71
"\"\"\" \"\"\"",
72
"''' '''",
73
};
74
// NOTE: StringName, NodePath and r-strings are not listed here.
75
return delimiters;
76
}
77
78
bool GDScriptLanguage::is_using_templates() {
79
return true;
80
}
81
82
Ref<Script> GDScriptLanguage::make_template(const String &p_template, const String &p_class_name, const String &p_base_class_name) const {
83
Ref<GDScript> scr;
84
scr.instantiate();
85
86
String processed_template = p_template;
87
88
#ifdef TOOLS_ENABLED
89
const bool type_hints = EditorSettings::get_singleton()->get_setting("text_editor/completion/add_type_hints");
90
#else
91
const bool type_hints = true;
92
#endif
93
94
if (!type_hints) {
95
processed_template = processed_template.replace(": int", "")
96
.replace(": Shader.Mode", "")
97
.replace(": VisualShader.Type", "")
98
.replace(": float", "")
99
.replace(": String", "")
100
.replace(": Array[String]", "")
101
.replace(": Node", "")
102
.replace(": CharFXTransform", "")
103
.replace(":=", "=")
104
.replace(" -> void", "")
105
.replace(" -> bool", "")
106
.replace(" -> int", "")
107
.replace(" -> PortType", "")
108
.replace(" -> String", "")
109
.replace(" -> Object", "");
110
}
111
112
processed_template = processed_template.replace("_BASE_", p_base_class_name)
113
.replace("_CLASS_SNAKE_CASE_", p_class_name.to_snake_case().validate_unicode_identifier())
114
.replace("_CLASS_", p_class_name.to_pascal_case().validate_unicode_identifier())
115
.replace("_TS_", _get_indentation());
116
scr->set_source_code(processed_template);
117
118
return scr;
119
}
120
121
Vector<ScriptLanguage::ScriptTemplate> GDScriptLanguage::get_built_in_templates(const StringName &p_object) {
122
Vector<ScriptLanguage::ScriptTemplate> templates;
123
#ifdef TOOLS_ENABLED
124
for (int i = 0; i < TEMPLATES_ARRAY_SIZE; i++) {
125
if (TEMPLATES[i].inherit == p_object) {
126
templates.append(TEMPLATES[i]);
127
}
128
}
129
#endif
130
return templates;
131
}
132
133
static void get_function_names_recursively(const GDScriptParser::ClassNode *p_class, const String &p_prefix, HashMap<int, String> &r_funcs) {
134
for (int i = 0; i < p_class->members.size(); i++) {
135
if (p_class->members[i].type == GDScriptParser::ClassNode::Member::FUNCTION) {
136
const GDScriptParser::FunctionNode *function = p_class->members[i].function;
137
r_funcs[function->start_line] = p_prefix.is_empty() ? String(function->identifier->name) : p_prefix + "." + String(function->identifier->name);
138
} else if (p_class->members[i].type == GDScriptParser::ClassNode::Member::CLASS) {
139
String new_prefix = p_class->members[i].m_class->identifier->name;
140
get_function_names_recursively(p_class->members[i].m_class, p_prefix.is_empty() ? new_prefix : p_prefix + "." + new_prefix, r_funcs);
141
}
142
}
143
}
144
145
bool GDScriptLanguage::validate(const String &p_script, const String &p_path, List<String> *r_functions, List<ScriptLanguage::ScriptError> *r_errors, List<ScriptLanguage::Warning> *r_warnings, HashSet<int> *r_safe_lines) const {
146
GDScriptParser parser;
147
GDScriptAnalyzer analyzer(&parser);
148
149
Error err = parser.parse(p_script, p_path, false);
150
if (err == OK) {
151
err = analyzer.analyze();
152
}
153
#ifdef DEBUG_ENABLED
154
if (r_warnings) {
155
for (const GDScriptWarning &E : parser.get_warnings()) {
156
const GDScriptWarning &warn = E;
157
ScriptLanguage::Warning w;
158
w.start_line = warn.start_line;
159
w.end_line = warn.end_line;
160
w.code = (int)warn.code;
161
w.string_code = GDScriptWarning::get_name_from_code(warn.code);
162
w.message = warn.get_message();
163
r_warnings->push_back(w);
164
}
165
}
166
#endif
167
if (err) {
168
if (r_errors) {
169
for (const GDScriptParser::ParserError &pe : parser.get_errors()) {
170
ScriptLanguage::ScriptError e;
171
e.path = p_path;
172
e.line = pe.line;
173
e.column = pe.column;
174
e.message = pe.message;
175
r_errors->push_back(e);
176
}
177
178
for (KeyValue<String, Ref<GDScriptParserRef>> E : parser.get_depended_parsers()) {
179
GDScriptParser *depended_parser = E.value->get_parser();
180
for (const GDScriptParser::ParserError &pe : depended_parser->get_errors()) {
181
ScriptLanguage::ScriptError e;
182
e.path = E.key;
183
e.line = pe.line;
184
e.column = pe.column;
185
e.message = pe.message;
186
r_errors->push_back(e);
187
}
188
}
189
}
190
return false;
191
} else if (r_functions) {
192
const GDScriptParser::ClassNode *cl = parser.get_tree();
193
HashMap<int, String> funcs;
194
195
get_function_names_recursively(cl, "", funcs);
196
197
for (const KeyValue<int, String> &E : funcs) {
198
r_functions->push_back(E.value + ":" + itos(E.key));
199
}
200
}
201
202
#ifdef DEBUG_ENABLED
203
if (r_safe_lines) {
204
const HashSet<int> &unsafe_lines = parser.get_unsafe_lines();
205
for (int i = 1; i <= parser.get_last_line_number(); i++) {
206
if (!unsafe_lines.has(i)) {
207
r_safe_lines->insert(i);
208
}
209
}
210
}
211
#endif
212
213
return true;
214
}
215
216
bool GDScriptLanguage::supports_builtin_mode() const {
217
return true;
218
}
219
220
bool GDScriptLanguage::supports_documentation() const {
221
return true;
222
}
223
224
int GDScriptLanguage::find_function(const String &p_function, const String &p_code) const {
225
GDScriptTokenizerText tokenizer;
226
tokenizer.set_source_code(p_code);
227
int indent = 0;
228
GDScriptTokenizer::Token current = tokenizer.scan();
229
while (current.type != GDScriptTokenizer::Token::TK_EOF && current.type != GDScriptTokenizer::Token::ERROR) {
230
if (current.type == GDScriptTokenizer::Token::INDENT) {
231
indent++;
232
} else if (current.type == GDScriptTokenizer::Token::DEDENT) {
233
indent--;
234
}
235
if (indent == 0 && current.type == GDScriptTokenizer::Token::FUNC) {
236
current = tokenizer.scan();
237
if (current.is_identifier()) {
238
String identifier = current.get_identifier();
239
if (identifier == p_function) {
240
return current.start_line;
241
}
242
}
243
}
244
current = tokenizer.scan();
245
}
246
return -1;
247
}
248
249
Script *GDScriptLanguage::create_script() const {
250
return memnew(GDScript);
251
}
252
253
/* DEBUGGER FUNCTIONS */
254
255
thread_local int GDScriptLanguage::_debug_parse_err_line = -1;
256
thread_local String GDScriptLanguage::_debug_parse_err_file;
257
thread_local String GDScriptLanguage::_debug_error;
258
259
bool GDScriptLanguage::debug_break_parse(const String &p_file, int p_line, const String &p_error) {
260
// break because of parse error
261
262
if (EngineDebugger::is_active() && Thread::get_caller_id() == Thread::get_main_id()) {
263
_debug_parse_err_line = p_line;
264
_debug_parse_err_file = p_file;
265
_debug_error = p_error;
266
EngineDebugger::get_script_debugger()->debug(this, false, true);
267
// Because this is thread local, clear the memory afterwards.
268
_debug_parse_err_file = String();
269
_debug_error = String();
270
return true;
271
} else {
272
return false;
273
}
274
}
275
276
bool GDScriptLanguage::debug_break(const String &p_error, bool p_allow_continue) {
277
if (EngineDebugger::is_active()) {
278
_debug_parse_err_line = -1;
279
_debug_parse_err_file = "";
280
_debug_error = p_error;
281
bool is_error_breakpoint = p_error != "Breakpoint";
282
EngineDebugger::get_script_debugger()->debug(this, p_allow_continue, is_error_breakpoint);
283
// Because this is thread local, clear the memory afterwards.
284
_debug_parse_err_file = String();
285
_debug_error = String();
286
return true;
287
} else {
288
return false;
289
}
290
}
291
292
String GDScriptLanguage::debug_get_error() const {
293
return _debug_error;
294
}
295
296
int GDScriptLanguage::debug_get_stack_level_count() const {
297
if (_debug_parse_err_line >= 0) {
298
return 1;
299
}
300
301
return _call_stack_size;
302
}
303
304
int GDScriptLanguage::debug_get_stack_level_line(int p_level) const {
305
if (_debug_parse_err_line >= 0) {
306
return _debug_parse_err_line;
307
}
308
309
ERR_FAIL_INDEX_V(p_level, (int)_call_stack_size, -1);
310
311
return *(_get_stack_level(p_level)->line);
312
}
313
314
String GDScriptLanguage::debug_get_stack_level_function(int p_level) const {
315
if (_debug_parse_err_line >= 0) {
316
return "";
317
}
318
319
ERR_FAIL_INDEX_V(p_level, (int)_call_stack_size, "");
320
GDScriptFunction *func = _get_stack_level(p_level)->function;
321
return func ? func->get_name().operator String() : "";
322
}
323
324
String GDScriptLanguage::debug_get_stack_level_source(int p_level) const {
325
if (_debug_parse_err_line >= 0) {
326
return _debug_parse_err_file;
327
}
328
329
ERR_FAIL_INDEX_V(p_level, (int)_call_stack_size, "");
330
return _get_stack_level(p_level)->function->get_source();
331
}
332
333
void GDScriptLanguage::debug_get_stack_level_locals(int p_level, List<String> *p_locals, List<Variant> *p_values, int p_max_subitems, int p_max_depth) {
334
if (_debug_parse_err_line >= 0) {
335
return;
336
}
337
338
ERR_FAIL_INDEX(p_level, (int)_call_stack_size);
339
340
CallLevel *cl = _get_stack_level(p_level);
341
GDScriptFunction *f = cl->function;
342
343
List<Pair<StringName, int>> locals;
344
345
f->debug_get_stack_member_state(*cl->line, &locals);
346
for (const Pair<StringName, int> &E : locals) {
347
p_locals->push_back(E.first);
348
p_values->push_back(cl->stack[E.second]);
349
}
350
}
351
352
void GDScriptLanguage::debug_get_stack_level_members(int p_level, List<String> *p_members, List<Variant> *p_values, int p_max_subitems, int p_max_depth) {
353
if (_debug_parse_err_line >= 0) {
354
return;
355
}
356
357
ERR_FAIL_INDEX(p_level, (int)_call_stack_size);
358
359
CallLevel *cl = _get_stack_level(p_level);
360
GDScriptInstance *instance = cl->instance;
361
362
if (!instance) {
363
return;
364
}
365
366
Ref<GDScript> scr = instance->get_script();
367
ERR_FAIL_COND(scr.is_null());
368
369
const HashMap<StringName, GDScript::MemberInfo> &mi = scr->debug_get_member_indices();
370
371
for (const KeyValue<StringName, GDScript::MemberInfo> &E : mi) {
372
p_members->push_back(E.key);
373
p_values->push_back(instance->debug_get_member_by_index(E.value.index));
374
}
375
}
376
377
ScriptInstance *GDScriptLanguage::debug_get_stack_level_instance(int p_level) {
378
if (_debug_parse_err_line >= 0) {
379
return nullptr;
380
}
381
382
ERR_FAIL_INDEX_V(p_level, (int)_call_stack_size, nullptr);
383
384
return _get_stack_level(p_level)->instance;
385
}
386
387
void GDScriptLanguage::debug_get_globals(List<String> *p_globals, List<Variant> *p_values, int p_max_subitems, int p_max_depth) {
388
const HashMap<StringName, int> &name_idx = GDScriptLanguage::get_singleton()->get_global_map();
389
const Variant *gl_array = GDScriptLanguage::get_singleton()->get_global_array();
390
391
List<Pair<String, Variant>> cinfo;
392
get_public_constants(&cinfo);
393
394
for (const KeyValue<StringName, int> &E : name_idx) {
395
if (ClassDB::class_exists(E.key) || Engine::get_singleton()->has_singleton(E.key)) {
396
continue;
397
}
398
399
bool is_script_constant = false;
400
for (List<Pair<String, Variant>>::Element *CE = cinfo.front(); CE; CE = CE->next()) {
401
if (CE->get().first == E.key) {
402
is_script_constant = true;
403
break;
404
}
405
}
406
if (is_script_constant) {
407
continue;
408
}
409
410
const Variant &var = gl_array[E.value];
411
bool freed = false;
412
const Object *obj = var.get_validated_object_with_check(freed);
413
if (obj && !freed) {
414
if (Object::cast_to<GDScriptNativeClass>(obj)) {
415
continue;
416
}
417
}
418
419
bool skip = false;
420
for (int i = 0; i < CoreConstants::get_global_constant_count(); i++) {
421
if (E.key == CoreConstants::get_global_constant_name(i)) {
422
skip = true;
423
break;
424
}
425
}
426
if (skip) {
427
continue;
428
}
429
430
p_globals->push_back(E.key);
431
p_values->push_back(var);
432
}
433
}
434
435
String GDScriptLanguage::debug_parse_stack_level_expression(int p_level, const String &p_expression, int p_max_subitems, int p_max_depth) {
436
List<String> names;
437
List<Variant> values;
438
debug_get_stack_level_locals(p_level, &names, &values, p_max_subitems, p_max_depth);
439
440
Vector<String> name_vector;
441
for (const String &name : names) {
442
name_vector.push_back(name);
443
}
444
445
Array value_array;
446
for (const Variant &value : values) {
447
value_array.push_back(value);
448
}
449
450
Expression expression;
451
if (expression.parse(p_expression, name_vector) == OK) {
452
ScriptInstance *instance = debug_get_stack_level_instance(p_level);
453
if (instance) {
454
Variant return_val = expression.execute(value_array, instance->get_owner());
455
return return_val.get_construct_string();
456
}
457
}
458
459
return String();
460
}
461
462
void GDScriptLanguage::get_recognized_extensions(List<String> *p_extensions) const {
463
p_extensions->push_back("gd");
464
}
465
466
void GDScriptLanguage::get_public_functions(List<MethodInfo> *p_functions) const {
467
List<StringName> functions;
468
GDScriptUtilityFunctions::get_function_list(&functions);
469
470
for (const StringName &E : functions) {
471
p_functions->push_back(GDScriptUtilityFunctions::get_function_info(E));
472
}
473
474
// Not really "functions", but show in documentation.
475
{
476
MethodInfo mi;
477
mi.name = "preload";
478
mi.arguments.push_back(PropertyInfo(Variant::STRING, "path"));
479
mi.return_val = PropertyInfo(Variant::OBJECT, "", PROPERTY_HINT_RESOURCE_TYPE, "Resource");
480
p_functions->push_back(mi);
481
}
482
{
483
MethodInfo mi;
484
mi.name = "assert";
485
mi.return_val.type = Variant::NIL;
486
mi.arguments.push_back(PropertyInfo(Variant::BOOL, "condition"));
487
mi.arguments.push_back(PropertyInfo(Variant::STRING, "message"));
488
mi.default_arguments.push_back(String());
489
p_functions->push_back(mi);
490
}
491
}
492
493
void GDScriptLanguage::get_public_constants(List<Pair<String, Variant>> *p_constants) const {
494
Pair<String, Variant> pi;
495
pi.first = "PI";
496
pi.second = Math::PI;
497
p_constants->push_back(pi);
498
499
Pair<String, Variant> tau;
500
tau.first = "TAU";
501
tau.second = Math::TAU;
502
p_constants->push_back(tau);
503
504
Pair<String, Variant> infinity;
505
infinity.first = "INF";
506
infinity.second = Math::INF;
507
p_constants->push_back(infinity);
508
509
Pair<String, Variant> nan;
510
nan.first = "NAN";
511
nan.second = Math::NaN;
512
p_constants->push_back(nan);
513
}
514
515
void GDScriptLanguage::get_public_annotations(List<MethodInfo> *p_annotations) const {
516
GDScriptParser parser;
517
List<MethodInfo> annotations;
518
parser.get_annotation_list(&annotations);
519
520
for (const MethodInfo &E : annotations) {
521
p_annotations->push_back(E);
522
}
523
}
524
525
String GDScriptLanguage::make_function(const String &p_class, const String &p_name, const PackedStringArray &p_args) const {
526
#ifdef TOOLS_ENABLED
527
const bool type_hints = EditorSettings::get_singleton()->get_setting("text_editor/completion/add_type_hints");
528
#else
529
const bool type_hints = true;
530
#endif
531
532
String result = "func " + p_name + "(";
533
if (p_args.size()) {
534
for (int i = 0; i < p_args.size(); i++) {
535
if (i > 0) {
536
result += ", ";
537
}
538
539
const String name_unstripped = p_args[i].get_slicec(':', 0);
540
result += name_unstripped.strip_edges();
541
542
if (type_hints) {
543
const String type_stripped = p_args[i].substr(name_unstripped.length() + 1).strip_edges();
544
if (!type_stripped.is_empty()) {
545
result += ": " + type_stripped;
546
}
547
}
548
}
549
}
550
result += String(")") + (type_hints ? " -> void" : "") + ":\n" +
551
_get_indentation() + "pass # Replace with function body.\n";
552
553
return result;
554
}
555
556
//////// COMPLETION //////////
557
558
#ifdef TOOLS_ENABLED
559
560
#define COMPLETION_RECURSION_LIMIT 200
561
562
struct GDScriptCompletionIdentifier {
563
GDScriptParser::DataType type;
564
String enumeration;
565
Variant value;
566
const GDScriptParser::ExpressionNode *assigned_expression = nullptr;
567
};
568
569
// LOCATION METHODS
570
// These methods are used to populate the `CodeCompletionOption::location` integer.
571
// For these methods, the location is based on the depth in the inheritance chain that the property
572
// appears. For example, if you are completing code in a class that inherits Node2D, a property found on Node2D
573
// will have a "better" (lower) location "score" than a property that is found on CanvasItem.
574
575
static int _get_property_location(const StringName &p_class, const StringName &p_property) {
576
if (!ClassDB::has_property(p_class, p_property)) {
577
return ScriptLanguage::LOCATION_OTHER;
578
}
579
580
int depth = 0;
581
StringName class_test = p_class;
582
while (class_test && !ClassDB::has_property(class_test, p_property, true)) {
583
class_test = ClassDB::get_parent_class(class_test);
584
depth++;
585
}
586
587
return depth | ScriptLanguage::LOCATION_PARENT_MASK;
588
}
589
590
static int _get_property_location(Ref<Script> p_script, const StringName &p_property) {
591
int depth = 0;
592
Ref<Script> scr = p_script;
593
while (scr.is_valid()) {
594
if (scr->get_member_line(p_property) != -1) {
595
return depth | ScriptLanguage::LOCATION_PARENT_MASK;
596
}
597
depth++;
598
scr = scr->get_base_script();
599
}
600
return depth + _get_property_location(p_script->get_instance_base_type(), p_property);
601
}
602
603
static int _get_constant_location(const StringName &p_class, const StringName &p_constant) {
604
if (!ClassDB::has_integer_constant(p_class, p_constant)) {
605
return ScriptLanguage::LOCATION_OTHER;
606
}
607
608
int depth = 0;
609
StringName class_test = p_class;
610
while (class_test && !ClassDB::has_integer_constant(class_test, p_constant, true)) {
611
class_test = ClassDB::get_parent_class(class_test);
612
depth++;
613
}
614
615
return depth | ScriptLanguage::LOCATION_PARENT_MASK;
616
}
617
618
static int _get_constant_location(Ref<Script> p_script, const StringName &p_constant) {
619
int depth = 0;
620
Ref<Script> scr = p_script;
621
while (scr.is_valid()) {
622
if (scr->get_member_line(p_constant) != -1) {
623
return depth | ScriptLanguage::LOCATION_PARENT_MASK;
624
}
625
depth++;
626
scr = scr->get_base_script();
627
}
628
return depth + _get_constant_location(p_script->get_instance_base_type(), p_constant);
629
}
630
631
static int _get_signal_location(const StringName &p_class, const StringName &p_signal) {
632
if (!ClassDB::has_signal(p_class, p_signal)) {
633
return ScriptLanguage::LOCATION_OTHER;
634
}
635
636
int depth = 0;
637
StringName class_test = p_class;
638
while (class_test && !ClassDB::has_signal(class_test, p_signal, true)) {
639
class_test = ClassDB::get_parent_class(class_test);
640
depth++;
641
}
642
643
return depth | ScriptLanguage::LOCATION_PARENT_MASK;
644
}
645
646
static int _get_signal_location(Ref<Script> p_script, const StringName &p_signal) {
647
int depth = 0;
648
Ref<Script> scr = p_script;
649
while (scr.is_valid()) {
650
if (scr->get_member_line(p_signal) != -1) {
651
return depth | ScriptLanguage::LOCATION_PARENT_MASK;
652
}
653
depth++;
654
scr = scr->get_base_script();
655
}
656
return depth + _get_signal_location(p_script->get_instance_base_type(), p_signal);
657
}
658
659
static int _get_method_location(const StringName &p_class, const StringName &p_method) {
660
if (!ClassDB::has_method(p_class, p_method)) {
661
return ScriptLanguage::LOCATION_OTHER;
662
}
663
664
int depth = 0;
665
StringName class_test = p_class;
666
while (class_test && !ClassDB::has_method(class_test, p_method, true)) {
667
class_test = ClassDB::get_parent_class(class_test);
668
depth++;
669
}
670
671
return depth | ScriptLanguage::LOCATION_PARENT_MASK;
672
}
673
674
static int _get_enum_constant_location(const StringName &p_class, const StringName &p_enum_constant) {
675
if (!ClassDB::get_integer_constant_enum(p_class, p_enum_constant)) {
676
return ScriptLanguage::LOCATION_OTHER;
677
}
678
679
int depth = 0;
680
StringName class_test = p_class;
681
while (class_test && !ClassDB::get_integer_constant_enum(class_test, p_enum_constant, true)) {
682
class_test = ClassDB::get_parent_class(class_test);
683
depth++;
684
}
685
686
return depth | ScriptLanguage::LOCATION_PARENT_MASK;
687
}
688
689
static int _get_enum_location(const StringName &p_class, const StringName &p_enum) {
690
if (!ClassDB::has_enum(p_class, p_enum)) {
691
return ScriptLanguage::LOCATION_OTHER;
692
}
693
694
int depth = 0;
695
StringName class_test = p_class;
696
while (class_test && !ClassDB::has_enum(class_test, p_enum, true)) {
697
class_test = ClassDB::get_parent_class(class_test);
698
depth++;
699
}
700
701
return depth | ScriptLanguage::LOCATION_PARENT_MASK;
702
}
703
704
// END LOCATION METHODS
705
706
static String _trim_parent_class(const String &p_class, const String &p_base_class) {
707
if (p_base_class.is_empty()) {
708
return p_class;
709
}
710
Vector<String> names = p_class.split(".", false, 1);
711
if (names.size() == 2) {
712
const String &first = names[0];
713
if (ClassDB::class_exists(p_base_class) && ClassDB::class_exists(first) && ClassDB::is_parent_class(p_base_class, first)) {
714
const String &rest = names[1];
715
return rest;
716
}
717
}
718
return p_class;
719
}
720
721
static String _get_visual_datatype(const PropertyInfo &p_info, bool p_is_arg, const String &p_base_class = "") {
722
String class_name = p_info.class_name;
723
bool is_enum = p_info.type == Variant::INT && p_info.usage & PROPERTY_USAGE_CLASS_IS_ENUM;
724
// PROPERTY_USAGE_CLASS_IS_BITFIELD: BitField[T] isn't supported (yet?), use plain int.
725
726
if ((p_info.type == Variant::OBJECT || is_enum) && !class_name.is_empty()) {
727
if (is_enum && CoreConstants::is_global_enum(p_info.class_name)) {
728
return class_name;
729
}
730
return _trim_parent_class(class_name, p_base_class);
731
} else if (p_info.type == Variant::ARRAY && p_info.hint == PROPERTY_HINT_ARRAY_TYPE && !p_info.hint_string.is_empty()) {
732
return "Array[" + _trim_parent_class(p_info.hint_string, p_base_class) + "]";
733
} else if (p_info.type == Variant::DICTIONARY && p_info.hint == PROPERTY_HINT_DICTIONARY_TYPE && !p_info.hint_string.is_empty()) {
734
const String key = p_info.hint_string.get_slicec(';', 0);
735
const String value = p_info.hint_string.get_slicec(';', 1);
736
return "Dictionary[" + _trim_parent_class(key, p_base_class) + ", " + _trim_parent_class(value, p_base_class) + "]";
737
} else if (p_info.type == Variant::NIL) {
738
if (p_is_arg || (p_info.usage & PROPERTY_USAGE_NIL_IS_VARIANT)) {
739
return "Variant";
740
} else {
741
return "void";
742
}
743
}
744
745
return Variant::get_type_name(p_info.type);
746
}
747
748
static String _make_arguments_hint(const MethodInfo &p_info, int p_arg_idx, bool p_is_annotation = false) {
749
String arghint;
750
if (!p_is_annotation) {
751
arghint += _get_visual_datatype(p_info.return_val, false) + " ";
752
}
753
arghint += p_info.name + "(";
754
755
int def_args = p_info.arguments.size() - p_info.default_arguments.size();
756
int i = 0;
757
for (const PropertyInfo &E : p_info.arguments) {
758
if (i > 0) {
759
arghint += ", ";
760
}
761
762
if (i == p_arg_idx) {
763
arghint += String::chr(0xFFFF);
764
}
765
arghint += E.name + ": " + _get_visual_datatype(E, true);
766
767
if (i - def_args >= 0) {
768
arghint += String(" = ") + p_info.default_arguments[i - def_args].get_construct_string();
769
}
770
771
if (i == p_arg_idx) {
772
arghint += String::chr(0xFFFF);
773
}
774
775
i++;
776
}
777
778
if (p_info.flags & METHOD_FLAG_VARARG) {
779
if (p_info.arguments.size() > 0) {
780
arghint += ", ";
781
}
782
if (p_arg_idx >= p_info.arguments.size()) {
783
arghint += String::chr(0xFFFF);
784
}
785
arghint += "...args: Array"; // `MethodInfo` does not support the rest parameter name.
786
if (p_arg_idx >= p_info.arguments.size()) {
787
arghint += String::chr(0xFFFF);
788
}
789
}
790
791
arghint += ")";
792
793
return arghint;
794
}
795
796
static String _make_arguments_hint(const GDScriptParser::FunctionNode *p_function, int p_arg_idx, bool p_just_args = false) {
797
String arghint;
798
799
if (p_just_args) {
800
arghint = "(";
801
} else {
802
if (p_function->get_datatype().builtin_type == Variant::NIL) {
803
arghint = "void " + p_function->identifier->name + "(";
804
} else {
805
arghint = p_function->get_datatype().to_string() + " " + p_function->identifier->name + "(";
806
}
807
}
808
809
for (int i = 0; i < p_function->parameters.size(); i++) {
810
if (i > 0) {
811
arghint += ", ";
812
}
813
814
if (i == p_arg_idx) {
815
arghint += String::chr(0xFFFF);
816
}
817
const GDScriptParser::ParameterNode *par = p_function->parameters[i];
818
if (!par->get_datatype().is_hard_type()) {
819
arghint += par->identifier->name.operator String() + ": Variant";
820
} else {
821
arghint += par->identifier->name.operator String() + ": " + par->get_datatype().to_string();
822
}
823
824
if (par->initializer) {
825
String def_val = "<unknown>";
826
switch (par->initializer->type) {
827
case GDScriptParser::Node::LITERAL: {
828
const GDScriptParser::LiteralNode *literal = static_cast<const GDScriptParser::LiteralNode *>(par->initializer);
829
def_val = literal->value.get_construct_string();
830
} break;
831
case GDScriptParser::Node::IDENTIFIER: {
832
const GDScriptParser::IdentifierNode *id = static_cast<const GDScriptParser::IdentifierNode *>(par->initializer);
833
def_val = id->name.operator String();
834
} break;
835
case GDScriptParser::Node::CALL: {
836
const GDScriptParser::CallNode *call = static_cast<const GDScriptParser::CallNode *>(par->initializer);
837
if (call->is_constant && call->reduced) {
838
def_val = call->reduced_value.get_construct_string();
839
} else if (call->get_callee_type() == GDScriptParser::Node::IDENTIFIER) {
840
def_val = call->function_name.operator String() + (call->arguments.is_empty() ? "()" : "(...)");
841
}
842
} break;
843
case GDScriptParser::Node::ARRAY: {
844
const GDScriptParser::ArrayNode *arr = static_cast<const GDScriptParser::ArrayNode *>(par->initializer);
845
if (arr->is_constant && arr->reduced) {
846
def_val = arr->reduced_value.get_construct_string();
847
} else {
848
def_val = arr->elements.is_empty() ? "[]" : "[...]";
849
}
850
} break;
851
case GDScriptParser::Node::DICTIONARY: {
852
const GDScriptParser::DictionaryNode *dict = static_cast<const GDScriptParser::DictionaryNode *>(par->initializer);
853
if (dict->is_constant && dict->reduced) {
854
def_val = dict->reduced_value.get_construct_string();
855
} else {
856
def_val = dict->elements.is_empty() ? "{}" : "{...}";
857
}
858
} break;
859
case GDScriptParser::Node::SUBSCRIPT: {
860
const GDScriptParser::SubscriptNode *sub = static_cast<const GDScriptParser::SubscriptNode *>(par->initializer);
861
if (sub->is_attribute && sub->datatype.kind == GDScriptParser::DataType::ENUM && !sub->datatype.is_meta_type) {
862
def_val = sub->get_datatype().to_string() + "." + sub->attribute->name;
863
} else if (sub->is_constant && sub->reduced) {
864
def_val = sub->reduced_value.get_construct_string();
865
}
866
} break;
867
default:
868
break;
869
}
870
arghint += " = " + def_val;
871
}
872
if (i == p_arg_idx) {
873
arghint += String::chr(0xFFFF);
874
}
875
}
876
877
if (p_function->is_vararg()) {
878
if (!p_function->parameters.is_empty()) {
879
arghint += ", ";
880
}
881
if (p_arg_idx >= p_function->parameters.size()) {
882
arghint += String::chr(0xFFFF);
883
}
884
const GDScriptParser::ParameterNode *rest_param = p_function->rest_parameter;
885
arghint += "..." + rest_param->identifier->name + ": " + rest_param->get_datatype().to_string();
886
if (p_arg_idx >= p_function->parameters.size()) {
887
arghint += String::chr(0xFFFF);
888
}
889
}
890
891
arghint += ")";
892
893
return arghint;
894
}
895
896
static void _get_directory_contents(EditorFileSystemDirectory *p_dir, HashMap<String, ScriptLanguage::CodeCompletionOption> &r_list, const StringName &p_required_type = StringName()) {
897
const String quote_style = EDITOR_GET("text_editor/completion/use_single_quotes") ? "'" : "\"";
898
const bool requires_type = !p_required_type.is_empty();
899
900
for (int i = 0; i < p_dir->get_file_count(); i++) {
901
if (requires_type && !ClassDB::is_parent_class(p_dir->get_file_type(i), p_required_type)) {
902
continue;
903
}
904
ScriptLanguage::CodeCompletionOption option(p_dir->get_file_path(i).quote(quote_style), ScriptLanguage::CODE_COMPLETION_KIND_FILE_PATH);
905
r_list.insert(option.display, option);
906
}
907
908
for (int i = 0; i < p_dir->get_subdir_count(); i++) {
909
_get_directory_contents(p_dir->get_subdir(i), r_list, p_required_type);
910
}
911
}
912
913
static void _find_annotation_arguments(const GDScriptParser::AnnotationNode *p_annotation, int p_argument, const String p_quote_style, HashMap<String, ScriptLanguage::CodeCompletionOption> &r_result, String &r_arghint) {
914
ERR_FAIL_NULL(p_annotation);
915
916
if (p_annotation->info != nullptr) {
917
r_arghint = _make_arguments_hint(p_annotation->info->info, p_argument, true);
918
}
919
if (p_annotation->name == SNAME("@export_range")) {
920
if (p_argument == 3 || p_argument == 4 || p_argument == 5) {
921
// Slider hint.
922
ScriptLanguage::CodeCompletionOption slider1("or_greater", ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
923
slider1.insert_text = slider1.display.quote(p_quote_style);
924
r_result.insert(slider1.display, slider1);
925
ScriptLanguage::CodeCompletionOption slider2("or_less", ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
926
slider2.insert_text = slider2.display.quote(p_quote_style);
927
r_result.insert(slider2.display, slider2);
928
ScriptLanguage::CodeCompletionOption slider3("prefer_slider", ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
929
slider3.insert_text = slider3.display.quote(p_quote_style);
930
r_result.insert(slider3.display, slider3);
931
ScriptLanguage::CodeCompletionOption slider4("hide_control", ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
932
slider4.insert_text = slider4.display.quote(p_quote_style);
933
r_result.insert(slider4.display, slider4);
934
}
935
} else if (p_annotation->name == SNAME("@export_exp_easing")) {
936
if (p_argument == 0 || p_argument == 1) {
937
// Easing hint.
938
ScriptLanguage::CodeCompletionOption hint1("attenuation", ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
939
hint1.insert_text = hint1.display.quote(p_quote_style);
940
r_result.insert(hint1.display, hint1);
941
ScriptLanguage::CodeCompletionOption hint2("inout", ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
942
hint2.insert_text = hint2.display.quote(p_quote_style);
943
r_result.insert(hint2.display, hint2);
944
}
945
} else if (p_annotation->name == SNAME("@export_node_path")) {
946
ScriptLanguage::CodeCompletionOption node("Node", ScriptLanguage::CODE_COMPLETION_KIND_CLASS);
947
node.insert_text = node.display.quote(p_quote_style);
948
r_result.insert(node.display, node);
949
950
LocalVector<StringName> native_classes;
951
ClassDB::get_inheriters_from_class("Node", native_classes);
952
for (const StringName &E : native_classes) {
953
if (!ClassDB::is_class_exposed(E)) {
954
continue;
955
}
956
ScriptLanguage::CodeCompletionOption option(E, ScriptLanguage::CODE_COMPLETION_KIND_CLASS);
957
option.insert_text = option.display.quote(p_quote_style);
958
r_result.insert(option.display, option);
959
}
960
961
LocalVector<StringName> global_script_classes;
962
ScriptServer::get_global_class_list(global_script_classes);
963
for (const StringName &class_name : global_script_classes) {
964
if (!ClassDB::is_parent_class(ScriptServer::get_global_class_native_base(class_name), "Node")) {
965
continue;
966
}
967
ScriptLanguage::CodeCompletionOption option(class_name, ScriptLanguage::CODE_COMPLETION_KIND_CLASS);
968
option.insert_text = option.display.quote(p_quote_style);
969
r_result.insert(option.display, option);
970
}
971
} else if (p_annotation->name == SNAME("@export_tool_button")) {
972
if (p_argument == 1) {
973
const Ref<Theme> theme = EditorNode::get_singleton()->get_editor_theme();
974
if (theme.is_valid()) {
975
List<StringName> icon_list;
976
theme->get_icon_list(EditorStringName(EditorIcons), &icon_list);
977
for (const StringName &E : icon_list) {
978
ScriptLanguage::CodeCompletionOption option(E, ScriptLanguage::CODE_COMPLETION_KIND_CLASS);
979
option.insert_text = option.display.quote(p_quote_style);
980
r_result.insert(option.display, option);
981
}
982
}
983
}
984
} else if (p_annotation->name == SNAME("@export_custom")) {
985
switch (p_argument) {
986
case 0: {
987
static HashMap<StringName, int64_t> items;
988
if (unlikely(items.is_empty())) {
989
CoreConstants::get_enum_values(SNAME("PropertyHint"), &items);
990
}
991
for (const KeyValue<StringName, int64_t> &item : items) {
992
ScriptLanguage::CodeCompletionOption option(item.key, ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT);
993
r_result.insert(option.display, option);
994
}
995
} break;
996
case 2: {
997
static HashMap<StringName, int64_t> items;
998
if (unlikely(items.is_empty())) {
999
CoreConstants::get_enum_values(SNAME("PropertyUsageFlags"), &items);
1000
}
1001
for (const KeyValue<StringName, int64_t> &item : items) {
1002
ScriptLanguage::CodeCompletionOption option(item.key, ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT);
1003
r_result.insert(option.display, option);
1004
}
1005
} break;
1006
}
1007
} else if (p_annotation->name == SNAME("@warning_ignore") || p_annotation->name == SNAME("@warning_ignore_start") || p_annotation->name == SNAME("@warning_ignore_restore")) {
1008
for (int warning_code = 0; warning_code < GDScriptWarning::WARNING_MAX; warning_code++) {
1009
#ifndef DISABLE_DEPRECATED
1010
if (warning_code >= GDScriptWarning::FIRST_DEPRECATED_WARNING) {
1011
break; // Don't suggest deprecated warnings as they are never produced.
1012
}
1013
#endif
1014
ScriptLanguage::CodeCompletionOption warning(GDScriptWarning::get_name_from_code((GDScriptWarning::Code)warning_code).to_lower(), ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
1015
warning.insert_text = warning.display.quote(p_quote_style);
1016
r_result.insert(warning.display, warning);
1017
}
1018
} else if (p_annotation->name == SNAME("@rpc")) {
1019
if (p_argument == 0 || p_argument == 1 || p_argument == 2) {
1020
static const char *options[7] = { "call_local", "call_remote", "any_peer", "authority", "reliable", "unreliable", "unreliable_ordered" };
1021
for (int i = 0; i < 7; i++) {
1022
ScriptLanguage::CodeCompletionOption option(options[i], ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
1023
option.insert_text = option.display.quote(p_quote_style);
1024
r_result.insert(option.display, option);
1025
}
1026
}
1027
}
1028
}
1029
1030
static void _find_built_in_variants(HashMap<String, ScriptLanguage::CodeCompletionOption> &r_result, bool exclude_nil = false) {
1031
for (int i = 0; i < Variant::VARIANT_MAX; i++) {
1032
if (!exclude_nil && Variant::Type(i) == Variant::Type::NIL) {
1033
ScriptLanguage::CodeCompletionOption option("null", ScriptLanguage::CODE_COMPLETION_KIND_CLASS);
1034
r_result.insert(option.display, option);
1035
} else {
1036
ScriptLanguage::CodeCompletionOption option(Variant::get_type_name(Variant::Type(i)), ScriptLanguage::CODE_COMPLETION_KIND_CLASS);
1037
r_result.insert(option.display, option);
1038
}
1039
}
1040
}
1041
1042
static void _find_global_enums(HashMap<String, ScriptLanguage::CodeCompletionOption> &r_result) {
1043
List<StringName> global_enums;
1044
CoreConstants::get_global_enums(&global_enums);
1045
for (const StringName &enum_name : global_enums) {
1046
ScriptLanguage::CodeCompletionOption option(enum_name, ScriptLanguage::CODE_COMPLETION_KIND_ENUM, ScriptLanguage::LOCATION_OTHER);
1047
r_result.insert(option.display, option);
1048
}
1049
}
1050
1051
static void _list_available_types(bool p_inherit_only, GDScriptParser::CompletionContext &p_context, HashMap<String, ScriptLanguage::CodeCompletionOption> &r_result) {
1052
// Built-in Variant Types
1053
_find_built_in_variants(r_result, true);
1054
1055
LocalVector<StringName> native_types;
1056
ClassDB::get_class_list(native_types);
1057
for (const StringName &type : native_types) {
1058
if (ClassDB::is_class_exposed(type) && !Engine::get_singleton()->has_singleton(type)) {
1059
ScriptLanguage::CodeCompletionOption option(type, ScriptLanguage::CODE_COMPLETION_KIND_CLASS);
1060
r_result.insert(option.display, option);
1061
}
1062
}
1063
1064
// TODO: Unify with _find_identifiers_in_class.
1065
if (p_context.current_class) {
1066
if (!p_inherit_only && p_context.current_class->base_type.is_set()) {
1067
// Native enums from base class
1068
List<StringName> enums;
1069
ClassDB::get_enum_list(p_context.current_class->base_type.native_type, &enums);
1070
for (const StringName &E : enums) {
1071
ScriptLanguage::CodeCompletionOption option(E, ScriptLanguage::CODE_COMPLETION_KIND_ENUM);
1072
r_result.insert(option.display, option);
1073
}
1074
}
1075
// Check current class for potential types.
1076
// TODO: Also check classes the current class inherits from.
1077
const GDScriptParser::ClassNode *current = p_context.current_class;
1078
int location_offset = 0;
1079
while (current) {
1080
for (int i = 0; i < current->members.size(); i++) {
1081
const GDScriptParser::ClassNode::Member &member = current->members[i];
1082
switch (member.type) {
1083
case GDScriptParser::ClassNode::Member::CLASS: {
1084
ScriptLanguage::CodeCompletionOption option(member.m_class->identifier->name, ScriptLanguage::CODE_COMPLETION_KIND_CLASS, ScriptLanguage::LOCATION_LOCAL + location_offset);
1085
r_result.insert(option.display, option);
1086
} break;
1087
case GDScriptParser::ClassNode::Member::ENUM: {
1088
if (!p_inherit_only) {
1089
ScriptLanguage::CodeCompletionOption option(member.m_enum->identifier->name, ScriptLanguage::CODE_COMPLETION_KIND_ENUM, ScriptLanguage::LOCATION_LOCAL + location_offset);
1090
r_result.insert(option.display, option);
1091
}
1092
} break;
1093
case GDScriptParser::ClassNode::Member::CONSTANT: {
1094
if (member.constant->get_datatype().is_meta_type) {
1095
ScriptLanguage::CodeCompletionOption option(member.constant->identifier->name, ScriptLanguage::CODE_COMPLETION_KIND_CLASS, ScriptLanguage::LOCATION_LOCAL + location_offset);
1096
r_result.insert(option.display, option);
1097
}
1098
} break;
1099
default:
1100
break;
1101
}
1102
}
1103
location_offset += 1;
1104
current = current->outer;
1105
}
1106
}
1107
1108
// Global scripts
1109
LocalVector<StringName> global_classes;
1110
ScriptServer::get_global_class_list(global_classes);
1111
for (const StringName &class_name : global_classes) {
1112
ScriptLanguage::CodeCompletionOption option(class_name, ScriptLanguage::CODE_COMPLETION_KIND_CLASS, ScriptLanguage::LOCATION_OTHER_USER_CODE);
1113
r_result.insert(option.display, option);
1114
}
1115
1116
// Global enums
1117
if (!p_inherit_only) {
1118
_find_global_enums(r_result);
1119
}
1120
1121
// Autoload singletons
1122
HashMap<StringName, ProjectSettings::AutoloadInfo> autoloads = ProjectSettings::get_singleton()->get_autoload_list();
1123
1124
for (const KeyValue<StringName, ProjectSettings::AutoloadInfo> &E : autoloads) {
1125
const ProjectSettings::AutoloadInfo &info = E.value;
1126
if (!info.is_singleton || info.path.get_extension().to_lower() != "gd") {
1127
continue;
1128
}
1129
ScriptLanguage::CodeCompletionOption option(info.name, ScriptLanguage::CODE_COMPLETION_KIND_CLASS, ScriptLanguage::LOCATION_OTHER_USER_CODE);
1130
r_result.insert(option.display, option);
1131
}
1132
}
1133
1134
static void _find_identifiers_in_suite(const GDScriptParser::SuiteNode *p_suite, HashMap<String, ScriptLanguage::CodeCompletionOption> &r_result, int p_recursion_depth = 0) {
1135
for (int i = 0; i < p_suite->locals.size(); i++) {
1136
ScriptLanguage::CodeCompletionOption option;
1137
int location = p_recursion_depth == 0 ? ScriptLanguage::LOCATION_LOCAL : (p_recursion_depth | ScriptLanguage::LOCATION_PARENT_MASK);
1138
if (p_suite->locals[i].type == GDScriptParser::SuiteNode::Local::CONSTANT) {
1139
option = ScriptLanguage::CodeCompletionOption(p_suite->locals[i].name, ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT, location);
1140
option.default_value = p_suite->locals[i].constant->initializer->reduced_value;
1141
} else {
1142
option = ScriptLanguage::CodeCompletionOption(p_suite->locals[i].name, ScriptLanguage::CODE_COMPLETION_KIND_VARIABLE, location);
1143
}
1144
r_result.insert(option.display, option);
1145
}
1146
if (p_suite->parent_block) {
1147
_find_identifiers_in_suite(p_suite->parent_block, r_result, p_recursion_depth + 1);
1148
}
1149
}
1150
1151
static void _find_identifiers_in_base(const GDScriptCompletionIdentifier &p_base, bool p_only_functions, bool p_types_only, bool p_add_braces, HashMap<String, ScriptLanguage::CodeCompletionOption> &r_result, int p_recursion_depth);
1152
1153
static void _find_identifiers_in_class(const GDScriptParser::ClassNode *p_class, bool p_only_functions, bool p_types_only, bool p_static, bool p_parent_only, bool p_add_braces, HashMap<String, ScriptLanguage::CodeCompletionOption> &r_result, int p_recursion_depth) {
1154
ERR_FAIL_COND(p_recursion_depth > COMPLETION_RECURSION_LIMIT);
1155
1156
if (!p_parent_only) {
1157
bool outer = false;
1158
const GDScriptParser::ClassNode *clss = p_class;
1159
int classes_processed = 0;
1160
while (clss) {
1161
for (int i = 0; i < clss->members.size(); i++) {
1162
const int location = p_recursion_depth == 0 ? classes_processed : (p_recursion_depth | ScriptLanguage::LOCATION_PARENT_MASK);
1163
const GDScriptParser::ClassNode::Member &member = clss->members[i];
1164
ScriptLanguage::CodeCompletionOption option;
1165
switch (member.type) {
1166
case GDScriptParser::ClassNode::Member::VARIABLE:
1167
if (p_types_only || p_only_functions || outer || (p_static && !member.variable->is_static)) {
1168
continue;
1169
}
1170
option = ScriptLanguage::CodeCompletionOption(member.variable->identifier->name, ScriptLanguage::CODE_COMPLETION_KIND_MEMBER, location);
1171
break;
1172
case GDScriptParser::ClassNode::Member::CONSTANT:
1173
if ((p_types_only && !member.constant->datatype.is_meta_type) || p_only_functions) {
1174
continue;
1175
}
1176
if (r_result.has(member.constant->identifier->name)) {
1177
continue;
1178
}
1179
option = ScriptLanguage::CodeCompletionOption(member.constant->identifier->name, ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT, location);
1180
if (member.constant->initializer) {
1181
option.default_value = member.constant->initializer->reduced_value;
1182
}
1183
break;
1184
case GDScriptParser::ClassNode::Member::CLASS:
1185
if (p_only_functions) {
1186
continue;
1187
}
1188
option = ScriptLanguage::CodeCompletionOption(member.m_class->identifier->name, ScriptLanguage::CODE_COMPLETION_KIND_CLASS, location);
1189
break;
1190
case GDScriptParser::ClassNode::Member::ENUM_VALUE:
1191
if (p_types_only || p_only_functions) {
1192
continue;
1193
}
1194
option = ScriptLanguage::CodeCompletionOption(member.enum_value.identifier->name, ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT, location);
1195
break;
1196
case GDScriptParser::ClassNode::Member::ENUM:
1197
if (p_only_functions) {
1198
continue;
1199
}
1200
option = ScriptLanguage::CodeCompletionOption(member.m_enum->identifier->name, ScriptLanguage::CODE_COMPLETION_KIND_ENUM, location);
1201
break;
1202
case GDScriptParser::ClassNode::Member::FUNCTION:
1203
if (p_types_only || outer || (p_static && !member.function->is_static) || member.function->identifier->name.operator String().begins_with("@")) {
1204
continue;
1205
}
1206
option = ScriptLanguage::CodeCompletionOption(member.function->identifier->name, ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION, location);
1207
if (p_add_braces) {
1208
if (member.function->parameters.size() > 0 || (member.function->info.flags & METHOD_FLAG_VARARG)) {
1209
option.insert_text += "(";
1210
option.display += U"(\u2026)";
1211
} else {
1212
option.insert_text += "()";
1213
option.display += "()";
1214
}
1215
}
1216
break;
1217
case GDScriptParser::ClassNode::Member::SIGNAL:
1218
if (p_types_only || p_only_functions || outer || p_static) {
1219
continue;
1220
}
1221
option = ScriptLanguage::CodeCompletionOption(member.signal->identifier->name, ScriptLanguage::CODE_COMPLETION_KIND_SIGNAL, location);
1222
break;
1223
case GDScriptParser::ClassNode::Member::GROUP:
1224
break; // No-op, but silences warnings.
1225
case GDScriptParser::ClassNode::Member::UNDEFINED:
1226
break;
1227
}
1228
r_result.insert(option.display, option);
1229
}
1230
if (p_types_only) {
1231
break; // Otherwise, it will fill the results with types from the outer class (which is undesired for that case).
1232
}
1233
1234
outer = true;
1235
clss = clss->outer;
1236
classes_processed++;
1237
}
1238
}
1239
1240
// Parents.
1241
GDScriptCompletionIdentifier base_type;
1242
base_type.type = p_class->base_type;
1243
base_type.type.is_meta_type = p_static;
1244
1245
_find_identifiers_in_base(base_type, p_only_functions, p_types_only, p_add_braces, r_result, p_recursion_depth + 1);
1246
}
1247
1248
static void _find_identifiers_in_base(const GDScriptCompletionIdentifier &p_base, bool p_only_functions, bool p_types_only, bool p_add_braces, HashMap<String, ScriptLanguage::CodeCompletionOption> &r_result, int p_recursion_depth) {
1249
ERR_FAIL_COND(p_recursion_depth > COMPLETION_RECURSION_LIMIT);
1250
1251
GDScriptParser::DataType base_type = p_base.type;
1252
1253
if (!p_types_only && base_type.is_meta_type && base_type.kind != GDScriptParser::DataType::BUILTIN && base_type.kind != GDScriptParser::DataType::ENUM) {
1254
ScriptLanguage::CodeCompletionOption option("new", ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION, ScriptLanguage::LOCATION_LOCAL);
1255
if (p_add_braces) {
1256
option.insert_text += "(";
1257
option.display += U"(\u2026)";
1258
}
1259
r_result.insert(option.display, option);
1260
}
1261
1262
while (!base_type.has_no_type()) {
1263
switch (base_type.kind) {
1264
case GDScriptParser::DataType::CLASS: {
1265
_find_identifiers_in_class(base_type.class_type, p_only_functions, p_types_only, base_type.is_meta_type, false, p_add_braces, r_result, p_recursion_depth);
1266
// This already finds all parent identifiers, so we are done.
1267
base_type = GDScriptParser::DataType();
1268
} break;
1269
case GDScriptParser::DataType::SCRIPT: {
1270
Ref<Script> scr = base_type.script_type;
1271
if (scr.is_valid()) {
1272
if (p_types_only) {
1273
// TODO: Need to implement Script::get_script_enum_list and retrieve the enum list from a script.
1274
} else if (!p_only_functions) {
1275
if (!base_type.is_meta_type) {
1276
List<PropertyInfo> members;
1277
scr->get_script_property_list(&members);
1278
for (const PropertyInfo &E : members) {
1279
if (E.usage & (PROPERTY_USAGE_CATEGORY | PROPERTY_USAGE_GROUP | PROPERTY_USAGE_SUBGROUP | PROPERTY_USAGE_INTERNAL)) {
1280
continue;
1281
}
1282
if (E.name.contains_char('/')) {
1283
continue;
1284
}
1285
int location = p_recursion_depth + _get_property_location(scr, E.name);
1286
ScriptLanguage::CodeCompletionOption option(E.name, ScriptLanguage::CODE_COMPLETION_KIND_MEMBER, location);
1287
r_result.insert(option.display, option);
1288
}
1289
1290
List<MethodInfo> signals;
1291
scr->get_script_signal_list(&signals);
1292
for (const MethodInfo &E : signals) {
1293
int location = p_recursion_depth + _get_signal_location(scr, E.name);
1294
ScriptLanguage::CodeCompletionOption option(E.name, ScriptLanguage::CODE_COMPLETION_KIND_SIGNAL, location);
1295
r_result.insert(option.display, option);
1296
}
1297
}
1298
HashMap<StringName, Variant> constants;
1299
scr->get_constants(&constants);
1300
for (const KeyValue<StringName, Variant> &E : constants) {
1301
int location = p_recursion_depth + _get_constant_location(scr, E.key);
1302
ScriptLanguage::CodeCompletionOption option(E.key.operator String(), ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT, location);
1303
r_result.insert(option.display, option);
1304
}
1305
}
1306
1307
if (!p_types_only) {
1308
List<MethodInfo> methods;
1309
scr->get_script_method_list(&methods);
1310
for (const MethodInfo &E : methods) {
1311
if (E.name.begins_with("@")) {
1312
continue;
1313
}
1314
int location = p_recursion_depth + _get_method_location(scr->get_class_name(), E.name);
1315
ScriptLanguage::CodeCompletionOption option(E.name, ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION, location);
1316
if (p_add_braces) {
1317
if (E.arguments.size() || (E.flags & METHOD_FLAG_VARARG)) {
1318
option.insert_text += "(";
1319
option.display += U"(\u2026)";
1320
} else {
1321
option.insert_text += "()";
1322
option.display += "()";
1323
}
1324
}
1325
r_result.insert(option.display, option);
1326
}
1327
}
1328
1329
Ref<Script> base_script = scr->get_base_script();
1330
if (base_script.is_valid()) {
1331
base_type.script_type = base_script;
1332
} else {
1333
base_type.kind = GDScriptParser::DataType::NATIVE;
1334
base_type.builtin_type = Variant::OBJECT;
1335
base_type.native_type = scr->get_instance_base_type();
1336
}
1337
} else {
1338
return;
1339
}
1340
} break;
1341
case GDScriptParser::DataType::NATIVE: {
1342
StringName type = base_type.native_type;
1343
if (!ClassDB::class_exists(type)) {
1344
return;
1345
}
1346
1347
List<StringName> enums;
1348
ClassDB::get_enum_list(type, &enums);
1349
for (const StringName &E : enums) {
1350
int location = p_recursion_depth + _get_enum_location(type, E);
1351
ScriptLanguage::CodeCompletionOption option(E, ScriptLanguage::CODE_COMPLETION_KIND_ENUM, location);
1352
r_result.insert(option.display, option);
1353
}
1354
1355
if (p_types_only) {
1356
return;
1357
}
1358
1359
if (!p_only_functions) {
1360
List<String> constants;
1361
ClassDB::get_integer_constant_list(type, &constants);
1362
for (const String &E : constants) {
1363
int location = p_recursion_depth + _get_constant_location(type, StringName(E));
1364
ScriptLanguage::CodeCompletionOption option(E, ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT, location);
1365
r_result.insert(option.display, option);
1366
}
1367
1368
if (!base_type.is_meta_type || Engine::get_singleton()->has_singleton(type)) {
1369
List<PropertyInfo> pinfo;
1370
ClassDB::get_property_list(type, &pinfo);
1371
for (const PropertyInfo &E : pinfo) {
1372
if (E.usage & (PROPERTY_USAGE_CATEGORY | PROPERTY_USAGE_GROUP | PROPERTY_USAGE_SUBGROUP | PROPERTY_USAGE_INTERNAL)) {
1373
continue;
1374
}
1375
if (E.name.contains_char('/')) {
1376
continue;
1377
}
1378
int location = p_recursion_depth + _get_property_location(type, E.name);
1379
ScriptLanguage::CodeCompletionOption option(E.name, ScriptLanguage::CODE_COMPLETION_KIND_MEMBER, location);
1380
r_result.insert(option.display, option);
1381
}
1382
1383
List<MethodInfo> signals;
1384
ClassDB::get_signal_list(type, &signals);
1385
for (const MethodInfo &E : signals) {
1386
int location = p_recursion_depth + _get_signal_location(type, StringName(E.name));
1387
ScriptLanguage::CodeCompletionOption option(E.name, ScriptLanguage::CODE_COMPLETION_KIND_SIGNAL, location);
1388
r_result.insert(option.display, option);
1389
}
1390
}
1391
}
1392
1393
bool only_static = base_type.is_meta_type && !Engine::get_singleton()->has_singleton(type);
1394
1395
List<MethodInfo> methods;
1396
ClassDB::get_method_list(type, &methods, false, true);
1397
for (const MethodInfo &E : methods) {
1398
if (only_static && (E.flags & METHOD_FLAG_STATIC) == 0) {
1399
continue;
1400
}
1401
if (E.name.begins_with("_")) {
1402
continue;
1403
}
1404
int location = p_recursion_depth + _get_method_location(type, E.name);
1405
ScriptLanguage::CodeCompletionOption option(E.name, ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION, location);
1406
if (p_add_braces) {
1407
if (E.arguments.size() || (E.flags & METHOD_FLAG_VARARG)) {
1408
option.insert_text += "(";
1409
option.display += U"(\u2026)";
1410
} else {
1411
option.insert_text += "()";
1412
option.display += "()";
1413
}
1414
}
1415
r_result.insert(option.display, option);
1416
}
1417
return;
1418
} break;
1419
case GDScriptParser::DataType::ENUM: {
1420
if (p_types_only) {
1421
return;
1422
}
1423
1424
String type_str = base_type.native_type;
1425
1426
if (type_str.contains_char('.')) {
1427
StringName type = type_str.get_slicec('.', 0);
1428
StringName type_enum = base_type.enum_type;
1429
1430
List<StringName> enum_values;
1431
1432
ClassDB::get_enum_constants(type, type_enum, &enum_values);
1433
1434
for (const StringName &E : enum_values) {
1435
int location = p_recursion_depth + _get_enum_constant_location(type, E);
1436
ScriptLanguage::CodeCompletionOption option(E, ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT, location);
1437
r_result.insert(option.display, option);
1438
}
1439
} else if (CoreConstants::is_global_enum(base_type.enum_type)) {
1440
HashMap<StringName, int64_t> enum_values;
1441
CoreConstants::get_enum_values(base_type.enum_type, &enum_values);
1442
1443
for (const KeyValue<StringName, int64_t> &enum_value : enum_values) {
1444
int location = p_recursion_depth + ScriptLanguage::LOCATION_OTHER;
1445
ScriptLanguage::CodeCompletionOption option(enum_value.key, ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT, location);
1446
r_result.insert(option.display, option);
1447
}
1448
}
1449
}
1450
[[fallthrough]];
1451
case GDScriptParser::DataType::BUILTIN: {
1452
if (p_types_only) {
1453
return;
1454
}
1455
1456
Callable::CallError err;
1457
Variant tmp;
1458
Variant::construct(base_type.builtin_type, tmp, nullptr, 0, err);
1459
if (err.error != Callable::CallError::CALL_OK) {
1460
return;
1461
}
1462
1463
int location = ScriptLanguage::LOCATION_OTHER;
1464
1465
if (!p_only_functions) {
1466
List<PropertyInfo> members;
1467
if (p_base.value.get_type() != Variant::NIL) {
1468
p_base.value.get_property_list(&members);
1469
} else {
1470
tmp.get_property_list(&members);
1471
}
1472
1473
for (const PropertyInfo &E : members) {
1474
if (E.usage & (PROPERTY_USAGE_CATEGORY | PROPERTY_USAGE_GROUP | PROPERTY_USAGE_SUBGROUP | PROPERTY_USAGE_INTERNAL)) {
1475
continue;
1476
}
1477
if (!String(E.name).contains_char('/')) {
1478
ScriptLanguage::CodeCompletionOption option(E.name, ScriptLanguage::CODE_COMPLETION_KIND_MEMBER, location);
1479
if (base_type.kind == GDScriptParser::DataType::ENUM) {
1480
// Sort enum members in their declaration order.
1481
location += 1;
1482
}
1483
if (GDScriptParser::theme_color_names.has(E.name)) {
1484
option.theme_color_name = GDScriptParser::theme_color_names[E.name];
1485
}
1486
r_result.insert(option.display, option);
1487
}
1488
}
1489
}
1490
1491
List<MethodInfo> methods;
1492
tmp.get_method_list(&methods);
1493
for (const MethodInfo &E : methods) {
1494
if (base_type.kind == GDScriptParser::DataType::ENUM && base_type.is_meta_type && !(E.flags & METHOD_FLAG_CONST)) {
1495
// Enum types are static and cannot change, therefore we skip non-const dictionary methods.
1496
continue;
1497
}
1498
ScriptLanguage::CodeCompletionOption option(E.name, ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION, location);
1499
if (p_add_braces) {
1500
if (E.arguments.size() || (E.flags & METHOD_FLAG_VARARG)) {
1501
option.insert_text += "(";
1502
option.display += U"(\u2026)";
1503
} else {
1504
option.insert_text += "()";
1505
option.display += "()";
1506
}
1507
}
1508
r_result.insert(option.display, option);
1509
}
1510
1511
return;
1512
} break;
1513
default: {
1514
return;
1515
} break;
1516
}
1517
}
1518
}
1519
1520
static void _find_identifiers(const GDScriptParser::CompletionContext &p_context, bool p_only_functions, bool p_add_braces, HashMap<String, ScriptLanguage::CodeCompletionOption> &r_result, int p_recursion_depth) {
1521
if (!p_only_functions && p_context.current_suite) {
1522
// This includes function parameters, since they are also locals.
1523
_find_identifiers_in_suite(p_context.current_suite, r_result);
1524
}
1525
1526
if (p_context.current_class) {
1527
_find_identifiers_in_class(p_context.current_class, p_only_functions, false, (!p_context.current_function || p_context.current_function->is_static), false, p_add_braces, r_result, p_recursion_depth);
1528
}
1529
1530
List<StringName> functions;
1531
GDScriptUtilityFunctions::get_function_list(&functions);
1532
1533
for (const StringName &E : functions) {
1534
MethodInfo function = GDScriptUtilityFunctions::get_function_info(E);
1535
ScriptLanguage::CodeCompletionOption option(String(E), ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION);
1536
if (p_add_braces) {
1537
if (function.arguments.size() || (function.flags & METHOD_FLAG_VARARG)) {
1538
option.insert_text += "(";
1539
option.display += U"(\u2026)";
1540
} else {
1541
option.insert_text += "()";
1542
option.display += "()";
1543
}
1544
}
1545
r_result.insert(option.display, option);
1546
}
1547
1548
if (p_only_functions) {
1549
return;
1550
}
1551
1552
_find_built_in_variants(r_result);
1553
1554
static const char *_keywords[] = {
1555
"true", "false", "PI", "TAU", "INF", "NAN", "null", "self", "super",
1556
"break", "breakpoint", "continue", "pass", "return",
1557
nullptr
1558
};
1559
1560
const char **kw = _keywords;
1561
while (*kw) {
1562
ScriptLanguage::CodeCompletionOption option(*kw, ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
1563
r_result.insert(option.display, option);
1564
kw++;
1565
}
1566
1567
static const char *_keywords_with_space[] = {
1568
"and", "not", "or", "in", "as", "class", "class_name", "extends", "is", "func", "signal", "await",
1569
"const", "enum", "static", "var", "if", "elif", "else", "for", "match", "when", "while",
1570
nullptr
1571
};
1572
1573
const char **kws = _keywords_with_space;
1574
while (*kws) {
1575
ScriptLanguage::CodeCompletionOption option(*kws, ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
1576
option.insert_text += " ";
1577
r_result.insert(option.display, option);
1578
kws++;
1579
}
1580
1581
static const char *_keywords_with_args[] = {
1582
"assert", "preload",
1583
nullptr
1584
};
1585
1586
const char **kwa = _keywords_with_args;
1587
while (*kwa) {
1588
ScriptLanguage::CodeCompletionOption option(*kwa, ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION);
1589
if (p_add_braces) {
1590
option.insert_text += "(";
1591
option.display += U"(\u2026)";
1592
}
1593
r_result.insert(option.display, option);
1594
kwa++;
1595
}
1596
1597
List<StringName> utility_func_names;
1598
Variant::get_utility_function_list(&utility_func_names);
1599
1600
for (const StringName &util_func_name : utility_func_names) {
1601
ScriptLanguage::CodeCompletionOption option(util_func_name, ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION);
1602
if (p_add_braces) {
1603
option.insert_text += "(";
1604
option.display += U"(\u2026)"; // As all utility functions contain an argument or more, this is hardcoded here.
1605
}
1606
r_result.insert(option.display, option);
1607
}
1608
1609
for (const KeyValue<StringName, ProjectSettings::AutoloadInfo> &E : ProjectSettings::get_singleton()->get_autoload_list()) {
1610
if (!E.value.is_singleton) {
1611
continue;
1612
}
1613
ScriptLanguage::CodeCompletionOption option(E.key, ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT);
1614
r_result.insert(option.display, option);
1615
}
1616
1617
// Native classes and global constants.
1618
for (const KeyValue<StringName, int> &E : GDScriptLanguage::get_singleton()->get_global_map()) {
1619
ScriptLanguage::CodeCompletionOption option;
1620
if (ClassDB::class_exists(E.key) || Engine::get_singleton()->has_singleton(E.key)) {
1621
option = ScriptLanguage::CodeCompletionOption(E.key.operator String(), ScriptLanguage::CODE_COMPLETION_KIND_CLASS);
1622
} else {
1623
option = ScriptLanguage::CodeCompletionOption(E.key.operator String(), ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT);
1624
}
1625
r_result.insert(option.display, option);
1626
}
1627
1628
// Global enums
1629
_find_global_enums(r_result);
1630
1631
// Global classes
1632
LocalVector<StringName> global_classes;
1633
ScriptServer::get_global_class_list(global_classes);
1634
for (const StringName &class_name : global_classes) {
1635
ScriptLanguage::CodeCompletionOption option(class_name, ScriptLanguage::CODE_COMPLETION_KIND_CLASS, ScriptLanguage::LOCATION_OTHER_USER_CODE);
1636
r_result.insert(option.display, option);
1637
}
1638
}
1639
1640
static GDScriptCompletionIdentifier _type_from_variant(const Variant &p_value, GDScriptParser::CompletionContext &p_context) {
1641
GDScriptCompletionIdentifier ci;
1642
ci.value = p_value;
1643
ci.type.is_constant = true;
1644
ci.type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
1645
ci.type.kind = GDScriptParser::DataType::BUILTIN;
1646
ci.type.builtin_type = p_value.get_type();
1647
1648
if (ci.type.builtin_type == Variant::OBJECT) {
1649
Object *obj = p_value.operator Object *();
1650
if (!obj) {
1651
return ci;
1652
}
1653
ci.type.native_type = obj->get_class_name();
1654
Ref<Script> scr = p_value;
1655
if (scr.is_valid()) {
1656
ci.type.is_meta_type = true;
1657
} else {
1658
ci.type.is_meta_type = false;
1659
scr = obj->get_script();
1660
}
1661
if (scr.is_valid()) {
1662
ci.type.script_path = scr->get_path();
1663
ci.type.script_type = scr;
1664
ci.type.native_type = scr->get_instance_base_type();
1665
ci.type.kind = GDScriptParser::DataType::SCRIPT;
1666
1667
if (scr->get_path().ends_with(".gd")) {
1668
Ref<GDScriptParserRef> parser = p_context.parser->get_depended_parser_for(scr->get_path());
1669
if (parser.is_valid() && parser->raise_status(GDScriptParserRef::INTERFACE_SOLVED) == OK) {
1670
ci.type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
1671
ci.type.class_type = parser->get_parser()->get_tree();
1672
ci.type.kind = GDScriptParser::DataType::CLASS;
1673
return ci;
1674
}
1675
}
1676
} else {
1677
ci.type.kind = GDScriptParser::DataType::NATIVE;
1678
}
1679
}
1680
1681
return ci;
1682
}
1683
1684
static GDScriptCompletionIdentifier _type_from_property(const PropertyInfo &p_property) {
1685
GDScriptCompletionIdentifier ci;
1686
1687
if (p_property.type == Variant::NIL) {
1688
// Variant
1689
ci.type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
1690
ci.type.kind = GDScriptParser::DataType::VARIANT;
1691
return ci;
1692
}
1693
1694
if (p_property.usage & (PROPERTY_USAGE_CLASS_IS_ENUM | PROPERTY_USAGE_CLASS_IS_BITFIELD)) {
1695
ci.enumeration = p_property.class_name;
1696
}
1697
1698
ci.type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
1699
ci.type.builtin_type = p_property.type;
1700
if (p_property.type == Variant::OBJECT) {
1701
if (ScriptServer::is_global_class(p_property.class_name)) {
1702
ci.type.kind = GDScriptParser::DataType::SCRIPT;
1703
ci.type.script_path = ScriptServer::get_global_class_path(p_property.class_name);
1704
ci.type.native_type = ScriptServer::get_global_class_native_base(p_property.class_name);
1705
1706
Ref<Script> scr = ResourceLoader::load(ScriptServer::get_global_class_path(p_property.class_name));
1707
if (scr.is_valid()) {
1708
ci.type.script_type = scr;
1709
}
1710
} else {
1711
ci.type.kind = GDScriptParser::DataType::NATIVE;
1712
ci.type.native_type = p_property.class_name == StringName() ? "Object" : p_property.class_name;
1713
}
1714
} else {
1715
ci.type.kind = GDScriptParser::DataType::BUILTIN;
1716
}
1717
return ci;
1718
}
1719
1720
static GDScriptCompletionIdentifier _callable_type_from_method_info(const MethodInfo &p_method) {
1721
GDScriptCompletionIdentifier ci;
1722
ci.type.kind = GDScriptParser::DataType::BUILTIN;
1723
ci.type.builtin_type = Variant::CALLABLE;
1724
ci.type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
1725
ci.type.is_constant = true;
1726
ci.type.method_info = p_method;
1727
return ci;
1728
}
1729
1730
#define MAX_COMPLETION_RECURSION 100
1731
struct RecursionCheck {
1732
int *counter;
1733
_FORCE_INLINE_ bool check() {
1734
return (*counter) > MAX_COMPLETION_RECURSION;
1735
}
1736
RecursionCheck(int *p_counter) :
1737
counter(p_counter) {
1738
(*counter)++;
1739
}
1740
~RecursionCheck() {
1741
(*counter)--;
1742
}
1743
};
1744
1745
static bool _guess_identifier_type(GDScriptParser::CompletionContext &p_context, const GDScriptParser::IdentifierNode *p_identifier, GDScriptCompletionIdentifier &r_type);
1746
static bool _guess_identifier_type_from_base(GDScriptParser::CompletionContext &p_context, const GDScriptCompletionIdentifier &p_base, const StringName &p_identifier, GDScriptCompletionIdentifier &r_type);
1747
static bool _guess_method_return_type_from_base(GDScriptParser::CompletionContext &p_context, const GDScriptCompletionIdentifier &p_base, const StringName &p_method, GDScriptCompletionIdentifier &r_type);
1748
1749
static bool _is_expression_named_identifier(const GDScriptParser::ExpressionNode *p_expression, const StringName &p_name) {
1750
if (p_expression) {
1751
switch (p_expression->type) {
1752
case GDScriptParser::Node::IDENTIFIER: {
1753
const GDScriptParser::IdentifierNode *id = static_cast<const GDScriptParser::IdentifierNode *>(p_expression);
1754
if (id->name == p_name) {
1755
return true;
1756
}
1757
} break;
1758
case GDScriptParser::Node::CAST: {
1759
const GDScriptParser::CastNode *cn = static_cast<const GDScriptParser::CastNode *>(p_expression);
1760
return _is_expression_named_identifier(cn->operand, p_name);
1761
} break;
1762
default:
1763
break;
1764
}
1765
}
1766
1767
return false;
1768
}
1769
1770
// Creates a map of exemplary results for some functions that return a structured dictionary.
1771
// Setting this example as value allows autocompletion to suggest the specific keys in some cases.
1772
static HashMap<String, Dictionary> make_structure_samples() {
1773
HashMap<String, Dictionary> res;
1774
const Array arr;
1775
1776
{
1777
Dictionary d;
1778
d.set("major", 0);
1779
d.set("minor", 0);
1780
d.set("patch", 0);
1781
d.set("hex", 0);
1782
d.set("status", String());
1783
d.set("build", String());
1784
d.set("hash", String());
1785
d.set("timestamp", 0);
1786
d.set("string", String());
1787
res["Engine::get_version_info"] = d;
1788
}
1789
1790
{
1791
Dictionary d;
1792
d.set("lead_developers", arr);
1793
d.set("founders", arr);
1794
d.set("project_managers", arr);
1795
d.set("developers", arr);
1796
res["Engine::get_author_info"] = d;
1797
}
1798
1799
{
1800
Dictionary d;
1801
d.set("platinum_sponsors", arr);
1802
d.set("gold_sponsors", arr);
1803
d.set("silver_sponsors", arr);
1804
d.set("bronze_sponsors", arr);
1805
d.set("mini_sponsors", arr);
1806
d.set("gold_donors", arr);
1807
d.set("silver_donors", arr);
1808
d.set("bronze_donors", arr);
1809
res["Engine::get_donor_info"] = d;
1810
}
1811
1812
{
1813
Dictionary d;
1814
d.set("physical", -1);
1815
d.set("free", -1);
1816
d.set("available", -1);
1817
d.set("stack", -1);
1818
res["OS::get_memory_info"] = d;
1819
}
1820
1821
{
1822
Dictionary d;
1823
d.set("year", 0);
1824
d.set("month", 0);
1825
d.set("day", 0);
1826
d.set("weekday", 0);
1827
d.set("hour", 0);
1828
d.set("minute", 0);
1829
d.set("second", 0);
1830
d.set("dst", 0);
1831
res["Time::get_datetime_dict_from_system"] = d;
1832
}
1833
1834
{
1835
Dictionary d;
1836
d.set("year", 0);
1837
d.set("month", 0);
1838
d.set("day", 0);
1839
d.set("weekday", 0);
1840
d.set("hour", 0);
1841
d.set("minute", 0);
1842
d.set("second", 0);
1843
res["Time::get_datetime_dict_from_unix_time"] = d;
1844
}
1845
1846
{
1847
Dictionary d;
1848
d.set("year", 0);
1849
d.set("month", 0);
1850
d.set("day", 0);
1851
d.set("weekday", 0);
1852
res["Time::get_date_dict_from_system"] = d;
1853
res["Time::get_date_dict_from_unix_time"] = d;
1854
}
1855
1856
{
1857
Dictionary d;
1858
d.set("hour", 0);
1859
d.set("minute", 0);
1860
d.set("second", 0);
1861
res["Time::get_time_dict_from_system"] = d;
1862
res["Time::get_time_dict_from_unix_time"] = d;
1863
}
1864
1865
{
1866
Dictionary d;
1867
d.set("bias", 0);
1868
d.set("name", String());
1869
res["Time::get_time_zone_from_system"] = d;
1870
}
1871
1872
return res;
1873
}
1874
1875
static const HashMap<String, Dictionary> structure_examples = make_structure_samples();
1876
1877
static bool _guess_expression_type(GDScriptParser::CompletionContext &p_context, const GDScriptParser::ExpressionNode *p_expression, GDScriptCompletionIdentifier &r_type) {
1878
bool found = false;
1879
1880
if (p_expression == nullptr) {
1881
return false;
1882
}
1883
1884
static int recursion_depth = 0;
1885
RecursionCheck recursion(&recursion_depth);
1886
if (unlikely(recursion.check())) {
1887
ERR_FAIL_V_MSG(false, "Reached recursion limit while trying to guess type.");
1888
}
1889
1890
if (p_expression->is_constant) {
1891
// Already has a value, so just use that.
1892
r_type = _type_from_variant(p_expression->reduced_value, p_context);
1893
switch (p_expression->get_datatype().kind) {
1894
case GDScriptParser::DataType::ENUM:
1895
case GDScriptParser::DataType::CLASS:
1896
r_type.type = p_expression->get_datatype();
1897
break;
1898
default:
1899
break;
1900
}
1901
found = true;
1902
} else {
1903
switch (p_expression->type) {
1904
case GDScriptParser::Node::IDENTIFIER: {
1905
const GDScriptParser::IdentifierNode *id = static_cast<const GDScriptParser::IdentifierNode *>(p_expression);
1906
found = _guess_identifier_type(p_context, id, r_type);
1907
} break;
1908
case GDScriptParser::Node::DICTIONARY: {
1909
// Try to recreate the dictionary.
1910
const GDScriptParser::DictionaryNode *dn = static_cast<const GDScriptParser::DictionaryNode *>(p_expression);
1911
Dictionary d;
1912
bool full = true;
1913
for (int i = 0; i < dn->elements.size(); i++) {
1914
GDScriptCompletionIdentifier key;
1915
if (_guess_expression_type(p_context, dn->elements[i].key, key)) {
1916
if (!key.type.is_constant) {
1917
full = false;
1918
break;
1919
}
1920
GDScriptCompletionIdentifier value;
1921
if (_guess_expression_type(p_context, dn->elements[i].value, value)) {
1922
if (!value.type.is_constant) {
1923
full = false;
1924
break;
1925
}
1926
d[key.value] = value.value;
1927
} else {
1928
full = false;
1929
break;
1930
}
1931
} else {
1932
full = false;
1933
break;
1934
}
1935
}
1936
if (full) {
1937
r_type.value = d;
1938
r_type.type.is_constant = true;
1939
}
1940
r_type.type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
1941
r_type.type.kind = GDScriptParser::DataType::BUILTIN;
1942
r_type.type.builtin_type = Variant::DICTIONARY;
1943
found = true;
1944
} break;
1945
case GDScriptParser::Node::ARRAY: {
1946
// Try to recreate the array
1947
const GDScriptParser::ArrayNode *an = static_cast<const GDScriptParser::ArrayNode *>(p_expression);
1948
Array a;
1949
bool full = true;
1950
a.resize(an->elements.size());
1951
for (int i = 0; i < an->elements.size(); i++) {
1952
GDScriptCompletionIdentifier value;
1953
if (_guess_expression_type(p_context, an->elements[i], value)) {
1954
if (value.type.is_constant) {
1955
a[i] = value.value;
1956
} else {
1957
full = false;
1958
break;
1959
}
1960
} else {
1961
full = false;
1962
break;
1963
}
1964
}
1965
if (full) {
1966
// If not fully constant, setting this value is detrimental to the inference.
1967
r_type.value = a;
1968
r_type.type.is_constant = true;
1969
}
1970
r_type.type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
1971
r_type.type.kind = GDScriptParser::DataType::BUILTIN;
1972
r_type.type.builtin_type = Variant::ARRAY;
1973
found = true;
1974
} break;
1975
case GDScriptParser::Node::CAST: {
1976
const GDScriptParser::CastNode *cn = static_cast<const GDScriptParser::CastNode *>(p_expression);
1977
GDScriptCompletionIdentifier value;
1978
if (_guess_expression_type(p_context, cn->operand, r_type)) {
1979
r_type.type = cn->get_datatype();
1980
found = true;
1981
}
1982
} break;
1983
case GDScriptParser::Node::CALL: {
1984
const GDScriptParser::CallNode *call = static_cast<const GDScriptParser::CallNode *>(p_expression);
1985
GDScriptParser::CompletionContext c = p_context;
1986
c.current_line = call->start_line;
1987
1988
GDScriptParser::Node::Type callee_type = call->get_callee_type();
1989
1990
GDScriptCompletionIdentifier base;
1991
if (callee_type == GDScriptParser::Node::IDENTIFIER || call->is_super) {
1992
// Simple call, so base is 'self'.
1993
if (p_context.current_class) {
1994
if (call->is_super) {
1995
base.type = p_context.current_class->base_type;
1996
base.value = p_context.base;
1997
} else {
1998
base.type.kind = GDScriptParser::DataType::CLASS;
1999
base.type.type_source = GDScriptParser::DataType::INFERRED;
2000
base.type.is_constant = true;
2001
base.type.class_type = p_context.current_class;
2002
base.value = p_context.base;
2003
}
2004
} else {
2005
break;
2006
}
2007
} else if (callee_type == GDScriptParser::Node::SUBSCRIPT && static_cast<const GDScriptParser::SubscriptNode *>(call->callee)->is_attribute) {
2008
if (!_guess_expression_type(c, static_cast<const GDScriptParser::SubscriptNode *>(call->callee)->base, base)) {
2009
found = false;
2010
break;
2011
}
2012
} else {
2013
break;
2014
}
2015
2016
// Apply additional behavior aware inference that the analyzer can't do.
2017
if (base.type.is_set()) {
2018
// Maintain type for duplicate methods.
2019
if (call->function_name == SNAME("duplicate")) {
2020
if (base.type.builtin_type == Variant::OBJECT && (ClassDB::is_parent_class(base.type.native_type, SNAME("Resource")) || ClassDB::is_parent_class(base.type.native_type, SNAME("Node")))) {
2021
r_type.type = base.type;
2022
found = true;
2023
break;
2024
}
2025
}
2026
2027
// Simulate generics for some typed array methods.
2028
if (base.type.builtin_type == Variant::ARRAY && base.type.has_container_element_types() && (call->function_name == SNAME("back") || call->function_name == SNAME("front") || call->function_name == SNAME("get") || call->function_name == SNAME("max") || call->function_name == SNAME("min") || call->function_name == SNAME("pick_random") || call->function_name == SNAME("pop_at") || call->function_name == SNAME("pop_back") || call->function_name == SNAME("pop_front"))) {
2029
r_type.type = base.type.get_container_element_type(0);
2030
found = true;
2031
break;
2032
}
2033
2034
// Insert example values for functions which a structured dictionary response.
2035
if (!base.type.is_meta_type) {
2036
const Dictionary *example = structure_examples.getptr(base.type.native_type.operator String() + "::" + call->function_name);
2037
if (example != nullptr) {
2038
r_type = _type_from_variant(*example, p_context);
2039
found = true;
2040
break;
2041
}
2042
}
2043
}
2044
2045
if (!found) {
2046
found = _guess_method_return_type_from_base(c, base, call->function_name, r_type);
2047
}
2048
} break;
2049
case GDScriptParser::Node::SUBSCRIPT: {
2050
const GDScriptParser::SubscriptNode *subscript = static_cast<const GDScriptParser::SubscriptNode *>(p_expression);
2051
if (subscript->is_attribute) {
2052
GDScriptParser::CompletionContext c = p_context;
2053
c.current_line = subscript->start_line;
2054
2055
GDScriptCompletionIdentifier base;
2056
if (!_guess_expression_type(c, subscript->base, base)) {
2057
found = false;
2058
break;
2059
}
2060
2061
if (base.value.get_type() == Variant::DICTIONARY && base.value.operator Dictionary().has(String(subscript->attribute->name))) {
2062
Variant value = base.value.operator Dictionary()[String(subscript->attribute->name)];
2063
r_type = _type_from_variant(value, p_context);
2064
found = true;
2065
break;
2066
}
2067
2068
const GDScriptParser::DictionaryNode *dn = nullptr;
2069
if (subscript->base->type == GDScriptParser::Node::DICTIONARY) {
2070
dn = static_cast<const GDScriptParser::DictionaryNode *>(subscript->base);
2071
} else if (base.assigned_expression && base.assigned_expression->type == GDScriptParser::Node::DICTIONARY) {
2072
dn = static_cast<const GDScriptParser::DictionaryNode *>(base.assigned_expression);
2073
}
2074
2075
if (dn) {
2076
for (int i = 0; i < dn->elements.size(); i++) {
2077
GDScriptCompletionIdentifier key;
2078
if (!_guess_expression_type(c, dn->elements[i].key, key)) {
2079
continue;
2080
}
2081
if (key.value == String(subscript->attribute->name)) {
2082
r_type.assigned_expression = dn->elements[i].value;
2083
found = _guess_expression_type(c, dn->elements[i].value, r_type);
2084
break;
2085
}
2086
}
2087
}
2088
2089
if (!found) {
2090
found = _guess_identifier_type_from_base(c, base, subscript->attribute->name, r_type);
2091
}
2092
} else {
2093
if (subscript->index == nullptr) {
2094
found = false;
2095
break;
2096
}
2097
2098
GDScriptParser::CompletionContext c = p_context;
2099
c.current_line = subscript->start_line;
2100
2101
GDScriptCompletionIdentifier base;
2102
if (!_guess_expression_type(c, subscript->base, base)) {
2103
found = false;
2104
break;
2105
}
2106
2107
GDScriptCompletionIdentifier index;
2108
if (!_guess_expression_type(c, subscript->index, index)) {
2109
found = false;
2110
break;
2111
}
2112
2113
if (base.type.is_constant && index.type.is_constant) {
2114
if (base.value.get_type() == Variant::DICTIONARY) {
2115
Dictionary base_dict = base.value.operator Dictionary();
2116
if (base_dict.get_key_validator().test_validate(index.value) && base_dict.has(index.value)) {
2117
r_type = _type_from_variant(base_dict[index.value], p_context);
2118
found = true;
2119
break;
2120
}
2121
} else {
2122
bool valid;
2123
Variant value = base.value.get(index.value, &valid);
2124
if (valid) {
2125
r_type = _type_from_variant(value, p_context);
2126
found = true;
2127
break;
2128
}
2129
}
2130
}
2131
2132
// Look if it is a dictionary node.
2133
const GDScriptParser::DictionaryNode *dn = nullptr;
2134
if (subscript->base->type == GDScriptParser::Node::DICTIONARY) {
2135
dn = static_cast<const GDScriptParser::DictionaryNode *>(subscript->base);
2136
} else if (base.assigned_expression && base.assigned_expression->type == GDScriptParser::Node::DICTIONARY) {
2137
dn = static_cast<const GDScriptParser::DictionaryNode *>(base.assigned_expression);
2138
}
2139
2140
if (dn) {
2141
for (int i = 0; i < dn->elements.size(); i++) {
2142
GDScriptCompletionIdentifier key;
2143
if (!_guess_expression_type(c, dn->elements[i].key, key)) {
2144
continue;
2145
}
2146
if (key.value == index.value) {
2147
r_type.assigned_expression = dn->elements[i].value;
2148
found = _guess_expression_type(p_context, dn->elements[i].value, r_type);
2149
break;
2150
}
2151
}
2152
}
2153
2154
// Look if it is an array node.
2155
if (!found && index.value.is_num()) {
2156
int idx = index.value;
2157
const GDScriptParser::ArrayNode *an = nullptr;
2158
if (subscript->base->type == GDScriptParser::Node::ARRAY) {
2159
an = static_cast<const GDScriptParser::ArrayNode *>(subscript->base);
2160
} else if (base.assigned_expression && base.assigned_expression->type == GDScriptParser::Node::ARRAY) {
2161
an = static_cast<const GDScriptParser::ArrayNode *>(base.assigned_expression);
2162
}
2163
2164
if (an && idx >= 0 && an->elements.size() > idx) {
2165
r_type.assigned_expression = an->elements[idx];
2166
found = _guess_expression_type(c, an->elements[idx], r_type);
2167
break;
2168
}
2169
}
2170
2171
// Look for valid indexing in other types
2172
if (!found && (index.value.is_string() || index.value.get_type() == Variant::NODE_PATH)) {
2173
StringName id = index.value;
2174
found = _guess_identifier_type_from_base(c, base, id, r_type);
2175
} else if (!found && index.type.kind == GDScriptParser::DataType::BUILTIN) {
2176
Callable::CallError err;
2177
Variant base_val;
2178
Variant::construct(base.type.builtin_type, base_val, nullptr, 0, err);
2179
bool valid = false;
2180
Variant res = base_val.get(index.value, &valid);
2181
if (valid) {
2182
r_type = _type_from_variant(res, p_context);
2183
r_type.value = Variant();
2184
r_type.type.is_constant = false;
2185
found = true;
2186
}
2187
}
2188
}
2189
} break;
2190
case GDScriptParser::Node::BINARY_OPERATOR: {
2191
const GDScriptParser::BinaryOpNode *op = static_cast<const GDScriptParser::BinaryOpNode *>(p_expression);
2192
2193
if (op->variant_op == Variant::OP_MAX) {
2194
break;
2195
}
2196
2197
GDScriptParser::CompletionContext context = p_context;
2198
context.current_line = op->start_line;
2199
2200
GDScriptCompletionIdentifier p1;
2201
GDScriptCompletionIdentifier p2;
2202
2203
if (!_guess_expression_type(context, op->left_operand, p1)) {
2204
found = false;
2205
break;
2206
}
2207
2208
if (!_guess_expression_type(context, op->right_operand, p2)) {
2209
found = false;
2210
break;
2211
}
2212
2213
Callable::CallError ce;
2214
bool v1_use_value = p1.value.get_type() != Variant::NIL && p1.value.get_type() != Variant::OBJECT;
2215
Variant d1;
2216
Variant::construct(p1.type.builtin_type, d1, nullptr, 0, ce);
2217
Variant d2;
2218
Variant::construct(p2.type.builtin_type, d2, nullptr, 0, ce);
2219
2220
Variant v1 = (v1_use_value) ? p1.value : d1;
2221
bool v2_use_value = p2.value.get_type() != Variant::NIL && p2.value.get_type() != Variant::OBJECT;
2222
Variant v2 = (v2_use_value) ? p2.value : d2;
2223
// avoid potential invalid ops
2224
if ((op->variant_op == Variant::OP_DIVIDE || op->variant_op == Variant::OP_MODULE) && v2.get_type() == Variant::INT) {
2225
v2 = 1;
2226
v2_use_value = false;
2227
}
2228
if (op->variant_op == Variant::OP_DIVIDE && v2.get_type() == Variant::FLOAT) {
2229
v2 = 1.0;
2230
v2_use_value = false;
2231
}
2232
2233
Variant res;
2234
bool valid;
2235
Variant::evaluate(op->variant_op, v1, v2, res, valid);
2236
if (!valid) {
2237
found = false;
2238
break;
2239
}
2240
r_type = _type_from_variant(res, p_context);
2241
if (!v1_use_value || !v2_use_value) {
2242
r_type.value = Variant();
2243
r_type.type.is_constant = false;
2244
}
2245
2246
found = true;
2247
} break;
2248
default:
2249
break;
2250
}
2251
}
2252
2253
// It may have found a null, but that's never useful
2254
if (found && r_type.type.kind == GDScriptParser::DataType::BUILTIN && r_type.type.builtin_type == Variant::NIL) {
2255
found = false;
2256
}
2257
2258
// If the found type was not fully analyzed we analyze it now.
2259
if (found && r_type.type.kind == GDScriptParser::DataType::CLASS && !r_type.type.class_type->resolved_body) {
2260
Error err;
2261
Ref<GDScriptParserRef> r = GDScriptCache::get_parser(r_type.type.script_path, GDScriptParserRef::FULLY_SOLVED, err);
2262
}
2263
2264
// Check type hint last. For collections we want chance to get the actual value first
2265
// This way we can detect types from the content of dictionaries and arrays
2266
if (!found && p_expression->get_datatype().is_hard_type()) {
2267
r_type.type = p_expression->get_datatype();
2268
if (!r_type.assigned_expression) {
2269
r_type.assigned_expression = p_expression;
2270
}
2271
found = true;
2272
}
2273
2274
return found;
2275
}
2276
2277
static bool _guess_identifier_type(GDScriptParser::CompletionContext &p_context, const GDScriptParser::IdentifierNode *p_identifier, GDScriptCompletionIdentifier &r_type) {
2278
static int recursion_depth = 0;
2279
RecursionCheck recursion(&recursion_depth);
2280
if (unlikely(recursion.check())) {
2281
ERR_FAIL_V_MSG(false, "Reached recursion limit while trying to guess type.");
2282
}
2283
2284
// Look in blocks first.
2285
int last_assign_line = -1;
2286
const GDScriptParser::ExpressionNode *last_assigned_expression = nullptr;
2287
GDScriptCompletionIdentifier id_type;
2288
GDScriptParser::SuiteNode *suite = p_context.current_suite;
2289
bool is_function_parameter = false;
2290
2291
bool can_be_local = true;
2292
switch (p_identifier->source) {
2293
case GDScriptParser::IdentifierNode::MEMBER_VARIABLE:
2294
case GDScriptParser::IdentifierNode::MEMBER_CONSTANT:
2295
case GDScriptParser::IdentifierNode::MEMBER_FUNCTION:
2296
case GDScriptParser::IdentifierNode::MEMBER_SIGNAL:
2297
case GDScriptParser::IdentifierNode::MEMBER_CLASS:
2298
case GDScriptParser::IdentifierNode::INHERITED_VARIABLE:
2299
case GDScriptParser::IdentifierNode::STATIC_VARIABLE:
2300
case GDScriptParser::IdentifierNode::NATIVE_CLASS:
2301
can_be_local = false;
2302
break;
2303
default:
2304
break;
2305
}
2306
2307
if (can_be_local && suite && suite->has_local(p_identifier->name)) {
2308
const GDScriptParser::SuiteNode::Local &local = suite->get_local(p_identifier->name);
2309
2310
id_type.type = local.get_datatype();
2311
2312
// Check initializer as the first assignment.
2313
switch (local.type) {
2314
case GDScriptParser::SuiteNode::Local::VARIABLE:
2315
if (local.variable->initializer) {
2316
last_assign_line = local.variable->initializer->end_line;
2317
last_assigned_expression = local.variable->initializer;
2318
}
2319
break;
2320
case GDScriptParser::SuiteNode::Local::CONSTANT:
2321
if (local.constant->initializer) {
2322
last_assign_line = local.constant->initializer->end_line;
2323
last_assigned_expression = local.constant->initializer;
2324
}
2325
break;
2326
case GDScriptParser::SuiteNode::Local::PARAMETER:
2327
if (local.parameter->initializer) {
2328
last_assign_line = local.parameter->initializer->end_line;
2329
last_assigned_expression = local.parameter->initializer;
2330
}
2331
is_function_parameter = true;
2332
break;
2333
default:
2334
break;
2335
}
2336
} else {
2337
if (p_context.current_class) {
2338
GDScriptCompletionIdentifier base_identifier;
2339
2340
GDScriptCompletionIdentifier base;
2341
base.value = p_context.base;
2342
base.type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
2343
base.type.kind = GDScriptParser::DataType::CLASS;
2344
base.type.class_type = p_context.current_class;
2345
base.type.is_meta_type = p_context.current_function && p_context.current_function->is_static;
2346
2347
if (_guess_identifier_type_from_base(p_context, base, p_identifier->name, base_identifier)) {
2348
id_type = base_identifier;
2349
}
2350
}
2351
}
2352
2353
while (suite) {
2354
for (int i = 0; i < suite->statements.size(); i++) {
2355
if (suite->statements[i]->end_line >= p_context.current_line) {
2356
break;
2357
}
2358
2359
switch (suite->statements[i]->type) {
2360
case GDScriptParser::Node::ASSIGNMENT: {
2361
const GDScriptParser::AssignmentNode *assign = static_cast<const GDScriptParser::AssignmentNode *>(suite->statements[i]);
2362
if (assign->end_line > last_assign_line && assign->assignee && assign->assigned_value && assign->assignee->type == GDScriptParser::Node::IDENTIFIER) {
2363
const GDScriptParser::IdentifierNode *id = static_cast<const GDScriptParser::IdentifierNode *>(assign->assignee);
2364
if (id->name == p_identifier->name && id->source == p_identifier->source) {
2365
last_assign_line = assign->assigned_value->end_line;
2366
last_assigned_expression = assign->assigned_value;
2367
}
2368
}
2369
} break;
2370
default:
2371
// TODO: Check sub blocks (control flow statements) as they might also reassign stuff.
2372
break;
2373
}
2374
}
2375
2376
if (suite->parent_if && suite->parent_if->condition && suite->parent_if->condition->type == GDScriptParser::Node::TYPE_TEST) {
2377
// Operator `is` used, check if identifier is in there! this helps resolve in blocks that are (if (identifier is value)): which are very common..
2378
// Super dirty hack, but very useful.
2379
// Credit: Zylann.
2380
// TODO: this could be hacked to detect AND-ed conditions too...
2381
const GDScriptParser::TypeTestNode *type_test = static_cast<const GDScriptParser::TypeTestNode *>(suite->parent_if->condition);
2382
if (type_test->operand && type_test->test_type && type_test->operand->type == GDScriptParser::Node::IDENTIFIER && static_cast<const GDScriptParser::IdentifierNode *>(type_test->operand)->name == p_identifier->name && static_cast<const GDScriptParser::IdentifierNode *>(type_test->operand)->source == p_identifier->source) {
2383
// Bingo.
2384
GDScriptParser::CompletionContext c = p_context;
2385
c.current_line = type_test->operand->start_line;
2386
c.current_suite = suite;
2387
if (type_test->test_datatype.is_hard_type()) {
2388
id_type.type = type_test->test_datatype;
2389
if (last_assign_line < c.current_line) {
2390
// Override last assignment.
2391
last_assign_line = c.current_line;
2392
last_assigned_expression = nullptr;
2393
}
2394
}
2395
}
2396
}
2397
2398
suite = suite->parent_block;
2399
}
2400
2401
if (last_assigned_expression && last_assign_line < p_context.current_line) {
2402
GDScriptParser::CompletionContext c = p_context;
2403
c.current_line = last_assign_line;
2404
GDScriptCompletionIdentifier assigned_type;
2405
if (_guess_expression_type(c, last_assigned_expression, assigned_type)) {
2406
if (id_type.type.is_set() && (assigned_type.type.kind == GDScriptParser::DataType::VARIANT || (assigned_type.type.is_set() && !GDScriptAnalyzer::check_type_compatibility(id_type.type, assigned_type.type)))) {
2407
// The assigned type is incompatible. The annotated type takes priority.
2408
r_type = id_type;
2409
r_type.assigned_expression = last_assigned_expression;
2410
} else {
2411
r_type = assigned_type;
2412
}
2413
return true;
2414
}
2415
}
2416
2417
if (is_function_parameter && p_context.current_function && p_context.current_function->source_lambda == nullptr && p_context.current_class) {
2418
// Check if it's override of native function, then we can assume the type from the signature.
2419
GDScriptParser::DataType base_type = p_context.current_class->base_type;
2420
while (base_type.is_set()) {
2421
switch (base_type.kind) {
2422
case GDScriptParser::DataType::CLASS:
2423
if (base_type.class_type->has_function(p_context.current_function->identifier->name)) {
2424
GDScriptParser::FunctionNode *parent_function = base_type.class_type->get_member(p_context.current_function->identifier->name).function;
2425
if (parent_function->parameters_indices.has(p_identifier->name)) {
2426
const GDScriptParser::ParameterNode *parameter = parent_function->parameters[parent_function->parameters_indices[p_identifier->name]];
2427
if ((!id_type.type.is_set() || id_type.type.is_variant()) && parameter->get_datatype().is_hard_type()) {
2428
id_type.type = parameter->get_datatype();
2429
}
2430
if (parameter->initializer) {
2431
GDScriptParser::CompletionContext c = p_context;
2432
c.current_function = parent_function;
2433
c.current_class = base_type.class_type;
2434
c.base = nullptr;
2435
if (_guess_expression_type(c, parameter->initializer, r_type)) {
2436
return true;
2437
}
2438
}
2439
}
2440
}
2441
base_type = base_type.class_type->base_type;
2442
break;
2443
case GDScriptParser::DataType::NATIVE: {
2444
if (id_type.type.is_set() && !id_type.type.is_variant()) {
2445
base_type = GDScriptParser::DataType();
2446
break;
2447
}
2448
MethodInfo info;
2449
if (ClassDB::get_method_info(base_type.native_type, p_context.current_function->identifier->name, &info)) {
2450
for (const PropertyInfo &E : info.arguments) {
2451
if (E.name == p_identifier->name) {
2452
r_type = _type_from_property(E);
2453
return true;
2454
}
2455
}
2456
}
2457
base_type = GDScriptParser::DataType();
2458
} break;
2459
default:
2460
break;
2461
}
2462
}
2463
}
2464
2465
if (id_type.type.is_set() && !id_type.type.is_variant()) {
2466
r_type = id_type;
2467
return true;
2468
}
2469
2470
// Check global scripts.
2471
if (ScriptServer::is_global_class(p_identifier->name)) {
2472
String script = ScriptServer::get_global_class_path(p_identifier->name);
2473
if (script.to_lower().ends_with(".gd")) {
2474
Ref<GDScriptParserRef> parser = p_context.parser->get_depended_parser_for(script);
2475
if (parser.is_valid() && parser->raise_status(GDScriptParserRef::INTERFACE_SOLVED) == OK) {
2476
r_type.type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
2477
r_type.type.script_path = script;
2478
r_type.type.class_type = parser->get_parser()->get_tree();
2479
r_type.type.is_meta_type = true;
2480
r_type.type.is_constant = false;
2481
r_type.type.kind = GDScriptParser::DataType::CLASS;
2482
r_type.value = Variant();
2483
return true;
2484
}
2485
} else {
2486
Ref<Script> scr = ResourceLoader::load(ScriptServer::get_global_class_path(p_identifier->name));
2487
if (scr.is_valid()) {
2488
r_type = _type_from_variant(scr, p_context);
2489
r_type.type.is_meta_type = true;
2490
return true;
2491
}
2492
}
2493
return false;
2494
}
2495
2496
// Check global variables (including autoloads).
2497
if (GDScriptLanguage::get_singleton()->get_named_globals_map().has(p_identifier->name)) {
2498
r_type = _type_from_variant(GDScriptLanguage::get_singleton()->get_named_globals_map()[p_identifier->name], p_context);
2499
return true;
2500
}
2501
2502
// Check ClassDB.
2503
if (ClassDB::class_exists(p_identifier->name) && ClassDB::is_class_exposed(p_identifier->name)) {
2504
r_type.type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
2505
r_type.type.kind = GDScriptParser::DataType::NATIVE;
2506
r_type.type.builtin_type = Variant::OBJECT;
2507
r_type.type.native_type = p_identifier->name;
2508
r_type.type.is_constant = true;
2509
if (Engine::get_singleton()->has_singleton(p_identifier->name)) {
2510
r_type.type.is_meta_type = false;
2511
r_type.value = Engine::get_singleton()->get_singleton_object(p_identifier->name);
2512
} else {
2513
r_type.type.is_meta_type = true;
2514
r_type.value = Variant();
2515
}
2516
return true;
2517
}
2518
2519
return false;
2520
}
2521
2522
static bool _guess_identifier_type_from_base(GDScriptParser::CompletionContext &p_context, const GDScriptCompletionIdentifier &p_base, const StringName &p_identifier, GDScriptCompletionIdentifier &r_type) {
2523
static int recursion_depth = 0;
2524
RecursionCheck recursion(&recursion_depth);
2525
if (unlikely(recursion.check())) {
2526
ERR_FAIL_V_MSG(false, "Reached recursion limit while trying to guess type.");
2527
}
2528
2529
GDScriptParser::DataType base_type = p_base.type;
2530
bool is_static = base_type.is_meta_type;
2531
while (base_type.is_set()) {
2532
switch (base_type.kind) {
2533
case GDScriptParser::DataType::CLASS:
2534
if (base_type.class_type->has_member(p_identifier)) {
2535
const GDScriptParser::ClassNode::Member &member = base_type.class_type->get_member(p_identifier);
2536
switch (member.type) {
2537
case GDScriptParser::ClassNode::Member::CONSTANT:
2538
r_type.type = member.constant->get_datatype();
2539
if (member.constant->initializer && member.constant->initializer->is_constant) {
2540
r_type.value = member.constant->initializer->reduced_value;
2541
}
2542
return true;
2543
case GDScriptParser::ClassNode::Member::VARIABLE:
2544
if (!is_static || member.variable->is_static) {
2545
if (member.variable->get_datatype().is_set() && !member.variable->get_datatype().is_variant()) {
2546
r_type.type = member.variable->get_datatype();
2547
return true;
2548
} else if (member.variable->initializer) {
2549
const GDScriptParser::ExpressionNode *init = member.variable->initializer;
2550
if (init->is_constant) {
2551
r_type.value = init->reduced_value;
2552
r_type = _type_from_variant(init->reduced_value, p_context);
2553
return true;
2554
} else if (init->start_line == p_context.current_line) {
2555
return false;
2556
// Detects if variable is assigned to itself
2557
} else if (_is_expression_named_identifier(init, member.variable->identifier->name)) {
2558
if (member.variable->initializer->get_datatype().is_set()) {
2559
r_type.type = member.variable->initializer->get_datatype();
2560
} else if (member.variable->get_datatype().is_set() && !member.variable->get_datatype().is_variant()) {
2561
r_type.type = member.variable->get_datatype();
2562
}
2563
return true;
2564
} else if (_guess_expression_type(p_context, init, r_type)) {
2565
return true;
2566
} else if (init->get_datatype().is_set() && !init->get_datatype().is_variant()) {
2567
r_type.type = init->get_datatype();
2568
return true;
2569
}
2570
}
2571
}
2572
// TODO: Check assignments in constructor.
2573
return false;
2574
case GDScriptParser::ClassNode::Member::ENUM:
2575
r_type.type = member.m_enum->get_datatype();
2576
r_type.enumeration = member.m_enum->identifier->name;
2577
return true;
2578
case GDScriptParser::ClassNode::Member::ENUM_VALUE:
2579
r_type = _type_from_variant(member.enum_value.value, p_context);
2580
return true;
2581
case GDScriptParser::ClassNode::Member::SIGNAL:
2582
r_type.type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
2583
r_type.type.kind = GDScriptParser::DataType::BUILTIN;
2584
r_type.type.builtin_type = Variant::SIGNAL;
2585
r_type.type.method_info = member.signal->method_info;
2586
return true;
2587
case GDScriptParser::ClassNode::Member::FUNCTION:
2588
if (is_static && !member.function->is_static) {
2589
return false;
2590
}
2591
r_type = _callable_type_from_method_info(member.function->info);
2592
return true;
2593
case GDScriptParser::ClassNode::Member::CLASS:
2594
r_type.type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
2595
r_type.type.kind = GDScriptParser::DataType::CLASS;
2596
r_type.type.class_type = member.m_class;
2597
r_type.type.is_meta_type = true;
2598
return true;
2599
case GDScriptParser::ClassNode::Member::GROUP:
2600
return false; // No-op, but silences warnings.
2601
case GDScriptParser::ClassNode::Member::UNDEFINED:
2602
return false; // Unreachable.
2603
}
2604
return false;
2605
}
2606
base_type = base_type.class_type->base_type;
2607
break;
2608
case GDScriptParser::DataType::SCRIPT: {
2609
Ref<Script> scr = base_type.script_type;
2610
if (scr.is_valid()) {
2611
HashMap<StringName, Variant> constants;
2612
scr->get_constants(&constants);
2613
if (constants.has(p_identifier)) {
2614
r_type = _type_from_variant(constants[p_identifier], p_context);
2615
return true;
2616
}
2617
2618
List<PropertyInfo> members;
2619
if (is_static) {
2620
scr->get_property_list(&members);
2621
} else {
2622
scr->get_script_property_list(&members);
2623
}
2624
for (const PropertyInfo &prop : members) {
2625
if (prop.name == p_identifier) {
2626
r_type = _type_from_property(prop);
2627
return true;
2628
}
2629
}
2630
2631
if (scr->has_method(p_identifier)) {
2632
MethodInfo mi = scr->get_method_info(p_identifier);
2633
r_type = _callable_type_from_method_info(mi);
2634
return true;
2635
}
2636
2637
Ref<Script> parent = scr->get_base_script();
2638
if (parent.is_valid()) {
2639
base_type.script_type = parent;
2640
} else {
2641
base_type.kind = GDScriptParser::DataType::NATIVE;
2642
base_type.builtin_type = Variant::OBJECT;
2643
base_type.native_type = scr->get_instance_base_type();
2644
}
2645
} else {
2646
return false;
2647
}
2648
} break;
2649
case GDScriptParser::DataType::NATIVE: {
2650
StringName class_name = base_type.native_type;
2651
if (!ClassDB::class_exists(class_name)) {
2652
return false;
2653
}
2654
2655
// Skip constants since they're all integers. Type does not matter because int has no members.
2656
2657
PropertyInfo prop;
2658
if (ClassDB::get_property_info(class_name, p_identifier, &prop)) {
2659
StringName getter = ClassDB::get_property_getter(class_name, p_identifier);
2660
if (getter != StringName()) {
2661
MethodBind *g = ClassDB::get_method(class_name, getter);
2662
if (g) {
2663
r_type = _type_from_property(g->get_return_info());
2664
return true;
2665
}
2666
} else {
2667
r_type = _type_from_property(prop);
2668
return true;
2669
}
2670
}
2671
2672
MethodInfo method;
2673
if (ClassDB::get_method_info(class_name, p_identifier, &method)) {
2674
r_type = _callable_type_from_method_info(method);
2675
return true;
2676
}
2677
2678
if (ClassDB::has_enum(class_name, p_identifier)) {
2679
r_type.type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
2680
r_type.type.kind = GDScriptParser::DataType::ENUM;
2681
r_type.type.enum_type = p_identifier;
2682
r_type.type.is_constant = true;
2683
r_type.type.is_meta_type = true;
2684
r_type.type.native_type = String(class_name) + "." + p_identifier;
2685
return true;
2686
}
2687
2688
return false;
2689
} break;
2690
case GDScriptParser::DataType::BUILTIN: {
2691
if (Variant::has_builtin_method(base_type.builtin_type, p_identifier)) {
2692
r_type = _callable_type_from_method_info(Variant::get_builtin_method_info(base_type.builtin_type, p_identifier));
2693
return true;
2694
} else {
2695
Callable::CallError err;
2696
Variant tmp;
2697
Variant::construct(base_type.builtin_type, tmp, nullptr, 0, err);
2698
2699
if (err.error != Callable::CallError::CALL_OK) {
2700
return false;
2701
}
2702
bool valid = false;
2703
Variant res = tmp.get(p_identifier, &valid);
2704
if (valid) {
2705
r_type = _type_from_variant(res, p_context);
2706
r_type.value = Variant();
2707
r_type.type.is_constant = false;
2708
return true;
2709
}
2710
}
2711
return false;
2712
} break;
2713
default: {
2714
return false;
2715
} break;
2716
}
2717
}
2718
return false;
2719
}
2720
2721
static void _find_last_return_in_block(GDScriptParser::CompletionContext &p_context, int &r_last_return_line, const GDScriptParser::ExpressionNode **r_last_returned_value) {
2722
if (!p_context.current_suite) {
2723
return;
2724
}
2725
2726
for (int i = 0; i < p_context.current_suite->statements.size(); i++) {
2727
if (p_context.current_suite->statements[i]->start_line < r_last_return_line) {
2728
break;
2729
}
2730
2731
GDScriptParser::CompletionContext c = p_context;
2732
switch (p_context.current_suite->statements[i]->type) {
2733
case GDScriptParser::Node::FOR:
2734
c.current_suite = static_cast<const GDScriptParser::ForNode *>(p_context.current_suite->statements[i])->loop;
2735
_find_last_return_in_block(c, r_last_return_line, r_last_returned_value);
2736
break;
2737
case GDScriptParser::Node::WHILE:
2738
c.current_suite = static_cast<const GDScriptParser::WhileNode *>(p_context.current_suite->statements[i])->loop;
2739
_find_last_return_in_block(c, r_last_return_line, r_last_returned_value);
2740
break;
2741
case GDScriptParser::Node::IF: {
2742
const GDScriptParser::IfNode *_if = static_cast<const GDScriptParser::IfNode *>(p_context.current_suite->statements[i]);
2743
c.current_suite = _if->true_block;
2744
_find_last_return_in_block(c, r_last_return_line, r_last_returned_value);
2745
if (_if->false_block) {
2746
c.current_suite = _if->false_block;
2747
_find_last_return_in_block(c, r_last_return_line, r_last_returned_value);
2748
}
2749
} break;
2750
case GDScriptParser::Node::MATCH: {
2751
const GDScriptParser::MatchNode *match = static_cast<const GDScriptParser::MatchNode *>(p_context.current_suite->statements[i]);
2752
for (int j = 0; j < match->branches.size(); j++) {
2753
c.current_suite = match->branches[j]->block;
2754
_find_last_return_in_block(c, r_last_return_line, r_last_returned_value);
2755
}
2756
} break;
2757
case GDScriptParser::Node::RETURN: {
2758
const GDScriptParser::ReturnNode *ret = static_cast<const GDScriptParser::ReturnNode *>(p_context.current_suite->statements[i]);
2759
if (ret->return_value) {
2760
if (ret->start_line > r_last_return_line) {
2761
r_last_return_line = ret->start_line;
2762
*r_last_returned_value = ret->return_value;
2763
}
2764
}
2765
} break;
2766
default:
2767
break;
2768
}
2769
}
2770
}
2771
2772
static bool _guess_method_return_type_from_base(GDScriptParser::CompletionContext &p_context, const GDScriptCompletionIdentifier &p_base, const StringName &p_method, GDScriptCompletionIdentifier &r_type) {
2773
static int recursion_depth = 0;
2774
RecursionCheck recursion(&recursion_depth);
2775
if (unlikely(recursion.check())) {
2776
ERR_FAIL_V_MSG(false, "Reached recursion limit while trying to guess type.");
2777
}
2778
2779
GDScriptParser::DataType base_type = p_base.type;
2780
bool is_static = base_type.is_meta_type;
2781
2782
if (is_static && p_method == SNAME("new")) {
2783
r_type.type = base_type;
2784
r_type.type.is_meta_type = false;
2785
r_type.type.is_constant = false;
2786
return true;
2787
}
2788
2789
while (base_type.is_set() && !base_type.is_variant()) {
2790
switch (base_type.kind) {
2791
case GDScriptParser::DataType::CLASS:
2792
if (base_type.class_type->has_function(p_method)) {
2793
GDScriptParser::FunctionNode *method = base_type.class_type->get_member(p_method).function;
2794
if (!is_static || method->is_static) {
2795
if (method->get_datatype().is_set() && !method->get_datatype().is_variant()) {
2796
r_type.type = method->get_datatype();
2797
return true;
2798
}
2799
2800
int last_return_line = -1;
2801
const GDScriptParser::ExpressionNode *last_returned_value = nullptr;
2802
GDScriptParser::CompletionContext c = p_context;
2803
c.current_class = base_type.class_type;
2804
c.current_function = method;
2805
c.current_suite = method->body;
2806
2807
_find_last_return_in_block(c, last_return_line, &last_returned_value);
2808
if (last_returned_value) {
2809
c.current_line = c.current_suite->end_line;
2810
if (_guess_expression_type(c, last_returned_value, r_type)) {
2811
return true;
2812
}
2813
}
2814
}
2815
}
2816
base_type = base_type.class_type->base_type;
2817
break;
2818
case GDScriptParser::DataType::SCRIPT: {
2819
Ref<Script> scr = base_type.script_type;
2820
if (scr.is_valid()) {
2821
List<MethodInfo> methods;
2822
scr->get_script_method_list(&methods);
2823
for (const MethodInfo &mi : methods) {
2824
if (mi.name == p_method) {
2825
r_type = _type_from_property(mi.return_val);
2826
return true;
2827
}
2828
}
2829
Ref<Script> base_script = scr->get_base_script();
2830
if (base_script.is_valid()) {
2831
base_type.script_type = base_script;
2832
} else {
2833
base_type.kind = GDScriptParser::DataType::NATIVE;
2834
base_type.builtin_type = Variant::OBJECT;
2835
base_type.native_type = scr->get_instance_base_type();
2836
}
2837
} else {
2838
return false;
2839
}
2840
} break;
2841
case GDScriptParser::DataType::NATIVE: {
2842
if (!ClassDB::class_exists(base_type.native_type)) {
2843
return false;
2844
}
2845
MethodBind *mb = ClassDB::get_method(base_type.native_type, p_method);
2846
if (mb) {
2847
r_type = _type_from_property(mb->get_return_info());
2848
return true;
2849
}
2850
return false;
2851
} break;
2852
case GDScriptParser::DataType::BUILTIN: {
2853
Callable::CallError err;
2854
Variant tmp;
2855
Variant::construct(base_type.builtin_type, tmp, nullptr, 0, err);
2856
if (err.error != Callable::CallError::CALL_OK) {
2857
return false;
2858
}
2859
2860
List<MethodInfo> methods;
2861
tmp.get_method_list(&methods);
2862
2863
for (const MethodInfo &mi : methods) {
2864
if (mi.name == p_method) {
2865
r_type = _type_from_property(mi.return_val);
2866
return true;
2867
}
2868
}
2869
return false;
2870
} break;
2871
default: {
2872
return false;
2873
}
2874
}
2875
}
2876
2877
return false;
2878
}
2879
2880
static bool _guess_expecting_callable(GDScriptParser::CompletionContext &p_context) {
2881
if (p_context.call.call != nullptr && p_context.call.call->type == GDScriptParser::Node::CALL) {
2882
GDScriptParser::CallNode *call_node = static_cast<GDScriptParser::CallNode *>(p_context.call.call);
2883
GDScriptCompletionIdentifier ci;
2884
if (_guess_expression_type(p_context, call_node->callee, ci)) {
2885
if (ci.type.kind == GDScriptParser::DataType::BUILTIN && ci.type.builtin_type == Variant::CALLABLE) {
2886
if (p_context.call.argument >= 0 && p_context.call.argument < ci.type.method_info.arguments.size()) {
2887
return ci.type.method_info.arguments.get(p_context.call.argument).type == Variant::CALLABLE;
2888
}
2889
}
2890
}
2891
}
2892
2893
return false;
2894
}
2895
2896
static void _find_enumeration_candidates(GDScriptParser::CompletionContext &p_context, const String &p_enum_hint, HashMap<String, ScriptLanguage::CodeCompletionOption> &r_result) {
2897
if (!p_enum_hint.contains_char('.')) {
2898
// Global constant or in the current class.
2899
StringName current_enum = p_enum_hint;
2900
if (p_context.current_class && p_context.current_class->has_member(current_enum) && p_context.current_class->get_member(current_enum).type == GDScriptParser::ClassNode::Member::ENUM) {
2901
const GDScriptParser::EnumNode *_enum = p_context.current_class->get_member(current_enum).m_enum;
2902
for (int i = 0; i < _enum->values.size(); i++) {
2903
ScriptLanguage::CodeCompletionOption option(_enum->values[i].identifier->name, ScriptLanguage::CODE_COMPLETION_KIND_ENUM);
2904
r_result.insert(option.display, option);
2905
}
2906
} else {
2907
for (int i = 0; i < CoreConstants::get_global_constant_count(); i++) {
2908
if (CoreConstants::get_global_constant_enum(i) == current_enum) {
2909
ScriptLanguage::CodeCompletionOption option(CoreConstants::get_global_constant_name(i), ScriptLanguage::CODE_COMPLETION_KIND_ENUM);
2910
r_result.insert(option.display, option);
2911
}
2912
}
2913
}
2914
} else {
2915
String class_name = p_enum_hint.get_slicec('.', 0);
2916
String enum_name = p_enum_hint.get_slicec('.', 1);
2917
2918
if (!ClassDB::class_exists(class_name)) {
2919
return;
2920
}
2921
2922
List<StringName> enum_constants;
2923
ClassDB::get_enum_constants(class_name, enum_name, &enum_constants);
2924
for (const StringName &E : enum_constants) {
2925
String candidate = class_name + "." + E;
2926
int location = _get_enum_constant_location(class_name, E);
2927
ScriptLanguage::CodeCompletionOption option(candidate, ScriptLanguage::CODE_COMPLETION_KIND_ENUM, location);
2928
r_result.insert(option.display, option);
2929
}
2930
}
2931
}
2932
2933
static void _list_call_arguments(GDScriptParser::CompletionContext &p_context, const GDScriptCompletionIdentifier &p_base, const GDScriptParser::CallNode *p_call, int p_argidx, bool p_static, HashMap<String, ScriptLanguage::CodeCompletionOption> &r_result, String &r_arghint) {
2934
Variant base = p_base.value;
2935
GDScriptParser::DataType base_type = p_base.type;
2936
const StringName &method = p_call->function_name;
2937
2938
const String quote_style = EDITOR_GET("text_editor/completion/use_single_quotes") ? "'" : "\"";
2939
const bool use_string_names = EDITOR_GET("text_editor/completion/add_string_name_literals");
2940
const bool use_node_paths = EDITOR_GET("text_editor/completion/add_node_path_literals");
2941
2942
while (base_type.is_set() && !base_type.is_variant()) {
2943
switch (base_type.kind) {
2944
case GDScriptParser::DataType::CLASS: {
2945
if (base_type.is_meta_type && method == SNAME("new")) {
2946
const GDScriptParser::ClassNode *current = base_type.class_type;
2947
2948
do {
2949
if (current->has_member("_init")) {
2950
const GDScriptParser::ClassNode::Member &member = current->get_member("_init");
2951
2952
if (member.type == GDScriptParser::ClassNode::Member::FUNCTION) {
2953
r_arghint = base_type.class_type->get_datatype().to_string() + " new" + _make_arguments_hint(member.function, p_argidx, true);
2954
return;
2955
}
2956
}
2957
current = current->base_type.class_type;
2958
} while (current != nullptr);
2959
2960
r_arghint = base_type.class_type->get_datatype().to_string() + " new()";
2961
return;
2962
}
2963
2964
if (base_type.class_type->has_member(method)) {
2965
const GDScriptParser::ClassNode::Member &member = base_type.class_type->get_member(method);
2966
2967
if (member.type == GDScriptParser::ClassNode::Member::FUNCTION) {
2968
r_arghint = _make_arguments_hint(member.function, p_argidx);
2969
return;
2970
}
2971
}
2972
2973
base_type = base_type.class_type->base_type;
2974
} break;
2975
case GDScriptParser::DataType::SCRIPT: {
2976
if (base_type.script_type->is_valid() && base_type.script_type->has_method(method)) {
2977
r_arghint = _make_arguments_hint(base_type.script_type->get_method_info(method), p_argidx);
2978
return;
2979
}
2980
Ref<Script> base_script = base_type.script_type->get_base_script();
2981
if (base_script.is_valid()) {
2982
base_type.script_type = base_script;
2983
} else {
2984
base_type.kind = GDScriptParser::DataType::NATIVE;
2985
base_type.builtin_type = Variant::OBJECT;
2986
base_type.native_type = base_type.script_type->get_instance_base_type();
2987
}
2988
} break;
2989
case GDScriptParser::DataType::NATIVE: {
2990
StringName class_name = base_type.native_type;
2991
if (!ClassDB::class_exists(class_name)) {
2992
base_type.kind = GDScriptParser::DataType::UNRESOLVED;
2993
break;
2994
}
2995
2996
MethodInfo info;
2997
int method_args = 0;
2998
2999
if (ClassDB::get_method_info(class_name, method, &info)) {
3000
method_args = info.arguments.size();
3001
if (base.get_type() == Variant::OBJECT) {
3002
Object *obj = base.operator Object *();
3003
if (obj) {
3004
List<String> options;
3005
obj->get_argument_options(method, p_argidx, &options);
3006
for (String &opt : options) {
3007
// Handle user preference.
3008
if (opt.is_quoted()) {
3009
opt = opt.unquote().quote(quote_style);
3010
if (use_string_names && info.arguments[p_argidx].type == Variant::STRING_NAME) {
3011
if (p_call->arguments.size() > p_argidx && p_call->arguments[p_argidx] && p_call->arguments[p_argidx]->type == GDScriptParser::Node::LITERAL) {
3012
GDScriptParser::LiteralNode *literal = static_cast<GDScriptParser::LiteralNode *>(p_call->arguments[p_argidx]);
3013
if (literal->value.get_type() == Variant::STRING) {
3014
opt = "&" + opt;
3015
}
3016
} else {
3017
opt = "&" + opt;
3018
}
3019
} else if (use_node_paths && info.arguments[p_argidx].type == Variant::NODE_PATH) {
3020
if (p_call->arguments.size() > p_argidx && p_call->arguments[p_argidx] && p_call->arguments[p_argidx]->type == GDScriptParser::Node::LITERAL) {
3021
GDScriptParser::LiteralNode *literal = static_cast<GDScriptParser::LiteralNode *>(p_call->arguments[p_argidx]);
3022
if (literal->value.get_type() == Variant::STRING) {
3023
opt = "^" + opt;
3024
}
3025
} else {
3026
opt = "^" + opt;
3027
}
3028
}
3029
}
3030
ScriptLanguage::CodeCompletionOption option(opt, ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION);
3031
r_result.insert(option.display, option);
3032
}
3033
}
3034
}
3035
3036
if (p_argidx < method_args) {
3037
const PropertyInfo &arg_info = info.arguments[p_argidx];
3038
if (arg_info.usage & (PROPERTY_USAGE_CLASS_IS_ENUM | PROPERTY_USAGE_CLASS_IS_BITFIELD)) {
3039
_find_enumeration_candidates(p_context, arg_info.class_name, r_result);
3040
}
3041
}
3042
3043
r_arghint = _make_arguments_hint(info, p_argidx);
3044
}
3045
3046
if (p_argidx == 1 && p_call && ClassDB::is_parent_class(class_name, SNAME("Tween")) && method == SNAME("tween_property")) {
3047
// Get tweened objects properties.
3048
if (p_call->arguments.is_empty()) {
3049
base_type.kind = GDScriptParser::DataType::UNRESOLVED;
3050
break;
3051
}
3052
GDScriptParser::ExpressionNode *tweened_object = p_call->arguments[0];
3053
if (!tweened_object) {
3054
base_type.kind = GDScriptParser::DataType::UNRESOLVED;
3055
break;
3056
}
3057
StringName native_type = tweened_object->datatype.native_type;
3058
switch (tweened_object->datatype.kind) {
3059
case GDScriptParser::DataType::SCRIPT: {
3060
Ref<Script> script = tweened_object->datatype.script_type;
3061
native_type = script->get_instance_base_type();
3062
int n = 0;
3063
while (script.is_valid()) {
3064
List<PropertyInfo> properties;
3065
script->get_script_property_list(&properties);
3066
for (const PropertyInfo &E : properties) {
3067
if (E.usage & (PROPERTY_USAGE_SUBGROUP | PROPERTY_USAGE_GROUP | PROPERTY_USAGE_CATEGORY | PROPERTY_USAGE_INTERNAL)) {
3068
continue;
3069
}
3070
String name = E.name.quote(quote_style);
3071
if (use_node_paths) {
3072
if (p_call->arguments.size() > p_argidx && p_call->arguments[p_argidx] && p_call->arguments[p_argidx]->type == GDScriptParser::Node::LITERAL) {
3073
GDScriptParser::LiteralNode *literal = static_cast<GDScriptParser::LiteralNode *>(p_call->arguments[p_argidx]);
3074
if (literal->value.get_type() == Variant::STRING) {
3075
name = "^" + name;
3076
}
3077
} else {
3078
name = "^" + name;
3079
}
3080
}
3081
ScriptLanguage::CodeCompletionOption option(name, ScriptLanguage::CODE_COMPLETION_KIND_MEMBER, ScriptLanguage::CodeCompletionLocation::LOCATION_LOCAL + n);
3082
r_result.insert(option.display, option);
3083
}
3084
script = script->get_base_script();
3085
n++;
3086
}
3087
} break;
3088
case GDScriptParser::DataType::CLASS: {
3089
GDScriptParser::ClassNode *clss = tweened_object->datatype.class_type;
3090
native_type = clss->base_type.native_type;
3091
int n = 0;
3092
while (clss) {
3093
for (GDScriptParser::ClassNode::Member member : clss->members) {
3094
if (member.type == GDScriptParser::ClassNode::Member::VARIABLE) {
3095
String name = member.get_name().quote(quote_style);
3096
if (use_node_paths) {
3097
if (p_call->arguments.size() > p_argidx && p_call->arguments[p_argidx] && p_call->arguments[p_argidx]->type == GDScriptParser::Node::LITERAL) {
3098
GDScriptParser::LiteralNode *literal = static_cast<GDScriptParser::LiteralNode *>(p_call->arguments[p_argidx]);
3099
if (literal->value.get_type() == Variant::STRING) {
3100
name = "^" + name;
3101
}
3102
} else {
3103
name = "^" + name;
3104
}
3105
}
3106
ScriptLanguage::CodeCompletionOption option(name, ScriptLanguage::CODE_COMPLETION_KIND_MEMBER, ScriptLanguage::CodeCompletionLocation::LOCATION_LOCAL + n);
3107
r_result.insert(option.display, option);
3108
}
3109
}
3110
if (clss->base_type.kind == GDScriptParser::DataType::Kind::CLASS) {
3111
clss = clss->base_type.class_type;
3112
n++;
3113
} else {
3114
native_type = clss->base_type.native_type;
3115
clss = nullptr;
3116
}
3117
}
3118
} break;
3119
default:
3120
break;
3121
}
3122
3123
List<PropertyInfo> properties;
3124
ClassDB::get_property_list(native_type, &properties);
3125
for (const PropertyInfo &E : properties) {
3126
if (E.usage & (PROPERTY_USAGE_SUBGROUP | PROPERTY_USAGE_GROUP | PROPERTY_USAGE_CATEGORY | PROPERTY_USAGE_INTERNAL)) {
3127
continue;
3128
}
3129
String name = E.name.quote(quote_style);
3130
if (use_node_paths) {
3131
if (p_call->arguments.size() > p_argidx && p_call->arguments[p_argidx] && p_call->arguments[p_argidx]->type == GDScriptParser::Node::LITERAL) {
3132
GDScriptParser::LiteralNode *literal = static_cast<GDScriptParser::LiteralNode *>(p_call->arguments[p_argidx]);
3133
if (literal->value.get_type() == Variant::STRING) {
3134
name = "^" + name;
3135
}
3136
} else {
3137
name = "^" + name;
3138
}
3139
}
3140
ScriptLanguage::CodeCompletionOption option(name, ScriptLanguage::CODE_COMPLETION_KIND_MEMBER);
3141
r_result.insert(option.display, option);
3142
}
3143
}
3144
3145
if (p_argidx == 0 && ClassDB::is_parent_class(class_name, SNAME("Node")) && (method == SNAME("get_node") || method == SNAME("has_node"))) {
3146
// Get autoloads
3147
List<PropertyInfo> props;
3148
ProjectSettings::get_singleton()->get_property_list(&props);
3149
3150
for (const PropertyInfo &E : props) {
3151
String s = E.name;
3152
if (!s.begins_with("autoload/")) {
3153
continue;
3154
}
3155
String name = s.get_slicec('/', 1);
3156
String path = ("/root/" + name).quote(quote_style);
3157
if (use_node_paths) {
3158
if (p_call->arguments.size() > p_argidx && p_call->arguments[p_argidx] && p_call->arguments[p_argidx]->type == GDScriptParser::Node::LITERAL) {
3159
GDScriptParser::LiteralNode *literal = static_cast<GDScriptParser::LiteralNode *>(p_call->arguments[p_argidx]);
3160
if (literal->value.get_type() == Variant::STRING) {
3161
path = "^" + path;
3162
}
3163
} else {
3164
path = "^" + path;
3165
}
3166
}
3167
ScriptLanguage::CodeCompletionOption option(path, ScriptLanguage::CODE_COMPLETION_KIND_NODE_PATH);
3168
r_result.insert(option.display, option);
3169
}
3170
}
3171
3172
if (p_argidx == 0 && method_args > 0 && ClassDB::is_parent_class(class_name, SNAME("InputEvent")) && method.operator String().contains("action")) {
3173
// Get input actions
3174
List<PropertyInfo> props;
3175
ProjectSettings::get_singleton()->get_property_list(&props);
3176
for (const PropertyInfo &E : props) {
3177
String s = E.name;
3178
if (!s.begins_with("input/")) {
3179
continue;
3180
}
3181
String name = s.get_slicec('/', 1).quote(quote_style);
3182
if (use_string_names) {
3183
if (p_call->arguments.size() > p_argidx && p_call->arguments[p_argidx] && p_call->arguments[p_argidx]->type == GDScriptParser::Node::LITERAL) {
3184
GDScriptParser::LiteralNode *literal = static_cast<GDScriptParser::LiteralNode *>(p_call->arguments[p_argidx]);
3185
if (literal->value.get_type() == Variant::STRING) {
3186
name = "&" + name;
3187
}
3188
} else {
3189
name = "&" + name;
3190
}
3191
}
3192
ScriptLanguage::CodeCompletionOption option(name, ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT);
3193
r_result.insert(option.display, option);
3194
}
3195
}
3196
if (EDITOR_GET("text_editor/completion/complete_file_paths")) {
3197
if (p_argidx == 0 && method == SNAME("change_scene_to_file") && ClassDB::is_parent_class(class_name, SNAME("SceneTree"))) {
3198
HashMap<String, ScriptLanguage::CodeCompletionOption> list;
3199
_get_directory_contents(EditorFileSystem::get_singleton()->get_filesystem(), list, SNAME("PackedScene"));
3200
for (const KeyValue<String, ScriptLanguage::CodeCompletionOption> &key_value_pair : list) {
3201
ScriptLanguage::CodeCompletionOption option = key_value_pair.value;
3202
r_result.insert(option.display, option);
3203
}
3204
}
3205
}
3206
3207
base_type.kind = GDScriptParser::DataType::UNRESOLVED;
3208
} break;
3209
case GDScriptParser::DataType::BUILTIN: {
3210
if (base.get_type() == Variant::NIL) {
3211
Callable::CallError err;
3212
Variant::construct(base_type.builtin_type, base, nullptr, 0, err);
3213
if (err.error != Callable::CallError::CALL_OK) {
3214
return;
3215
}
3216
}
3217
3218
List<MethodInfo> methods;
3219
base.get_method_list(&methods);
3220
for (const MethodInfo &E : methods) {
3221
if (E.name == method) {
3222
r_arghint = _make_arguments_hint(E, p_argidx);
3223
return;
3224
}
3225
}
3226
3227
base_type.kind = GDScriptParser::DataType::UNRESOLVED;
3228
} break;
3229
default: {
3230
base_type.kind = GDScriptParser::DataType::UNRESOLVED;
3231
} break;
3232
}
3233
}
3234
}
3235
3236
static bool _get_subscript_type(GDScriptParser::CompletionContext &p_context, const GDScriptParser::SubscriptNode *p_subscript, GDScriptParser::DataType &r_base_type, Variant *r_base = nullptr) {
3237
if (p_context.base == nullptr) {
3238
return false;
3239
}
3240
3241
const GDScriptParser::GetNodeNode *get_node = nullptr;
3242
3243
switch (p_subscript->base->type) {
3244
case GDScriptParser::Node::GET_NODE: {
3245
get_node = static_cast<GDScriptParser::GetNodeNode *>(p_subscript->base);
3246
} break;
3247
3248
case GDScriptParser::Node::IDENTIFIER: {
3249
const GDScriptParser::IdentifierNode *identifier_node = static_cast<GDScriptParser::IdentifierNode *>(p_subscript->base);
3250
3251
switch (identifier_node->source) {
3252
case GDScriptParser::IdentifierNode::Source::MEMBER_VARIABLE: {
3253
if (p_context.current_class != nullptr) {
3254
const StringName &member_name = identifier_node->name;
3255
const GDScriptParser::ClassNode *current_class = p_context.current_class;
3256
3257
if (current_class->has_member(member_name)) {
3258
const GDScriptParser::ClassNode::Member &member = current_class->get_member(member_name);
3259
3260
if (member.type == GDScriptParser::ClassNode::Member::VARIABLE) {
3261
const GDScriptParser::VariableNode *variable = static_cast<GDScriptParser::VariableNode *>(member.variable);
3262
3263
if (variable->initializer && variable->initializer->type == GDScriptParser::Node::GET_NODE) {
3264
get_node = static_cast<GDScriptParser::GetNodeNode *>(variable->initializer);
3265
}
3266
}
3267
}
3268
}
3269
} break;
3270
case GDScriptParser::IdentifierNode::Source::LOCAL_VARIABLE: {
3271
// TODO: Do basic assignment flow analysis like in `_guess_expression_type`.
3272
const GDScriptParser::SuiteNode::Local local = identifier_node->suite->get_local(identifier_node->name);
3273
switch (local.type) {
3274
case GDScriptParser::SuiteNode::Local::CONSTANT: {
3275
if (local.constant->initializer && local.constant->initializer->type == GDScriptParser::Node::GET_NODE) {
3276
get_node = static_cast<GDScriptParser::GetNodeNode *>(local.constant->initializer);
3277
}
3278
} break;
3279
case GDScriptParser::SuiteNode::Local::VARIABLE: {
3280
if (local.variable->initializer && local.variable->initializer->type == GDScriptParser::Node::GET_NODE) {
3281
get_node = static_cast<GDScriptParser::GetNodeNode *>(local.variable->initializer);
3282
}
3283
} break;
3284
default: {
3285
} break;
3286
}
3287
} break;
3288
default: {
3289
} break;
3290
}
3291
} break;
3292
default: {
3293
} break;
3294
}
3295
3296
if (get_node != nullptr) {
3297
const Object *node = p_context.base->call("get_node_or_null", NodePath(get_node->full_path));
3298
if (node != nullptr) {
3299
GDScriptParser::DataType assigned_type = _type_from_variant(node, p_context).type;
3300
GDScriptParser::DataType base_type = p_subscript->base->datatype;
3301
3302
if (p_subscript->base->type == GDScriptParser::Node::IDENTIFIER && base_type.type_source == GDScriptParser::DataType::ANNOTATED_EXPLICIT && (assigned_type.kind != base_type.kind || assigned_type.script_path != base_type.script_path || assigned_type.native_type != base_type.native_type)) {
3303
// Annotated type takes precedence.
3304
return false;
3305
}
3306
3307
if (r_base != nullptr) {
3308
*r_base = node;
3309
}
3310
3311
r_base_type.type_source = GDScriptParser::DataType::INFERRED;
3312
r_base_type.builtin_type = Variant::OBJECT;
3313
r_base_type.native_type = node->get_class_name();
3314
3315
Ref<Script> scr = node->get_script();
3316
if (scr.is_null()) {
3317
r_base_type.kind = GDScriptParser::DataType::NATIVE;
3318
} else {
3319
r_base_type.kind = GDScriptParser::DataType::SCRIPT;
3320
r_base_type.script_type = scr;
3321
}
3322
3323
return true;
3324
}
3325
}
3326
3327
return false;
3328
}
3329
3330
static void _find_call_arguments(GDScriptParser::CompletionContext &p_context, const GDScriptParser::Node *p_call, int p_argidx, HashMap<String, ScriptLanguage::CodeCompletionOption> &r_result, bool &r_forced, String &r_arghint) {
3331
if (p_call->type == GDScriptParser::Node::PRELOAD) {
3332
if (p_argidx == 0 && bool(EDITOR_GET("text_editor/completion/complete_file_paths"))) {
3333
_get_directory_contents(EditorFileSystem::get_singleton()->get_filesystem(), r_result);
3334
}
3335
3336
MethodInfo mi(PropertyInfo(Variant::OBJECT, "resource", PROPERTY_HINT_RESOURCE_TYPE, "Resource"), "preload", PropertyInfo(Variant::STRING, "path"));
3337
r_arghint = _make_arguments_hint(mi, p_argidx);
3338
return;
3339
} else if (p_call->type != GDScriptParser::Node::CALL) {
3340
return;
3341
}
3342
3343
Variant base;
3344
GDScriptParser::DataType base_type;
3345
bool _static = false;
3346
const GDScriptParser::CallNode *call = static_cast<const GDScriptParser::CallNode *>(p_call);
3347
GDScriptParser::Node::Type callee_type = call->get_callee_type();
3348
3349
if (callee_type == GDScriptParser::Node::SUBSCRIPT) {
3350
const GDScriptParser::SubscriptNode *subscript = static_cast<const GDScriptParser::SubscriptNode *>(call->callee);
3351
3352
if (subscript->base != nullptr && subscript->base->type == GDScriptParser::Node::IDENTIFIER) {
3353
const GDScriptParser::IdentifierNode *base_identifier = static_cast<const GDScriptParser::IdentifierNode *>(subscript->base);
3354
3355
Variant::Type method_type = GDScriptParser::get_builtin_type(base_identifier->name);
3356
if (method_type < Variant::VARIANT_MAX) {
3357
Variant v;
3358
Callable::CallError err;
3359
Variant::construct(method_type, v, nullptr, 0, err);
3360
if (err.error != Callable::CallError::CALL_OK) {
3361
return;
3362
}
3363
List<MethodInfo> methods;
3364
v.get_method_list(&methods);
3365
3366
for (MethodInfo &E : methods) {
3367
if (p_argidx >= E.arguments.size()) {
3368
continue;
3369
}
3370
if (E.name == call->function_name) {
3371
r_arghint += _make_arguments_hint(E, p_argidx);
3372
return;
3373
}
3374
}
3375
}
3376
}
3377
3378
if (subscript->is_attribute) {
3379
bool found_type = _get_subscript_type(p_context, subscript, base_type, &base);
3380
3381
if (!found_type) {
3382
GDScriptCompletionIdentifier ci;
3383
if (_guess_expression_type(p_context, subscript->base, ci)) {
3384
base_type = ci.type;
3385
base = ci.value;
3386
} else {
3387
return;
3388
}
3389
}
3390
3391
_static = base_type.is_meta_type;
3392
}
3393
} else if (Variant::has_utility_function(call->function_name)) {
3394
MethodInfo info = Variant::get_utility_function_info(call->function_name);
3395
r_arghint = _make_arguments_hint(info, p_argidx);
3396
return;
3397
} else if (GDScriptUtilityFunctions::function_exists(call->function_name)) {
3398
MethodInfo info = GDScriptUtilityFunctions::get_function_info(call->function_name);
3399
r_arghint = _make_arguments_hint(info, p_argidx);
3400
return;
3401
} else if (GDScriptParser::get_builtin_type(call->function_name) < Variant::VARIANT_MAX) {
3402
// Complete constructor.
3403
List<MethodInfo> constructors;
3404
Variant::get_constructor_list(GDScriptParser::get_builtin_type(call->function_name), &constructors);
3405
3406
int i = 0;
3407
for (const MethodInfo &E : constructors) {
3408
if (p_argidx >= E.arguments.size()) {
3409
continue;
3410
}
3411
if (i > 0) {
3412
r_arghint += "\n";
3413
}
3414
r_arghint += _make_arguments_hint(E, p_argidx);
3415
i++;
3416
}
3417
return;
3418
} else if (call->is_super || callee_type == GDScriptParser::Node::IDENTIFIER) {
3419
base = p_context.base;
3420
3421
if (p_context.current_class) {
3422
base_type = p_context.current_class->get_datatype();
3423
_static = !p_context.current_function || p_context.current_function->is_static;
3424
}
3425
} else {
3426
return;
3427
}
3428
3429
GDScriptCompletionIdentifier ci;
3430
ci.type = base_type;
3431
ci.value = base;
3432
_list_call_arguments(p_context, ci, call, p_argidx, _static, r_result, r_arghint);
3433
3434
r_forced = r_result.size() > 0;
3435
}
3436
3437
::Error GDScriptLanguage::complete_code(const String &p_code, const String &p_path, Object *p_owner, List<ScriptLanguage::CodeCompletionOption> *r_options, bool &r_forced, String &r_call_hint) {
3438
const String quote_style = EDITOR_GET("text_editor/completion/use_single_quotes") ? "'" : "\"";
3439
3440
GDScriptParser parser;
3441
GDScriptAnalyzer analyzer(&parser);
3442
3443
parser.parse(p_code, p_path, true);
3444
analyzer.analyze();
3445
3446
r_forced = false;
3447
HashMap<String, ScriptLanguage::CodeCompletionOption> options;
3448
3449
GDScriptParser::CompletionContext completion_context = parser.get_completion_context();
3450
if (completion_context.current_class != nullptr && completion_context.current_class->outer == nullptr) {
3451
completion_context.base = p_owner;
3452
}
3453
bool is_function = false;
3454
3455
switch (completion_context.type) {
3456
case GDScriptParser::COMPLETION_NONE:
3457
break;
3458
case GDScriptParser::COMPLETION_ANNOTATION: {
3459
List<MethodInfo> annotations;
3460
parser.get_annotation_list(&annotations);
3461
for (const MethodInfo &E : annotations) {
3462
ScriptLanguage::CodeCompletionOption option(E.name.substr(1), ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
3463
if (E.arguments.size() > 0) {
3464
option.insert_text += "(";
3465
}
3466
options.insert(option.display, option);
3467
}
3468
r_forced = true;
3469
} break;
3470
case GDScriptParser::COMPLETION_ANNOTATION_ARGUMENTS: {
3471
if (completion_context.node == nullptr || completion_context.node->type != GDScriptParser::Node::ANNOTATION) {
3472
break;
3473
}
3474
const GDScriptParser::AnnotationNode *annotation = static_cast<const GDScriptParser::AnnotationNode *>(completion_context.node);
3475
_find_annotation_arguments(annotation, completion_context.current_argument, quote_style, options, r_call_hint);
3476
r_forced = true;
3477
} break;
3478
case GDScriptParser::COMPLETION_BUILT_IN_TYPE_CONSTANT_OR_STATIC_METHOD: {
3479
// Constants.
3480
{
3481
List<StringName> constants;
3482
Variant::get_constants_for_type(completion_context.builtin_type, &constants);
3483
for (const StringName &E : constants) {
3484
ScriptLanguage::CodeCompletionOption option(E, ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT);
3485
bool valid = false;
3486
Variant default_value = Variant::get_constant_value(completion_context.builtin_type, E, &valid);
3487
if (valid) {
3488
option.default_value = default_value;
3489
}
3490
options.insert(option.display, option);
3491
}
3492
}
3493
// Methods.
3494
{
3495
List<StringName> methods;
3496
Variant::get_builtin_method_list(completion_context.builtin_type, &methods);
3497
for (const StringName &E : methods) {
3498
if (Variant::is_builtin_method_static(completion_context.builtin_type, E)) {
3499
ScriptLanguage::CodeCompletionOption option(E, ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION);
3500
if (!_guess_expecting_callable(completion_context)) {
3501
if (Variant::get_builtin_method_argument_count(completion_context.builtin_type, E) > 0 || Variant::is_builtin_method_vararg(completion_context.builtin_type, E)) {
3502
option.insert_text += "(";
3503
} else {
3504
option.insert_text += "()";
3505
}
3506
}
3507
options.insert(option.display, option);
3508
}
3509
}
3510
}
3511
} break;
3512
case GDScriptParser::COMPLETION_INHERIT_TYPE: {
3513
_list_available_types(true, completion_context, options);
3514
r_forced = true;
3515
} break;
3516
case GDScriptParser::COMPLETION_TYPE_NAME_OR_VOID: {
3517
ScriptLanguage::CodeCompletionOption option("void", ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
3518
options.insert(option.display, option);
3519
}
3520
[[fallthrough]];
3521
case GDScriptParser::COMPLETION_TYPE_NAME: {
3522
_list_available_types(false, completion_context, options);
3523
r_forced = true;
3524
} break;
3525
case GDScriptParser::COMPLETION_PROPERTY_DECLARATION_OR_TYPE: {
3526
_list_available_types(false, completion_context, options);
3527
ScriptLanguage::CodeCompletionOption get("get", ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
3528
options.insert(get.display, get);
3529
ScriptLanguage::CodeCompletionOption set("set", ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
3530
options.insert(set.display, set);
3531
r_forced = true;
3532
} break;
3533
case GDScriptParser::COMPLETION_PROPERTY_DECLARATION: {
3534
ScriptLanguage::CodeCompletionOption get("get", ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
3535
options.insert(get.display, get);
3536
ScriptLanguage::CodeCompletionOption set("set", ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
3537
options.insert(set.display, set);
3538
r_forced = true;
3539
} break;
3540
case GDScriptParser::COMPLETION_PROPERTY_METHOD: {
3541
if (!completion_context.current_class) {
3542
break;
3543
}
3544
for (int i = 0; i < completion_context.current_class->members.size(); i++) {
3545
const GDScriptParser::ClassNode::Member &member = completion_context.current_class->members[i];
3546
if (member.type != GDScriptParser::ClassNode::Member::FUNCTION) {
3547
continue;
3548
}
3549
if (member.function->is_static) {
3550
continue;
3551
}
3552
ScriptLanguage::CodeCompletionOption option(member.function->identifier->name, ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION);
3553
options.insert(option.display, option);
3554
}
3555
r_forced = true;
3556
} break;
3557
case GDScriptParser::COMPLETION_ASSIGN: {
3558
GDScriptCompletionIdentifier type;
3559
if (!completion_context.node || completion_context.node->type != GDScriptParser::Node::ASSIGNMENT) {
3560
break;
3561
}
3562
if (!_guess_expression_type(completion_context, static_cast<const GDScriptParser::AssignmentNode *>(completion_context.node)->assignee, type)) {
3563
_find_identifiers(completion_context, false, true, options, 0);
3564
r_forced = true;
3565
break;
3566
}
3567
3568
if (!type.enumeration.is_empty()) {
3569
_find_enumeration_candidates(completion_context, type.enumeration, options);
3570
r_forced = options.size() > 0;
3571
} else {
3572
_find_identifiers(completion_context, false, true, options, 0);
3573
r_forced = true;
3574
}
3575
} break;
3576
case GDScriptParser::COMPLETION_METHOD:
3577
is_function = true;
3578
[[fallthrough]];
3579
case GDScriptParser::COMPLETION_IDENTIFIER: {
3580
_find_identifiers(completion_context, is_function, !_guess_expecting_callable(completion_context), options, 0);
3581
} break;
3582
case GDScriptParser::COMPLETION_ATTRIBUTE_METHOD:
3583
is_function = true;
3584
[[fallthrough]];
3585
case GDScriptParser::COMPLETION_ATTRIBUTE: {
3586
r_forced = true;
3587
const GDScriptParser::SubscriptNode *attr = static_cast<const GDScriptParser::SubscriptNode *>(completion_context.node);
3588
if (attr->base) {
3589
GDScriptCompletionIdentifier base;
3590
bool found_type = _get_subscript_type(completion_context, attr, base.type);
3591
if (!found_type && !_guess_expression_type(completion_context, attr->base, base)) {
3592
break;
3593
}
3594
3595
_find_identifiers_in_base(base, is_function, false, !_guess_expecting_callable(completion_context), options, 0);
3596
}
3597
} break;
3598
case GDScriptParser::COMPLETION_SUBSCRIPT: {
3599
const GDScriptParser::SubscriptNode *subscript = static_cast<const GDScriptParser::SubscriptNode *>(completion_context.node);
3600
GDScriptCompletionIdentifier base;
3601
const bool res = _guess_expression_type(completion_context, subscript->base, base);
3602
3603
// If the type is not known, we assume it is BUILTIN, since indices on arrays is the most common use case.
3604
if (!subscript->is_attribute && (!res || base.type.kind == GDScriptParser::DataType::BUILTIN || base.type.is_variant())) {
3605
if (base.value.get_type() == Variant::DICTIONARY) {
3606
List<PropertyInfo> members;
3607
base.value.get_property_list(&members);
3608
3609
for (const PropertyInfo &E : members) {
3610
ScriptLanguage::CodeCompletionOption option(E.name.quote(quote_style), ScriptLanguage::CODE_COMPLETION_KIND_MEMBER, ScriptLanguage::LOCATION_LOCAL);
3611
options.insert(option.display, option);
3612
}
3613
}
3614
if (!subscript->index || subscript->index->type != GDScriptParser::Node::LITERAL) {
3615
_find_identifiers(completion_context, false, !_guess_expecting_callable(completion_context), options, 0);
3616
}
3617
} else if (res) {
3618
if (!subscript->is_attribute) {
3619
// Quote the options if they are not accessed as attribute.
3620
3621
HashMap<String, ScriptLanguage::CodeCompletionOption> opt;
3622
_find_identifiers_in_base(base, false, false, false, opt, 0);
3623
for (const KeyValue<String, CodeCompletionOption> &E : opt) {
3624
ScriptLanguage::CodeCompletionOption option(E.value.insert_text.quote(quote_style), E.value.kind, E.value.location);
3625
options.insert(option.display, option);
3626
}
3627
} else {
3628
_find_identifiers_in_base(base, false, false, !_guess_expecting_callable(completion_context), options, 0);
3629
}
3630
}
3631
} break;
3632
case GDScriptParser::COMPLETION_TYPE_ATTRIBUTE: {
3633
if (!completion_context.current_class) {
3634
break;
3635
}
3636
3637
const GDScriptParser::TypeNode *type = static_cast<const GDScriptParser::TypeNode *>(completion_context.node);
3638
ERR_FAIL_INDEX_V_MSG(completion_context.type_chain_index - 1, type->type_chain.size(), Error::ERR_BUG, "Could not complete type argument with out of bounds type chain index.");
3639
3640
GDScriptCompletionIdentifier base;
3641
3642
if (_guess_identifier_type(completion_context, type->type_chain[0], base)) {
3643
bool found = true;
3644
for (int i = 1; i < completion_context.type_chain_index; i++) {
3645
GDScriptCompletionIdentifier ci;
3646
found = _guess_identifier_type_from_base(completion_context, base, type->type_chain[i]->name, ci);
3647
base = ci;
3648
if (!found) {
3649
break;
3650
}
3651
}
3652
if (found) {
3653
_find_identifiers_in_base(base, false, true, true, options, 0);
3654
}
3655
}
3656
3657
r_forced = true;
3658
} break;
3659
case GDScriptParser::COMPLETION_RESOURCE_PATH: {
3660
if (EDITOR_GET("text_editor/completion/complete_file_paths")) {
3661
_get_directory_contents(EditorFileSystem::get_singleton()->get_filesystem(), options);
3662
r_forced = true;
3663
}
3664
} break;
3665
case GDScriptParser::COMPLETION_CALL_ARGUMENTS: {
3666
if (!completion_context.node) {
3667
break;
3668
}
3669
_find_call_arguments(completion_context, completion_context.node, completion_context.current_argument, options, r_forced, r_call_hint);
3670
} break;
3671
case GDScriptParser::COMPLETION_OVERRIDE_METHOD: {
3672
GDScriptParser::DataType native_type = completion_context.current_class->base_type;
3673
GDScriptParser::FunctionNode *function_node = static_cast<GDScriptParser::FunctionNode *>(completion_context.node);
3674
bool is_static = function_node != nullptr && function_node->is_static;
3675
while (native_type.is_set() && native_type.kind != GDScriptParser::DataType::NATIVE) {
3676
switch (native_type.kind) {
3677
case GDScriptParser::DataType::CLASS: {
3678
for (const GDScriptParser::ClassNode::Member &member : native_type.class_type->members) {
3679
if (member.type != GDScriptParser::ClassNode::Member::FUNCTION) {
3680
continue;
3681
}
3682
3683
if (options.has(member.function->identifier->name)) {
3684
continue;
3685
}
3686
3687
if (completion_context.current_class->has_function(member.get_name()) && completion_context.current_class->get_member(member.get_name()).function != function_node) {
3688
continue;
3689
}
3690
3691
if (is_static != member.function->is_static) {
3692
continue;
3693
}
3694
3695
String display_name = member.function->identifier->name;
3696
display_name += member.function->signature + ":";
3697
ScriptLanguage::CodeCompletionOption option(display_name, ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION);
3698
options.insert(member.function->identifier->name, option); // Insert name instead of display to track duplicates.
3699
}
3700
native_type = native_type.class_type->base_type;
3701
} break;
3702
default: {
3703
native_type.kind = GDScriptParser::DataType::UNRESOLVED;
3704
} break;
3705
}
3706
}
3707
3708
if (!native_type.is_set()) {
3709
break;
3710
}
3711
3712
StringName class_name = native_type.native_type;
3713
if (!ClassDB::class_exists(class_name)) {
3714
break;
3715
}
3716
3717
const bool type_hints = EditorSettings::get_singleton()->get_setting("text_editor/completion/add_type_hints");
3718
3719
List<MethodInfo> virtual_methods;
3720
if (is_static) {
3721
// Not truly a virtual method, but can also be "overridden".
3722
MethodInfo static_init("_static_init");
3723
static_init.return_val.type = Variant::NIL;
3724
static_init.flags |= METHOD_FLAG_STATIC | METHOD_FLAG_VIRTUAL;
3725
virtual_methods.push_back(static_init);
3726
} else {
3727
ClassDB::get_virtual_methods(class_name, &virtual_methods);
3728
}
3729
3730
for (const MethodInfo &mi : virtual_methods) {
3731
if (options.has(mi.name)) {
3732
continue;
3733
}
3734
if (completion_context.current_class->has_function(mi.name) && completion_context.current_class->get_member(mi.name).function != function_node) {
3735
continue;
3736
}
3737
String method_hint = mi.name;
3738
if (method_hint.contains_char(':')) {
3739
method_hint = method_hint.get_slicec(':', 0);
3740
}
3741
method_hint += "(";
3742
3743
for (int64_t i = 0; i < mi.arguments.size(); ++i) {
3744
if (i > 0) {
3745
method_hint += ", ";
3746
}
3747
String arg = mi.arguments[i].name;
3748
if (arg.contains_char(':')) {
3749
arg = arg.substr(0, arg.find_char(':'));
3750
}
3751
method_hint += arg;
3752
if (type_hints) {
3753
method_hint += ": " + _get_visual_datatype(mi.arguments[i], true, class_name);
3754
}
3755
}
3756
if (mi.flags & METHOD_FLAG_VARARG) {
3757
if (!mi.arguments.is_empty()) {
3758
method_hint += ", ";
3759
}
3760
method_hint += "...args"; // `MethodInfo` does not support the rest parameter name.
3761
if (type_hints) {
3762
method_hint += ": Array";
3763
}
3764
}
3765
method_hint += ")";
3766
if (type_hints) {
3767
method_hint += " -> " + _get_visual_datatype(mi.return_val, false, class_name);
3768
}
3769
method_hint += ":";
3770
3771
ScriptLanguage::CodeCompletionOption option(method_hint, ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION);
3772
options.insert(option.display, option);
3773
}
3774
} break;
3775
case GDScriptParser::COMPLETION_GET_NODE: {
3776
// Handles the `$Node/Path` or `$"Some NodePath"` syntax specifically.
3777
if (p_owner) {
3778
List<String> opts;
3779
p_owner->get_argument_options("get_node", 0, &opts);
3780
3781
bool for_unique_name = false;
3782
if (completion_context.node != nullptr && completion_context.node->type == GDScriptParser::Node::GET_NODE && !static_cast<GDScriptParser::GetNodeNode *>(completion_context.node)->use_dollar) {
3783
for_unique_name = true;
3784
}
3785
3786
for (const String &E : opts) {
3787
r_forced = true;
3788
String opt = E.strip_edges();
3789
if (opt.is_quoted()) {
3790
// Remove quotes so that we can handle user preferred quote style,
3791
// or handle NodePaths which are valid identifiers and don't need quotes.
3792
opt = opt.unquote();
3793
}
3794
3795
if (for_unique_name) {
3796
if (!opt.begins_with("%")) {
3797
continue;
3798
}
3799
opt = opt.substr(1);
3800
}
3801
3802
// The path needs quotes if at least one of its components (excluding `%` prefix and `/` separations)
3803
// is not a valid identifier.
3804
bool path_needs_quote = false;
3805
for (const String &part : opt.trim_prefix("%").split("/")) {
3806
if (!part.is_valid_ascii_identifier()) {
3807
path_needs_quote = true;
3808
break;
3809
}
3810
}
3811
3812
if (path_needs_quote) {
3813
// Ignore quote_style and just use double quotes for paths with apostrophes.
3814
// Double quotes don't need to be checked because they're not valid in node and property names.
3815
opt = opt.quote(opt.contains_char('\'') ? "\"" : quote_style); // Handle user preference.
3816
}
3817
ScriptLanguage::CodeCompletionOption option(opt, ScriptLanguage::CODE_COMPLETION_KIND_NODE_PATH);
3818
options.insert(option.display, option);
3819
}
3820
3821
if (!for_unique_name) {
3822
// Get autoloads.
3823
for (const KeyValue<StringName, ProjectSettings::AutoloadInfo> &E : ProjectSettings::get_singleton()->get_autoload_list()) {
3824
String path = "/root/" + E.key;
3825
ScriptLanguage::CodeCompletionOption option(path.quote(quote_style), ScriptLanguage::CODE_COMPLETION_KIND_NODE_PATH);
3826
options.insert(option.display, option);
3827
}
3828
}
3829
}
3830
} break;
3831
case GDScriptParser::COMPLETION_SUPER:
3832
break;
3833
case GDScriptParser::COMPLETION_SUPER_METHOD: {
3834
if (!completion_context.current_class) {
3835
break;
3836
}
3837
_find_identifiers_in_class(completion_context.current_class, true, false, false, true, !_guess_expecting_callable(completion_context), options, 0);
3838
} break;
3839
}
3840
3841
for (const KeyValue<String, ScriptLanguage::CodeCompletionOption> &E : options) {
3842
r_options->push_back(E.value);
3843
}
3844
3845
return OK;
3846
}
3847
3848
#else // !TOOLS_ENABLED
3849
3850
Error GDScriptLanguage::complete_code(const String &p_code, const String &p_path, Object *p_owner, List<ScriptLanguage::CodeCompletionOption> *r_options, bool &r_forced, String &r_call_hint) {
3851
return OK;
3852
}
3853
3854
#endif // TOOLS_ENABLED
3855
3856
//////// END COMPLETION //////////
3857
3858
String GDScriptLanguage::_get_indentation() const {
3859
#ifdef TOOLS_ENABLED
3860
if (Engine::get_singleton()->is_editor_hint()) {
3861
bool use_space_indentation = EDITOR_GET("text_editor/behavior/indent/type");
3862
3863
if (use_space_indentation) {
3864
int indent_size = EDITOR_GET("text_editor/behavior/indent/size");
3865
return String(" ").repeat(indent_size);
3866
}
3867
}
3868
#endif
3869
return "\t";
3870
}
3871
3872
void GDScriptLanguage::auto_indent_code(String &p_code, int p_from_line, int p_to_line) const {
3873
String indent = _get_indentation();
3874
3875
Vector<String> lines = p_code.split("\n");
3876
List<int> indent_stack;
3877
3878
for (int i = 0; i < lines.size(); i++) {
3879
String l = lines[i];
3880
int tc = 0;
3881
for (int j = 0; j < l.length(); j++) {
3882
if (l[j] == ' ' || l[j] == '\t') {
3883
tc++;
3884
} else {
3885
break;
3886
}
3887
}
3888
3889
String st = l.substr(tc).strip_edges();
3890
if (st.is_empty() || st.begins_with("#")) {
3891
continue; //ignore!
3892
}
3893
3894
int ilevel = 0;
3895
if (indent_stack.size()) {
3896
ilevel = indent_stack.back()->get();
3897
}
3898
3899
if (tc > ilevel) {
3900
indent_stack.push_back(tc);
3901
} else if (tc < ilevel) {
3902
while (indent_stack.size() && indent_stack.back()->get() > tc) {
3903
indent_stack.pop_back();
3904
}
3905
3906
if (indent_stack.size() && indent_stack.back()->get() != tc) {
3907
indent_stack.push_back(tc); // this is not right but gets the job done
3908
}
3909
}
3910
3911
if (i >= p_from_line) {
3912
l = indent.repeat(indent_stack.size()) + st;
3913
} else if (i > p_to_line) {
3914
break;
3915
}
3916
3917
lines.write[i] = l;
3918
}
3919
3920
p_code = "";
3921
for (int i = 0; i < lines.size(); i++) {
3922
if (i > 0) {
3923
p_code += "\n";
3924
}
3925
p_code += lines[i];
3926
}
3927
}
3928
3929
#ifdef TOOLS_ENABLED
3930
3931
static Error _lookup_symbol_from_base(const GDScriptParser::DataType &p_base, const String &p_symbol, GDScriptLanguage::LookupResult &r_result) {
3932
GDScriptParser::DataType base_type = p_base;
3933
3934
while (true) {
3935
switch (base_type.kind) {
3936
case GDScriptParser::DataType::CLASS: {
3937
ERR_FAIL_NULL_V(base_type.class_type, ERR_BUG);
3938
3939
String name = p_symbol;
3940
if (name == "new") {
3941
name = "_init";
3942
}
3943
3944
if (!base_type.class_type->has_member(name)) {
3945
base_type = base_type.class_type->base_type;
3946
break;
3947
}
3948
3949
const GDScriptParser::ClassNode::Member &member = base_type.class_type->get_member(name);
3950
3951
switch (member.type) {
3952
case GDScriptParser::ClassNode::Member::UNDEFINED:
3953
case GDScriptParser::ClassNode::Member::GROUP:
3954
return ERR_BUG;
3955
case GDScriptParser::ClassNode::Member::CLASS: {
3956
String doc_type_name;
3957
String doc_enum_name;
3958
GDScriptDocGen::doctype_from_gdtype(GDScriptAnalyzer::type_from_metatype(member.get_datatype()), doc_type_name, doc_enum_name);
3959
3960
r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS;
3961
r_result.class_name = doc_type_name;
3962
} break;
3963
case GDScriptParser::ClassNode::Member::CONSTANT:
3964
r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_CONSTANT;
3965
break;
3966
case GDScriptParser::ClassNode::Member::FUNCTION:
3967
r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_METHOD;
3968
break;
3969
case GDScriptParser::ClassNode::Member::SIGNAL:
3970
r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_SIGNAL;
3971
break;
3972
case GDScriptParser::ClassNode::Member::VARIABLE:
3973
r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_PROPERTY;
3974
break;
3975
case GDScriptParser::ClassNode::Member::ENUM:
3976
r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_ENUM;
3977
break;
3978
case GDScriptParser::ClassNode::Member::ENUM_VALUE:
3979
r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_CONSTANT;
3980
break;
3981
}
3982
3983
if (member.type != GDScriptParser::ClassNode::Member::CLASS) {
3984
String doc_type_name;
3985
String doc_enum_name;
3986
GDScriptDocGen::doctype_from_gdtype(GDScriptAnalyzer::type_from_metatype(base_type), doc_type_name, doc_enum_name);
3987
3988
r_result.class_name = doc_type_name;
3989
r_result.class_member = name;
3990
}
3991
3992
Error err = OK;
3993
r_result.script = GDScriptCache::get_shallow_script(base_type.script_path, err);
3994
r_result.script_path = base_type.script_path;
3995
r_result.location = member.get_line();
3996
return err;
3997
} break;
3998
case GDScriptParser::DataType::SCRIPT: {
3999
const Ref<Script> scr = base_type.script_type;
4000
4001
if (scr.is_null()) {
4002
return ERR_CANT_RESOLVE;
4003
}
4004
4005
String name = p_symbol;
4006
if (name == "new") {
4007
name = "_init";
4008
}
4009
4010
const int line = scr->get_member_line(name);
4011
if (line >= 0) {
4012
bool found_type = false;
4013
r_result.type = ScriptLanguage::LOOKUP_RESULT_SCRIPT_LOCATION;
4014
{
4015
List<PropertyInfo> properties;
4016
scr->get_script_property_list(&properties);
4017
for (const PropertyInfo &property : properties) {
4018
if (property.name == name && (property.usage & PROPERTY_USAGE_SCRIPT_VARIABLE)) {
4019
found_type = true;
4020
r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_PROPERTY;
4021
r_result.class_name = scr->get_doc_class_name();
4022
r_result.class_member = name;
4023
break;
4024
}
4025
}
4026
}
4027
if (!found_type) {
4028
List<MethodInfo> methods;
4029
scr->get_script_method_list(&methods);
4030
for (const MethodInfo &method : methods) {
4031
if (method.name == name) {
4032
found_type = true;
4033
r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_METHOD;
4034
r_result.class_name = scr->get_doc_class_name();
4035
r_result.class_member = name;
4036
break;
4037
}
4038
}
4039
}
4040
if (!found_type) {
4041
List<MethodInfo> signals;
4042
scr->get_script_method_list(&signals);
4043
for (const MethodInfo &signal : signals) {
4044
if (signal.name == name) {
4045
found_type = true;
4046
r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_SIGNAL;
4047
r_result.class_name = scr->get_doc_class_name();
4048
r_result.class_member = name;
4049
break;
4050
}
4051
}
4052
}
4053
if (!found_type) {
4054
const Ref<GDScript> gds = scr;
4055
if (gds.is_valid()) {
4056
const Ref<GDScript> *subclass = gds->get_subclasses().getptr(name);
4057
if (subclass != nullptr) {
4058
found_type = true;
4059
r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS;
4060
r_result.class_name = subclass->ptr()->get_doc_class_name();
4061
}
4062
// TODO: enums.
4063
}
4064
}
4065
if (!found_type) {
4066
HashMap<StringName, Variant> constants;
4067
scr->get_constants(&constants);
4068
if (constants.has(name)) {
4069
found_type = true;
4070
r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_CONSTANT;
4071
r_result.class_name = scr->get_doc_class_name();
4072
r_result.class_member = name;
4073
}
4074
}
4075
4076
r_result.script = scr;
4077
r_result.script_path = base_type.script_path;
4078
r_result.location = line;
4079
return OK;
4080
}
4081
4082
const Ref<Script> base_script = scr->get_base_script();
4083
if (base_script.is_valid()) {
4084
base_type.script_type = base_script;
4085
} else {
4086
base_type.kind = GDScriptParser::DataType::NATIVE;
4087
base_type.builtin_type = Variant::OBJECT;
4088
base_type.native_type = scr->get_instance_base_type();
4089
}
4090
} break;
4091
case GDScriptParser::DataType::NATIVE: {
4092
const StringName &class_name = base_type.native_type;
4093
4094
ERR_FAIL_COND_V(!ClassDB::class_exists(class_name), ERR_BUG);
4095
4096
if (ClassDB::has_method(class_name, p_symbol, true)) {
4097
r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_METHOD;
4098
r_result.class_name = class_name;
4099
r_result.class_member = p_symbol;
4100
return OK;
4101
}
4102
4103
List<MethodInfo> virtual_methods;
4104
ClassDB::get_virtual_methods(class_name, &virtual_methods, true);
4105
for (const MethodInfo &E : virtual_methods) {
4106
if (E.name == p_symbol) {
4107
r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_METHOD;
4108
r_result.class_name = class_name;
4109
r_result.class_member = p_symbol;
4110
return OK;
4111
}
4112
}
4113
4114
if (ClassDB::has_signal(class_name, p_symbol, true)) {
4115
r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_SIGNAL;
4116
r_result.class_name = class_name;
4117
r_result.class_member = p_symbol;
4118
return OK;
4119
}
4120
4121
List<StringName> enums;
4122
ClassDB::get_enum_list(class_name, &enums);
4123
for (const StringName &E : enums) {
4124
if (E == p_symbol) {
4125
r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_ENUM;
4126
r_result.class_name = class_name;
4127
r_result.class_member = p_symbol;
4128
return OK;
4129
}
4130
}
4131
4132
if (!String(ClassDB::get_integer_constant_enum(class_name, p_symbol, true)).is_empty()) {
4133
r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_CONSTANT;
4134
r_result.class_name = class_name;
4135
r_result.class_member = p_symbol;
4136
return OK;
4137
}
4138
4139
List<String> constants;
4140
ClassDB::get_integer_constant_list(class_name, &constants, true);
4141
for (const String &E : constants) {
4142
if (E == p_symbol) {
4143
r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_CONSTANT;
4144
r_result.class_name = class_name;
4145
r_result.class_member = p_symbol;
4146
return OK;
4147
}
4148
}
4149
4150
if (ClassDB::has_property(class_name, p_symbol, true)) {
4151
PropertyInfo prop_info;
4152
ClassDB::get_property_info(class_name, p_symbol, &prop_info, true);
4153
if (prop_info.usage & PROPERTY_USAGE_INTERNAL) {
4154
return ERR_CANT_RESOLVE;
4155
}
4156
4157
r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_PROPERTY;
4158
r_result.class_name = class_name;
4159
r_result.class_member = p_symbol;
4160
return OK;
4161
}
4162
4163
const StringName parent_class = ClassDB::get_parent_class(class_name);
4164
if (parent_class != StringName()) {
4165
base_type.native_type = parent_class;
4166
} else {
4167
return ERR_CANT_RESOLVE;
4168
}
4169
} break;
4170
case GDScriptParser::DataType::BUILTIN: {
4171
if (base_type.is_meta_type) {
4172
if (Variant::has_enum(base_type.builtin_type, p_symbol)) {
4173
r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_ENUM;
4174
r_result.class_name = Variant::get_type_name(base_type.builtin_type);
4175
r_result.class_member = p_symbol;
4176
return OK;
4177
}
4178
4179
if (Variant::has_constant(base_type.builtin_type, p_symbol)) {
4180
r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_CONSTANT;
4181
r_result.class_name = Variant::get_type_name(base_type.builtin_type);
4182
r_result.class_member = p_symbol;
4183
return OK;
4184
}
4185
} else {
4186
if (Variant::has_member(base_type.builtin_type, p_symbol)) {
4187
r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_PROPERTY;
4188
r_result.class_name = Variant::get_type_name(base_type.builtin_type);
4189
r_result.class_member = p_symbol;
4190
return OK;
4191
}
4192
}
4193
4194
if (Variant::has_builtin_method(base_type.builtin_type, p_symbol)) {
4195
r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_METHOD;
4196
r_result.class_name = Variant::get_type_name(base_type.builtin_type);
4197
r_result.class_member = p_symbol;
4198
return OK;
4199
}
4200
4201
return ERR_CANT_RESOLVE;
4202
} break;
4203
case GDScriptParser::DataType::ENUM: {
4204
if (base_type.is_meta_type) {
4205
if (base_type.enum_values.has(p_symbol)) {
4206
String doc_type_name;
4207
String doc_enum_name;
4208
GDScriptDocGen::doctype_from_gdtype(GDScriptAnalyzer::type_from_metatype(base_type), doc_type_name, doc_enum_name);
4209
4210
if (CoreConstants::is_global_enum(doc_enum_name)) {
4211
r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_CONSTANT;
4212
r_result.class_name = "@GlobalScope";
4213
r_result.class_member = p_symbol;
4214
return OK;
4215
} else {
4216
const int dot_pos = doc_enum_name.rfind_char('.');
4217
if (dot_pos >= 0) {
4218
Error err = OK;
4219
r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_CONSTANT;
4220
if (base_type.class_type != nullptr) {
4221
// For script enums the value isn't accessible as class constant so we need the full enum name.
4222
r_result.class_name = doc_enum_name;
4223
r_result.class_member = p_symbol;
4224
r_result.script = GDScriptCache::get_shallow_script(base_type.script_path, err);
4225
r_result.script_path = base_type.script_path;
4226
const String enum_name = doc_enum_name.substr(dot_pos + 1);
4227
if (base_type.class_type->has_member(enum_name)) {
4228
const GDScriptParser::ClassNode::Member member = base_type.class_type->get_member(enum_name);
4229
if (member.type == GDScriptParser::ClassNode::Member::ENUM) {
4230
for (const GDScriptParser::EnumNode::Value &value : member.m_enum->values) {
4231
if (value.identifier->name == p_symbol) {
4232
r_result.location = value.line;
4233
break;
4234
}
4235
}
4236
}
4237
}
4238
} else if (base_type.script_type.is_valid()) {
4239
// For script enums the value isn't accessible as class constant so we need the full enum name.
4240
r_result.class_name = doc_enum_name;
4241
r_result.class_member = p_symbol;
4242
r_result.script = base_type.script_type;
4243
r_result.script_path = base_type.script_path;
4244
// TODO: Find a way to obtain enum value location for a script
4245
r_result.location = base_type.script_type->get_member_line(doc_enum_name.substr(dot_pos + 1));
4246
} else {
4247
r_result.class_name = doc_enum_name.left(dot_pos);
4248
r_result.class_member = p_symbol;
4249
}
4250
return err;
4251
}
4252
}
4253
} else if (Variant::has_builtin_method(Variant::DICTIONARY, p_symbol)) {
4254
r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_METHOD;
4255
r_result.class_name = "Dictionary";
4256
r_result.class_member = p_symbol;
4257
return OK;
4258
}
4259
}
4260
4261
return ERR_CANT_RESOLVE;
4262
} break;
4263
case GDScriptParser::DataType::VARIANT: {
4264
if (base_type.is_meta_type) {
4265
const String enum_name = "Variant." + p_symbol;
4266
if (CoreConstants::is_global_enum(enum_name)) {
4267
r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_ENUM;
4268
r_result.class_name = "@GlobalScope";
4269
r_result.class_member = enum_name;
4270
return OK;
4271
}
4272
}
4273
4274
return ERR_CANT_RESOLVE;
4275
} break;
4276
case GDScriptParser::DataType::RESOLVING:
4277
case GDScriptParser::DataType::UNRESOLVED: {
4278
return ERR_CANT_RESOLVE;
4279
} break;
4280
}
4281
}
4282
4283
return ERR_CANT_RESOLVE;
4284
}
4285
4286
::Error GDScriptLanguage::lookup_code(const String &p_code, const String &p_symbol, const String &p_path, Object *p_owner, LookupResult &r_result) {
4287
// Before parsing, try the usual stuff.
4288
if (ClassDB::class_exists(p_symbol)) {
4289
r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS;
4290
r_result.class_name = p_symbol;
4291
return OK;
4292
}
4293
4294
if (Variant::get_type_by_name(p_symbol) < Variant::VARIANT_MAX) {
4295
r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS;
4296
r_result.class_name = p_symbol;
4297
return OK;
4298
}
4299
4300
if (p_symbol == "Variant") {
4301
r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS;
4302
r_result.class_name = "Variant";
4303
return OK;
4304
}
4305
4306
if (p_symbol == "PI" || p_symbol == "TAU" || p_symbol == "INF" || p_symbol == "NAN") {
4307
r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_CONSTANT;
4308
r_result.class_name = "@GDScript";
4309
r_result.class_member = p_symbol;
4310
return OK;
4311
}
4312
4313
GDScriptParser parser;
4314
parser.parse(p_code, p_path, true);
4315
4316
GDScriptParser::CompletionContext context = parser.get_completion_context();
4317
context.base = p_owner;
4318
4319
// Allows class functions with the names like built-ins to be handled properly.
4320
if (context.type != GDScriptParser::COMPLETION_ATTRIBUTE) {
4321
// Need special checks for `assert` and `preload` as they are technically
4322
// keywords, so are not registered in `GDScriptUtilityFunctions`.
4323
if (GDScriptUtilityFunctions::function_exists(p_symbol) || p_symbol == "assert" || p_symbol == "preload") {
4324
r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_METHOD;
4325
r_result.class_name = "@GDScript";
4326
r_result.class_member = p_symbol;
4327
return OK;
4328
}
4329
}
4330
4331
GDScriptAnalyzer analyzer(&parser);
4332
analyzer.analyze();
4333
4334
if (context.current_class && context.current_class->extends.size() > 0) {
4335
StringName class_name = context.current_class->extends[0]->name;
4336
4337
bool success = false;
4338
ClassDB::get_integer_constant(class_name, p_symbol, &success);
4339
if (success) {
4340
r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_CONSTANT;
4341
r_result.class_name = class_name;
4342
r_result.class_member = p_symbol;
4343
return OK;
4344
}
4345
do {
4346
List<StringName> enums;
4347
ClassDB::get_enum_list(class_name, &enums, true);
4348
for (const StringName &enum_name : enums) {
4349
if (enum_name == p_symbol) {
4350
r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_ENUM;
4351
r_result.class_name = class_name;
4352
r_result.class_member = p_symbol;
4353
return OK;
4354
}
4355
}
4356
class_name = ClassDB::get_parent_class_nocheck(class_name);
4357
} while (class_name != StringName());
4358
}
4359
4360
const GDScriptParser::TypeNode *type_node = dynamic_cast<const GDScriptParser::TypeNode *>(context.node);
4361
if (type_node != nullptr && !type_node->type_chain.is_empty()) {
4362
StringName class_name = type_node->type_chain[0]->name;
4363
if (ScriptServer::is_global_class(class_name)) {
4364
class_name = ScriptServer::get_global_class_native_base(class_name);
4365
}
4366
do {
4367
List<StringName> enums;
4368
ClassDB::get_enum_list(class_name, &enums, true);
4369
for (const StringName &enum_name : enums) {
4370
if (enum_name == p_symbol) {
4371
r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_ENUM;
4372
r_result.class_name = class_name;
4373
r_result.class_member = p_symbol;
4374
return OK;
4375
}
4376
}
4377
class_name = ClassDB::get_parent_class_nocheck(class_name);
4378
} while (class_name != StringName());
4379
}
4380
4381
bool is_function = false;
4382
4383
switch (context.type) {
4384
case GDScriptParser::COMPLETION_BUILT_IN_TYPE_CONSTANT_OR_STATIC_METHOD: {
4385
GDScriptParser::DataType base_type;
4386
base_type.kind = GDScriptParser::DataType::BUILTIN;
4387
base_type.builtin_type = context.builtin_type;
4388
base_type.is_meta_type = true;
4389
if (_lookup_symbol_from_base(base_type, p_symbol, r_result) == OK) {
4390
return OK;
4391
}
4392
} break;
4393
case GDScriptParser::COMPLETION_SUPER: {
4394
if (context.current_class && context.current_function) {
4395
if (_lookup_symbol_from_base(context.current_class->base_type, context.current_function->info.name, r_result) == OK) {
4396
return OK;
4397
}
4398
}
4399
} break;
4400
case GDScriptParser::COMPLETION_SUPER_METHOD:
4401
case GDScriptParser::COMPLETION_METHOD:
4402
case GDScriptParser::COMPLETION_ASSIGN:
4403
case GDScriptParser::COMPLETION_CALL_ARGUMENTS:
4404
case GDScriptParser::COMPLETION_IDENTIFIER:
4405
case GDScriptParser::COMPLETION_PROPERTY_METHOD:
4406
case GDScriptParser::COMPLETION_SUBSCRIPT: {
4407
GDScriptParser::DataType base_type;
4408
if (context.current_class) {
4409
if (context.type != GDScriptParser::COMPLETION_SUPER_METHOD) {
4410
base_type = context.current_class->get_datatype();
4411
} else {
4412
base_type = context.current_class->base_type;
4413
}
4414
} else {
4415
break;
4416
}
4417
4418
if (!is_function && context.current_suite) {
4419
// Lookup local variables.
4420
const GDScriptParser::SuiteNode *suite = context.current_suite;
4421
while (suite) {
4422
if (suite->has_local(p_symbol)) {
4423
const GDScriptParser::SuiteNode::Local &local = suite->get_local(p_symbol);
4424
4425
switch (local.type) {
4426
case GDScriptParser::SuiteNode::Local::UNDEFINED:
4427
return ERR_BUG;
4428
case GDScriptParser::SuiteNode::Local::CONSTANT:
4429
r_result.type = ScriptLanguage::LOOKUP_RESULT_LOCAL_CONSTANT;
4430
r_result.description = local.constant->doc_data.description;
4431
r_result.is_deprecated = local.constant->doc_data.is_deprecated;
4432
r_result.deprecated_message = local.constant->doc_data.deprecated_message;
4433
r_result.is_experimental = local.constant->doc_data.is_experimental;
4434
r_result.experimental_message = local.constant->doc_data.experimental_message;
4435
if (local.constant->initializer != nullptr) {
4436
r_result.value = GDScriptDocGen::docvalue_from_expression(local.constant->initializer);
4437
}
4438
break;
4439
case GDScriptParser::SuiteNode::Local::VARIABLE:
4440
r_result.type = ScriptLanguage::LOOKUP_RESULT_LOCAL_VARIABLE;
4441
r_result.description = local.variable->doc_data.description;
4442
r_result.is_deprecated = local.variable->doc_data.is_deprecated;
4443
r_result.deprecated_message = local.variable->doc_data.deprecated_message;
4444
r_result.is_experimental = local.variable->doc_data.is_experimental;
4445
r_result.experimental_message = local.variable->doc_data.experimental_message;
4446
if (local.variable->initializer != nullptr) {
4447
r_result.value = GDScriptDocGen::docvalue_from_expression(local.variable->initializer);
4448
}
4449
break;
4450
case GDScriptParser::SuiteNode::Local::PARAMETER:
4451
case GDScriptParser::SuiteNode::Local::FOR_VARIABLE:
4452
case GDScriptParser::SuiteNode::Local::PATTERN_BIND:
4453
r_result.type = ScriptLanguage::LOOKUP_RESULT_LOCAL_VARIABLE;
4454
break;
4455
}
4456
4457
GDScriptDocGen::doctype_from_gdtype(local.get_datatype(), r_result.doc_type, r_result.enumeration);
4458
4459
Error err = OK;
4460
r_result.script = GDScriptCache::get_shallow_script(base_type.script_path, err);
4461
r_result.script_path = base_type.script_path;
4462
r_result.location = local.start_line;
4463
return err;
4464
}
4465
suite = suite->parent_block;
4466
}
4467
}
4468
4469
if (_lookup_symbol_from_base(base_type, p_symbol, r_result) == OK) {
4470
return OK;
4471
}
4472
4473
if (!is_function) {
4474
if (ProjectSettings::get_singleton()->has_autoload(p_symbol)) {
4475
const ProjectSettings::AutoloadInfo &autoload = ProjectSettings::get_singleton()->get_autoload(p_symbol);
4476
if (autoload.is_singleton) {
4477
String scr_path = autoload.path;
4478
if (!scr_path.ends_with(".gd")) {
4479
// Not a script, try find the script anyway, may have some success.
4480
scr_path = scr_path.get_basename() + ".gd";
4481
}
4482
4483
if (FileAccess::exists(scr_path)) {
4484
r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS;
4485
r_result.class_name = p_symbol;
4486
r_result.script = ResourceLoader::load(scr_path);
4487
r_result.script_path = scr_path;
4488
r_result.location = 0;
4489
return OK;
4490
}
4491
}
4492
}
4493
4494
if (ScriptServer::is_global_class(p_symbol)) {
4495
const String scr_path = ScriptServer::get_global_class_path(p_symbol);
4496
const Ref<Script> scr = ResourceLoader::load(scr_path);
4497
if (scr.is_null()) {
4498
return ERR_BUG;
4499
}
4500
r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS;
4501
r_result.class_name = scr->get_doc_class_name();
4502
r_result.script = scr;
4503
r_result.script_path = scr_path;
4504
r_result.location = 0;
4505
return OK;
4506
}
4507
4508
const HashMap<StringName, int> &global_map = GDScriptLanguage::get_singleton()->get_global_map();
4509
if (global_map.has(p_symbol)) {
4510
Variant value = GDScriptLanguage::get_singleton()->get_global_array()[global_map[p_symbol]];
4511
if (value.get_type() == Variant::OBJECT) {
4512
const Object *obj = value;
4513
if (obj) {
4514
if (Object::cast_to<GDScriptNativeClass>(obj)) {
4515
r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS;
4516
r_result.class_name = Object::cast_to<GDScriptNativeClass>(obj)->get_name();
4517
} else {
4518
r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS;
4519
r_result.class_name = obj->get_class();
4520
}
4521
return OK;
4522
}
4523
}
4524
}
4525
4526
if (CoreConstants::is_global_enum(p_symbol)) {
4527
r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_ENUM;
4528
r_result.class_name = "@GlobalScope";
4529
r_result.class_member = p_symbol;
4530
return OK;
4531
}
4532
4533
if (CoreConstants::is_global_constant(p_symbol)) {
4534
r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_CONSTANT;
4535
r_result.class_name = "@GlobalScope";
4536
r_result.class_member = p_symbol;
4537
return OK;
4538
}
4539
4540
if (Variant::has_utility_function(p_symbol)) {
4541
r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_METHOD;
4542
r_result.class_name = "@GlobalScope";
4543
r_result.class_member = p_symbol;
4544
return OK;
4545
}
4546
}
4547
} break;
4548
case GDScriptParser::COMPLETION_ATTRIBUTE_METHOD:
4549
case GDScriptParser::COMPLETION_ATTRIBUTE: {
4550
if (context.node->type != GDScriptParser::Node::SUBSCRIPT) {
4551
break;
4552
}
4553
const GDScriptParser::SubscriptNode *subscript = static_cast<const GDScriptParser::SubscriptNode *>(context.node);
4554
if (!subscript->is_attribute) {
4555
break;
4556
}
4557
GDScriptCompletionIdentifier base;
4558
4559
bool found_type = _get_subscript_type(context, subscript, base.type);
4560
if (!found_type && !_guess_expression_type(context, subscript->base, base)) {
4561
break;
4562
}
4563
4564
if (_lookup_symbol_from_base(base.type, p_symbol, r_result) == OK) {
4565
return OK;
4566
}
4567
} break;
4568
case GDScriptParser::COMPLETION_TYPE_ATTRIBUTE: {
4569
if (context.node == nullptr || context.node->type != GDScriptParser::Node::TYPE) {
4570
break;
4571
}
4572
const GDScriptParser::TypeNode *type = static_cast<const GDScriptParser::TypeNode *>(context.node);
4573
4574
GDScriptParser::DataType base_type;
4575
const GDScriptParser::IdentifierNode *prev = nullptr;
4576
for (const GDScriptParser::IdentifierNode *E : type->type_chain) {
4577
if (E->name == p_symbol && prev != nullptr) {
4578
base_type = prev->get_datatype();
4579
break;
4580
}
4581
prev = E;
4582
}
4583
if (base_type.kind != GDScriptParser::DataType::CLASS) {
4584
GDScriptCompletionIdentifier base;
4585
if (!_guess_expression_type(context, prev, base)) {
4586
break;
4587
}
4588
base_type = base.type;
4589
}
4590
4591
if (_lookup_symbol_from_base(base_type, p_symbol, r_result) == OK) {
4592
return OK;
4593
}
4594
} break;
4595
case GDScriptParser::COMPLETION_OVERRIDE_METHOD: {
4596
GDScriptParser::DataType base_type = context.current_class->base_type;
4597
4598
if (_lookup_symbol_from_base(base_type, p_symbol, r_result) == OK) {
4599
return OK;
4600
}
4601
} break;
4602
case GDScriptParser::COMPLETION_PROPERTY_DECLARATION_OR_TYPE:
4603
case GDScriptParser::COMPLETION_TYPE_NAME_OR_VOID:
4604
case GDScriptParser::COMPLETION_TYPE_NAME: {
4605
GDScriptParser::DataType base_type = context.current_class->get_datatype();
4606
4607
if (_lookup_symbol_from_base(base_type, p_symbol, r_result) == OK) {
4608
return OK;
4609
}
4610
} break;
4611
case GDScriptParser::COMPLETION_ANNOTATION: {
4612
const String annotation_symbol = "@" + p_symbol;
4613
if (parser.annotation_exists(annotation_symbol)) {
4614
r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_ANNOTATION;
4615
r_result.class_name = "@GDScript";
4616
r_result.class_member = annotation_symbol;
4617
return OK;
4618
}
4619
} break;
4620
default: {
4621
}
4622
}
4623
4624
return ERR_CANT_RESOLVE;
4625
}
4626
4627
#endif // TOOLS_ENABLED
4628
4629