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