Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/modules/gdscript/gdscript_editor.cpp
10277 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("hide_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
}
932
} else if (p_annotation->name == SNAME("@export_exp_easing")) {
933
if (p_argument == 0 || p_argument == 1) {
934
// Easing hint.
935
ScriptLanguage::CodeCompletionOption hint1("attenuation", ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
936
hint1.insert_text = hint1.display.quote(p_quote_style);
937
r_result.insert(hint1.display, hint1);
938
ScriptLanguage::CodeCompletionOption hint2("inout", ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
939
hint2.insert_text = hint2.display.quote(p_quote_style);
940
r_result.insert(hint2.display, hint2);
941
}
942
} else if (p_annotation->name == SNAME("@export_node_path")) {
943
ScriptLanguage::CodeCompletionOption node("Node", ScriptLanguage::CODE_COMPLETION_KIND_CLASS);
944
node.insert_text = node.display.quote(p_quote_style);
945
r_result.insert(node.display, node);
946
947
LocalVector<StringName> native_classes;
948
ClassDB::get_inheriters_from_class("Node", native_classes);
949
for (const StringName &E : native_classes) {
950
if (!ClassDB::is_class_exposed(E)) {
951
continue;
952
}
953
ScriptLanguage::CodeCompletionOption option(E, ScriptLanguage::CODE_COMPLETION_KIND_CLASS);
954
option.insert_text = option.display.quote(p_quote_style);
955
r_result.insert(option.display, option);
956
}
957
958
List<StringName> global_script_classes;
959
ScriptServer::get_global_class_list(&global_script_classes);
960
for (const StringName &E : global_script_classes) {
961
if (!ClassDB::is_parent_class(ScriptServer::get_global_class_native_base(E), "Node")) {
962
continue;
963
}
964
ScriptLanguage::CodeCompletionOption option(E, ScriptLanguage::CODE_COMPLETION_KIND_CLASS);
965
option.insert_text = option.display.quote(p_quote_style);
966
r_result.insert(option.display, option);
967
}
968
} else if (p_annotation->name == SNAME("@export_tool_button")) {
969
if (p_argument == 1) {
970
const Ref<Theme> theme = EditorNode::get_singleton()->get_editor_theme();
971
if (theme.is_valid()) {
972
List<StringName> icon_list;
973
theme->get_icon_list(EditorStringName(EditorIcons), &icon_list);
974
for (const StringName &E : icon_list) {
975
ScriptLanguage::CodeCompletionOption option(E, ScriptLanguage::CODE_COMPLETION_KIND_CLASS);
976
option.insert_text = option.display.quote(p_quote_style);
977
r_result.insert(option.display, option);
978
}
979
}
980
}
981
} else if (p_annotation->name == SNAME("@export_custom")) {
982
switch (p_argument) {
983
case 0: {
984
static HashMap<StringName, int64_t> items;
985
if (unlikely(items.is_empty())) {
986
CoreConstants::get_enum_values(SNAME("PropertyHint"), &items);
987
}
988
for (const KeyValue<StringName, int64_t> &item : items) {
989
ScriptLanguage::CodeCompletionOption option(item.key, ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT);
990
r_result.insert(option.display, option);
991
}
992
} break;
993
case 2: {
994
static HashMap<StringName, int64_t> items;
995
if (unlikely(items.is_empty())) {
996
CoreConstants::get_enum_values(SNAME("PropertyUsageFlags"), &items);
997
}
998
for (const KeyValue<StringName, int64_t> &item : items) {
999
ScriptLanguage::CodeCompletionOption option(item.key, ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT);
1000
r_result.insert(option.display, option);
1001
}
1002
} break;
1003
}
1004
} else if (p_annotation->name == SNAME("@warning_ignore") || p_annotation->name == SNAME("@warning_ignore_start") || p_annotation->name == SNAME("@warning_ignore_restore")) {
1005
for (int warning_code = 0; warning_code < GDScriptWarning::WARNING_MAX; warning_code++) {
1006
#ifndef DISABLE_DEPRECATED
1007
if (warning_code >= GDScriptWarning::FIRST_DEPRECATED_WARNING) {
1008
break; // Don't suggest deprecated warnings as they are never produced.
1009
}
1010
#endif
1011
ScriptLanguage::CodeCompletionOption warning(GDScriptWarning::get_name_from_code((GDScriptWarning::Code)warning_code).to_lower(), ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
1012
warning.insert_text = warning.display.quote(p_quote_style);
1013
r_result.insert(warning.display, warning);
1014
}
1015
} else if (p_annotation->name == SNAME("@rpc")) {
1016
if (p_argument == 0 || p_argument == 1 || p_argument == 2) {
1017
static const char *options[7] = { "call_local", "call_remote", "any_peer", "authority", "reliable", "unreliable", "unreliable_ordered" };
1018
for (int i = 0; i < 7; i++) {
1019
ScriptLanguage::CodeCompletionOption option(options[i], ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
1020
option.insert_text = option.display.quote(p_quote_style);
1021
r_result.insert(option.display, option);
1022
}
1023
}
1024
}
1025
}
1026
1027
static void _find_built_in_variants(HashMap<String, ScriptLanguage::CodeCompletionOption> &r_result, bool exclude_nil = false) {
1028
for (int i = 0; i < Variant::VARIANT_MAX; i++) {
1029
if (!exclude_nil && Variant::Type(i) == Variant::Type::NIL) {
1030
ScriptLanguage::CodeCompletionOption option("null", ScriptLanguage::CODE_COMPLETION_KIND_CLASS);
1031
r_result.insert(option.display, option);
1032
} else {
1033
ScriptLanguage::CodeCompletionOption option(Variant::get_type_name(Variant::Type(i)), ScriptLanguage::CODE_COMPLETION_KIND_CLASS);
1034
r_result.insert(option.display, option);
1035
}
1036
}
1037
}
1038
1039
static void _find_global_enums(HashMap<String, ScriptLanguage::CodeCompletionOption> &r_result) {
1040
List<StringName> global_enums;
1041
CoreConstants::get_global_enums(&global_enums);
1042
for (const StringName &enum_name : global_enums) {
1043
ScriptLanguage::CodeCompletionOption option(enum_name, ScriptLanguage::CODE_COMPLETION_KIND_ENUM, ScriptLanguage::LOCATION_OTHER);
1044
r_result.insert(option.display, option);
1045
}
1046
}
1047
1048
static void _list_available_types(bool p_inherit_only, GDScriptParser::CompletionContext &p_context, HashMap<String, ScriptLanguage::CodeCompletionOption> &r_result) {
1049
// Built-in Variant Types
1050
_find_built_in_variants(r_result, true);
1051
1052
List<StringName> native_types;
1053
ClassDB::get_class_list(&native_types);
1054
for (const StringName &E : native_types) {
1055
if (ClassDB::is_class_exposed(E) && !Engine::get_singleton()->has_singleton(E)) {
1056
ScriptLanguage::CodeCompletionOption option(E, ScriptLanguage::CODE_COMPLETION_KIND_CLASS);
1057
r_result.insert(option.display, option);
1058
}
1059
}
1060
1061
// TODO: Unify with _find_identifiers_in_class.
1062
if (p_context.current_class) {
1063
if (!p_inherit_only && p_context.current_class->base_type.is_set()) {
1064
// Native enums from base class
1065
List<StringName> enums;
1066
ClassDB::get_enum_list(p_context.current_class->base_type.native_type, &enums);
1067
for (const StringName &E : enums) {
1068
ScriptLanguage::CodeCompletionOption option(E, ScriptLanguage::CODE_COMPLETION_KIND_ENUM);
1069
r_result.insert(option.display, option);
1070
}
1071
}
1072
// Check current class for potential types.
1073
// TODO: Also check classes the current class inherits from.
1074
const GDScriptParser::ClassNode *current = p_context.current_class;
1075
int location_offset = 0;
1076
while (current) {
1077
for (int i = 0; i < current->members.size(); i++) {
1078
const GDScriptParser::ClassNode::Member &member = current->members[i];
1079
switch (member.type) {
1080
case GDScriptParser::ClassNode::Member::CLASS: {
1081
ScriptLanguage::CodeCompletionOption option(member.m_class->identifier->name, ScriptLanguage::CODE_COMPLETION_KIND_CLASS, ScriptLanguage::LOCATION_LOCAL + location_offset);
1082
r_result.insert(option.display, option);
1083
} break;
1084
case GDScriptParser::ClassNode::Member::ENUM: {
1085
if (!p_inherit_only) {
1086
ScriptLanguage::CodeCompletionOption option(member.m_enum->identifier->name, ScriptLanguage::CODE_COMPLETION_KIND_ENUM, ScriptLanguage::LOCATION_LOCAL + location_offset);
1087
r_result.insert(option.display, option);
1088
}
1089
} break;
1090
case GDScriptParser::ClassNode::Member::CONSTANT: {
1091
if (member.constant->get_datatype().is_meta_type) {
1092
ScriptLanguage::CodeCompletionOption option(member.constant->identifier->name, ScriptLanguage::CODE_COMPLETION_KIND_CLASS, ScriptLanguage::LOCATION_LOCAL + location_offset);
1093
r_result.insert(option.display, option);
1094
}
1095
} break;
1096
default:
1097
break;
1098
}
1099
}
1100
location_offset += 1;
1101
current = current->outer;
1102
}
1103
}
1104
1105
// Global scripts
1106
List<StringName> global_classes;
1107
ScriptServer::get_global_class_list(&global_classes);
1108
for (const StringName &E : global_classes) {
1109
ScriptLanguage::CodeCompletionOption option(E, ScriptLanguage::CODE_COMPLETION_KIND_CLASS, ScriptLanguage::LOCATION_OTHER_USER_CODE);
1110
r_result.insert(option.display, option);
1111
}
1112
1113
// Global enums
1114
if (!p_inherit_only) {
1115
_find_global_enums(r_result);
1116
}
1117
1118
// Autoload singletons
1119
HashMap<StringName, ProjectSettings::AutoloadInfo> autoloads = ProjectSettings::get_singleton()->get_autoload_list();
1120
1121
for (const KeyValue<StringName, ProjectSettings::AutoloadInfo> &E : autoloads) {
1122
const ProjectSettings::AutoloadInfo &info = E.value;
1123
if (!info.is_singleton || info.path.get_extension().to_lower() != "gd") {
1124
continue;
1125
}
1126
ScriptLanguage::CodeCompletionOption option(info.name, ScriptLanguage::CODE_COMPLETION_KIND_CLASS, ScriptLanguage::LOCATION_OTHER_USER_CODE);
1127
r_result.insert(option.display, option);
1128
}
1129
}
1130
1131
static void _find_identifiers_in_suite(const GDScriptParser::SuiteNode *p_suite, HashMap<String, ScriptLanguage::CodeCompletionOption> &r_result, int p_recursion_depth = 0) {
1132
for (int i = 0; i < p_suite->locals.size(); i++) {
1133
ScriptLanguage::CodeCompletionOption option;
1134
int location = p_recursion_depth == 0 ? ScriptLanguage::LOCATION_LOCAL : (p_recursion_depth | ScriptLanguage::LOCATION_PARENT_MASK);
1135
if (p_suite->locals[i].type == GDScriptParser::SuiteNode::Local::CONSTANT) {
1136
option = ScriptLanguage::CodeCompletionOption(p_suite->locals[i].name, ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT, location);
1137
option.default_value = p_suite->locals[i].constant->initializer->reduced_value;
1138
} else {
1139
option = ScriptLanguage::CodeCompletionOption(p_suite->locals[i].name, ScriptLanguage::CODE_COMPLETION_KIND_VARIABLE, location);
1140
}
1141
r_result.insert(option.display, option);
1142
}
1143
if (p_suite->parent_block) {
1144
_find_identifiers_in_suite(p_suite->parent_block, r_result, p_recursion_depth + 1);
1145
}
1146
}
1147
1148
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);
1149
1150
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) {
1151
ERR_FAIL_COND(p_recursion_depth > COMPLETION_RECURSION_LIMIT);
1152
1153
if (!p_parent_only) {
1154
bool outer = false;
1155
const GDScriptParser::ClassNode *clss = p_class;
1156
int classes_processed = 0;
1157
while (clss) {
1158
for (int i = 0; i < clss->members.size(); i++) {
1159
const int location = p_recursion_depth == 0 ? classes_processed : (p_recursion_depth | ScriptLanguage::LOCATION_PARENT_MASK);
1160
const GDScriptParser::ClassNode::Member &member = clss->members[i];
1161
ScriptLanguage::CodeCompletionOption option;
1162
switch (member.type) {
1163
case GDScriptParser::ClassNode::Member::VARIABLE:
1164
if (p_types_only || p_only_functions || outer || (p_static && !member.variable->is_static)) {
1165
continue;
1166
}
1167
option = ScriptLanguage::CodeCompletionOption(member.variable->identifier->name, ScriptLanguage::CODE_COMPLETION_KIND_MEMBER, location);
1168
break;
1169
case GDScriptParser::ClassNode::Member::CONSTANT:
1170
if ((p_types_only && !member.constant->datatype.is_meta_type) || p_only_functions) {
1171
continue;
1172
}
1173
if (r_result.has(member.constant->identifier->name)) {
1174
continue;
1175
}
1176
option = ScriptLanguage::CodeCompletionOption(member.constant->identifier->name, ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT, location);
1177
if (member.constant->initializer) {
1178
option.default_value = member.constant->initializer->reduced_value;
1179
}
1180
break;
1181
case GDScriptParser::ClassNode::Member::CLASS:
1182
if (p_only_functions) {
1183
continue;
1184
}
1185
option = ScriptLanguage::CodeCompletionOption(member.m_class->identifier->name, ScriptLanguage::CODE_COMPLETION_KIND_CLASS, location);
1186
break;
1187
case GDScriptParser::ClassNode::Member::ENUM_VALUE:
1188
if (p_types_only || p_only_functions) {
1189
continue;
1190
}
1191
option = ScriptLanguage::CodeCompletionOption(member.enum_value.identifier->name, ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT, location);
1192
break;
1193
case GDScriptParser::ClassNode::Member::ENUM:
1194
if (p_only_functions) {
1195
continue;
1196
}
1197
option = ScriptLanguage::CodeCompletionOption(member.m_enum->identifier->name, ScriptLanguage::CODE_COMPLETION_KIND_ENUM, location);
1198
break;
1199
case GDScriptParser::ClassNode::Member::FUNCTION:
1200
if (p_types_only || outer || (p_static && !member.function->is_static) || member.function->identifier->name.operator String().begins_with("@")) {
1201
continue;
1202
}
1203
option = ScriptLanguage::CodeCompletionOption(member.function->identifier->name, ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION, location);
1204
if (p_add_braces) {
1205
if (member.function->parameters.size() > 0 || (member.function->info.flags & METHOD_FLAG_VARARG)) {
1206
option.insert_text += "(";
1207
option.display += U"(\u2026)";
1208
} else {
1209
option.insert_text += "()";
1210
option.display += "()";
1211
}
1212
}
1213
break;
1214
case GDScriptParser::ClassNode::Member::SIGNAL:
1215
if (p_types_only || p_only_functions || outer || p_static) {
1216
continue;
1217
}
1218
option = ScriptLanguage::CodeCompletionOption(member.signal->identifier->name, ScriptLanguage::CODE_COMPLETION_KIND_SIGNAL, location);
1219
break;
1220
case GDScriptParser::ClassNode::Member::GROUP:
1221
break; // No-op, but silences warnings.
1222
case GDScriptParser::ClassNode::Member::UNDEFINED:
1223
break;
1224
}
1225
r_result.insert(option.display, option);
1226
}
1227
if (p_types_only) {
1228
break; // Otherwise, it will fill the results with types from the outer class (which is undesired for that case).
1229
}
1230
1231
outer = true;
1232
clss = clss->outer;
1233
classes_processed++;
1234
}
1235
}
1236
1237
// Parents.
1238
GDScriptCompletionIdentifier base_type;
1239
base_type.type = p_class->base_type;
1240
base_type.type.is_meta_type = p_static;
1241
1242
_find_identifiers_in_base(base_type, p_only_functions, p_types_only, p_add_braces, r_result, p_recursion_depth + 1);
1243
}
1244
1245
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) {
1246
ERR_FAIL_COND(p_recursion_depth > COMPLETION_RECURSION_LIMIT);
1247
1248
GDScriptParser::DataType base_type = p_base.type;
1249
1250
if (!p_types_only && base_type.is_meta_type && base_type.kind != GDScriptParser::DataType::BUILTIN && base_type.kind != GDScriptParser::DataType::ENUM) {
1251
ScriptLanguage::CodeCompletionOption option("new", ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION, ScriptLanguage::LOCATION_LOCAL);
1252
if (p_add_braces) {
1253
option.insert_text += "(";
1254
option.display += U"(\u2026)";
1255
}
1256
r_result.insert(option.display, option);
1257
}
1258
1259
while (!base_type.has_no_type()) {
1260
switch (base_type.kind) {
1261
case GDScriptParser::DataType::CLASS: {
1262
_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);
1263
// This already finds all parent identifiers, so we are done.
1264
base_type = GDScriptParser::DataType();
1265
} break;
1266
case GDScriptParser::DataType::SCRIPT: {
1267
Ref<Script> scr = base_type.script_type;
1268
if (scr.is_valid()) {
1269
if (p_types_only) {
1270
// TODO: Need to implement Script::get_script_enum_list and retrieve the enum list from a script.
1271
} else if (!p_only_functions) {
1272
if (!base_type.is_meta_type) {
1273
List<PropertyInfo> members;
1274
scr->get_script_property_list(&members);
1275
for (const PropertyInfo &E : members) {
1276
if (E.usage & (PROPERTY_USAGE_CATEGORY | PROPERTY_USAGE_GROUP | PROPERTY_USAGE_SUBGROUP | PROPERTY_USAGE_INTERNAL)) {
1277
continue;
1278
}
1279
if (E.name.contains_char('/')) {
1280
continue;
1281
}
1282
int location = p_recursion_depth + _get_property_location(scr, E.name);
1283
ScriptLanguage::CodeCompletionOption option(E.name, ScriptLanguage::CODE_COMPLETION_KIND_MEMBER, location);
1284
r_result.insert(option.display, option);
1285
}
1286
1287
List<MethodInfo> signals;
1288
scr->get_script_signal_list(&signals);
1289
for (const MethodInfo &E : signals) {
1290
int location = p_recursion_depth + _get_signal_location(scr, E.name);
1291
ScriptLanguage::CodeCompletionOption option(E.name, ScriptLanguage::CODE_COMPLETION_KIND_SIGNAL, location);
1292
r_result.insert(option.display, option);
1293
}
1294
}
1295
HashMap<StringName, Variant> constants;
1296
scr->get_constants(&constants);
1297
for (const KeyValue<StringName, Variant> &E : constants) {
1298
int location = p_recursion_depth + _get_constant_location(scr, E.key);
1299
ScriptLanguage::CodeCompletionOption option(E.key.operator String(), ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT, location);
1300
r_result.insert(option.display, option);
1301
}
1302
}
1303
1304
if (!p_types_only) {
1305
List<MethodInfo> methods;
1306
scr->get_script_method_list(&methods);
1307
for (const MethodInfo &E : methods) {
1308
if (E.name.begins_with("@")) {
1309
continue;
1310
}
1311
int location = p_recursion_depth + _get_method_location(scr->get_class_name(), E.name);
1312
ScriptLanguage::CodeCompletionOption option(E.name, ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION, location);
1313
if (p_add_braces) {
1314
if (E.arguments.size() || (E.flags & METHOD_FLAG_VARARG)) {
1315
option.insert_text += "(";
1316
option.display += U"(\u2026)";
1317
} else {
1318
option.insert_text += "()";
1319
option.display += "()";
1320
}
1321
}
1322
r_result.insert(option.display, option);
1323
}
1324
}
1325
1326
Ref<Script> base_script = scr->get_base_script();
1327
if (base_script.is_valid()) {
1328
base_type.script_type = base_script;
1329
} else {
1330
base_type.kind = GDScriptParser::DataType::NATIVE;
1331
base_type.builtin_type = Variant::OBJECT;
1332
base_type.native_type = scr->get_instance_base_type();
1333
}
1334
} else {
1335
return;
1336
}
1337
} break;
1338
case GDScriptParser::DataType::NATIVE: {
1339
StringName type = base_type.native_type;
1340
if (!ClassDB::class_exists(type)) {
1341
return;
1342
}
1343
1344
List<StringName> enums;
1345
ClassDB::get_enum_list(type, &enums);
1346
for (const StringName &E : enums) {
1347
int location = p_recursion_depth + _get_enum_location(type, E);
1348
ScriptLanguage::CodeCompletionOption option(E, ScriptLanguage::CODE_COMPLETION_KIND_ENUM, location);
1349
r_result.insert(option.display, option);
1350
}
1351
1352
if (p_types_only) {
1353
return;
1354
}
1355
1356
if (!p_only_functions) {
1357
List<String> constants;
1358
ClassDB::get_integer_constant_list(type, &constants);
1359
for (const String &E : constants) {
1360
int location = p_recursion_depth + _get_constant_location(type, StringName(E));
1361
ScriptLanguage::CodeCompletionOption option(E, ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT, location);
1362
r_result.insert(option.display, option);
1363
}
1364
1365
if (!base_type.is_meta_type || Engine::get_singleton()->has_singleton(type)) {
1366
List<PropertyInfo> pinfo;
1367
ClassDB::get_property_list(type, &pinfo);
1368
for (const PropertyInfo &E : pinfo) {
1369
if (E.usage & (PROPERTY_USAGE_CATEGORY | PROPERTY_USAGE_GROUP | PROPERTY_USAGE_SUBGROUP | PROPERTY_USAGE_INTERNAL)) {
1370
continue;
1371
}
1372
if (E.name.contains_char('/')) {
1373
continue;
1374
}
1375
int location = p_recursion_depth + _get_property_location(type, E.name);
1376
ScriptLanguage::CodeCompletionOption option(E.name, ScriptLanguage::CODE_COMPLETION_KIND_MEMBER, location);
1377
r_result.insert(option.display, option);
1378
}
1379
1380
List<MethodInfo> signals;
1381
ClassDB::get_signal_list(type, &signals);
1382
for (const MethodInfo &E : signals) {
1383
int location = p_recursion_depth + _get_signal_location(type, StringName(E.name));
1384
ScriptLanguage::CodeCompletionOption option(E.name, ScriptLanguage::CODE_COMPLETION_KIND_SIGNAL, location);
1385
r_result.insert(option.display, option);
1386
}
1387
}
1388
}
1389
1390
bool only_static = base_type.is_meta_type && !Engine::get_singleton()->has_singleton(type);
1391
1392
List<MethodInfo> methods;
1393
ClassDB::get_method_list(type, &methods, false, true);
1394
for (const MethodInfo &E : methods) {
1395
if (only_static && (E.flags & METHOD_FLAG_STATIC) == 0) {
1396
continue;
1397
}
1398
if (E.name.begins_with("_")) {
1399
continue;
1400
}
1401
int location = p_recursion_depth + _get_method_location(type, E.name);
1402
ScriptLanguage::CodeCompletionOption option(E.name, ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION, location);
1403
if (p_add_braces) {
1404
if (E.arguments.size() || (E.flags & METHOD_FLAG_VARARG)) {
1405
option.insert_text += "(";
1406
option.display += U"(\u2026)";
1407
} else {
1408
option.insert_text += "()";
1409
option.display += "()";
1410
}
1411
}
1412
r_result.insert(option.display, option);
1413
}
1414
return;
1415
} break;
1416
case GDScriptParser::DataType::ENUM: {
1417
if (p_types_only) {
1418
return;
1419
}
1420
1421
String type_str = base_type.native_type;
1422
1423
if (type_str.contains_char('.')) {
1424
StringName type = type_str.get_slicec('.', 0);
1425
StringName type_enum = base_type.enum_type;
1426
1427
List<StringName> enum_values;
1428
1429
ClassDB::get_enum_constants(type, type_enum, &enum_values);
1430
1431
for (const StringName &E : enum_values) {
1432
int location = p_recursion_depth + _get_enum_constant_location(type, E);
1433
ScriptLanguage::CodeCompletionOption option(E, ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT, location);
1434
r_result.insert(option.display, option);
1435
}
1436
} else if (CoreConstants::is_global_enum(base_type.enum_type)) {
1437
HashMap<StringName, int64_t> enum_values;
1438
CoreConstants::get_enum_values(base_type.enum_type, &enum_values);
1439
1440
for (const KeyValue<StringName, int64_t> &enum_value : enum_values) {
1441
int location = p_recursion_depth + ScriptLanguage::LOCATION_OTHER;
1442
ScriptLanguage::CodeCompletionOption option(enum_value.key, ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT, location);
1443
r_result.insert(option.display, option);
1444
}
1445
}
1446
}
1447
[[fallthrough]];
1448
case GDScriptParser::DataType::BUILTIN: {
1449
if (p_types_only) {
1450
return;
1451
}
1452
1453
Callable::CallError err;
1454
Variant tmp;
1455
Variant::construct(base_type.builtin_type, tmp, nullptr, 0, err);
1456
if (err.error != Callable::CallError::CALL_OK) {
1457
return;
1458
}
1459
1460
int location = ScriptLanguage::LOCATION_OTHER;
1461
1462
if (!p_only_functions) {
1463
List<PropertyInfo> members;
1464
if (p_base.value.get_type() != Variant::NIL) {
1465
p_base.value.get_property_list(&members);
1466
} else {
1467
tmp.get_property_list(&members);
1468
}
1469
1470
for (const PropertyInfo &E : members) {
1471
if (E.usage & (PROPERTY_USAGE_CATEGORY | PROPERTY_USAGE_GROUP | PROPERTY_USAGE_SUBGROUP | PROPERTY_USAGE_INTERNAL)) {
1472
continue;
1473
}
1474
if (!String(E.name).contains_char('/')) {
1475
ScriptLanguage::CodeCompletionOption option(E.name, ScriptLanguage::CODE_COMPLETION_KIND_MEMBER, location);
1476
if (base_type.kind == GDScriptParser::DataType::ENUM) {
1477
// Sort enum members in their declaration order.
1478
location += 1;
1479
}
1480
if (GDScriptParser::theme_color_names.has(E.name)) {
1481
option.theme_color_name = GDScriptParser::theme_color_names[E.name];
1482
}
1483
r_result.insert(option.display, option);
1484
}
1485
}
1486
}
1487
1488
List<MethodInfo> methods;
1489
tmp.get_method_list(&methods);
1490
for (const MethodInfo &E : methods) {
1491
if (base_type.kind == GDScriptParser::DataType::ENUM && base_type.is_meta_type && !(E.flags & METHOD_FLAG_CONST)) {
1492
// Enum types are static and cannot change, therefore we skip non-const dictionary methods.
1493
continue;
1494
}
1495
ScriptLanguage::CodeCompletionOption option(E.name, ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION, location);
1496
if (p_add_braces) {
1497
if (E.arguments.size() || (E.flags & METHOD_FLAG_VARARG)) {
1498
option.insert_text += "(";
1499
option.display += U"(\u2026)";
1500
} else {
1501
option.insert_text += "()";
1502
option.display += "()";
1503
}
1504
}
1505
r_result.insert(option.display, option);
1506
}
1507
1508
return;
1509
} break;
1510
default: {
1511
return;
1512
} break;
1513
}
1514
}
1515
}
1516
1517
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) {
1518
if (!p_only_functions && p_context.current_suite) {
1519
// This includes function parameters, since they are also locals.
1520
_find_identifiers_in_suite(p_context.current_suite, r_result);
1521
}
1522
1523
if (p_context.current_class) {
1524
_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);
1525
}
1526
1527
List<StringName> functions;
1528
GDScriptUtilityFunctions::get_function_list(&functions);
1529
1530
for (const StringName &E : functions) {
1531
MethodInfo function = GDScriptUtilityFunctions::get_function_info(E);
1532
ScriptLanguage::CodeCompletionOption option(String(E), ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION);
1533
if (p_add_braces) {
1534
if (function.arguments.size() || (function.flags & METHOD_FLAG_VARARG)) {
1535
option.insert_text += "(";
1536
option.display += U"(\u2026)";
1537
} else {
1538
option.insert_text += "()";
1539
option.display += "()";
1540
}
1541
}
1542
r_result.insert(option.display, option);
1543
}
1544
1545
if (p_only_functions) {
1546
return;
1547
}
1548
1549
_find_built_in_variants(r_result);
1550
1551
static const char *_keywords[] = {
1552
"true", "false", "PI", "TAU", "INF", "NAN", "null", "self", "super",
1553
"break", "breakpoint", "continue", "pass", "return",
1554
nullptr
1555
};
1556
1557
const char **kw = _keywords;
1558
while (*kw) {
1559
ScriptLanguage::CodeCompletionOption option(*kw, ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
1560
r_result.insert(option.display, option);
1561
kw++;
1562
}
1563
1564
static const char *_keywords_with_space[] = {
1565
"and", "not", "or", "in", "as", "class", "class_name", "extends", "is", "func", "signal", "await",
1566
"const", "enum", "static", "var", "if", "elif", "else", "for", "match", "when", "while",
1567
nullptr
1568
};
1569
1570
const char **kws = _keywords_with_space;
1571
while (*kws) {
1572
ScriptLanguage::CodeCompletionOption option(*kws, ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
1573
option.insert_text += " ";
1574
r_result.insert(option.display, option);
1575
kws++;
1576
}
1577
1578
static const char *_keywords_with_args[] = {
1579
"assert", "preload",
1580
nullptr
1581
};
1582
1583
const char **kwa = _keywords_with_args;
1584
while (*kwa) {
1585
ScriptLanguage::CodeCompletionOption option(*kwa, ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION);
1586
if (p_add_braces) {
1587
option.insert_text += "(";
1588
option.display += U"(\u2026)";
1589
}
1590
r_result.insert(option.display, option);
1591
kwa++;
1592
}
1593
1594
List<StringName> utility_func_names;
1595
Variant::get_utility_function_list(&utility_func_names);
1596
1597
for (const StringName &util_func_name : utility_func_names) {
1598
ScriptLanguage::CodeCompletionOption option(util_func_name, ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION);
1599
if (p_add_braces) {
1600
option.insert_text += "(";
1601
option.display += U"(\u2026)"; // As all utility functions contain an argument or more, this is hardcoded here.
1602
}
1603
r_result.insert(option.display, option);
1604
}
1605
1606
for (const KeyValue<StringName, ProjectSettings::AutoloadInfo> &E : ProjectSettings::get_singleton()->get_autoload_list()) {
1607
if (!E.value.is_singleton) {
1608
continue;
1609
}
1610
ScriptLanguage::CodeCompletionOption option(E.key, ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT);
1611
r_result.insert(option.display, option);
1612
}
1613
1614
// Native classes and global constants.
1615
for (const KeyValue<StringName, int> &E : GDScriptLanguage::get_singleton()->get_global_map()) {
1616
ScriptLanguage::CodeCompletionOption option;
1617
if (ClassDB::class_exists(E.key) || Engine::get_singleton()->has_singleton(E.key)) {
1618
option = ScriptLanguage::CodeCompletionOption(E.key.operator String(), ScriptLanguage::CODE_COMPLETION_KIND_CLASS);
1619
} else {
1620
option = ScriptLanguage::CodeCompletionOption(E.key.operator String(), ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT);
1621
}
1622
r_result.insert(option.display, option);
1623
}
1624
1625
// Global enums
1626
_find_global_enums(r_result);
1627
1628
// Global classes
1629
List<StringName> global_classes;
1630
ScriptServer::get_global_class_list(&global_classes);
1631
for (const StringName &E : global_classes) {
1632
ScriptLanguage::CodeCompletionOption option(E, ScriptLanguage::CODE_COMPLETION_KIND_CLASS, ScriptLanguage::LOCATION_OTHER_USER_CODE);
1633
r_result.insert(option.display, option);
1634
}
1635
}
1636
1637
static GDScriptCompletionIdentifier _type_from_variant(const Variant &p_value, GDScriptParser::CompletionContext &p_context) {
1638
GDScriptCompletionIdentifier ci;
1639
ci.value = p_value;
1640
ci.type.is_constant = true;
1641
ci.type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
1642
ci.type.kind = GDScriptParser::DataType::BUILTIN;
1643
ci.type.builtin_type = p_value.get_type();
1644
1645
if (ci.type.builtin_type == Variant::OBJECT) {
1646
Object *obj = p_value.operator Object *();
1647
if (!obj) {
1648
return ci;
1649
}
1650
ci.type.native_type = obj->get_class_name();
1651
Ref<Script> scr = p_value;
1652
if (scr.is_valid()) {
1653
ci.type.is_meta_type = true;
1654
} else {
1655
ci.type.is_meta_type = false;
1656
scr = obj->get_script();
1657
}
1658
if (scr.is_valid()) {
1659
ci.type.script_path = scr->get_path();
1660
ci.type.script_type = scr;
1661
ci.type.native_type = scr->get_instance_base_type();
1662
ci.type.kind = GDScriptParser::DataType::SCRIPT;
1663
1664
if (scr->get_path().ends_with(".gd")) {
1665
Ref<GDScriptParserRef> parser = p_context.parser->get_depended_parser_for(scr->get_path());
1666
if (parser.is_valid() && parser->raise_status(GDScriptParserRef::INTERFACE_SOLVED) == OK) {
1667
ci.type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
1668
ci.type.class_type = parser->get_parser()->get_tree();
1669
ci.type.kind = GDScriptParser::DataType::CLASS;
1670
return ci;
1671
}
1672
}
1673
} else {
1674
ci.type.kind = GDScriptParser::DataType::NATIVE;
1675
}
1676
}
1677
1678
return ci;
1679
}
1680
1681
static GDScriptCompletionIdentifier _type_from_property(const PropertyInfo &p_property) {
1682
GDScriptCompletionIdentifier ci;
1683
1684
if (p_property.type == Variant::NIL) {
1685
// Variant
1686
ci.type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
1687
ci.type.kind = GDScriptParser::DataType::VARIANT;
1688
return ci;
1689
}
1690
1691
if (p_property.usage & (PROPERTY_USAGE_CLASS_IS_ENUM | PROPERTY_USAGE_CLASS_IS_BITFIELD)) {
1692
ci.enumeration = p_property.class_name;
1693
}
1694
1695
ci.type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
1696
ci.type.builtin_type = p_property.type;
1697
if (p_property.type == Variant::OBJECT) {
1698
if (ScriptServer::is_global_class(p_property.class_name)) {
1699
ci.type.kind = GDScriptParser::DataType::SCRIPT;
1700
ci.type.script_path = ScriptServer::get_global_class_path(p_property.class_name);
1701
ci.type.native_type = ScriptServer::get_global_class_native_base(p_property.class_name);
1702
1703
Ref<Script> scr = ResourceLoader::load(ScriptServer::get_global_class_path(p_property.class_name));
1704
if (scr.is_valid()) {
1705
ci.type.script_type = scr;
1706
}
1707
} else {
1708
ci.type.kind = GDScriptParser::DataType::NATIVE;
1709
ci.type.native_type = p_property.class_name == StringName() ? "Object" : p_property.class_name;
1710
}
1711
} else {
1712
ci.type.kind = GDScriptParser::DataType::BUILTIN;
1713
}
1714
return ci;
1715
}
1716
1717
static GDScriptCompletionIdentifier _callable_type_from_method_info(const MethodInfo &p_method) {
1718
GDScriptCompletionIdentifier ci;
1719
ci.type.kind = GDScriptParser::DataType::BUILTIN;
1720
ci.type.builtin_type = Variant::CALLABLE;
1721
ci.type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
1722
ci.type.is_constant = true;
1723
ci.type.method_info = p_method;
1724
return ci;
1725
}
1726
1727
#define MAX_COMPLETION_RECURSION 100
1728
struct RecursionCheck {
1729
int *counter;
1730
_FORCE_INLINE_ bool check() {
1731
return (*counter) > MAX_COMPLETION_RECURSION;
1732
}
1733
RecursionCheck(int *p_counter) :
1734
counter(p_counter) {
1735
(*counter)++;
1736
}
1737
~RecursionCheck() {
1738
(*counter)--;
1739
}
1740
};
1741
1742
static bool _guess_identifier_type(GDScriptParser::CompletionContext &p_context, const GDScriptParser::IdentifierNode *p_identifier, GDScriptCompletionIdentifier &r_type);
1743
static bool _guess_identifier_type_from_base(GDScriptParser::CompletionContext &p_context, const GDScriptCompletionIdentifier &p_base, const StringName &p_identifier, GDScriptCompletionIdentifier &r_type);
1744
static bool _guess_method_return_type_from_base(GDScriptParser::CompletionContext &p_context, const GDScriptCompletionIdentifier &p_base, const StringName &p_method, GDScriptCompletionIdentifier &r_type);
1745
1746
static bool _is_expression_named_identifier(const GDScriptParser::ExpressionNode *p_expression, const StringName &p_name) {
1747
if (p_expression) {
1748
switch (p_expression->type) {
1749
case GDScriptParser::Node::IDENTIFIER: {
1750
const GDScriptParser::IdentifierNode *id = static_cast<const GDScriptParser::IdentifierNode *>(p_expression);
1751
if (id->name == p_name) {
1752
return true;
1753
}
1754
} break;
1755
case GDScriptParser::Node::CAST: {
1756
const GDScriptParser::CastNode *cn = static_cast<const GDScriptParser::CastNode *>(p_expression);
1757
return _is_expression_named_identifier(cn->operand, p_name);
1758
} break;
1759
default:
1760
break;
1761
}
1762
}
1763
1764
return false;
1765
}
1766
1767
// Creates a map of exemplary results for some functions that return a structured dictionary.
1768
// Setting this example as value allows autocompletion to suggest the specific keys in some cases.
1769
static HashMap<String, Dictionary> make_structure_samples() {
1770
HashMap<String, Dictionary> res;
1771
const Array arr;
1772
1773
{
1774
Dictionary d;
1775
d.set("major", 0);
1776
d.set("minor", 0);
1777
d.set("patch", 0);
1778
d.set("hex", 0);
1779
d.set("status", String());
1780
d.set("build", String());
1781
d.set("hash", String());
1782
d.set("timestamp", 0);
1783
d.set("string", String());
1784
res["Engine::get_version_info"] = d;
1785
}
1786
1787
{
1788
Dictionary d;
1789
d.set("lead_developers", arr);
1790
d.set("founders", arr);
1791
d.set("project_managers", arr);
1792
d.set("developers", arr);
1793
res["Engine::get_author_info"] = d;
1794
}
1795
1796
{
1797
Dictionary d;
1798
d.set("platinum_sponsors", arr);
1799
d.set("gold_sponsors", arr);
1800
d.set("silver_sponsors", arr);
1801
d.set("bronze_sponsors", arr);
1802
d.set("mini_sponsors", arr);
1803
d.set("gold_donors", arr);
1804
d.set("silver_donors", arr);
1805
d.set("bronze_donors", arr);
1806
res["Engine::get_donor_info"] = d;
1807
}
1808
1809
{
1810
Dictionary d;
1811
d.set("physical", -1);
1812
d.set("free", -1);
1813
d.set("available", -1);
1814
d.set("stack", -1);
1815
res["OS::get_memory_info"] = d;
1816
}
1817
1818
{
1819
Dictionary d;
1820
d.set("year", 0);
1821
d.set("month", 0);
1822
d.set("day", 0);
1823
d.set("weekday", 0);
1824
d.set("hour", 0);
1825
d.set("minute", 0);
1826
d.set("second", 0);
1827
d.set("dst", 0);
1828
res["Time::get_datetime_dict_from_system"] = d;
1829
}
1830
1831
{
1832
Dictionary d;
1833
d.set("year", 0);
1834
d.set("month", 0);
1835
d.set("day", 0);
1836
d.set("weekday", 0);
1837
d.set("hour", 0);
1838
d.set("minute", 0);
1839
d.set("second", 0);
1840
res["Time::get_datetime_dict_from_unix_time"] = d;
1841
}
1842
1843
{
1844
Dictionary d;
1845
d.set("year", 0);
1846
d.set("month", 0);
1847
d.set("day", 0);
1848
d.set("weekday", 0);
1849
res["Time::get_date_dict_from_system"] = d;
1850
res["Time::get_date_dict_from_unix_time"] = d;
1851
}
1852
1853
{
1854
Dictionary d;
1855
d.set("hour", 0);
1856
d.set("minute", 0);
1857
d.set("second", 0);
1858
res["Time::get_time_dict_from_system"] = d;
1859
res["Time::get_time_dict_from_unix_time"] = d;
1860
}
1861
1862
{
1863
Dictionary d;
1864
d.set("bias", 0);
1865
d.set("name", String());
1866
res["Time::get_time_zone_from_system"] = d;
1867
}
1868
1869
return res;
1870
}
1871
1872
static const HashMap<String, Dictionary> structure_examples = make_structure_samples();
1873
1874
static bool _guess_expression_type(GDScriptParser::CompletionContext &p_context, const GDScriptParser::ExpressionNode *p_expression, GDScriptCompletionIdentifier &r_type) {
1875
bool found = false;
1876
1877
if (p_expression == nullptr) {
1878
return false;
1879
}
1880
1881
static int recursion_depth = 0;
1882
RecursionCheck recursion(&recursion_depth);
1883
if (unlikely(recursion.check())) {
1884
ERR_FAIL_V_MSG(false, "Reached recursion limit while trying to guess type.");
1885
}
1886
1887
if (p_expression->is_constant) {
1888
// Already has a value, so just use that.
1889
r_type = _type_from_variant(p_expression->reduced_value, p_context);
1890
switch (p_expression->get_datatype().kind) {
1891
case GDScriptParser::DataType::ENUM:
1892
case GDScriptParser::DataType::CLASS:
1893
r_type.type = p_expression->get_datatype();
1894
break;
1895
default:
1896
break;
1897
}
1898
found = true;
1899
} else {
1900
switch (p_expression->type) {
1901
case GDScriptParser::Node::LITERAL: {
1902
const GDScriptParser::LiteralNode *literal = static_cast<const GDScriptParser::LiteralNode *>(p_expression);
1903
r_type = _type_from_variant(literal->value, p_context);
1904
found = true;
1905
} break;
1906
case GDScriptParser::Node::SELF: {
1907
if (p_context.current_class) {
1908
r_type.type = p_context.current_class->get_datatype();
1909
r_type.type.is_meta_type = false;
1910
found = true;
1911
}
1912
} break;
1913
case GDScriptParser::Node::IDENTIFIER: {
1914
const GDScriptParser::IdentifierNode *id = static_cast<const GDScriptParser::IdentifierNode *>(p_expression);
1915
found = _guess_identifier_type(p_context, id, r_type);
1916
} break;
1917
case GDScriptParser::Node::DICTIONARY: {
1918
// Try to recreate the dictionary.
1919
const GDScriptParser::DictionaryNode *dn = static_cast<const GDScriptParser::DictionaryNode *>(p_expression);
1920
Dictionary d;
1921
bool full = true;
1922
for (int i = 0; i < dn->elements.size(); i++) {
1923
GDScriptCompletionIdentifier key;
1924
if (_guess_expression_type(p_context, dn->elements[i].key, key)) {
1925
if (!key.type.is_constant) {
1926
full = false;
1927
break;
1928
}
1929
GDScriptCompletionIdentifier value;
1930
if (_guess_expression_type(p_context, dn->elements[i].value, value)) {
1931
if (!value.type.is_constant) {
1932
full = false;
1933
break;
1934
}
1935
d[key.value] = value.value;
1936
} else {
1937
full = false;
1938
break;
1939
}
1940
} else {
1941
full = false;
1942
break;
1943
}
1944
}
1945
if (full) {
1946
r_type.value = d;
1947
r_type.type.is_constant = true;
1948
}
1949
r_type.type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
1950
r_type.type.kind = GDScriptParser::DataType::BUILTIN;
1951
r_type.type.builtin_type = Variant::DICTIONARY;
1952
found = true;
1953
} break;
1954
case GDScriptParser::Node::ARRAY: {
1955
// Try to recreate the array
1956
const GDScriptParser::ArrayNode *an = static_cast<const GDScriptParser::ArrayNode *>(p_expression);
1957
Array a;
1958
bool full = true;
1959
a.resize(an->elements.size());
1960
for (int i = 0; i < an->elements.size(); i++) {
1961
GDScriptCompletionIdentifier value;
1962
if (_guess_expression_type(p_context, an->elements[i], value)) {
1963
if (value.type.is_constant) {
1964
a[i] = value.value;
1965
} else {
1966
full = false;
1967
break;
1968
}
1969
} else {
1970
full = false;
1971
break;
1972
}
1973
}
1974
if (full) {
1975
// If not fully constant, setting this value is detrimental to the inference.
1976
r_type.value = a;
1977
r_type.type.is_constant = true;
1978
}
1979
r_type.type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
1980
r_type.type.kind = GDScriptParser::DataType::BUILTIN;
1981
r_type.type.builtin_type = Variant::ARRAY;
1982
found = true;
1983
} break;
1984
case GDScriptParser::Node::CAST: {
1985
const GDScriptParser::CastNode *cn = static_cast<const GDScriptParser::CastNode *>(p_expression);
1986
GDScriptCompletionIdentifier value;
1987
if (_guess_expression_type(p_context, cn->operand, r_type)) {
1988
r_type.type = cn->get_datatype();
1989
found = true;
1990
}
1991
} break;
1992
case GDScriptParser::Node::CALL: {
1993
const GDScriptParser::CallNode *call = static_cast<const GDScriptParser::CallNode *>(p_expression);
1994
GDScriptParser::CompletionContext c = p_context;
1995
c.current_line = call->start_line;
1996
1997
GDScriptParser::Node::Type callee_type = call->get_callee_type();
1998
1999
GDScriptCompletionIdentifier base;
2000
if (callee_type == GDScriptParser::Node::IDENTIFIER || call->is_super) {
2001
// Simple call, so base is 'self'.
2002
if (p_context.current_class) {
2003
if (call->is_super) {
2004
base.type = p_context.current_class->base_type;
2005
base.value = p_context.base;
2006
} else {
2007
base.type.kind = GDScriptParser::DataType::CLASS;
2008
base.type.type_source = GDScriptParser::DataType::INFERRED;
2009
base.type.is_constant = true;
2010
base.type.class_type = p_context.current_class;
2011
base.value = p_context.base;
2012
}
2013
} else {
2014
break;
2015
}
2016
} else if (callee_type == GDScriptParser::Node::SUBSCRIPT && static_cast<const GDScriptParser::SubscriptNode *>(call->callee)->is_attribute) {
2017
if (!_guess_expression_type(c, static_cast<const GDScriptParser::SubscriptNode *>(call->callee)->base, base)) {
2018
found = false;
2019
break;
2020
}
2021
} else {
2022
break;
2023
}
2024
2025
// Apply additional behavior aware inference that the analyzer can't do.
2026
if (base.type.is_set()) {
2027
// Maintain type for duplicate methods.
2028
if (call->function_name == SNAME("duplicate")) {
2029
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")))) {
2030
r_type.type = base.type;
2031
found = true;
2032
break;
2033
}
2034
}
2035
2036
// Simulate generics for some typed array methods.
2037
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"))) {
2038
r_type.type = base.type.get_container_element_type(0);
2039
found = true;
2040
break;
2041
}
2042
2043
// Insert example values for functions which a structured dictionary response.
2044
if (!base.type.is_meta_type) {
2045
const Dictionary *example = structure_examples.getptr(base.type.native_type.operator String() + "::" + call->function_name);
2046
if (example != nullptr) {
2047
r_type = _type_from_variant(*example, p_context);
2048
found = true;
2049
break;
2050
}
2051
}
2052
}
2053
2054
if (!found) {
2055
found = _guess_method_return_type_from_base(c, base, call->function_name, r_type);
2056
}
2057
} break;
2058
case GDScriptParser::Node::SUBSCRIPT: {
2059
const GDScriptParser::SubscriptNode *subscript = static_cast<const GDScriptParser::SubscriptNode *>(p_expression);
2060
if (subscript->is_attribute) {
2061
GDScriptParser::CompletionContext c = p_context;
2062
c.current_line = subscript->start_line;
2063
2064
GDScriptCompletionIdentifier base;
2065
if (!_guess_expression_type(c, subscript->base, base)) {
2066
found = false;
2067
break;
2068
}
2069
2070
if (base.value.get_type() == Variant::DICTIONARY && base.value.operator Dictionary().has(String(subscript->attribute->name))) {
2071
Variant value = base.value.operator Dictionary()[String(subscript->attribute->name)];
2072
r_type = _type_from_variant(value, p_context);
2073
found = true;
2074
break;
2075
}
2076
2077
const GDScriptParser::DictionaryNode *dn = nullptr;
2078
if (subscript->base->type == GDScriptParser::Node::DICTIONARY) {
2079
dn = static_cast<const GDScriptParser::DictionaryNode *>(subscript->base);
2080
} else if (base.assigned_expression && base.assigned_expression->type == GDScriptParser::Node::DICTIONARY) {
2081
dn = static_cast<const GDScriptParser::DictionaryNode *>(base.assigned_expression);
2082
}
2083
2084
if (dn) {
2085
for (int i = 0; i < dn->elements.size(); i++) {
2086
GDScriptCompletionIdentifier key;
2087
if (!_guess_expression_type(c, dn->elements[i].key, key)) {
2088
continue;
2089
}
2090
if (key.value == String(subscript->attribute->name)) {
2091
r_type.assigned_expression = dn->elements[i].value;
2092
found = _guess_expression_type(c, dn->elements[i].value, r_type);
2093
break;
2094
}
2095
}
2096
}
2097
2098
if (!found) {
2099
found = _guess_identifier_type_from_base(c, base, subscript->attribute->name, r_type);
2100
}
2101
} else {
2102
if (subscript->index == nullptr) {
2103
found = false;
2104
break;
2105
}
2106
2107
GDScriptParser::CompletionContext c = p_context;
2108
c.current_line = subscript->start_line;
2109
2110
GDScriptCompletionIdentifier base;
2111
if (!_guess_expression_type(c, subscript->base, base)) {
2112
found = false;
2113
break;
2114
}
2115
2116
GDScriptCompletionIdentifier index;
2117
if (!_guess_expression_type(c, subscript->index, index)) {
2118
found = false;
2119
break;
2120
}
2121
2122
if (base.type.is_constant && index.type.is_constant) {
2123
if (base.value.get_type() == Variant::DICTIONARY) {
2124
Dictionary base_dict = base.value.operator Dictionary();
2125
if (base_dict.get_key_validator().test_validate(index.value) && base_dict.has(index.value)) {
2126
r_type = _type_from_variant(base_dict[index.value], p_context);
2127
found = true;
2128
break;
2129
}
2130
} else {
2131
bool valid;
2132
Variant value = base.value.get(index.value, &valid);
2133
if (valid) {
2134
r_type = _type_from_variant(value, p_context);
2135
found = true;
2136
break;
2137
}
2138
}
2139
}
2140
2141
// Look if it is a dictionary node.
2142
const GDScriptParser::DictionaryNode *dn = nullptr;
2143
if (subscript->base->type == GDScriptParser::Node::DICTIONARY) {
2144
dn = static_cast<const GDScriptParser::DictionaryNode *>(subscript->base);
2145
} else if (base.assigned_expression && base.assigned_expression->type == GDScriptParser::Node::DICTIONARY) {
2146
dn = static_cast<const GDScriptParser::DictionaryNode *>(base.assigned_expression);
2147
}
2148
2149
if (dn) {
2150
for (int i = 0; i < dn->elements.size(); i++) {
2151
GDScriptCompletionIdentifier key;
2152
if (!_guess_expression_type(c, dn->elements[i].key, key)) {
2153
continue;
2154
}
2155
if (key.value == index.value) {
2156
r_type.assigned_expression = dn->elements[i].value;
2157
found = _guess_expression_type(p_context, dn->elements[i].value, r_type);
2158
break;
2159
}
2160
}
2161
}
2162
2163
// Look if it is an array node.
2164
if (!found && index.value.is_num()) {
2165
int idx = index.value;
2166
const GDScriptParser::ArrayNode *an = nullptr;
2167
if (subscript->base->type == GDScriptParser::Node::ARRAY) {
2168
an = static_cast<const GDScriptParser::ArrayNode *>(subscript->base);
2169
} else if (base.assigned_expression && base.assigned_expression->type == GDScriptParser::Node::ARRAY) {
2170
an = static_cast<const GDScriptParser::ArrayNode *>(base.assigned_expression);
2171
}
2172
2173
if (an && idx >= 0 && an->elements.size() > idx) {
2174
r_type.assigned_expression = an->elements[idx];
2175
found = _guess_expression_type(c, an->elements[idx], r_type);
2176
break;
2177
}
2178
}
2179
2180
// Look for valid indexing in other types
2181
if (!found && (index.value.is_string() || index.value.get_type() == Variant::NODE_PATH)) {
2182
StringName id = index.value;
2183
found = _guess_identifier_type_from_base(c, base, id, r_type);
2184
} else if (!found && index.type.kind == GDScriptParser::DataType::BUILTIN) {
2185
Callable::CallError err;
2186
Variant base_val;
2187
Variant::construct(base.type.builtin_type, base_val, nullptr, 0, err);
2188
bool valid = false;
2189
Variant res = base_val.get(index.value, &valid);
2190
if (valid) {
2191
r_type = _type_from_variant(res, p_context);
2192
r_type.value = Variant();
2193
r_type.type.is_constant = false;
2194
found = true;
2195
}
2196
}
2197
}
2198
} break;
2199
case GDScriptParser::Node::BINARY_OPERATOR: {
2200
const GDScriptParser::BinaryOpNode *op = static_cast<const GDScriptParser::BinaryOpNode *>(p_expression);
2201
2202
if (op->variant_op == Variant::OP_MAX) {
2203
break;
2204
}
2205
2206
GDScriptParser::CompletionContext context = p_context;
2207
context.current_line = op->start_line;
2208
2209
GDScriptCompletionIdentifier p1;
2210
GDScriptCompletionIdentifier p2;
2211
2212
if (!_guess_expression_type(context, op->left_operand, p1)) {
2213
found = false;
2214
break;
2215
}
2216
2217
if (!_guess_expression_type(context, op->right_operand, p2)) {
2218
found = false;
2219
break;
2220
}
2221
2222
Callable::CallError ce;
2223
bool v1_use_value = p1.value.get_type() != Variant::NIL && p1.value.get_type() != Variant::OBJECT;
2224
Variant d1;
2225
Variant::construct(p1.type.builtin_type, d1, nullptr, 0, ce);
2226
Variant d2;
2227
Variant::construct(p2.type.builtin_type, d2, nullptr, 0, ce);
2228
2229
Variant v1 = (v1_use_value) ? p1.value : d1;
2230
bool v2_use_value = p2.value.get_type() != Variant::NIL && p2.value.get_type() != Variant::OBJECT;
2231
Variant v2 = (v2_use_value) ? p2.value : d2;
2232
// avoid potential invalid ops
2233
if ((op->variant_op == Variant::OP_DIVIDE || op->variant_op == Variant::OP_MODULE) && v2.get_type() == Variant::INT) {
2234
v2 = 1;
2235
v2_use_value = false;
2236
}
2237
if (op->variant_op == Variant::OP_DIVIDE && v2.get_type() == Variant::FLOAT) {
2238
v2 = 1.0;
2239
v2_use_value = false;
2240
}
2241
2242
Variant res;
2243
bool valid;
2244
Variant::evaluate(op->variant_op, v1, v2, res, valid);
2245
if (!valid) {
2246
found = false;
2247
break;
2248
}
2249
r_type = _type_from_variant(res, p_context);
2250
if (!v1_use_value || !v2_use_value) {
2251
r_type.value = Variant();
2252
r_type.type.is_constant = false;
2253
}
2254
2255
found = true;
2256
} break;
2257
default:
2258
break;
2259
}
2260
}
2261
2262
// It may have found a null, but that's never useful
2263
if (found && r_type.type.kind == GDScriptParser::DataType::BUILTIN && r_type.type.builtin_type == Variant::NIL) {
2264
found = false;
2265
}
2266
2267
// If the found type was not fully analyzed we analyze it now.
2268
if (found && r_type.type.kind == GDScriptParser::DataType::CLASS && !r_type.type.class_type->resolved_body) {
2269
Error err;
2270
Ref<GDScriptParserRef> r = GDScriptCache::get_parser(r_type.type.script_path, GDScriptParserRef::FULLY_SOLVED, err);
2271
}
2272
2273
// Check type hint last. For collections we want chance to get the actual value first
2274
// This way we can detect types from the content of dictionaries and arrays
2275
if (!found && p_expression->get_datatype().is_hard_type()) {
2276
r_type.type = p_expression->get_datatype();
2277
if (!r_type.assigned_expression) {
2278
r_type.assigned_expression = p_expression;
2279
}
2280
found = true;
2281
}
2282
2283
return found;
2284
}
2285
2286
static bool _guess_identifier_type(GDScriptParser::CompletionContext &p_context, const GDScriptParser::IdentifierNode *p_identifier, GDScriptCompletionIdentifier &r_type) {
2287
static int recursion_depth = 0;
2288
RecursionCheck recursion(&recursion_depth);
2289
if (unlikely(recursion.check())) {
2290
ERR_FAIL_V_MSG(false, "Reached recursion limit while trying to guess type.");
2291
}
2292
2293
// Look in blocks first.
2294
int last_assign_line = -1;
2295
const GDScriptParser::ExpressionNode *last_assigned_expression = nullptr;
2296
GDScriptCompletionIdentifier id_type;
2297
GDScriptParser::SuiteNode *suite = p_context.current_suite;
2298
bool is_function_parameter = false;
2299
2300
bool can_be_local = true;
2301
switch (p_identifier->source) {
2302
case GDScriptParser::IdentifierNode::MEMBER_VARIABLE:
2303
case GDScriptParser::IdentifierNode::MEMBER_CONSTANT:
2304
case GDScriptParser::IdentifierNode::MEMBER_FUNCTION:
2305
case GDScriptParser::IdentifierNode::MEMBER_SIGNAL:
2306
case GDScriptParser::IdentifierNode::MEMBER_CLASS:
2307
case GDScriptParser::IdentifierNode::INHERITED_VARIABLE:
2308
case GDScriptParser::IdentifierNode::STATIC_VARIABLE:
2309
case GDScriptParser::IdentifierNode::NATIVE_CLASS:
2310
can_be_local = false;
2311
break;
2312
default:
2313
break;
2314
}
2315
2316
if (can_be_local && suite && suite->has_local(p_identifier->name)) {
2317
const GDScriptParser::SuiteNode::Local &local = suite->get_local(p_identifier->name);
2318
2319
id_type.type = local.get_datatype();
2320
2321
// Check initializer as the first assignment.
2322
switch (local.type) {
2323
case GDScriptParser::SuiteNode::Local::VARIABLE:
2324
if (local.variable->initializer) {
2325
last_assign_line = local.variable->initializer->end_line;
2326
last_assigned_expression = local.variable->initializer;
2327
}
2328
break;
2329
case GDScriptParser::SuiteNode::Local::CONSTANT:
2330
if (local.constant->initializer) {
2331
last_assign_line = local.constant->initializer->end_line;
2332
last_assigned_expression = local.constant->initializer;
2333
}
2334
break;
2335
case GDScriptParser::SuiteNode::Local::PARAMETER:
2336
if (local.parameter->initializer) {
2337
last_assign_line = local.parameter->initializer->end_line;
2338
last_assigned_expression = local.parameter->initializer;
2339
}
2340
is_function_parameter = true;
2341
break;
2342
default:
2343
break;
2344
}
2345
} else {
2346
if (p_context.current_class) {
2347
GDScriptCompletionIdentifier base_identifier;
2348
2349
GDScriptCompletionIdentifier base;
2350
base.value = p_context.base;
2351
base.type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
2352
base.type.kind = GDScriptParser::DataType::CLASS;
2353
base.type.class_type = p_context.current_class;
2354
base.type.is_meta_type = p_context.current_function && p_context.current_function->is_static;
2355
2356
if (_guess_identifier_type_from_base(p_context, base, p_identifier->name, base_identifier)) {
2357
id_type = base_identifier;
2358
}
2359
}
2360
}
2361
2362
while (suite) {
2363
for (int i = 0; i < suite->statements.size(); i++) {
2364
if (suite->statements[i]->end_line >= p_context.current_line) {
2365
break;
2366
}
2367
2368
switch (suite->statements[i]->type) {
2369
case GDScriptParser::Node::ASSIGNMENT: {
2370
const GDScriptParser::AssignmentNode *assign = static_cast<const GDScriptParser::AssignmentNode *>(suite->statements[i]);
2371
if (assign->end_line > last_assign_line && assign->assignee && assign->assigned_value && assign->assignee->type == GDScriptParser::Node::IDENTIFIER) {
2372
const GDScriptParser::IdentifierNode *id = static_cast<const GDScriptParser::IdentifierNode *>(assign->assignee);
2373
if (id->name == p_identifier->name && id->source == p_identifier->source) {
2374
last_assign_line = assign->assigned_value->end_line;
2375
last_assigned_expression = assign->assigned_value;
2376
}
2377
}
2378
} break;
2379
default:
2380
// TODO: Check sub blocks (control flow statements) as they might also reassign stuff.
2381
break;
2382
}
2383
}
2384
2385
if (suite->parent_if && suite->parent_if->condition && suite->parent_if->condition->type == GDScriptParser::Node::TYPE_TEST) {
2386
// Operator `is` used, check if identifier is in there! this helps resolve in blocks that are (if (identifier is value)): which are very common..
2387
// Super dirty hack, but very useful.
2388
// Credit: Zylann.
2389
// TODO: this could be hacked to detect AND-ed conditions too...
2390
const GDScriptParser::TypeTestNode *type_test = static_cast<const GDScriptParser::TypeTestNode *>(suite->parent_if->condition);
2391
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) {
2392
// Bingo.
2393
GDScriptParser::CompletionContext c = p_context;
2394
c.current_line = type_test->operand->start_line;
2395
c.current_suite = suite;
2396
if (type_test->test_datatype.is_hard_type()) {
2397
id_type.type = type_test->test_datatype;
2398
if (last_assign_line < c.current_line) {
2399
// Override last assignment.
2400
last_assign_line = c.current_line;
2401
last_assigned_expression = nullptr;
2402
}
2403
}
2404
}
2405
}
2406
2407
suite = suite->parent_block;
2408
}
2409
2410
if (last_assigned_expression && last_assign_line < p_context.current_line) {
2411
GDScriptParser::CompletionContext c = p_context;
2412
c.current_line = last_assign_line;
2413
GDScriptCompletionIdentifier assigned_type;
2414
if (_guess_expression_type(c, last_assigned_expression, assigned_type)) {
2415
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)))) {
2416
// The assigned type is incompatible. The annotated type takes priority.
2417
r_type = id_type;
2418
r_type.assigned_expression = last_assigned_expression;
2419
} else {
2420
r_type = assigned_type;
2421
}
2422
return true;
2423
}
2424
}
2425
2426
if (is_function_parameter && p_context.current_function && p_context.current_function->source_lambda == nullptr && p_context.current_class) {
2427
// Check if it's override of native function, then we can assume the type from the signature.
2428
GDScriptParser::DataType base_type = p_context.current_class->base_type;
2429
while (base_type.is_set()) {
2430
switch (base_type.kind) {
2431
case GDScriptParser::DataType::CLASS:
2432
if (base_type.class_type->has_function(p_context.current_function->identifier->name)) {
2433
GDScriptParser::FunctionNode *parent_function = base_type.class_type->get_member(p_context.current_function->identifier->name).function;
2434
if (parent_function->parameters_indices.has(p_identifier->name)) {
2435
const GDScriptParser::ParameterNode *parameter = parent_function->parameters[parent_function->parameters_indices[p_identifier->name]];
2436
if ((!id_type.type.is_set() || id_type.type.is_variant()) && parameter->get_datatype().is_hard_type()) {
2437
id_type.type = parameter->get_datatype();
2438
}
2439
if (parameter->initializer) {
2440
GDScriptParser::CompletionContext c = p_context;
2441
c.current_function = parent_function;
2442
c.current_class = base_type.class_type;
2443
c.base = nullptr;
2444
if (_guess_expression_type(c, parameter->initializer, r_type)) {
2445
return true;
2446
}
2447
}
2448
}
2449
}
2450
base_type = base_type.class_type->base_type;
2451
break;
2452
case GDScriptParser::DataType::NATIVE: {
2453
if (id_type.type.is_set() && !id_type.type.is_variant()) {
2454
base_type = GDScriptParser::DataType();
2455
break;
2456
}
2457
MethodInfo info;
2458
if (ClassDB::get_method_info(base_type.native_type, p_context.current_function->identifier->name, &info)) {
2459
for (const PropertyInfo &E : info.arguments) {
2460
if (E.name == p_identifier->name) {
2461
r_type = _type_from_property(E);
2462
return true;
2463
}
2464
}
2465
}
2466
base_type = GDScriptParser::DataType();
2467
} break;
2468
default:
2469
break;
2470
}
2471
}
2472
}
2473
2474
if (id_type.type.is_set() && !id_type.type.is_variant()) {
2475
r_type = id_type;
2476
return true;
2477
}
2478
2479
// Check global scripts.
2480
if (ScriptServer::is_global_class(p_identifier->name)) {
2481
String script = ScriptServer::get_global_class_path(p_identifier->name);
2482
if (script.to_lower().ends_with(".gd")) {
2483
Ref<GDScriptParserRef> parser = p_context.parser->get_depended_parser_for(script);
2484
if (parser.is_valid() && parser->raise_status(GDScriptParserRef::INTERFACE_SOLVED) == OK) {
2485
r_type.type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
2486
r_type.type.script_path = script;
2487
r_type.type.class_type = parser->get_parser()->get_tree();
2488
r_type.type.is_meta_type = true;
2489
r_type.type.is_constant = false;
2490
r_type.type.kind = GDScriptParser::DataType::CLASS;
2491
r_type.value = Variant();
2492
return true;
2493
}
2494
} else {
2495
Ref<Script> scr = ResourceLoader::load(ScriptServer::get_global_class_path(p_identifier->name));
2496
if (scr.is_valid()) {
2497
r_type = _type_from_variant(scr, p_context);
2498
r_type.type.is_meta_type = true;
2499
return true;
2500
}
2501
}
2502
return false;
2503
}
2504
2505
// Check global variables (including autoloads).
2506
if (GDScriptLanguage::get_singleton()->get_named_globals_map().has(p_identifier->name)) {
2507
r_type = _type_from_variant(GDScriptLanguage::get_singleton()->get_named_globals_map()[p_identifier->name], p_context);
2508
return true;
2509
}
2510
2511
// Check ClassDB.
2512
if (ClassDB::class_exists(p_identifier->name) && ClassDB::is_class_exposed(p_identifier->name)) {
2513
r_type.type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
2514
r_type.type.kind = GDScriptParser::DataType::NATIVE;
2515
r_type.type.builtin_type = Variant::OBJECT;
2516
r_type.type.native_type = p_identifier->name;
2517
r_type.type.is_constant = true;
2518
if (Engine::get_singleton()->has_singleton(p_identifier->name)) {
2519
r_type.type.is_meta_type = false;
2520
r_type.value = Engine::get_singleton()->get_singleton_object(p_identifier->name);
2521
} else {
2522
r_type.type.is_meta_type = true;
2523
r_type.value = Variant();
2524
}
2525
return true;
2526
}
2527
2528
return false;
2529
}
2530
2531
static bool _guess_identifier_type_from_base(GDScriptParser::CompletionContext &p_context, const GDScriptCompletionIdentifier &p_base, const StringName &p_identifier, GDScriptCompletionIdentifier &r_type) {
2532
static int recursion_depth = 0;
2533
RecursionCheck recursion(&recursion_depth);
2534
if (unlikely(recursion.check())) {
2535
ERR_FAIL_V_MSG(false, "Reached recursion limit while trying to guess type.");
2536
}
2537
2538
GDScriptParser::DataType base_type = p_base.type;
2539
bool is_static = base_type.is_meta_type;
2540
while (base_type.is_set()) {
2541
switch (base_type.kind) {
2542
case GDScriptParser::DataType::CLASS:
2543
if (base_type.class_type->has_member(p_identifier)) {
2544
const GDScriptParser::ClassNode::Member &member = base_type.class_type->get_member(p_identifier);
2545
switch (member.type) {
2546
case GDScriptParser::ClassNode::Member::CONSTANT:
2547
r_type.type = member.constant->get_datatype();
2548
if (member.constant->initializer && member.constant->initializer->is_constant) {
2549
r_type.value = member.constant->initializer->reduced_value;
2550
}
2551
return true;
2552
case GDScriptParser::ClassNode::Member::VARIABLE:
2553
if (!is_static || member.variable->is_static) {
2554
if (member.variable->get_datatype().is_set() && !member.variable->get_datatype().is_variant()) {
2555
r_type.type = member.variable->get_datatype();
2556
return true;
2557
} else if (member.variable->initializer) {
2558
const GDScriptParser::ExpressionNode *init = member.variable->initializer;
2559
if (init->is_constant) {
2560
r_type.value = init->reduced_value;
2561
r_type = _type_from_variant(init->reduced_value, p_context);
2562
return true;
2563
} else if (init->start_line == p_context.current_line) {
2564
return false;
2565
// Detects if variable is assigned to itself
2566
} else if (_is_expression_named_identifier(init, member.variable->identifier->name)) {
2567
if (member.variable->initializer->get_datatype().is_set()) {
2568
r_type.type = member.variable->initializer->get_datatype();
2569
} else if (member.variable->get_datatype().is_set() && !member.variable->get_datatype().is_variant()) {
2570
r_type.type = member.variable->get_datatype();
2571
}
2572
return true;
2573
} else if (_guess_expression_type(p_context, init, r_type)) {
2574
return true;
2575
} else if (init->get_datatype().is_set() && !init->get_datatype().is_variant()) {
2576
r_type.type = init->get_datatype();
2577
return true;
2578
}
2579
}
2580
}
2581
// TODO: Check assignments in constructor.
2582
return false;
2583
case GDScriptParser::ClassNode::Member::ENUM:
2584
r_type.type = member.m_enum->get_datatype();
2585
r_type.enumeration = member.m_enum->identifier->name;
2586
return true;
2587
case GDScriptParser::ClassNode::Member::ENUM_VALUE:
2588
r_type = _type_from_variant(member.enum_value.value, p_context);
2589
return true;
2590
case GDScriptParser::ClassNode::Member::SIGNAL:
2591
r_type.type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
2592
r_type.type.kind = GDScriptParser::DataType::BUILTIN;
2593
r_type.type.builtin_type = Variant::SIGNAL;
2594
r_type.type.method_info = member.signal->method_info;
2595
return true;
2596
case GDScriptParser::ClassNode::Member::FUNCTION:
2597
if (is_static && !member.function->is_static) {
2598
return false;
2599
}
2600
r_type = _callable_type_from_method_info(member.function->info);
2601
return true;
2602
case GDScriptParser::ClassNode::Member::CLASS:
2603
r_type.type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
2604
r_type.type.kind = GDScriptParser::DataType::CLASS;
2605
r_type.type.class_type = member.m_class;
2606
r_type.type.is_meta_type = true;
2607
return true;
2608
case GDScriptParser::ClassNode::Member::GROUP:
2609
return false; // No-op, but silences warnings.
2610
case GDScriptParser::ClassNode::Member::UNDEFINED:
2611
return false; // Unreachable.
2612
}
2613
return false;
2614
}
2615
base_type = base_type.class_type->base_type;
2616
break;
2617
case GDScriptParser::DataType::SCRIPT: {
2618
Ref<Script> scr = base_type.script_type;
2619
if (scr.is_valid()) {
2620
HashMap<StringName, Variant> constants;
2621
scr->get_constants(&constants);
2622
if (constants.has(p_identifier)) {
2623
r_type = _type_from_variant(constants[p_identifier], p_context);
2624
return true;
2625
}
2626
2627
List<PropertyInfo> members;
2628
if (is_static) {
2629
scr->get_property_list(&members);
2630
} else {
2631
scr->get_script_property_list(&members);
2632
}
2633
for (const PropertyInfo &prop : members) {
2634
if (prop.name == p_identifier) {
2635
r_type = _type_from_property(prop);
2636
return true;
2637
}
2638
}
2639
2640
if (scr->has_method(p_identifier)) {
2641
MethodInfo mi = scr->get_method_info(p_identifier);
2642
r_type = _callable_type_from_method_info(mi);
2643
return true;
2644
}
2645
2646
Ref<Script> parent = scr->get_base_script();
2647
if (parent.is_valid()) {
2648
base_type.script_type = parent;
2649
} else {
2650
base_type.kind = GDScriptParser::DataType::NATIVE;
2651
base_type.builtin_type = Variant::OBJECT;
2652
base_type.native_type = scr->get_instance_base_type();
2653
}
2654
} else {
2655
return false;
2656
}
2657
} break;
2658
case GDScriptParser::DataType::NATIVE: {
2659
StringName class_name = base_type.native_type;
2660
if (!ClassDB::class_exists(class_name)) {
2661
return false;
2662
}
2663
2664
// Skip constants since they're all integers. Type does not matter because int has no members.
2665
2666
PropertyInfo prop;
2667
if (ClassDB::get_property_info(class_name, p_identifier, &prop)) {
2668
StringName getter = ClassDB::get_property_getter(class_name, p_identifier);
2669
if (getter != StringName()) {
2670
MethodBind *g = ClassDB::get_method(class_name, getter);
2671
if (g) {
2672
r_type = _type_from_property(g->get_return_info());
2673
return true;
2674
}
2675
} else {
2676
r_type = _type_from_property(prop);
2677
return true;
2678
}
2679
}
2680
2681
MethodInfo method;
2682
if (ClassDB::get_method_info(class_name, p_identifier, &method)) {
2683
r_type = _callable_type_from_method_info(method);
2684
return true;
2685
}
2686
2687
if (ClassDB::has_enum(class_name, p_identifier)) {
2688
r_type.type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
2689
r_type.type.kind = GDScriptParser::DataType::ENUM;
2690
r_type.type.enum_type = p_identifier;
2691
r_type.type.is_constant = true;
2692
r_type.type.is_meta_type = true;
2693
r_type.type.native_type = String(class_name) + "." + p_identifier;
2694
return true;
2695
}
2696
2697
return false;
2698
} break;
2699
case GDScriptParser::DataType::BUILTIN: {
2700
if (Variant::has_builtin_method(base_type.builtin_type, p_identifier)) {
2701
r_type = _callable_type_from_method_info(Variant::get_builtin_method_info(base_type.builtin_type, p_identifier));
2702
return true;
2703
} else {
2704
Callable::CallError err;
2705
Variant tmp;
2706
Variant::construct(base_type.builtin_type, tmp, nullptr, 0, err);
2707
2708
if (err.error != Callable::CallError::CALL_OK) {
2709
return false;
2710
}
2711
bool valid = false;
2712
Variant res = tmp.get(p_identifier, &valid);
2713
if (valid) {
2714
r_type = _type_from_variant(res, p_context);
2715
r_type.value = Variant();
2716
r_type.type.is_constant = false;
2717
return true;
2718
}
2719
}
2720
return false;
2721
} break;
2722
default: {
2723
return false;
2724
} break;
2725
}
2726
}
2727
return false;
2728
}
2729
2730
static void _find_last_return_in_block(GDScriptParser::CompletionContext &p_context, int &r_last_return_line, const GDScriptParser::ExpressionNode **r_last_returned_value) {
2731
if (!p_context.current_suite) {
2732
return;
2733
}
2734
2735
for (int i = 0; i < p_context.current_suite->statements.size(); i++) {
2736
if (p_context.current_suite->statements[i]->start_line < r_last_return_line) {
2737
break;
2738
}
2739
2740
GDScriptParser::CompletionContext c = p_context;
2741
switch (p_context.current_suite->statements[i]->type) {
2742
case GDScriptParser::Node::FOR:
2743
c.current_suite = static_cast<const GDScriptParser::ForNode *>(p_context.current_suite->statements[i])->loop;
2744
_find_last_return_in_block(c, r_last_return_line, r_last_returned_value);
2745
break;
2746
case GDScriptParser::Node::WHILE:
2747
c.current_suite = static_cast<const GDScriptParser::WhileNode *>(p_context.current_suite->statements[i])->loop;
2748
_find_last_return_in_block(c, r_last_return_line, r_last_returned_value);
2749
break;
2750
case GDScriptParser::Node::IF: {
2751
const GDScriptParser::IfNode *_if = static_cast<const GDScriptParser::IfNode *>(p_context.current_suite->statements[i]);
2752
c.current_suite = _if->true_block;
2753
_find_last_return_in_block(c, r_last_return_line, r_last_returned_value);
2754
if (_if->false_block) {
2755
c.current_suite = _if->false_block;
2756
_find_last_return_in_block(c, r_last_return_line, r_last_returned_value);
2757
}
2758
} break;
2759
case GDScriptParser::Node::MATCH: {
2760
const GDScriptParser::MatchNode *match = static_cast<const GDScriptParser::MatchNode *>(p_context.current_suite->statements[i]);
2761
for (int j = 0; j < match->branches.size(); j++) {
2762
c.current_suite = match->branches[j]->block;
2763
_find_last_return_in_block(c, r_last_return_line, r_last_returned_value);
2764
}
2765
} break;
2766
case GDScriptParser::Node::RETURN: {
2767
const GDScriptParser::ReturnNode *ret = static_cast<const GDScriptParser::ReturnNode *>(p_context.current_suite->statements[i]);
2768
if (ret->return_value) {
2769
if (ret->start_line > r_last_return_line) {
2770
r_last_return_line = ret->start_line;
2771
*r_last_returned_value = ret->return_value;
2772
}
2773
}
2774
} break;
2775
default:
2776
break;
2777
}
2778
}
2779
}
2780
2781
static bool _guess_method_return_type_from_base(GDScriptParser::CompletionContext &p_context, const GDScriptCompletionIdentifier &p_base, const StringName &p_method, GDScriptCompletionIdentifier &r_type) {
2782
static int recursion_depth = 0;
2783
RecursionCheck recursion(&recursion_depth);
2784
if (unlikely(recursion.check())) {
2785
ERR_FAIL_V_MSG(false, "Reached recursion limit while trying to guess type.");
2786
}
2787
2788
GDScriptParser::DataType base_type = p_base.type;
2789
bool is_static = base_type.is_meta_type;
2790
2791
if (is_static && p_method == SNAME("new")) {
2792
r_type.type = base_type;
2793
r_type.type.is_meta_type = false;
2794
r_type.type.is_constant = false;
2795
return true;
2796
}
2797
2798
while (base_type.is_set() && !base_type.is_variant()) {
2799
switch (base_type.kind) {
2800
case GDScriptParser::DataType::CLASS:
2801
if (base_type.class_type->has_function(p_method)) {
2802
GDScriptParser::FunctionNode *method = base_type.class_type->get_member(p_method).function;
2803
if (!is_static || method->is_static) {
2804
if (method->get_datatype().is_set() && !method->get_datatype().is_variant()) {
2805
r_type.type = method->get_datatype();
2806
return true;
2807
}
2808
2809
int last_return_line = -1;
2810
const GDScriptParser::ExpressionNode *last_returned_value = nullptr;
2811
GDScriptParser::CompletionContext c = p_context;
2812
c.current_class = base_type.class_type;
2813
c.current_function = method;
2814
c.current_suite = method->body;
2815
2816
_find_last_return_in_block(c, last_return_line, &last_returned_value);
2817
if (last_returned_value) {
2818
c.current_line = c.current_suite->end_line;
2819
if (_guess_expression_type(c, last_returned_value, r_type)) {
2820
return true;
2821
}
2822
}
2823
}
2824
}
2825
base_type = base_type.class_type->base_type;
2826
break;
2827
case GDScriptParser::DataType::SCRIPT: {
2828
Ref<Script> scr = base_type.script_type;
2829
if (scr.is_valid()) {
2830
List<MethodInfo> methods;
2831
scr->get_script_method_list(&methods);
2832
for (const MethodInfo &mi : methods) {
2833
if (mi.name == p_method) {
2834
r_type = _type_from_property(mi.return_val);
2835
return true;
2836
}
2837
}
2838
Ref<Script> base_script = scr->get_base_script();
2839
if (base_script.is_valid()) {
2840
base_type.script_type = base_script;
2841
} else {
2842
base_type.kind = GDScriptParser::DataType::NATIVE;
2843
base_type.builtin_type = Variant::OBJECT;
2844
base_type.native_type = scr->get_instance_base_type();
2845
}
2846
} else {
2847
return false;
2848
}
2849
} break;
2850
case GDScriptParser::DataType::NATIVE: {
2851
if (!ClassDB::class_exists(base_type.native_type)) {
2852
return false;
2853
}
2854
MethodBind *mb = ClassDB::get_method(base_type.native_type, p_method);
2855
if (mb) {
2856
r_type = _type_from_property(mb->get_return_info());
2857
return true;
2858
}
2859
return false;
2860
} break;
2861
case GDScriptParser::DataType::BUILTIN: {
2862
Callable::CallError err;
2863
Variant tmp;
2864
Variant::construct(base_type.builtin_type, tmp, nullptr, 0, err);
2865
if (err.error != Callable::CallError::CALL_OK) {
2866
return false;
2867
}
2868
2869
List<MethodInfo> methods;
2870
tmp.get_method_list(&methods);
2871
2872
for (const MethodInfo &mi : methods) {
2873
if (mi.name == p_method) {
2874
r_type = _type_from_property(mi.return_val);
2875
return true;
2876
}
2877
}
2878
return false;
2879
} break;
2880
default: {
2881
return false;
2882
}
2883
}
2884
}
2885
2886
return false;
2887
}
2888
2889
static bool _guess_expecting_callable(GDScriptParser::CompletionContext &p_context) {
2890
if (p_context.call.call != nullptr && p_context.call.call->type == GDScriptParser::Node::CALL) {
2891
GDScriptParser::CallNode *call_node = static_cast<GDScriptParser::CallNode *>(p_context.call.call);
2892
GDScriptCompletionIdentifier ci;
2893
if (_guess_expression_type(p_context, call_node->callee, ci)) {
2894
if (ci.type.kind == GDScriptParser::DataType::BUILTIN && ci.type.builtin_type == Variant::CALLABLE) {
2895
if (p_context.call.argument >= 0 && p_context.call.argument < ci.type.method_info.arguments.size()) {
2896
return ci.type.method_info.arguments.get(p_context.call.argument).type == Variant::CALLABLE;
2897
}
2898
}
2899
}
2900
}
2901
2902
return false;
2903
}
2904
2905
static void _find_enumeration_candidates(GDScriptParser::CompletionContext &p_context, const String &p_enum_hint, HashMap<String, ScriptLanguage::CodeCompletionOption> &r_result) {
2906
if (!p_enum_hint.contains_char('.')) {
2907
// Global constant or in the current class.
2908
StringName current_enum = p_enum_hint;
2909
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) {
2910
const GDScriptParser::EnumNode *_enum = p_context.current_class->get_member(current_enum).m_enum;
2911
for (int i = 0; i < _enum->values.size(); i++) {
2912
ScriptLanguage::CodeCompletionOption option(_enum->values[i].identifier->name, ScriptLanguage::CODE_COMPLETION_KIND_ENUM);
2913
r_result.insert(option.display, option);
2914
}
2915
} else {
2916
for (int i = 0; i < CoreConstants::get_global_constant_count(); i++) {
2917
if (CoreConstants::get_global_constant_enum(i) == current_enum) {
2918
ScriptLanguage::CodeCompletionOption option(CoreConstants::get_global_constant_name(i), ScriptLanguage::CODE_COMPLETION_KIND_ENUM);
2919
r_result.insert(option.display, option);
2920
}
2921
}
2922
}
2923
} else {
2924
String class_name = p_enum_hint.get_slicec('.', 0);
2925
String enum_name = p_enum_hint.get_slicec('.', 1);
2926
2927
if (!ClassDB::class_exists(class_name)) {
2928
return;
2929
}
2930
2931
List<StringName> enum_constants;
2932
ClassDB::get_enum_constants(class_name, enum_name, &enum_constants);
2933
for (const StringName &E : enum_constants) {
2934
String candidate = class_name + "." + E;
2935
int location = _get_enum_constant_location(class_name, E);
2936
ScriptLanguage::CodeCompletionOption option(candidate, ScriptLanguage::CODE_COMPLETION_KIND_ENUM, location);
2937
r_result.insert(option.display, option);
2938
}
2939
}
2940
}
2941
2942
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) {
2943
Variant base = p_base.value;
2944
GDScriptParser::DataType base_type = p_base.type;
2945
const StringName &method = p_call->function_name;
2946
2947
const String quote_style = EDITOR_GET("text_editor/completion/use_single_quotes") ? "'" : "\"";
2948
const bool use_string_names = EDITOR_GET("text_editor/completion/add_string_name_literals");
2949
const bool use_node_paths = EDITOR_GET("text_editor/completion/add_node_path_literals");
2950
2951
while (base_type.is_set() && !base_type.is_variant()) {
2952
switch (base_type.kind) {
2953
case GDScriptParser::DataType::CLASS: {
2954
if (base_type.is_meta_type && method == SNAME("new")) {
2955
const GDScriptParser::ClassNode *current = base_type.class_type;
2956
2957
do {
2958
if (current->has_member("_init")) {
2959
const GDScriptParser::ClassNode::Member &member = current->get_member("_init");
2960
2961
if (member.type == GDScriptParser::ClassNode::Member::FUNCTION) {
2962
r_arghint = base_type.class_type->get_datatype().to_string() + " new" + _make_arguments_hint(member.function, p_argidx, true);
2963
return;
2964
}
2965
}
2966
current = current->base_type.class_type;
2967
} while (current != nullptr);
2968
2969
r_arghint = base_type.class_type->get_datatype().to_string() + " new()";
2970
return;
2971
}
2972
2973
if (base_type.class_type->has_member(method)) {
2974
const GDScriptParser::ClassNode::Member &member = base_type.class_type->get_member(method);
2975
2976
if (member.type == GDScriptParser::ClassNode::Member::FUNCTION) {
2977
r_arghint = _make_arguments_hint(member.function, p_argidx);
2978
return;
2979
}
2980
}
2981
2982
base_type = base_type.class_type->base_type;
2983
} break;
2984
case GDScriptParser::DataType::SCRIPT: {
2985
if (base_type.script_type->is_valid() && base_type.script_type->has_method(method)) {
2986
r_arghint = _make_arguments_hint(base_type.script_type->get_method_info(method), p_argidx);
2987
return;
2988
}
2989
Ref<Script> base_script = base_type.script_type->get_base_script();
2990
if (base_script.is_valid()) {
2991
base_type.script_type = base_script;
2992
} else {
2993
base_type.kind = GDScriptParser::DataType::NATIVE;
2994
base_type.builtin_type = Variant::OBJECT;
2995
base_type.native_type = base_type.script_type->get_instance_base_type();
2996
}
2997
} break;
2998
case GDScriptParser::DataType::NATIVE: {
2999
StringName class_name = base_type.native_type;
3000
if (!ClassDB::class_exists(class_name)) {
3001
base_type.kind = GDScriptParser::DataType::UNRESOLVED;
3002
break;
3003
}
3004
3005
MethodInfo info;
3006
int method_args = 0;
3007
3008
if (ClassDB::get_method_info(class_name, method, &info)) {
3009
method_args = info.arguments.size();
3010
if (base.get_type() == Variant::OBJECT) {
3011
Object *obj = base.operator Object *();
3012
if (obj) {
3013
List<String> options;
3014
obj->get_argument_options(method, p_argidx, &options);
3015
for (String &opt : options) {
3016
// Handle user preference.
3017
if (opt.is_quoted()) {
3018
opt = opt.unquote().quote(quote_style);
3019
if (use_string_names && info.arguments[p_argidx].type == Variant::STRING_NAME) {
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
} else if (use_node_paths && info.arguments[p_argidx].type == Variant::NODE_PATH) {
3029
if (p_call->arguments.size() > p_argidx && p_call->arguments[p_argidx] && p_call->arguments[p_argidx]->type == GDScriptParser::Node::LITERAL) {
3030
GDScriptParser::LiteralNode *literal = static_cast<GDScriptParser::LiteralNode *>(p_call->arguments[p_argidx]);
3031
if (literal->value.get_type() == Variant::STRING) {
3032
opt = "^" + opt;
3033
}
3034
} else {
3035
opt = "^" + opt;
3036
}
3037
}
3038
}
3039
ScriptLanguage::CodeCompletionOption option(opt, ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION);
3040
r_result.insert(option.display, option);
3041
}
3042
}
3043
}
3044
3045
if (p_argidx < method_args) {
3046
const PropertyInfo &arg_info = info.arguments[p_argidx];
3047
if (arg_info.usage & (PROPERTY_USAGE_CLASS_IS_ENUM | PROPERTY_USAGE_CLASS_IS_BITFIELD)) {
3048
_find_enumeration_candidates(p_context, arg_info.class_name, r_result);
3049
}
3050
}
3051
3052
r_arghint = _make_arguments_hint(info, p_argidx);
3053
}
3054
3055
if (p_argidx == 1 && p_call && ClassDB::is_parent_class(class_name, SNAME("Tween")) && method == SNAME("tween_property")) {
3056
// Get tweened objects properties.
3057
if (p_call->arguments.is_empty()) {
3058
base_type.kind = GDScriptParser::DataType::UNRESOLVED;
3059
break;
3060
}
3061
GDScriptParser::ExpressionNode *tweened_object = p_call->arguments[0];
3062
if (!tweened_object) {
3063
base_type.kind = GDScriptParser::DataType::UNRESOLVED;
3064
break;
3065
}
3066
StringName native_type = tweened_object->datatype.native_type;
3067
switch (tweened_object->datatype.kind) {
3068
case GDScriptParser::DataType::SCRIPT: {
3069
Ref<Script> script = tweened_object->datatype.script_type;
3070
native_type = script->get_instance_base_type();
3071
int n = 0;
3072
while (script.is_valid()) {
3073
List<PropertyInfo> properties;
3074
script->get_script_property_list(&properties);
3075
for (const PropertyInfo &E : properties) {
3076
if (E.usage & (PROPERTY_USAGE_SUBGROUP | PROPERTY_USAGE_GROUP | PROPERTY_USAGE_CATEGORY | PROPERTY_USAGE_INTERNAL)) {
3077
continue;
3078
}
3079
String name = E.name.quote(quote_style);
3080
if (use_node_paths) {
3081
if (p_call->arguments.size() > p_argidx && p_call->arguments[p_argidx] && p_call->arguments[p_argidx]->type == GDScriptParser::Node::LITERAL) {
3082
GDScriptParser::LiteralNode *literal = static_cast<GDScriptParser::LiteralNode *>(p_call->arguments[p_argidx]);
3083
if (literal->value.get_type() == Variant::STRING) {
3084
name = "^" + name;
3085
}
3086
} else {
3087
name = "^" + name;
3088
}
3089
}
3090
ScriptLanguage::CodeCompletionOption option(name, ScriptLanguage::CODE_COMPLETION_KIND_MEMBER, ScriptLanguage::CodeCompletionLocation::LOCATION_LOCAL + n);
3091
r_result.insert(option.display, option);
3092
}
3093
script = script->get_base_script();
3094
n++;
3095
}
3096
} break;
3097
case GDScriptParser::DataType::CLASS: {
3098
GDScriptParser::ClassNode *clss = tweened_object->datatype.class_type;
3099
native_type = clss->base_type.native_type;
3100
int n = 0;
3101
while (clss) {
3102
for (GDScriptParser::ClassNode::Member member : clss->members) {
3103
if (member.type == GDScriptParser::ClassNode::Member::VARIABLE) {
3104
String name = member.get_name().quote(quote_style);
3105
if (use_node_paths) {
3106
if (p_call->arguments.size() > p_argidx && p_call->arguments[p_argidx] && p_call->arguments[p_argidx]->type == GDScriptParser::Node::LITERAL) {
3107
GDScriptParser::LiteralNode *literal = static_cast<GDScriptParser::LiteralNode *>(p_call->arguments[p_argidx]);
3108
if (literal->value.get_type() == Variant::STRING) {
3109
name = "^" + name;
3110
}
3111
} else {
3112
name = "^" + name;
3113
}
3114
}
3115
ScriptLanguage::CodeCompletionOption option(name, ScriptLanguage::CODE_COMPLETION_KIND_MEMBER, ScriptLanguage::CodeCompletionLocation::LOCATION_LOCAL + n);
3116
r_result.insert(option.display, option);
3117
}
3118
}
3119
if (clss->base_type.kind == GDScriptParser::DataType::Kind::CLASS) {
3120
clss = clss->base_type.class_type;
3121
n++;
3122
} else {
3123
native_type = clss->base_type.native_type;
3124
clss = nullptr;
3125
}
3126
}
3127
} break;
3128
default:
3129
break;
3130
}
3131
3132
List<PropertyInfo> properties;
3133
ClassDB::get_property_list(native_type, &properties);
3134
for (const PropertyInfo &E : properties) {
3135
if (E.usage & (PROPERTY_USAGE_SUBGROUP | PROPERTY_USAGE_GROUP | PROPERTY_USAGE_CATEGORY | PROPERTY_USAGE_INTERNAL)) {
3136
continue;
3137
}
3138
String name = E.name.quote(quote_style);
3139
if (use_node_paths) {
3140
if (p_call->arguments.size() > p_argidx && p_call->arguments[p_argidx] && p_call->arguments[p_argidx]->type == GDScriptParser::Node::LITERAL) {
3141
GDScriptParser::LiteralNode *literal = static_cast<GDScriptParser::LiteralNode *>(p_call->arguments[p_argidx]);
3142
if (literal->value.get_type() == Variant::STRING) {
3143
name = "^" + name;
3144
}
3145
} else {
3146
name = "^" + name;
3147
}
3148
}
3149
ScriptLanguage::CodeCompletionOption option(name, ScriptLanguage::CODE_COMPLETION_KIND_MEMBER);
3150
r_result.insert(option.display, option);
3151
}
3152
}
3153
3154
if (p_argidx == 0 && ClassDB::is_parent_class(class_name, SNAME("Node")) && (method == SNAME("get_node") || method == SNAME("has_node"))) {
3155
// Get autoloads
3156
List<PropertyInfo> props;
3157
ProjectSettings::get_singleton()->get_property_list(&props);
3158
3159
for (const PropertyInfo &E : props) {
3160
String s = E.name;
3161
if (!s.begins_with("autoload/")) {
3162
continue;
3163
}
3164
String name = s.get_slicec('/', 1);
3165
String path = ("/root/" + name).quote(quote_style);
3166
if (use_node_paths) {
3167
if (p_call->arguments.size() > p_argidx && p_call->arguments[p_argidx] && p_call->arguments[p_argidx]->type == GDScriptParser::Node::LITERAL) {
3168
GDScriptParser::LiteralNode *literal = static_cast<GDScriptParser::LiteralNode *>(p_call->arguments[p_argidx]);
3169
if (literal->value.get_type() == Variant::STRING) {
3170
path = "^" + path;
3171
}
3172
} else {
3173
path = "^" + path;
3174
}
3175
}
3176
ScriptLanguage::CodeCompletionOption option(path, ScriptLanguage::CODE_COMPLETION_KIND_NODE_PATH);
3177
r_result.insert(option.display, option);
3178
}
3179
}
3180
3181
if (p_argidx == 0 && method_args > 0 && ClassDB::is_parent_class(class_name, SNAME("InputEvent")) && method.operator String().contains("action")) {
3182
// Get input actions
3183
List<PropertyInfo> props;
3184
ProjectSettings::get_singleton()->get_property_list(&props);
3185
for (const PropertyInfo &E : props) {
3186
String s = E.name;
3187
if (!s.begins_with("input/")) {
3188
continue;
3189
}
3190
String name = s.get_slicec('/', 1).quote(quote_style);
3191
if (use_string_names) {
3192
if (p_call->arguments.size() > p_argidx && p_call->arguments[p_argidx] && p_call->arguments[p_argidx]->type == GDScriptParser::Node::LITERAL) {
3193
GDScriptParser::LiteralNode *literal = static_cast<GDScriptParser::LiteralNode *>(p_call->arguments[p_argidx]);
3194
if (literal->value.get_type() == Variant::STRING) {
3195
name = "&" + name;
3196
}
3197
} else {
3198
name = "&" + name;
3199
}
3200
}
3201
ScriptLanguage::CodeCompletionOption option(name, ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT);
3202
r_result.insert(option.display, option);
3203
}
3204
}
3205
if (EDITOR_GET("text_editor/completion/complete_file_paths")) {
3206
if (p_argidx == 0 && method == SNAME("change_scene_to_file") && ClassDB::is_parent_class(class_name, SNAME("SceneTree"))) {
3207
HashMap<String, ScriptLanguage::CodeCompletionOption> list;
3208
_get_directory_contents(EditorFileSystem::get_singleton()->get_filesystem(), list, SNAME("PackedScene"));
3209
for (const KeyValue<String, ScriptLanguage::CodeCompletionOption> &key_value_pair : list) {
3210
ScriptLanguage::CodeCompletionOption option = key_value_pair.value;
3211
r_result.insert(option.display, option);
3212
}
3213
}
3214
}
3215
3216
base_type.kind = GDScriptParser::DataType::UNRESOLVED;
3217
} break;
3218
case GDScriptParser::DataType::BUILTIN: {
3219
if (base.get_type() == Variant::NIL) {
3220
Callable::CallError err;
3221
Variant::construct(base_type.builtin_type, base, nullptr, 0, err);
3222
if (err.error != Callable::CallError::CALL_OK) {
3223
return;
3224
}
3225
}
3226
3227
List<MethodInfo> methods;
3228
base.get_method_list(&methods);
3229
for (const MethodInfo &E : methods) {
3230
if (E.name == method) {
3231
r_arghint = _make_arguments_hint(E, p_argidx);
3232
return;
3233
}
3234
}
3235
3236
base_type.kind = GDScriptParser::DataType::UNRESOLVED;
3237
} break;
3238
default: {
3239
base_type.kind = GDScriptParser::DataType::UNRESOLVED;
3240
} break;
3241
}
3242
}
3243
}
3244
3245
static bool _get_subscript_type(GDScriptParser::CompletionContext &p_context, const GDScriptParser::SubscriptNode *p_subscript, GDScriptParser::DataType &r_base_type, Variant *r_base = nullptr) {
3246
if (p_context.base == nullptr) {
3247
return false;
3248
}
3249
3250
const GDScriptParser::GetNodeNode *get_node = nullptr;
3251
3252
switch (p_subscript->base->type) {
3253
case GDScriptParser::Node::GET_NODE: {
3254
get_node = static_cast<GDScriptParser::GetNodeNode *>(p_subscript->base);
3255
} break;
3256
3257
case GDScriptParser::Node::IDENTIFIER: {
3258
const GDScriptParser::IdentifierNode *identifier_node = static_cast<GDScriptParser::IdentifierNode *>(p_subscript->base);
3259
3260
switch (identifier_node->source) {
3261
case GDScriptParser::IdentifierNode::Source::MEMBER_VARIABLE: {
3262
if (p_context.current_class != nullptr) {
3263
const StringName &member_name = identifier_node->name;
3264
const GDScriptParser::ClassNode *current_class = p_context.current_class;
3265
3266
if (current_class->has_member(member_name)) {
3267
const GDScriptParser::ClassNode::Member &member = current_class->get_member(member_name);
3268
3269
if (member.type == GDScriptParser::ClassNode::Member::VARIABLE) {
3270
const GDScriptParser::VariableNode *variable = static_cast<GDScriptParser::VariableNode *>(member.variable);
3271
3272
if (variable->initializer && variable->initializer->type == GDScriptParser::Node::GET_NODE) {
3273
get_node = static_cast<GDScriptParser::GetNodeNode *>(variable->initializer);
3274
}
3275
}
3276
}
3277
}
3278
} break;
3279
case GDScriptParser::IdentifierNode::Source::LOCAL_VARIABLE: {
3280
// TODO: Do basic assignment flow analysis like in `_guess_expression_type`.
3281
const GDScriptParser::SuiteNode::Local local = identifier_node->suite->get_local(identifier_node->name);
3282
switch (local.type) {
3283
case GDScriptParser::SuiteNode::Local::CONSTANT: {
3284
if (local.constant->initializer && local.constant->initializer->type == GDScriptParser::Node::GET_NODE) {
3285
get_node = static_cast<GDScriptParser::GetNodeNode *>(local.constant->initializer);
3286
}
3287
} break;
3288
case GDScriptParser::SuiteNode::Local::VARIABLE: {
3289
if (local.variable->initializer && local.variable->initializer->type == GDScriptParser::Node::GET_NODE) {
3290
get_node = static_cast<GDScriptParser::GetNodeNode *>(local.variable->initializer);
3291
}
3292
} break;
3293
default: {
3294
} break;
3295
}
3296
} break;
3297
default: {
3298
} break;
3299
}
3300
} break;
3301
default: {
3302
} break;
3303
}
3304
3305
if (get_node != nullptr) {
3306
const Object *node = p_context.base->call("get_node_or_null", NodePath(get_node->full_path));
3307
if (node != nullptr) {
3308
GDScriptParser::DataType assigned_type = _type_from_variant(node, p_context).type;
3309
GDScriptParser::DataType base_type = p_subscript->base->datatype;
3310
3311
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)) {
3312
// Annotated type takes precedence.
3313
return false;
3314
}
3315
3316
if (r_base != nullptr) {
3317
*r_base = node;
3318
}
3319
3320
r_base_type.type_source = GDScriptParser::DataType::INFERRED;
3321
r_base_type.builtin_type = Variant::OBJECT;
3322
r_base_type.native_type = node->get_class_name();
3323
3324
Ref<Script> scr = node->get_script();
3325
if (scr.is_null()) {
3326
r_base_type.kind = GDScriptParser::DataType::NATIVE;
3327
} else {
3328
r_base_type.kind = GDScriptParser::DataType::SCRIPT;
3329
r_base_type.script_type = scr;
3330
}
3331
3332
return true;
3333
}
3334
}
3335
3336
return false;
3337
}
3338
3339
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) {
3340
if (p_call->type == GDScriptParser::Node::PRELOAD) {
3341
if (p_argidx == 0 && bool(EDITOR_GET("text_editor/completion/complete_file_paths"))) {
3342
_get_directory_contents(EditorFileSystem::get_singleton()->get_filesystem(), r_result);
3343
}
3344
3345
MethodInfo mi(PropertyInfo(Variant::OBJECT, "resource", PROPERTY_HINT_RESOURCE_TYPE, "Resource"), "preload", PropertyInfo(Variant::STRING, "path"));
3346
r_arghint = _make_arguments_hint(mi, p_argidx);
3347
return;
3348
} else if (p_call->type != GDScriptParser::Node::CALL) {
3349
return;
3350
}
3351
3352
Variant base;
3353
GDScriptParser::DataType base_type;
3354
bool _static = false;
3355
const GDScriptParser::CallNode *call = static_cast<const GDScriptParser::CallNode *>(p_call);
3356
GDScriptParser::Node::Type callee_type = call->get_callee_type();
3357
3358
if (callee_type == GDScriptParser::Node::SUBSCRIPT) {
3359
const GDScriptParser::SubscriptNode *subscript = static_cast<const GDScriptParser::SubscriptNode *>(call->callee);
3360
3361
if (subscript->base != nullptr && subscript->base->type == GDScriptParser::Node::IDENTIFIER) {
3362
const GDScriptParser::IdentifierNode *base_identifier = static_cast<const GDScriptParser::IdentifierNode *>(subscript->base);
3363
3364
Variant::Type method_type = GDScriptParser::get_builtin_type(base_identifier->name);
3365
if (method_type < Variant::VARIANT_MAX) {
3366
Variant v;
3367
Callable::CallError err;
3368
Variant::construct(method_type, v, nullptr, 0, err);
3369
if (err.error != Callable::CallError::CALL_OK) {
3370
return;
3371
}
3372
List<MethodInfo> methods;
3373
v.get_method_list(&methods);
3374
3375
for (MethodInfo &E : methods) {
3376
if (p_argidx >= E.arguments.size()) {
3377
continue;
3378
}
3379
if (E.name == call->function_name) {
3380
r_arghint += _make_arguments_hint(E, p_argidx);
3381
return;
3382
}
3383
}
3384
}
3385
}
3386
3387
if (subscript->is_attribute) {
3388
bool found_type = _get_subscript_type(p_context, subscript, base_type, &base);
3389
3390
if (!found_type) {
3391
GDScriptCompletionIdentifier ci;
3392
if (_guess_expression_type(p_context, subscript->base, ci)) {
3393
base_type = ci.type;
3394
base = ci.value;
3395
} else {
3396
return;
3397
}
3398
}
3399
3400
_static = base_type.is_meta_type;
3401
}
3402
} else if (Variant::has_utility_function(call->function_name)) {
3403
MethodInfo info = Variant::get_utility_function_info(call->function_name);
3404
r_arghint = _make_arguments_hint(info, p_argidx);
3405
return;
3406
} else if (GDScriptUtilityFunctions::function_exists(call->function_name)) {
3407
MethodInfo info = GDScriptUtilityFunctions::get_function_info(call->function_name);
3408
r_arghint = _make_arguments_hint(info, p_argidx);
3409
return;
3410
} else if (GDScriptParser::get_builtin_type(call->function_name) < Variant::VARIANT_MAX) {
3411
// Complete constructor.
3412
List<MethodInfo> constructors;
3413
Variant::get_constructor_list(GDScriptParser::get_builtin_type(call->function_name), &constructors);
3414
3415
int i = 0;
3416
for (const MethodInfo &E : constructors) {
3417
if (p_argidx >= E.arguments.size()) {
3418
continue;
3419
}
3420
if (i > 0) {
3421
r_arghint += "\n";
3422
}
3423
r_arghint += _make_arguments_hint(E, p_argidx);
3424
i++;
3425
}
3426
return;
3427
} else if (call->is_super || callee_type == GDScriptParser::Node::IDENTIFIER) {
3428
base = p_context.base;
3429
3430
if (p_context.current_class) {
3431
base_type = p_context.current_class->get_datatype();
3432
_static = !p_context.current_function || p_context.current_function->is_static;
3433
}
3434
} else {
3435
return;
3436
}
3437
3438
GDScriptCompletionIdentifier ci;
3439
ci.type = base_type;
3440
ci.value = base;
3441
_list_call_arguments(p_context, ci, call, p_argidx, _static, r_result, r_arghint);
3442
3443
r_forced = r_result.size() > 0;
3444
}
3445
3446
::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) {
3447
const String quote_style = EDITOR_GET("text_editor/completion/use_single_quotes") ? "'" : "\"";
3448
3449
GDScriptParser parser;
3450
GDScriptAnalyzer analyzer(&parser);
3451
3452
parser.parse(p_code, p_path, true);
3453
analyzer.analyze();
3454
3455
r_forced = false;
3456
HashMap<String, ScriptLanguage::CodeCompletionOption> options;
3457
3458
GDScriptParser::CompletionContext completion_context = parser.get_completion_context();
3459
if (completion_context.current_class != nullptr && completion_context.current_class->outer == nullptr) {
3460
completion_context.base = p_owner;
3461
}
3462
bool is_function = false;
3463
3464
switch (completion_context.type) {
3465
case GDScriptParser::COMPLETION_NONE:
3466
break;
3467
case GDScriptParser::COMPLETION_ANNOTATION: {
3468
List<MethodInfo> annotations;
3469
parser.get_annotation_list(&annotations);
3470
for (const MethodInfo &E : annotations) {
3471
ScriptLanguage::CodeCompletionOption option(E.name.substr(1), ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
3472
if (E.arguments.size() > 0) {
3473
option.insert_text += "(";
3474
}
3475
options.insert(option.display, option);
3476
}
3477
r_forced = true;
3478
} break;
3479
case GDScriptParser::COMPLETION_ANNOTATION_ARGUMENTS: {
3480
if (completion_context.node == nullptr || completion_context.node->type != GDScriptParser::Node::ANNOTATION) {
3481
break;
3482
}
3483
const GDScriptParser::AnnotationNode *annotation = static_cast<const GDScriptParser::AnnotationNode *>(completion_context.node);
3484
_find_annotation_arguments(annotation, completion_context.current_argument, quote_style, options, r_call_hint);
3485
r_forced = true;
3486
} break;
3487
case GDScriptParser::COMPLETION_BUILT_IN_TYPE_CONSTANT_OR_STATIC_METHOD: {
3488
// Constants.
3489
{
3490
List<StringName> constants;
3491
Variant::get_constants_for_type(completion_context.builtin_type, &constants);
3492
for (const StringName &E : constants) {
3493
ScriptLanguage::CodeCompletionOption option(E, ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT);
3494
bool valid = false;
3495
Variant default_value = Variant::get_constant_value(completion_context.builtin_type, E, &valid);
3496
if (valid) {
3497
option.default_value = default_value;
3498
}
3499
options.insert(option.display, option);
3500
}
3501
}
3502
// Methods.
3503
{
3504
List<StringName> methods;
3505
Variant::get_builtin_method_list(completion_context.builtin_type, &methods);
3506
for (const StringName &E : methods) {
3507
if (Variant::is_builtin_method_static(completion_context.builtin_type, E)) {
3508
ScriptLanguage::CodeCompletionOption option(E, ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION);
3509
if (!_guess_expecting_callable(completion_context)) {
3510
if (Variant::get_builtin_method_argument_count(completion_context.builtin_type, E) > 0 || Variant::is_builtin_method_vararg(completion_context.builtin_type, E)) {
3511
option.insert_text += "(";
3512
} else {
3513
option.insert_text += "()";
3514
}
3515
}
3516
options.insert(option.display, option);
3517
}
3518
}
3519
}
3520
} break;
3521
case GDScriptParser::COMPLETION_INHERIT_TYPE: {
3522
_list_available_types(true, completion_context, options);
3523
r_forced = true;
3524
} break;
3525
case GDScriptParser::COMPLETION_TYPE_NAME_OR_VOID: {
3526
ScriptLanguage::CodeCompletionOption option("void", ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
3527
options.insert(option.display, option);
3528
}
3529
[[fallthrough]];
3530
case GDScriptParser::COMPLETION_TYPE_NAME: {
3531
_list_available_types(false, completion_context, options);
3532
r_forced = true;
3533
} break;
3534
case GDScriptParser::COMPLETION_PROPERTY_DECLARATION_OR_TYPE: {
3535
_list_available_types(false, completion_context, options);
3536
ScriptLanguage::CodeCompletionOption get("get", ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
3537
options.insert(get.display, get);
3538
ScriptLanguage::CodeCompletionOption set("set", ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
3539
options.insert(set.display, set);
3540
r_forced = true;
3541
} break;
3542
case GDScriptParser::COMPLETION_PROPERTY_DECLARATION: {
3543
ScriptLanguage::CodeCompletionOption get("get", ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
3544
options.insert(get.display, get);
3545
ScriptLanguage::CodeCompletionOption set("set", ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
3546
options.insert(set.display, set);
3547
r_forced = true;
3548
} break;
3549
case GDScriptParser::COMPLETION_PROPERTY_METHOD: {
3550
if (!completion_context.current_class) {
3551
break;
3552
}
3553
for (int i = 0; i < completion_context.current_class->members.size(); i++) {
3554
const GDScriptParser::ClassNode::Member &member = completion_context.current_class->members[i];
3555
if (member.type != GDScriptParser::ClassNode::Member::FUNCTION) {
3556
continue;
3557
}
3558
if (member.function->is_static) {
3559
continue;
3560
}
3561
ScriptLanguage::CodeCompletionOption option(member.function->identifier->name, ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION);
3562
options.insert(option.display, option);
3563
}
3564
r_forced = true;
3565
} break;
3566
case GDScriptParser::COMPLETION_ASSIGN: {
3567
GDScriptCompletionIdentifier type;
3568
if (!completion_context.node || completion_context.node->type != GDScriptParser::Node::ASSIGNMENT) {
3569
break;
3570
}
3571
if (!_guess_expression_type(completion_context, static_cast<const GDScriptParser::AssignmentNode *>(completion_context.node)->assignee, type)) {
3572
_find_identifiers(completion_context, false, true, options, 0);
3573
r_forced = true;
3574
break;
3575
}
3576
3577
if (!type.enumeration.is_empty()) {
3578
_find_enumeration_candidates(completion_context, type.enumeration, options);
3579
r_forced = options.size() > 0;
3580
} else {
3581
_find_identifiers(completion_context, false, true, options, 0);
3582
r_forced = true;
3583
}
3584
} break;
3585
case GDScriptParser::COMPLETION_METHOD:
3586
is_function = true;
3587
[[fallthrough]];
3588
case GDScriptParser::COMPLETION_IDENTIFIER: {
3589
_find_identifiers(completion_context, is_function, !_guess_expecting_callable(completion_context), options, 0);
3590
} break;
3591
case GDScriptParser::COMPLETION_ATTRIBUTE_METHOD:
3592
is_function = true;
3593
[[fallthrough]];
3594
case GDScriptParser::COMPLETION_ATTRIBUTE: {
3595
r_forced = true;
3596
const GDScriptParser::SubscriptNode *attr = static_cast<const GDScriptParser::SubscriptNode *>(completion_context.node);
3597
if (attr->base) {
3598
GDScriptCompletionIdentifier base;
3599
bool found_type = _get_subscript_type(completion_context, attr, base.type);
3600
if (!found_type && !_guess_expression_type(completion_context, attr->base, base)) {
3601
break;
3602
}
3603
3604
_find_identifiers_in_base(base, is_function, false, !_guess_expecting_callable(completion_context), options, 0);
3605
}
3606
} break;
3607
case GDScriptParser::COMPLETION_SUBSCRIPT: {
3608
const GDScriptParser::SubscriptNode *subscript = static_cast<const GDScriptParser::SubscriptNode *>(completion_context.node);
3609
GDScriptCompletionIdentifier base;
3610
const bool res = _guess_expression_type(completion_context, subscript->base, base);
3611
3612
// If the type is not known, we assume it is BUILTIN, since indices on arrays is the most common use case.
3613
if (!subscript->is_attribute && (!res || base.type.kind == GDScriptParser::DataType::BUILTIN || base.type.is_variant())) {
3614
if (base.value.get_type() == Variant::DICTIONARY) {
3615
List<PropertyInfo> members;
3616
base.value.get_property_list(&members);
3617
3618
for (const PropertyInfo &E : members) {
3619
ScriptLanguage::CodeCompletionOption option(E.name.quote(quote_style), ScriptLanguage::CODE_COMPLETION_KIND_MEMBER, ScriptLanguage::LOCATION_LOCAL);
3620
options.insert(option.display, option);
3621
}
3622
}
3623
if (!subscript->index || subscript->index->type != GDScriptParser::Node::LITERAL) {
3624
_find_identifiers(completion_context, false, !_guess_expecting_callable(completion_context), options, 0);
3625
}
3626
} else if (res) {
3627
if (!subscript->is_attribute) {
3628
// Quote the options if they are not accessed as attribute.
3629
3630
HashMap<String, ScriptLanguage::CodeCompletionOption> opt;
3631
_find_identifiers_in_base(base, false, false, false, opt, 0);
3632
for (const KeyValue<String, CodeCompletionOption> &E : opt) {
3633
ScriptLanguage::CodeCompletionOption option(E.value.insert_text.quote(quote_style), E.value.kind, E.value.location);
3634
options.insert(option.display, option);
3635
}
3636
} else {
3637
_find_identifiers_in_base(base, false, false, !_guess_expecting_callable(completion_context), options, 0);
3638
}
3639
}
3640
} break;
3641
case GDScriptParser::COMPLETION_TYPE_ATTRIBUTE: {
3642
if (!completion_context.current_class) {
3643
break;
3644
}
3645
3646
const GDScriptParser::TypeNode *type = static_cast<const GDScriptParser::TypeNode *>(completion_context.node);
3647
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.");
3648
3649
GDScriptCompletionIdentifier base;
3650
3651
if (_guess_identifier_type(completion_context, type->type_chain[0], base)) {
3652
bool found = true;
3653
for (int i = 1; i < completion_context.type_chain_index; i++) {
3654
GDScriptCompletionIdentifier ci;
3655
found = _guess_identifier_type_from_base(completion_context, base, type->type_chain[i]->name, ci);
3656
base = ci;
3657
if (!found) {
3658
break;
3659
}
3660
}
3661
if (found) {
3662
_find_identifiers_in_base(base, false, true, true, options, 0);
3663
}
3664
}
3665
3666
r_forced = true;
3667
} break;
3668
case GDScriptParser::COMPLETION_RESOURCE_PATH: {
3669
if (EDITOR_GET("text_editor/completion/complete_file_paths")) {
3670
_get_directory_contents(EditorFileSystem::get_singleton()->get_filesystem(), options);
3671
r_forced = true;
3672
}
3673
} break;
3674
case GDScriptParser::COMPLETION_CALL_ARGUMENTS: {
3675
if (!completion_context.node) {
3676
break;
3677
}
3678
_find_call_arguments(completion_context, completion_context.node, completion_context.current_argument, options, r_forced, r_call_hint);
3679
} break;
3680
case GDScriptParser::COMPLETION_OVERRIDE_METHOD: {
3681
GDScriptParser::DataType native_type = completion_context.current_class->base_type;
3682
GDScriptParser::FunctionNode *function_node = static_cast<GDScriptParser::FunctionNode *>(completion_context.node);
3683
bool is_static = function_node != nullptr && function_node->is_static;
3684
while (native_type.is_set() && native_type.kind != GDScriptParser::DataType::NATIVE) {
3685
switch (native_type.kind) {
3686
case GDScriptParser::DataType::CLASS: {
3687
for (const GDScriptParser::ClassNode::Member &member : native_type.class_type->members) {
3688
if (member.type != GDScriptParser::ClassNode::Member::FUNCTION) {
3689
continue;
3690
}
3691
3692
if (options.has(member.function->identifier->name)) {
3693
continue;
3694
}
3695
3696
if (completion_context.current_class->has_function(member.get_name()) && completion_context.current_class->get_member(member.get_name()).function != function_node) {
3697
continue;
3698
}
3699
3700
if (is_static != member.function->is_static) {
3701
continue;
3702
}
3703
3704
String display_name = member.function->identifier->name;
3705
display_name += member.function->signature + ":";
3706
ScriptLanguage::CodeCompletionOption option(display_name, ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION);
3707
options.insert(member.function->identifier->name, option); // Insert name instead of display to track duplicates.
3708
}
3709
native_type = native_type.class_type->base_type;
3710
} break;
3711
default: {
3712
native_type.kind = GDScriptParser::DataType::UNRESOLVED;
3713
} break;
3714
}
3715
}
3716
3717
if (!native_type.is_set()) {
3718
break;
3719
}
3720
3721
StringName class_name = native_type.native_type;
3722
if (!ClassDB::class_exists(class_name)) {
3723
break;
3724
}
3725
3726
const bool type_hints = EditorSettings::get_singleton()->get_setting("text_editor/completion/add_type_hints");
3727
3728
List<MethodInfo> virtual_methods;
3729
if (is_static) {
3730
// Not truly a virtual method, but can also be "overridden".
3731
MethodInfo static_init("_static_init");
3732
static_init.return_val.type = Variant::NIL;
3733
static_init.flags |= METHOD_FLAG_STATIC | METHOD_FLAG_VIRTUAL;
3734
virtual_methods.push_back(static_init);
3735
} else {
3736
ClassDB::get_virtual_methods(class_name, &virtual_methods);
3737
}
3738
3739
for (const MethodInfo &mi : virtual_methods) {
3740
if (options.has(mi.name)) {
3741
continue;
3742
}
3743
if (completion_context.current_class->has_function(mi.name) && completion_context.current_class->get_member(mi.name).function != function_node) {
3744
continue;
3745
}
3746
String method_hint = mi.name;
3747
if (method_hint.contains_char(':')) {
3748
method_hint = method_hint.get_slicec(':', 0);
3749
}
3750
method_hint += "(";
3751
3752
for (int64_t i = 0; i < mi.arguments.size(); ++i) {
3753
if (i > 0) {
3754
method_hint += ", ";
3755
}
3756
String arg = mi.arguments[i].name;
3757
if (arg.contains_char(':')) {
3758
arg = arg.substr(0, arg.find_char(':'));
3759
}
3760
method_hint += arg;
3761
if (type_hints) {
3762
method_hint += ": " + _get_visual_datatype(mi.arguments[i], true, class_name);
3763
}
3764
}
3765
if (mi.flags & METHOD_FLAG_VARARG) {
3766
if (!mi.arguments.is_empty()) {
3767
method_hint += ", ";
3768
}
3769
method_hint += "...args"; // `MethodInfo` does not support the rest parameter name.
3770
if (type_hints) {
3771
method_hint += ": Array";
3772
}
3773
}
3774
method_hint += ")";
3775
if (type_hints) {
3776
method_hint += " -> " + _get_visual_datatype(mi.return_val, false, class_name);
3777
}
3778
method_hint += ":";
3779
3780
ScriptLanguage::CodeCompletionOption option(method_hint, ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION);
3781
options.insert(option.display, option);
3782
}
3783
} break;
3784
case GDScriptParser::COMPLETION_GET_NODE: {
3785
// Handles the `$Node/Path` or `$"Some NodePath"` syntax specifically.
3786
if (p_owner) {
3787
List<String> opts;
3788
p_owner->get_argument_options("get_node", 0, &opts);
3789
3790
bool for_unique_name = false;
3791
if (completion_context.node != nullptr && completion_context.node->type == GDScriptParser::Node::GET_NODE && !static_cast<GDScriptParser::GetNodeNode *>(completion_context.node)->use_dollar) {
3792
for_unique_name = true;
3793
}
3794
3795
for (const String &E : opts) {
3796
r_forced = true;
3797
String opt = E.strip_edges();
3798
if (opt.is_quoted()) {
3799
// Remove quotes so that we can handle user preferred quote style,
3800
// or handle NodePaths which are valid identifiers and don't need quotes.
3801
opt = opt.unquote();
3802
}
3803
3804
if (for_unique_name) {
3805
if (!opt.begins_with("%")) {
3806
continue;
3807
}
3808
opt = opt.substr(1);
3809
}
3810
3811
// The path needs quotes if at least one of its components (excluding `%` prefix and `/` separations)
3812
// is not a valid identifier.
3813
bool path_needs_quote = false;
3814
for (const String &part : opt.trim_prefix("%").split("/")) {
3815
if (!part.is_valid_ascii_identifier()) {
3816
path_needs_quote = true;
3817
break;
3818
}
3819
}
3820
3821
if (path_needs_quote) {
3822
// Ignore quote_style and just use double quotes for paths with apostrophes.
3823
// Double quotes don't need to be checked because they're not valid in node and property names.
3824
opt = opt.quote(opt.contains_char('\'') ? "\"" : quote_style); // Handle user preference.
3825
}
3826
ScriptLanguage::CodeCompletionOption option(opt, ScriptLanguage::CODE_COMPLETION_KIND_NODE_PATH);
3827
options.insert(option.display, option);
3828
}
3829
3830
if (!for_unique_name) {
3831
// Get autoloads.
3832
for (const KeyValue<StringName, ProjectSettings::AutoloadInfo> &E : ProjectSettings::get_singleton()->get_autoload_list()) {
3833
String path = "/root/" + E.key;
3834
ScriptLanguage::CodeCompletionOption option(path.quote(quote_style), ScriptLanguage::CODE_COMPLETION_KIND_NODE_PATH);
3835
options.insert(option.display, option);
3836
}
3837
}
3838
}
3839
} break;
3840
case GDScriptParser::COMPLETION_SUPER:
3841
break;
3842
case GDScriptParser::COMPLETION_SUPER_METHOD: {
3843
if (!completion_context.current_class) {
3844
break;
3845
}
3846
_find_identifiers_in_class(completion_context.current_class, true, false, false, true, !_guess_expecting_callable(completion_context), options, 0);
3847
} break;
3848
}
3849
3850
for (const KeyValue<String, ScriptLanguage::CodeCompletionOption> &E : options) {
3851
r_options->push_back(E.value);
3852
}
3853
3854
return OK;
3855
}
3856
3857
#else // !TOOLS_ENABLED
3858
3859
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) {
3860
return OK;
3861
}
3862
3863
#endif // TOOLS_ENABLED
3864
3865
//////// END COMPLETION //////////
3866
3867
String GDScriptLanguage::_get_indentation() const {
3868
#ifdef TOOLS_ENABLED
3869
if (Engine::get_singleton()->is_editor_hint()) {
3870
bool use_space_indentation = EDITOR_GET("text_editor/behavior/indent/type");
3871
3872
if (use_space_indentation) {
3873
int indent_size = EDITOR_GET("text_editor/behavior/indent/size");
3874
return String(" ").repeat(indent_size);
3875
}
3876
}
3877
#endif
3878
return "\t";
3879
}
3880
3881
void GDScriptLanguage::auto_indent_code(String &p_code, int p_from_line, int p_to_line) const {
3882
String indent = _get_indentation();
3883
3884
Vector<String> lines = p_code.split("\n");
3885
List<int> indent_stack;
3886
3887
for (int i = 0; i < lines.size(); i++) {
3888
String l = lines[i];
3889
int tc = 0;
3890
for (int j = 0; j < l.length(); j++) {
3891
if (l[j] == ' ' || l[j] == '\t') {
3892
tc++;
3893
} else {
3894
break;
3895
}
3896
}
3897
3898
String st = l.substr(tc).strip_edges();
3899
if (st.is_empty() || st.begins_with("#")) {
3900
continue; //ignore!
3901
}
3902
3903
int ilevel = 0;
3904
if (indent_stack.size()) {
3905
ilevel = indent_stack.back()->get();
3906
}
3907
3908
if (tc > ilevel) {
3909
indent_stack.push_back(tc);
3910
} else if (tc < ilevel) {
3911
while (indent_stack.size() && indent_stack.back()->get() > tc) {
3912
indent_stack.pop_back();
3913
}
3914
3915
if (indent_stack.size() && indent_stack.back()->get() != tc) {
3916
indent_stack.push_back(tc); // this is not right but gets the job done
3917
}
3918
}
3919
3920
if (i >= p_from_line) {
3921
l = indent.repeat(indent_stack.size()) + st;
3922
} else if (i > p_to_line) {
3923
break;
3924
}
3925
3926
lines.write[i] = l;
3927
}
3928
3929
p_code = "";
3930
for (int i = 0; i < lines.size(); i++) {
3931
if (i > 0) {
3932
p_code += "\n";
3933
}
3934
p_code += lines[i];
3935
}
3936
}
3937
3938
#ifdef TOOLS_ENABLED
3939
3940
static Error _lookup_symbol_from_base(const GDScriptParser::DataType &p_base, const String &p_symbol, GDScriptLanguage::LookupResult &r_result) {
3941
GDScriptParser::DataType base_type = p_base;
3942
3943
while (true) {
3944
switch (base_type.kind) {
3945
case GDScriptParser::DataType::CLASS: {
3946
ERR_FAIL_NULL_V(base_type.class_type, ERR_BUG);
3947
3948
String name = p_symbol;
3949
if (name == "new") {
3950
name = "_init";
3951
}
3952
3953
if (!base_type.class_type->has_member(name)) {
3954
base_type = base_type.class_type->base_type;
3955
break;
3956
}
3957
3958
const GDScriptParser::ClassNode::Member &member = base_type.class_type->get_member(name);
3959
3960
switch (member.type) {
3961
case GDScriptParser::ClassNode::Member::UNDEFINED:
3962
case GDScriptParser::ClassNode::Member::GROUP:
3963
return ERR_BUG;
3964
case GDScriptParser::ClassNode::Member::CLASS: {
3965
String doc_type_name;
3966
String doc_enum_name;
3967
GDScriptDocGen::doctype_from_gdtype(GDScriptAnalyzer::type_from_metatype(member.get_datatype()), doc_type_name, doc_enum_name);
3968
3969
r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS;
3970
r_result.class_name = doc_type_name;
3971
} break;
3972
case GDScriptParser::ClassNode::Member::CONSTANT:
3973
r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_CONSTANT;
3974
break;
3975
case GDScriptParser::ClassNode::Member::FUNCTION:
3976
r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_METHOD;
3977
break;
3978
case GDScriptParser::ClassNode::Member::SIGNAL:
3979
r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_SIGNAL;
3980
break;
3981
case GDScriptParser::ClassNode::Member::VARIABLE:
3982
r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_PROPERTY;
3983
break;
3984
case GDScriptParser::ClassNode::Member::ENUM:
3985
r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_ENUM;
3986
break;
3987
case GDScriptParser::ClassNode::Member::ENUM_VALUE:
3988
r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_CONSTANT;
3989
break;
3990
}
3991
3992
if (member.type != GDScriptParser::ClassNode::Member::CLASS) {
3993
String doc_type_name;
3994
String doc_enum_name;
3995
GDScriptDocGen::doctype_from_gdtype(GDScriptAnalyzer::type_from_metatype(base_type), doc_type_name, doc_enum_name);
3996
3997
r_result.class_name = doc_type_name;
3998
r_result.class_member = name;
3999
}
4000
4001
Error err = OK;
4002
r_result.script = GDScriptCache::get_shallow_script(base_type.script_path, err);
4003
r_result.script_path = base_type.script_path;
4004
r_result.location = member.get_line();
4005
return err;
4006
} break;
4007
case GDScriptParser::DataType::SCRIPT: {
4008
const Ref<Script> scr = base_type.script_type;
4009
4010
if (scr.is_null()) {
4011
return ERR_CANT_RESOLVE;
4012
}
4013
4014
String name = p_symbol;
4015
if (name == "new") {
4016
name = "_init";
4017
}
4018
4019
const int line = scr->get_member_line(name);
4020
if (line >= 0) {
4021
bool found_type = false;
4022
r_result.type = ScriptLanguage::LOOKUP_RESULT_SCRIPT_LOCATION;
4023
{
4024
List<PropertyInfo> properties;
4025
scr->get_script_property_list(&properties);
4026
for (const PropertyInfo &property : properties) {
4027
if (property.name == name && (property.usage & PROPERTY_USAGE_SCRIPT_VARIABLE)) {
4028
found_type = true;
4029
r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_PROPERTY;
4030
r_result.class_name = scr->get_doc_class_name();
4031
r_result.class_member = name;
4032
break;
4033
}
4034
}
4035
}
4036
if (!found_type) {
4037
List<MethodInfo> methods;
4038
scr->get_script_method_list(&methods);
4039
for (const MethodInfo &method : methods) {
4040
if (method.name == name) {
4041
found_type = true;
4042
r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_METHOD;
4043
r_result.class_name = scr->get_doc_class_name();
4044
r_result.class_member = name;
4045
break;
4046
}
4047
}
4048
}
4049
if (!found_type) {
4050
List<MethodInfo> signals;
4051
scr->get_script_method_list(&signals);
4052
for (const MethodInfo &signal : signals) {
4053
if (signal.name == name) {
4054
found_type = true;
4055
r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_SIGNAL;
4056
r_result.class_name = scr->get_doc_class_name();
4057
r_result.class_member = name;
4058
break;
4059
}
4060
}
4061
}
4062
if (!found_type) {
4063
const Ref<GDScript> gds = scr;
4064
if (gds.is_valid()) {
4065
const Ref<GDScript> *subclass = gds->get_subclasses().getptr(name);
4066
if (subclass != nullptr) {
4067
found_type = true;
4068
r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS;
4069
r_result.class_name = subclass->ptr()->get_doc_class_name();
4070
}
4071
// TODO: enums.
4072
}
4073
}
4074
if (!found_type) {
4075
HashMap<StringName, Variant> constants;
4076
scr->get_constants(&constants);
4077
if (constants.has(name)) {
4078
found_type = true;
4079
r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_CONSTANT;
4080
r_result.class_name = scr->get_doc_class_name();
4081
r_result.class_member = name;
4082
}
4083
}
4084
4085
r_result.script = scr;
4086
r_result.script_path = base_type.script_path;
4087
r_result.location = line;
4088
return OK;
4089
}
4090
4091
const Ref<Script> base_script = scr->get_base_script();
4092
if (base_script.is_valid()) {
4093
base_type.script_type = base_script;
4094
} else {
4095
base_type.kind = GDScriptParser::DataType::NATIVE;
4096
base_type.builtin_type = Variant::OBJECT;
4097
base_type.native_type = scr->get_instance_base_type();
4098
}
4099
} break;
4100
case GDScriptParser::DataType::NATIVE: {
4101
const StringName &class_name = base_type.native_type;
4102
4103
ERR_FAIL_COND_V(!ClassDB::class_exists(class_name), ERR_BUG);
4104
4105
if (ClassDB::has_method(class_name, p_symbol, true)) {
4106
r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_METHOD;
4107
r_result.class_name = class_name;
4108
r_result.class_member = p_symbol;
4109
return OK;
4110
}
4111
4112
List<MethodInfo> virtual_methods;
4113
ClassDB::get_virtual_methods(class_name, &virtual_methods, true);
4114
for (const MethodInfo &E : virtual_methods) {
4115
if (E.name == p_symbol) {
4116
r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_METHOD;
4117
r_result.class_name = class_name;
4118
r_result.class_member = p_symbol;
4119
return OK;
4120
}
4121
}
4122
4123
if (ClassDB::has_signal(class_name, p_symbol, true)) {
4124
r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_SIGNAL;
4125
r_result.class_name = class_name;
4126
r_result.class_member = p_symbol;
4127
return OK;
4128
}
4129
4130
List<StringName> enums;
4131
ClassDB::get_enum_list(class_name, &enums);
4132
for (const StringName &E : enums) {
4133
if (E == p_symbol) {
4134
r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_ENUM;
4135
r_result.class_name = class_name;
4136
r_result.class_member = p_symbol;
4137
return OK;
4138
}
4139
}
4140
4141
if (!String(ClassDB::get_integer_constant_enum(class_name, p_symbol, true)).is_empty()) {
4142
r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_CONSTANT;
4143
r_result.class_name = class_name;
4144
r_result.class_member = p_symbol;
4145
return OK;
4146
}
4147
4148
List<String> constants;
4149
ClassDB::get_integer_constant_list(class_name, &constants, true);
4150
for (const String &E : constants) {
4151
if (E == p_symbol) {
4152
r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_CONSTANT;
4153
r_result.class_name = class_name;
4154
r_result.class_member = p_symbol;
4155
return OK;
4156
}
4157
}
4158
4159
if (ClassDB::has_property(class_name, p_symbol, true)) {
4160
PropertyInfo prop_info;
4161
ClassDB::get_property_info(class_name, p_symbol, &prop_info, true);
4162
if (prop_info.usage & PROPERTY_USAGE_INTERNAL) {
4163
return ERR_CANT_RESOLVE;
4164
}
4165
4166
r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_PROPERTY;
4167
r_result.class_name = class_name;
4168
r_result.class_member = p_symbol;
4169
return OK;
4170
}
4171
4172
const StringName parent_class = ClassDB::get_parent_class(class_name);
4173
if (parent_class != StringName()) {
4174
base_type.native_type = parent_class;
4175
} else {
4176
return ERR_CANT_RESOLVE;
4177
}
4178
} break;
4179
case GDScriptParser::DataType::BUILTIN: {
4180
if (base_type.is_meta_type) {
4181
if (Variant::has_enum(base_type.builtin_type, p_symbol)) {
4182
r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_ENUM;
4183
r_result.class_name = Variant::get_type_name(base_type.builtin_type);
4184
r_result.class_member = p_symbol;
4185
return OK;
4186
}
4187
4188
if (Variant::has_constant(base_type.builtin_type, p_symbol)) {
4189
r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_CONSTANT;
4190
r_result.class_name = Variant::get_type_name(base_type.builtin_type);
4191
r_result.class_member = p_symbol;
4192
return OK;
4193
}
4194
} else {
4195
if (Variant::has_member(base_type.builtin_type, p_symbol)) {
4196
r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_PROPERTY;
4197
r_result.class_name = Variant::get_type_name(base_type.builtin_type);
4198
r_result.class_member = p_symbol;
4199
return OK;
4200
}
4201
}
4202
4203
if (Variant::has_builtin_method(base_type.builtin_type, p_symbol)) {
4204
r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_METHOD;
4205
r_result.class_name = Variant::get_type_name(base_type.builtin_type);
4206
r_result.class_member = p_symbol;
4207
return OK;
4208
}
4209
4210
return ERR_CANT_RESOLVE;
4211
} break;
4212
case GDScriptParser::DataType::ENUM: {
4213
if (base_type.is_meta_type) {
4214
if (base_type.enum_values.has(p_symbol)) {
4215
String doc_type_name;
4216
String doc_enum_name;
4217
GDScriptDocGen::doctype_from_gdtype(GDScriptAnalyzer::type_from_metatype(base_type), doc_type_name, doc_enum_name);
4218
4219
if (CoreConstants::is_global_enum(doc_enum_name)) {
4220
r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_CONSTANT;
4221
r_result.class_name = "@GlobalScope";
4222
r_result.class_member = p_symbol;
4223
return OK;
4224
} else {
4225
const int dot_pos = doc_enum_name.rfind_char('.');
4226
if (dot_pos >= 0) {
4227
Error err = OK;
4228
r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_CONSTANT;
4229
if (base_type.class_type != nullptr) {
4230
// For script enums the value isn't accessible as class constant so we need the full enum name.
4231
r_result.class_name = doc_enum_name;
4232
r_result.class_member = p_symbol;
4233
r_result.script = GDScriptCache::get_shallow_script(base_type.script_path, err);
4234
r_result.script_path = base_type.script_path;
4235
const String enum_name = doc_enum_name.substr(dot_pos + 1);
4236
if (base_type.class_type->has_member(enum_name)) {
4237
const GDScriptParser::ClassNode::Member member = base_type.class_type->get_member(enum_name);
4238
if (member.type == GDScriptParser::ClassNode::Member::ENUM) {
4239
for (const GDScriptParser::EnumNode::Value &value : member.m_enum->values) {
4240
if (value.identifier->name == p_symbol) {
4241
r_result.location = value.line;
4242
break;
4243
}
4244
}
4245
}
4246
}
4247
} else if (base_type.script_type.is_valid()) {
4248
// For script enums the value isn't accessible as class constant so we need the full enum name.
4249
r_result.class_name = doc_enum_name;
4250
r_result.class_member = p_symbol;
4251
r_result.script = base_type.script_type;
4252
r_result.script_path = base_type.script_path;
4253
// TODO: Find a way to obtain enum value location for a script
4254
r_result.location = base_type.script_type->get_member_line(doc_enum_name.substr(dot_pos + 1));
4255
} else {
4256
r_result.class_name = doc_enum_name.left(dot_pos);
4257
r_result.class_member = p_symbol;
4258
}
4259
return err;
4260
}
4261
}
4262
} else if (Variant::has_builtin_method(Variant::DICTIONARY, p_symbol)) {
4263
r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_METHOD;
4264
r_result.class_name = "Dictionary";
4265
r_result.class_member = p_symbol;
4266
return OK;
4267
}
4268
}
4269
4270
return ERR_CANT_RESOLVE;
4271
} break;
4272
case GDScriptParser::DataType::VARIANT: {
4273
if (base_type.is_meta_type) {
4274
const String enum_name = "Variant." + p_symbol;
4275
if (CoreConstants::is_global_enum(enum_name)) {
4276
r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_ENUM;
4277
r_result.class_name = "@GlobalScope";
4278
r_result.class_member = enum_name;
4279
return OK;
4280
}
4281
}
4282
4283
return ERR_CANT_RESOLVE;
4284
} break;
4285
case GDScriptParser::DataType::RESOLVING:
4286
case GDScriptParser::DataType::UNRESOLVED: {
4287
return ERR_CANT_RESOLVE;
4288
} break;
4289
}
4290
}
4291
4292
return ERR_CANT_RESOLVE;
4293
}
4294
4295
::Error GDScriptLanguage::lookup_code(const String &p_code, const String &p_symbol, const String &p_path, Object *p_owner, LookupResult &r_result) {
4296
// Before parsing, try the usual stuff.
4297
if (ClassDB::class_exists(p_symbol)) {
4298
r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS;
4299
r_result.class_name = p_symbol;
4300
return OK;
4301
}
4302
4303
if (Variant::get_type_by_name(p_symbol) < Variant::VARIANT_MAX) {
4304
r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS;
4305
r_result.class_name = p_symbol;
4306
return OK;
4307
}
4308
4309
if (p_symbol == "Variant") {
4310
r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS;
4311
r_result.class_name = "Variant";
4312
return OK;
4313
}
4314
4315
if (p_symbol == "PI" || p_symbol == "TAU" || p_symbol == "INF" || p_symbol == "NAN") {
4316
r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_CONSTANT;
4317
r_result.class_name = "@GDScript";
4318
r_result.class_member = p_symbol;
4319
return OK;
4320
}
4321
4322
GDScriptParser parser;
4323
parser.parse(p_code, p_path, true);
4324
4325
GDScriptParser::CompletionContext context = parser.get_completion_context();
4326
context.base = p_owner;
4327
4328
// Allows class functions with the names like built-ins to be handled properly.
4329
if (context.type != GDScriptParser::COMPLETION_ATTRIBUTE) {
4330
// Need special checks for `assert` and `preload` as they are technically
4331
// keywords, so are not registered in `GDScriptUtilityFunctions`.
4332
if (GDScriptUtilityFunctions::function_exists(p_symbol) || p_symbol == "assert" || p_symbol == "preload") {
4333
r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_METHOD;
4334
r_result.class_name = "@GDScript";
4335
r_result.class_member = p_symbol;
4336
return OK;
4337
}
4338
}
4339
4340
GDScriptAnalyzer analyzer(&parser);
4341
analyzer.analyze();
4342
4343
if (context.current_class && context.current_class->extends.size() > 0) {
4344
StringName class_name = context.current_class->extends[0]->name;
4345
4346
bool success = false;
4347
ClassDB::get_integer_constant(class_name, p_symbol, &success);
4348
if (success) {
4349
r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_CONSTANT;
4350
r_result.class_name = class_name;
4351
r_result.class_member = p_symbol;
4352
return OK;
4353
}
4354
do {
4355
List<StringName> enums;
4356
ClassDB::get_enum_list(class_name, &enums, true);
4357
for (const StringName &enum_name : enums) {
4358
if (enum_name == p_symbol) {
4359
r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_ENUM;
4360
r_result.class_name = class_name;
4361
r_result.class_member = p_symbol;
4362
return OK;
4363
}
4364
}
4365
class_name = ClassDB::get_parent_class_nocheck(class_name);
4366
} while (class_name != StringName());
4367
}
4368
4369
const GDScriptParser::TypeNode *type_node = dynamic_cast<const GDScriptParser::TypeNode *>(context.node);
4370
if (type_node != nullptr && !type_node->type_chain.is_empty()) {
4371
StringName class_name = type_node->type_chain[0]->name;
4372
if (ScriptServer::is_global_class(class_name)) {
4373
class_name = ScriptServer::get_global_class_native_base(class_name);
4374
}
4375
do {
4376
List<StringName> enums;
4377
ClassDB::get_enum_list(class_name, &enums, true);
4378
for (const StringName &enum_name : enums) {
4379
if (enum_name == p_symbol) {
4380
r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_ENUM;
4381
r_result.class_name = class_name;
4382
r_result.class_member = p_symbol;
4383
return OK;
4384
}
4385
}
4386
class_name = ClassDB::get_parent_class_nocheck(class_name);
4387
} while (class_name != StringName());
4388
}
4389
4390
bool is_function = false;
4391
4392
switch (context.type) {
4393
case GDScriptParser::COMPLETION_BUILT_IN_TYPE_CONSTANT_OR_STATIC_METHOD: {
4394
GDScriptParser::DataType base_type;
4395
base_type.kind = GDScriptParser::DataType::BUILTIN;
4396
base_type.builtin_type = context.builtin_type;
4397
base_type.is_meta_type = true;
4398
if (_lookup_symbol_from_base(base_type, p_symbol, r_result) == OK) {
4399
return OK;
4400
}
4401
} break;
4402
case GDScriptParser::COMPLETION_SUPER: {
4403
if (context.current_class && context.current_function) {
4404
if (_lookup_symbol_from_base(context.current_class->base_type, context.current_function->info.name, r_result) == OK) {
4405
return OK;
4406
}
4407
}
4408
} break;
4409
case GDScriptParser::COMPLETION_SUPER_METHOD:
4410
case GDScriptParser::COMPLETION_METHOD:
4411
case GDScriptParser::COMPLETION_ASSIGN:
4412
case GDScriptParser::COMPLETION_CALL_ARGUMENTS:
4413
case GDScriptParser::COMPLETION_IDENTIFIER:
4414
case GDScriptParser::COMPLETION_PROPERTY_METHOD:
4415
case GDScriptParser::COMPLETION_SUBSCRIPT: {
4416
GDScriptParser::DataType base_type;
4417
if (context.current_class) {
4418
if (context.type != GDScriptParser::COMPLETION_SUPER_METHOD) {
4419
base_type = context.current_class->get_datatype();
4420
} else {
4421
base_type = context.current_class->base_type;
4422
}
4423
} else {
4424
break;
4425
}
4426
4427
if (!is_function && context.current_suite) {
4428
// Lookup local variables.
4429
const GDScriptParser::SuiteNode *suite = context.current_suite;
4430
while (suite) {
4431
if (suite->has_local(p_symbol)) {
4432
const GDScriptParser::SuiteNode::Local &local = suite->get_local(p_symbol);
4433
4434
switch (local.type) {
4435
case GDScriptParser::SuiteNode::Local::UNDEFINED:
4436
return ERR_BUG;
4437
case GDScriptParser::SuiteNode::Local::CONSTANT:
4438
r_result.type = ScriptLanguage::LOOKUP_RESULT_LOCAL_CONSTANT;
4439
r_result.description = local.constant->doc_data.description;
4440
r_result.is_deprecated = local.constant->doc_data.is_deprecated;
4441
r_result.deprecated_message = local.constant->doc_data.deprecated_message;
4442
r_result.is_experimental = local.constant->doc_data.is_experimental;
4443
r_result.experimental_message = local.constant->doc_data.experimental_message;
4444
if (local.constant->initializer != nullptr) {
4445
r_result.value = GDScriptDocGen::docvalue_from_expression(local.constant->initializer);
4446
}
4447
break;
4448
case GDScriptParser::SuiteNode::Local::VARIABLE:
4449
r_result.type = ScriptLanguage::LOOKUP_RESULT_LOCAL_VARIABLE;
4450
r_result.description = local.variable->doc_data.description;
4451
r_result.is_deprecated = local.variable->doc_data.is_deprecated;
4452
r_result.deprecated_message = local.variable->doc_data.deprecated_message;
4453
r_result.is_experimental = local.variable->doc_data.is_experimental;
4454
r_result.experimental_message = local.variable->doc_data.experimental_message;
4455
if (local.variable->initializer != nullptr) {
4456
r_result.value = GDScriptDocGen::docvalue_from_expression(local.variable->initializer);
4457
}
4458
break;
4459
case GDScriptParser::SuiteNode::Local::PARAMETER:
4460
case GDScriptParser::SuiteNode::Local::FOR_VARIABLE:
4461
case GDScriptParser::SuiteNode::Local::PATTERN_BIND:
4462
r_result.type = ScriptLanguage::LOOKUP_RESULT_LOCAL_VARIABLE;
4463
break;
4464
}
4465
4466
GDScriptDocGen::doctype_from_gdtype(local.get_datatype(), r_result.doc_type, r_result.enumeration);
4467
4468
Error err = OK;
4469
r_result.script = GDScriptCache::get_shallow_script(base_type.script_path, err);
4470
r_result.script_path = base_type.script_path;
4471
r_result.location = local.start_line;
4472
return err;
4473
}
4474
suite = suite->parent_block;
4475
}
4476
}
4477
4478
if (_lookup_symbol_from_base(base_type, p_symbol, r_result) == OK) {
4479
return OK;
4480
}
4481
4482
if (!is_function) {
4483
if (ProjectSettings::get_singleton()->has_autoload(p_symbol)) {
4484
const ProjectSettings::AutoloadInfo &autoload = ProjectSettings::get_singleton()->get_autoload(p_symbol);
4485
if (autoload.is_singleton) {
4486
String scr_path = autoload.path;
4487
if (!scr_path.ends_with(".gd")) {
4488
// Not a script, try find the script anyway, may have some success.
4489
scr_path = scr_path.get_basename() + ".gd";
4490
}
4491
4492
if (FileAccess::exists(scr_path)) {
4493
r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS;
4494
r_result.class_name = p_symbol;
4495
r_result.script = ResourceLoader::load(scr_path);
4496
r_result.script_path = scr_path;
4497
r_result.location = 0;
4498
return OK;
4499
}
4500
}
4501
}
4502
4503
if (ScriptServer::is_global_class(p_symbol)) {
4504
const String scr_path = ScriptServer::get_global_class_path(p_symbol);
4505
const Ref<Script> scr = ResourceLoader::load(scr_path);
4506
if (scr.is_null()) {
4507
return ERR_BUG;
4508
}
4509
r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS;
4510
r_result.class_name = scr->get_doc_class_name();
4511
r_result.script = scr;
4512
r_result.script_path = scr_path;
4513
r_result.location = 0;
4514
return OK;
4515
}
4516
4517
const HashMap<StringName, int> &global_map = GDScriptLanguage::get_singleton()->get_global_map();
4518
if (global_map.has(p_symbol)) {
4519
Variant value = GDScriptLanguage::get_singleton()->get_global_array()[global_map[p_symbol]];
4520
if (value.get_type() == Variant::OBJECT) {
4521
const Object *obj = value;
4522
if (obj) {
4523
if (Object::cast_to<GDScriptNativeClass>(obj)) {
4524
r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS;
4525
r_result.class_name = Object::cast_to<GDScriptNativeClass>(obj)->get_name();
4526
} else {
4527
r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS;
4528
r_result.class_name = obj->get_class();
4529
}
4530
return OK;
4531
}
4532
}
4533
}
4534
4535
if (CoreConstants::is_global_enum(p_symbol)) {
4536
r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_ENUM;
4537
r_result.class_name = "@GlobalScope";
4538
r_result.class_member = p_symbol;
4539
return OK;
4540
}
4541
4542
if (CoreConstants::is_global_constant(p_symbol)) {
4543
r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_CONSTANT;
4544
r_result.class_name = "@GlobalScope";
4545
r_result.class_member = p_symbol;
4546
return OK;
4547
}
4548
4549
if (Variant::has_utility_function(p_symbol)) {
4550
r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_METHOD;
4551
r_result.class_name = "@GlobalScope";
4552
r_result.class_member = p_symbol;
4553
return OK;
4554
}
4555
}
4556
} break;
4557
case GDScriptParser::COMPLETION_ATTRIBUTE_METHOD:
4558
case GDScriptParser::COMPLETION_ATTRIBUTE: {
4559
if (context.node->type != GDScriptParser::Node::SUBSCRIPT) {
4560
break;
4561
}
4562
const GDScriptParser::SubscriptNode *subscript = static_cast<const GDScriptParser::SubscriptNode *>(context.node);
4563
if (!subscript->is_attribute) {
4564
break;
4565
}
4566
GDScriptCompletionIdentifier base;
4567
4568
bool found_type = _get_subscript_type(context, subscript, base.type);
4569
if (!found_type && !_guess_expression_type(context, subscript->base, base)) {
4570
break;
4571
}
4572
4573
if (_lookup_symbol_from_base(base.type, p_symbol, r_result) == OK) {
4574
return OK;
4575
}
4576
} break;
4577
case GDScriptParser::COMPLETION_TYPE_ATTRIBUTE: {
4578
if (context.node == nullptr || context.node->type != GDScriptParser::Node::TYPE) {
4579
break;
4580
}
4581
const GDScriptParser::TypeNode *type = static_cast<const GDScriptParser::TypeNode *>(context.node);
4582
4583
GDScriptParser::DataType base_type;
4584
const GDScriptParser::IdentifierNode *prev = nullptr;
4585
for (const GDScriptParser::IdentifierNode *E : type->type_chain) {
4586
if (E->name == p_symbol && prev != nullptr) {
4587
base_type = prev->get_datatype();
4588
break;
4589
}
4590
prev = E;
4591
}
4592
if (base_type.kind != GDScriptParser::DataType::CLASS) {
4593
GDScriptCompletionIdentifier base;
4594
if (!_guess_expression_type(context, prev, base)) {
4595
break;
4596
}
4597
base_type = base.type;
4598
}
4599
4600
if (_lookup_symbol_from_base(base_type, p_symbol, r_result) == OK) {
4601
return OK;
4602
}
4603
} break;
4604
case GDScriptParser::COMPLETION_OVERRIDE_METHOD: {
4605
GDScriptParser::DataType base_type = context.current_class->base_type;
4606
4607
if (_lookup_symbol_from_base(base_type, p_symbol, r_result) == OK) {
4608
return OK;
4609
}
4610
} break;
4611
case GDScriptParser::COMPLETION_PROPERTY_DECLARATION_OR_TYPE:
4612
case GDScriptParser::COMPLETION_TYPE_NAME_OR_VOID:
4613
case GDScriptParser::COMPLETION_TYPE_NAME: {
4614
GDScriptParser::DataType base_type = context.current_class->get_datatype();
4615
4616
if (_lookup_symbol_from_base(base_type, p_symbol, r_result) == OK) {
4617
return OK;
4618
}
4619
} break;
4620
case GDScriptParser::COMPLETION_ANNOTATION: {
4621
const String annotation_symbol = "@" + p_symbol;
4622
if (parser.annotation_exists(annotation_symbol)) {
4623
r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_ANNOTATION;
4624
r_result.class_name = "@GDScript";
4625
r_result.class_member = annotation_symbol;
4626
return OK;
4627
}
4628
} break;
4629
default: {
4630
}
4631
}
4632
4633
return ERR_CANT_RESOLVE;
4634
}
4635
4636
#endif // TOOLS_ENABLED
4637
4638