gdscript_editor.cpp 111 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141
  1. /*************************************************************************/
  2. /* gdscript_editor.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  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. #include "gdscript.h"
  31. #include "core/config/engine.h"
  32. #include "core/core_constants.h"
  33. #include "core/os/file_access.h"
  34. #include "gdscript_analyzer.h"
  35. #include "gdscript_compiler.h"
  36. #include "gdscript_parser.h"
  37. #include "gdscript_tokenizer.h"
  38. #include "gdscript_utility_functions.h"
  39. #ifdef TOOLS_ENABLED
  40. #include "core/config/project_settings.h"
  41. #include "editor/editor_file_system.h"
  42. #include "editor/editor_settings.h"
  43. #endif
  44. void GDScriptLanguage::get_comment_delimiters(List<String> *p_delimiters) const {
  45. p_delimiters->push_back("#");
  46. }
  47. void GDScriptLanguage::get_string_delimiters(List<String> *p_delimiters) const {
  48. p_delimiters->push_back("\" \"");
  49. p_delimiters->push_back("' '");
  50. p_delimiters->push_back("\"\"\" \"\"\"");
  51. }
  52. String GDScriptLanguage::_get_processed_template(const String &p_template, const String &p_base_class_name) const {
  53. String processed_template = p_template;
  54. #ifdef TOOLS_ENABLED
  55. if (EDITOR_DEF("text_editor/completion/add_type_hints", false)) {
  56. processed_template = processed_template.replace("%INT_TYPE%", ": int");
  57. processed_template = processed_template.replace("%STRING_TYPE%", ": String");
  58. processed_template = processed_template.replace("%FLOAT_TYPE%", ": float");
  59. processed_template = processed_template.replace("%VOID_RETURN%", " -> void");
  60. } else {
  61. processed_template = processed_template.replace("%INT_TYPE%", "");
  62. processed_template = processed_template.replace("%STRING_TYPE%", "");
  63. processed_template = processed_template.replace("%FLOAT_TYPE%", "");
  64. processed_template = processed_template.replace("%VOID_RETURN%", "");
  65. }
  66. #else
  67. processed_template = processed_template.replace("%INT_TYPE%", "");
  68. processed_template = processed_template.replace("%STRING_TYPE%", "");
  69. processed_template = processed_template.replace("%FLOAT_TYPE%", "");
  70. processed_template = processed_template.replace("%VOID_RETURN%", "");
  71. #endif
  72. processed_template = processed_template.replace("%BASE%", p_base_class_name);
  73. processed_template = processed_template.replace("%TS%", _get_indentation());
  74. return processed_template;
  75. }
  76. Ref<Script> GDScriptLanguage::get_template(const String &p_class_name, const String &p_base_class_name) const {
  77. String _template = "extends %BASE%\n"
  78. "\n"
  79. "\n"
  80. "# Declare member variables here. Examples:\n"
  81. "# var a%INT_TYPE% = 2\n"
  82. "# var b%STRING_TYPE% = \"text\"\n"
  83. "\n"
  84. "\n"
  85. "# Called when the node enters the scene tree for the first time.\n"
  86. "func _ready()%VOID_RETURN%:\n"
  87. "%TS%pass # Replace with function body.\n"
  88. "\n"
  89. "\n"
  90. "# Called every frame. 'delta' is the elapsed time since the previous frame.\n"
  91. "#func _process(delta%FLOAT_TYPE%)%VOID_RETURN%:\n"
  92. "#%TS%pass\n";
  93. _template = _get_processed_template(_template, p_base_class_name);
  94. Ref<GDScript> script;
  95. script.instance();
  96. script->set_source_code(_template);
  97. return script;
  98. }
  99. bool GDScriptLanguage::is_using_templates() {
  100. return true;
  101. }
  102. void GDScriptLanguage::make_template(const String &p_class_name, const String &p_base_class_name, Ref<Script> &p_script) {
  103. String _template = _get_processed_template(p_script->get_source_code(), p_base_class_name);
  104. p_script->set_source_code(_template);
  105. }
  106. static void get_function_names_recursively(const GDScriptParser::ClassNode *p_class, const String &p_prefix, Map<int, String> &r_funcs) {
  107. for (int i = 0; i < p_class->members.size(); i++) {
  108. if (p_class->members[i].type == GDScriptParser::ClassNode::Member::FUNCTION) {
  109. const GDScriptParser::FunctionNode *function = p_class->members[i].function;
  110. r_funcs[function->start_line] = p_prefix.is_empty() ? String(function->identifier->name) : p_prefix + "." + String(function->identifier->name);
  111. } else if (p_class->members[i].type == GDScriptParser::ClassNode::Member::CLASS) {
  112. String new_prefix = p_class->members[i].m_class->identifier->name;
  113. get_function_names_recursively(p_class->members[i].m_class, p_prefix.is_empty() ? new_prefix : p_prefix + "." + new_prefix, r_funcs);
  114. }
  115. }
  116. }
  117. bool GDScriptLanguage::validate(const String &p_script, int &r_line_error, int &r_col_error, String &r_test_error, const String &p_path, List<String> *r_functions, List<ScriptLanguage::Warning> *r_warnings, Set<int> *r_safe_lines) const {
  118. GDScriptParser parser;
  119. GDScriptAnalyzer analyzer(&parser);
  120. Error err = parser.parse(p_script, p_path, false);
  121. if (err == OK) {
  122. err = analyzer.analyze();
  123. }
  124. #ifdef DEBUG_ENABLED
  125. if (r_warnings) {
  126. for (const List<GDScriptWarning>::Element *E = parser.get_warnings().front(); E; E = E->next()) {
  127. const GDScriptWarning &warn = E->get();
  128. ScriptLanguage::Warning w;
  129. w.start_line = warn.start_line;
  130. w.end_line = warn.end_line;
  131. w.leftmost_column = warn.leftmost_column;
  132. w.rightmost_column = warn.rightmost_column;
  133. w.code = (int)warn.code;
  134. w.string_code = GDScriptWarning::get_name_from_code(warn.code);
  135. w.message = warn.get_message();
  136. r_warnings->push_back(w);
  137. }
  138. }
  139. #endif
  140. if (err) {
  141. GDScriptParser::ParserError parse_error = parser.get_errors().front()->get();
  142. r_line_error = parse_error.line;
  143. r_col_error = parse_error.column;
  144. r_test_error = parse_error.message;
  145. return false;
  146. } else {
  147. const GDScriptParser::ClassNode *cl = parser.get_tree();
  148. Map<int, String> funcs;
  149. get_function_names_recursively(cl, "", funcs);
  150. for (Map<int, String>::Element *E = funcs.front(); E; E = E->next()) {
  151. r_functions->push_back(E->get() + ":" + itos(E->key()));
  152. }
  153. }
  154. #ifdef DEBUG_ENABLED
  155. if (r_safe_lines) {
  156. const Set<int> &unsafe_lines = parser.get_unsafe_lines();
  157. for (int i = 1; i <= parser.get_last_line_number(); i++) {
  158. if (!unsafe_lines.has(i)) {
  159. r_safe_lines->insert(i);
  160. }
  161. }
  162. }
  163. #endif
  164. return true;
  165. }
  166. bool GDScriptLanguage::has_named_classes() const {
  167. return false;
  168. }
  169. bool GDScriptLanguage::supports_builtin_mode() const {
  170. return true;
  171. }
  172. bool GDScriptLanguage::supports_documentation() const {
  173. return true;
  174. }
  175. int GDScriptLanguage::find_function(const String &p_function, const String &p_code) const {
  176. GDScriptTokenizer tokenizer;
  177. tokenizer.set_source_code(p_code);
  178. int indent = 0;
  179. GDScriptTokenizer::Token current = tokenizer.scan();
  180. while (current.type != GDScriptTokenizer::Token::TK_EOF && current.type != GDScriptTokenizer::Token::ERROR) {
  181. if (current.type == GDScriptTokenizer::Token::INDENT) {
  182. indent++;
  183. } else if (current.type == GDScriptTokenizer::Token::DEDENT) {
  184. indent--;
  185. }
  186. if (indent == 0 && current.type == GDScriptTokenizer::Token::FUNC) {
  187. current = tokenizer.scan();
  188. if (current.is_identifier()) {
  189. String identifier = current.get_identifier();
  190. if (identifier == p_function) {
  191. return current.start_line;
  192. }
  193. }
  194. }
  195. current = tokenizer.scan();
  196. }
  197. return -1;
  198. }
  199. Script *GDScriptLanguage::create_script() const {
  200. return memnew(GDScript);
  201. }
  202. /* DEBUGGER FUNCTIONS */
  203. bool GDScriptLanguage::debug_break_parse(const String &p_file, int p_line, const String &p_error) {
  204. //break because of parse error
  205. if (EngineDebugger::is_active() && Thread::get_caller_id() == Thread::get_main_id()) {
  206. _debug_parse_err_line = p_line;
  207. _debug_parse_err_file = p_file;
  208. _debug_error = p_error;
  209. EngineDebugger::get_script_debugger()->debug(this, false, true);
  210. return true;
  211. } else {
  212. return false;
  213. }
  214. }
  215. bool GDScriptLanguage::debug_break(const String &p_error, bool p_allow_continue) {
  216. if (EngineDebugger::is_active() && Thread::get_caller_id() == Thread::get_main_id()) {
  217. _debug_parse_err_line = -1;
  218. _debug_parse_err_file = "";
  219. _debug_error = p_error;
  220. bool is_error_breakpoint = p_error != "Breakpoint";
  221. EngineDebugger::get_script_debugger()->debug(this, p_allow_continue, is_error_breakpoint);
  222. return true;
  223. } else {
  224. return false;
  225. }
  226. }
  227. String GDScriptLanguage::debug_get_error() const {
  228. return _debug_error;
  229. }
  230. int GDScriptLanguage::debug_get_stack_level_count() const {
  231. if (_debug_parse_err_line >= 0) {
  232. return 1;
  233. }
  234. return _debug_call_stack_pos;
  235. }
  236. int GDScriptLanguage::debug_get_stack_level_line(int p_level) const {
  237. if (_debug_parse_err_line >= 0) {
  238. return _debug_parse_err_line;
  239. }
  240. ERR_FAIL_INDEX_V(p_level, _debug_call_stack_pos, -1);
  241. int l = _debug_call_stack_pos - p_level - 1;
  242. return *(_call_stack[l].line);
  243. }
  244. String GDScriptLanguage::debug_get_stack_level_function(int p_level) const {
  245. if (_debug_parse_err_line >= 0) {
  246. return "";
  247. }
  248. ERR_FAIL_INDEX_V(p_level, _debug_call_stack_pos, "");
  249. int l = _debug_call_stack_pos - p_level - 1;
  250. return _call_stack[l].function->get_name();
  251. }
  252. String GDScriptLanguage::debug_get_stack_level_source(int p_level) const {
  253. if (_debug_parse_err_line >= 0) {
  254. return _debug_parse_err_file;
  255. }
  256. ERR_FAIL_INDEX_V(p_level, _debug_call_stack_pos, "");
  257. int l = _debug_call_stack_pos - p_level - 1;
  258. return _call_stack[l].function->get_source();
  259. }
  260. 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) {
  261. if (_debug_parse_err_line >= 0) {
  262. return;
  263. }
  264. ERR_FAIL_INDEX(p_level, _debug_call_stack_pos);
  265. int l = _debug_call_stack_pos - p_level - 1;
  266. GDScriptFunction *f = _call_stack[l].function;
  267. List<Pair<StringName, int>> locals;
  268. f->debug_get_stack_member_state(*_call_stack[l].line, &locals);
  269. for (List<Pair<StringName, int>>::Element *E = locals.front(); E; E = E->next()) {
  270. p_locals->push_back(E->get().first);
  271. p_values->push_back(_call_stack[l].stack[E->get().second]);
  272. }
  273. }
  274. 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) {
  275. if (_debug_parse_err_line >= 0) {
  276. return;
  277. }
  278. ERR_FAIL_INDEX(p_level, _debug_call_stack_pos);
  279. int l = _debug_call_stack_pos - p_level - 1;
  280. GDScriptInstance *instance = _call_stack[l].instance;
  281. if (!instance) {
  282. return;
  283. }
  284. Ref<GDScript> script = instance->get_script();
  285. ERR_FAIL_COND(script.is_null());
  286. const Map<StringName, GDScript::MemberInfo> &mi = script->debug_get_member_indices();
  287. for (const Map<StringName, GDScript::MemberInfo>::Element *E = mi.front(); E; E = E->next()) {
  288. p_members->push_back(E->key());
  289. p_values->push_back(instance->debug_get_member_by_index(E->get().index));
  290. }
  291. }
  292. ScriptInstance *GDScriptLanguage::debug_get_stack_level_instance(int p_level) {
  293. if (_debug_parse_err_line >= 0) {
  294. return nullptr;
  295. }
  296. ERR_FAIL_INDEX_V(p_level, _debug_call_stack_pos, nullptr);
  297. int l = _debug_call_stack_pos - p_level - 1;
  298. ScriptInstance *instance = _call_stack[l].instance;
  299. return instance;
  300. }
  301. void GDScriptLanguage::debug_get_globals(List<String> *p_globals, List<Variant> *p_values, int p_max_subitems, int p_max_depth) {
  302. const Map<StringName, int> &name_idx = GDScriptLanguage::get_singleton()->get_global_map();
  303. const Variant *globals = GDScriptLanguage::get_singleton()->get_global_array();
  304. List<Pair<String, Variant>> cinfo;
  305. get_public_constants(&cinfo);
  306. for (const Map<StringName, int>::Element *E = name_idx.front(); E; E = E->next()) {
  307. if (ClassDB::class_exists(E->key()) || Engine::get_singleton()->has_singleton(E->key())) {
  308. continue;
  309. }
  310. bool is_script_constant = false;
  311. for (List<Pair<String, Variant>>::Element *CE = cinfo.front(); CE; CE = CE->next()) {
  312. if (CE->get().first == E->key()) {
  313. is_script_constant = true;
  314. break;
  315. }
  316. }
  317. if (is_script_constant) {
  318. continue;
  319. }
  320. const Variant &var = globals[E->value()];
  321. if (Object *obj = var) {
  322. if (Object::cast_to<GDScriptNativeClass>(obj)) {
  323. continue;
  324. }
  325. }
  326. bool skip = false;
  327. for (int i = 0; i < CoreConstants::get_global_constant_count(); i++) {
  328. if (E->key() == CoreConstants::get_global_constant_name(i)) {
  329. skip = true;
  330. break;
  331. }
  332. }
  333. if (skip) {
  334. continue;
  335. }
  336. p_globals->push_back(E->key());
  337. p_values->push_back(var);
  338. }
  339. }
  340. String GDScriptLanguage::debug_parse_stack_level_expression(int p_level, const String &p_expression, int p_max_subitems, int p_max_depth) {
  341. return "";
  342. }
  343. void GDScriptLanguage::get_recognized_extensions(List<String> *p_extensions) const {
  344. p_extensions->push_back("gd");
  345. }
  346. void GDScriptLanguage::get_public_functions(List<MethodInfo> *p_functions) const {
  347. List<StringName> functions;
  348. GDScriptUtilityFunctions::get_function_list(&functions);
  349. for (const List<StringName>::Element *E = functions.front(); E; E = E->next()) {
  350. p_functions->push_back(GDScriptUtilityFunctions::get_function_info(E->get()));
  351. }
  352. // Not really "functions", but show in documentation.
  353. {
  354. MethodInfo mi;
  355. mi.name = "preload";
  356. mi.arguments.push_back(PropertyInfo(Variant::STRING, "path"));
  357. mi.return_val = PropertyInfo(Variant::OBJECT, "", PROPERTY_HINT_RESOURCE_TYPE, "Resource");
  358. p_functions->push_back(mi);
  359. }
  360. {
  361. MethodInfo mi;
  362. mi.name = "assert";
  363. mi.return_val.type = Variant::NIL;
  364. mi.arguments.push_back(PropertyInfo(Variant::BOOL, "condition"));
  365. mi.arguments.push_back(PropertyInfo(Variant::STRING, "message"));
  366. mi.default_arguments.push_back(String());
  367. p_functions->push_back(mi);
  368. }
  369. }
  370. void GDScriptLanguage::get_public_constants(List<Pair<String, Variant>> *p_constants) const {
  371. Pair<String, Variant> pi;
  372. pi.first = "PI";
  373. pi.second = Math_PI;
  374. p_constants->push_back(pi);
  375. Pair<String, Variant> tau;
  376. tau.first = "TAU";
  377. tau.second = Math_TAU;
  378. p_constants->push_back(tau);
  379. Pair<String, Variant> infinity;
  380. infinity.first = "INF";
  381. infinity.second = Math_INF;
  382. p_constants->push_back(infinity);
  383. Pair<String, Variant> nan;
  384. nan.first = "NAN";
  385. nan.second = Math_NAN;
  386. p_constants->push_back(nan);
  387. }
  388. String GDScriptLanguage::make_function(const String &p_class, const String &p_name, const PackedStringArray &p_args) const {
  389. #ifdef TOOLS_ENABLED
  390. bool th = EditorSettings::get_singleton()->get_setting("text_editor/completion/add_type_hints");
  391. #else
  392. bool th = false;
  393. #endif
  394. String s = "func " + p_name + "(";
  395. if (p_args.size()) {
  396. for (int i = 0; i < p_args.size(); i++) {
  397. if (i > 0) {
  398. s += ", ";
  399. }
  400. s += p_args[i].get_slice(":", 0);
  401. if (th) {
  402. String type = p_args[i].get_slice(":", 1);
  403. if (!type.is_empty() && type != "var") {
  404. s += ": " + type;
  405. }
  406. }
  407. }
  408. }
  409. s += String(")") + (th ? " -> void" : "") + ":\n" + _get_indentation() + "pass # Replace with function body.\n";
  410. return s;
  411. }
  412. //////// COMPLETION //////////
  413. #ifdef TOOLS_ENABLED
  414. #define COMPLETION_RECURSION_LIMIT 200
  415. struct GDScriptCompletionIdentifier {
  416. GDScriptParser::DataType type;
  417. String enumeration;
  418. Variant value;
  419. const GDScriptParser::ExpressionNode *assigned_expression = nullptr;
  420. };
  421. static String _get_visual_datatype(const PropertyInfo &p_info, bool p_is_arg = true) {
  422. if (p_info.usage & PROPERTY_USAGE_CLASS_IS_ENUM) {
  423. String enum_name = p_info.class_name;
  424. if (enum_name.find(".") == -1) {
  425. return enum_name;
  426. }
  427. return enum_name.get_slice(".", 1);
  428. }
  429. String n = p_info.name;
  430. int idx = n.find(":");
  431. if (idx != -1) {
  432. return n.substr(idx + 1, n.length());
  433. }
  434. if (p_info.type == Variant::OBJECT) {
  435. if (p_info.hint == PROPERTY_HINT_RESOURCE_TYPE) {
  436. return p_info.hint_string;
  437. } else {
  438. return p_info.class_name.operator String();
  439. }
  440. }
  441. if (p_info.type == Variant::NIL) {
  442. if (p_is_arg || (p_info.usage & PROPERTY_USAGE_NIL_IS_VARIANT)) {
  443. return "Variant";
  444. } else {
  445. return "void";
  446. }
  447. }
  448. return Variant::get_type_name(p_info.type);
  449. }
  450. static String _make_arguments_hint(const MethodInfo &p_info, int p_arg_idx, bool p_is_annotation = false) {
  451. String arghint;
  452. if (!p_is_annotation) {
  453. arghint += _get_visual_datatype(p_info.return_val, false) + " ";
  454. }
  455. arghint += p_info.name + "(";
  456. int def_args = p_info.arguments.size() - p_info.default_arguments.size();
  457. int i = 0;
  458. for (const List<PropertyInfo>::Element *E = p_info.arguments.front(); E; E = E->next()) {
  459. if (i > 0) {
  460. arghint += ", ";
  461. }
  462. if (i == p_arg_idx) {
  463. arghint += String::chr(0xFFFF);
  464. }
  465. arghint += E->get().name + ": " + _get_visual_datatype(E->get(), true);
  466. if (i - def_args >= 0) {
  467. arghint += String(" = ") + p_info.default_arguments[i - def_args].get_construct_string();
  468. }
  469. if (i == p_arg_idx) {
  470. arghint += String::chr(0xFFFF);
  471. }
  472. i++;
  473. }
  474. if (p_info.flags & METHOD_FLAG_VARARG) {
  475. if (p_info.arguments.size() > 0) {
  476. arghint += ", ";
  477. }
  478. if (p_arg_idx >= p_info.arguments.size()) {
  479. arghint += String::chr(0xFFFF);
  480. }
  481. arghint += "...";
  482. if (p_arg_idx >= p_info.arguments.size()) {
  483. arghint += String::chr(0xFFFF);
  484. }
  485. }
  486. arghint += ")";
  487. return arghint;
  488. }
  489. static String _make_arguments_hint(const GDScriptParser::FunctionNode *p_function, int p_arg_idx) {
  490. String arghint = p_function->get_datatype().to_string() + " " + p_function->identifier->name.operator String() + "(";
  491. for (int i = 0; i < p_function->parameters.size(); i++) {
  492. if (i > 0) {
  493. arghint += ", ";
  494. }
  495. if (i == p_arg_idx) {
  496. arghint += String::chr(0xFFFF);
  497. }
  498. const GDScriptParser::ParameterNode *par = p_function->parameters[i];
  499. arghint += par->identifier->name.operator String() + ": " + par->get_datatype().to_string();
  500. if (par->default_value) {
  501. String def_val = "<unknown>";
  502. if (par->default_value->type == GDScriptParser::Node::LITERAL) {
  503. const GDScriptParser::LiteralNode *literal = static_cast<const GDScriptParser::LiteralNode *>(par->default_value);
  504. def_val = literal->value.get_construct_string();
  505. } else if (par->default_value->type == GDScriptParser::Node::IDENTIFIER) {
  506. const GDScriptParser::IdentifierNode *id = static_cast<const GDScriptParser::IdentifierNode *>(par->default_value);
  507. def_val = id->name.operator String();
  508. }
  509. arghint += " = " + def_val;
  510. }
  511. if (i == p_arg_idx) {
  512. arghint += String::chr(0xFFFF);
  513. }
  514. }
  515. arghint += ")";
  516. return arghint;
  517. }
  518. static void _get_directory_contents(EditorFileSystemDirectory *p_dir, Map<String, ScriptCodeCompletionOption> &r_list) {
  519. const String quote_style = EDITOR_DEF("text_editor/completion/use_single_quotes", false) ? "'" : "\"";
  520. for (int i = 0; i < p_dir->get_file_count(); i++) {
  521. ScriptCodeCompletionOption option(p_dir->get_file_path(i), ScriptCodeCompletionOption::KIND_FILE_PATH);
  522. option.insert_text = quote_style + option.display + quote_style;
  523. r_list.insert(option.display, option);
  524. }
  525. for (int i = 0; i < p_dir->get_subdir_count(); i++) {
  526. _get_directory_contents(p_dir->get_subdir(i), r_list);
  527. }
  528. }
  529. static void _find_annotation_arguments(const GDScriptParser::AnnotationNode *p_annotation, int p_argument, const String p_quote_style, Map<String, ScriptCodeCompletionOption> &r_result) {
  530. if (p_annotation->name == "@export_range" || p_annotation->name == "@export_exp_range") {
  531. if (p_argument == 3 || p_argument == 4) {
  532. // Slider hint.
  533. ScriptCodeCompletionOption slider1("or_greater", ScriptCodeCompletionOption::KIND_PLAIN_TEXT);
  534. slider1.insert_text = p_quote_style + slider1.display + p_quote_style;
  535. r_result.insert(slider1.display, slider1);
  536. ScriptCodeCompletionOption slider2("or_lesser", ScriptCodeCompletionOption::KIND_PLAIN_TEXT);
  537. slider2.insert_text = p_quote_style + slider2.display + p_quote_style;
  538. r_result.insert(slider2.display, slider2);
  539. }
  540. } else if (p_annotation->name == "@export_exp_easing") {
  541. if (p_argument == 0 || p_argument == 1) {
  542. // Easing hint.
  543. ScriptCodeCompletionOption hint1("attenuation", ScriptCodeCompletionOption::KIND_PLAIN_TEXT);
  544. hint1.insert_text = p_quote_style + hint1.display + p_quote_style;
  545. r_result.insert(hint1.display, hint1);
  546. ScriptCodeCompletionOption hint2("inout", ScriptCodeCompletionOption::KIND_PLAIN_TEXT);
  547. hint2.insert_text = p_quote_style + hint2.display + p_quote_style;
  548. r_result.insert(hint2.display, hint2);
  549. }
  550. } else if (p_annotation->name == "@export_node_path") {
  551. ScriptCodeCompletionOption node("Node", ScriptCodeCompletionOption::KIND_CLASS);
  552. r_result.insert(node.display, node);
  553. List<StringName> node_types;
  554. ClassDB::get_inheriters_from_class("Node", &node_types);
  555. for (const List<StringName>::Element *E = node_types.front(); E != nullptr; E = E->next()) {
  556. if (!ClassDB::is_class_exposed(E->get())) {
  557. continue;
  558. }
  559. ScriptCodeCompletionOption option(E->get(), ScriptCodeCompletionOption::KIND_CLASS);
  560. r_result.insert(option.display, option);
  561. }
  562. }
  563. }
  564. static void _list_available_types(bool p_inherit_only, GDScriptParser::CompletionContext &p_context, Map<String, ScriptCodeCompletionOption> &r_result) {
  565. List<StringName> native_types;
  566. ClassDB::get_class_list(&native_types);
  567. for (const List<StringName>::Element *E = native_types.front(); E != nullptr; E = E->next()) {
  568. if (ClassDB::is_class_exposed(E->get()) && !Engine::get_singleton()->has_singleton(E->get())) {
  569. ScriptCodeCompletionOption option(E->get(), ScriptCodeCompletionOption::KIND_CLASS);
  570. r_result.insert(option.display, option);
  571. }
  572. }
  573. if (p_context.current_class) {
  574. if (!p_inherit_only && p_context.current_class->base_type.is_set()) {
  575. // Native enums from base class
  576. List<StringName> enums;
  577. ClassDB::get_enum_list(p_context.current_class->base_type.native_type, &enums);
  578. for (const List<StringName>::Element *E = enums.front(); E != nullptr; E = E->next()) {
  579. ScriptCodeCompletionOption option(E->get(), ScriptCodeCompletionOption::KIND_ENUM);
  580. r_result.insert(option.display, option);
  581. }
  582. }
  583. // Check current class for potential types
  584. const GDScriptParser::ClassNode *current = p_context.current_class;
  585. while (current) {
  586. for (int i = 0; i < current->members.size(); i++) {
  587. const GDScriptParser::ClassNode::Member &member = current->members[i];
  588. switch (member.type) {
  589. case GDScriptParser::ClassNode::Member::CLASS: {
  590. ScriptCodeCompletionOption option(member.m_class->identifier->name, ScriptCodeCompletionOption::KIND_CLASS);
  591. r_result.insert(option.display, option);
  592. } break;
  593. case GDScriptParser::ClassNode::Member::ENUM: {
  594. if (!p_inherit_only) {
  595. ScriptCodeCompletionOption option(member.m_enum->identifier->name, ScriptCodeCompletionOption::KIND_ENUM);
  596. r_result.insert(option.display, option);
  597. }
  598. } break;
  599. case GDScriptParser::ClassNode::Member::CONSTANT: {
  600. if (member.constant->get_datatype().is_meta_type && p_context.current_class->outer != nullptr) {
  601. ScriptCodeCompletionOption option(member.constant->identifier->name, ScriptCodeCompletionOption::KIND_CLASS);
  602. r_result.insert(option.display, option);
  603. }
  604. } break;
  605. default:
  606. break;
  607. }
  608. }
  609. current = current->outer;
  610. }
  611. }
  612. // Global scripts
  613. List<StringName> global_classes;
  614. ScriptServer::get_global_class_list(&global_classes);
  615. for (const List<StringName>::Element *E = global_classes.front(); E != nullptr; E = E->next()) {
  616. ScriptCodeCompletionOption option(E->get(), ScriptCodeCompletionOption::KIND_CLASS);
  617. r_result.insert(option.display, option);
  618. }
  619. // Autoload singletons
  620. Map<StringName, ProjectSettings::AutoloadInfo> autoloads = ProjectSettings::get_singleton()->get_autoload_list();
  621. for (const Map<StringName, ProjectSettings::AutoloadInfo>::Element *E = autoloads.front(); E != nullptr; E = E->next()) {
  622. const ProjectSettings::AutoloadInfo &info = E->get();
  623. if (!info.is_singleton || info.path.get_extension().to_lower() != "gd") {
  624. continue;
  625. }
  626. ScriptCodeCompletionOption option(info.name, ScriptCodeCompletionOption::KIND_CLASS);
  627. r_result.insert(option.display, option);
  628. }
  629. }
  630. static void _find_identifiers_in_suite(const GDScriptParser::SuiteNode *p_suite, Map<String, ScriptCodeCompletionOption> &r_result) {
  631. for (int i = 0; i < p_suite->locals.size(); i++) {
  632. ScriptCodeCompletionOption option;
  633. if (p_suite->locals[i].type == GDScriptParser::SuiteNode::Local::CONSTANT) {
  634. option = ScriptCodeCompletionOption(p_suite->locals[i].name, ScriptCodeCompletionOption::KIND_CONSTANT);
  635. } else {
  636. option = ScriptCodeCompletionOption(p_suite->locals[i].name, ScriptCodeCompletionOption::KIND_VARIABLE);
  637. }
  638. r_result.insert(option.display, option);
  639. }
  640. if (p_suite->parent_block) {
  641. _find_identifiers_in_suite(p_suite->parent_block, r_result);
  642. }
  643. }
  644. static void _find_identifiers_in_base(const GDScriptCompletionIdentifier &p_base, bool p_only_functions, Map<String, ScriptCodeCompletionOption> &r_result, int p_recursion_depth);
  645. static void _find_identifiers_in_class(const GDScriptParser::ClassNode *p_class, bool p_only_functions, bool p_static, bool p_parent_only, Map<String, ScriptCodeCompletionOption> &r_result, int p_recursion_depth) {
  646. ERR_FAIL_COND(p_recursion_depth > COMPLETION_RECURSION_LIMIT);
  647. if (!p_parent_only) {
  648. bool outer = false;
  649. const GDScriptParser::ClassNode *clss = p_class;
  650. while (clss) {
  651. for (int i = 0; i < clss->members.size(); i++) {
  652. const GDScriptParser::ClassNode::Member &member = clss->members[i];
  653. ScriptCodeCompletionOption option;
  654. switch (member.type) {
  655. case GDScriptParser::ClassNode::Member::VARIABLE:
  656. if (p_only_functions || outer || (p_static)) {
  657. continue;
  658. }
  659. option = ScriptCodeCompletionOption(member.variable->identifier->name, ScriptCodeCompletionOption::KIND_MEMBER);
  660. break;
  661. case GDScriptParser::ClassNode::Member::CONSTANT:
  662. if (p_only_functions) {
  663. continue;
  664. }
  665. option = ScriptCodeCompletionOption(member.constant->identifier->name, ScriptCodeCompletionOption::KIND_CONSTANT);
  666. if (member.constant->initializer) {
  667. option.default_value = member.constant->initializer->reduced_value;
  668. }
  669. break;
  670. case GDScriptParser::ClassNode::Member::CLASS:
  671. if (p_only_functions) {
  672. continue;
  673. }
  674. option = ScriptCodeCompletionOption(member.m_class->identifier->name, ScriptCodeCompletionOption::KIND_CLASS);
  675. break;
  676. case GDScriptParser::ClassNode::Member::ENUM_VALUE:
  677. if (p_only_functions) {
  678. continue;
  679. }
  680. option = ScriptCodeCompletionOption(member.enum_value.identifier->name, ScriptCodeCompletionOption::KIND_CONSTANT);
  681. break;
  682. case GDScriptParser::ClassNode::Member::ENUM:
  683. if (p_only_functions) {
  684. continue;
  685. }
  686. option = ScriptCodeCompletionOption(member.m_enum->identifier->name, ScriptCodeCompletionOption::KIND_ENUM);
  687. break;
  688. case GDScriptParser::ClassNode::Member::FUNCTION:
  689. if (outer || (p_static && !member.function->is_static) || member.function->identifier->name.operator String().begins_with("@")) {
  690. continue;
  691. }
  692. option = ScriptCodeCompletionOption(member.function->identifier->name, ScriptCodeCompletionOption::KIND_FUNCTION);
  693. if (member.function->parameters.size() > 0) {
  694. option.insert_text += "(";
  695. } else {
  696. option.insert_text += "()";
  697. }
  698. break;
  699. case GDScriptParser::ClassNode::Member::SIGNAL:
  700. if (p_only_functions || outer) {
  701. continue;
  702. }
  703. option = ScriptCodeCompletionOption(member.signal->identifier->name, ScriptCodeCompletionOption::KIND_SIGNAL);
  704. break;
  705. case GDScriptParser::ClassNode::Member::UNDEFINED:
  706. break;
  707. }
  708. r_result.insert(option.display, option);
  709. }
  710. outer = true;
  711. clss = clss->outer;
  712. }
  713. }
  714. // Parents.
  715. GDScriptCompletionIdentifier base_type;
  716. base_type.type = p_class->base_type;
  717. base_type.type.is_meta_type = p_static;
  718. _find_identifiers_in_base(base_type, p_only_functions, r_result, p_recursion_depth + 1);
  719. }
  720. static void _find_identifiers_in_base(const GDScriptCompletionIdentifier &p_base, bool p_only_functions, Map<String, ScriptCodeCompletionOption> &r_result, int p_recursion_depth) {
  721. ERR_FAIL_COND(p_recursion_depth > COMPLETION_RECURSION_LIMIT);
  722. GDScriptParser::DataType base_type = p_base.type;
  723. bool _static = base_type.is_meta_type;
  724. if (_static && base_type.kind != GDScriptParser::DataType::BUILTIN) {
  725. ScriptCodeCompletionOption option("new", ScriptCodeCompletionOption::KIND_FUNCTION);
  726. option.insert_text += "(";
  727. r_result.insert(option.display, option);
  728. }
  729. while (!base_type.has_no_type()) {
  730. switch (base_type.kind) {
  731. case GDScriptParser::DataType::CLASS: {
  732. _find_identifiers_in_class(base_type.class_type, p_only_functions, _static, false, r_result, p_recursion_depth + 1);
  733. // This already finds all parent identifiers, so we are done.
  734. base_type = GDScriptParser::DataType();
  735. } break;
  736. case GDScriptParser::DataType::SCRIPT: {
  737. Ref<Script> scr = base_type.script_type;
  738. if (scr.is_valid()) {
  739. if (!p_only_functions) {
  740. if (!_static) {
  741. List<PropertyInfo> members;
  742. scr->get_script_property_list(&members);
  743. for (List<PropertyInfo>::Element *E = members.front(); E; E = E->next()) {
  744. ScriptCodeCompletionOption option(E->get().name, ScriptCodeCompletionOption::KIND_MEMBER);
  745. r_result.insert(option.display, option);
  746. }
  747. }
  748. Map<StringName, Variant> constants;
  749. scr->get_constants(&constants);
  750. for (Map<StringName, Variant>::Element *E = constants.front(); E; E = E->next()) {
  751. ScriptCodeCompletionOption option(E->key().operator String(), ScriptCodeCompletionOption::KIND_CONSTANT);
  752. r_result.insert(option.display, option);
  753. }
  754. List<MethodInfo> signals;
  755. scr->get_script_signal_list(&signals);
  756. for (List<MethodInfo>::Element *E = signals.front(); E; E = E->next()) {
  757. ScriptCodeCompletionOption option(E->get().name, ScriptCodeCompletionOption::KIND_SIGNAL);
  758. r_result.insert(option.display, option);
  759. }
  760. }
  761. List<MethodInfo> methods;
  762. scr->get_script_method_list(&methods);
  763. for (List<MethodInfo>::Element *E = methods.front(); E; E = E->next()) {
  764. if (E->get().name.begins_with("@")) {
  765. continue;
  766. }
  767. ScriptCodeCompletionOption option(E->get().name, ScriptCodeCompletionOption::KIND_FUNCTION);
  768. if (E->get().arguments.size()) {
  769. option.insert_text += "(";
  770. } else {
  771. option.insert_text += "()";
  772. }
  773. r_result.insert(option.display, option);
  774. }
  775. Ref<Script> base_script = scr->get_base_script();
  776. if (base_script.is_valid()) {
  777. base_type.script_type = base_script;
  778. } else {
  779. base_type.kind = GDScriptParser::DataType::NATIVE;
  780. base_type.native_type = scr->get_instance_base_type();
  781. }
  782. } else {
  783. return;
  784. }
  785. } break;
  786. case GDScriptParser::DataType::NATIVE: {
  787. StringName type = GDScriptParser::get_real_class_name(base_type.native_type);
  788. if (!ClassDB::class_exists(type)) {
  789. return;
  790. }
  791. if (!p_only_functions) {
  792. List<String> constants;
  793. ClassDB::get_integer_constant_list(type, &constants);
  794. for (List<String>::Element *E = constants.front(); E; E = E->next()) {
  795. ScriptCodeCompletionOption option(E->get(), ScriptCodeCompletionOption::KIND_CONSTANT);
  796. r_result.insert(option.display, option);
  797. }
  798. if (!_static || Engine::get_singleton()->has_singleton(type)) {
  799. List<PropertyInfo> pinfo;
  800. ClassDB::get_property_list(type, &pinfo);
  801. for (List<PropertyInfo>::Element *E = pinfo.front(); E; E = E->next()) {
  802. if (E->get().usage & (PROPERTY_USAGE_GROUP | PROPERTY_USAGE_CATEGORY)) {
  803. continue;
  804. }
  805. if (E->get().name.find("/") != -1) {
  806. continue;
  807. }
  808. ScriptCodeCompletionOption option(E->get().name, ScriptCodeCompletionOption::KIND_MEMBER);
  809. r_result.insert(option.display, option);
  810. }
  811. }
  812. }
  813. if (!_static || Engine::get_singleton()->has_singleton(type)) {
  814. List<MethodInfo> methods;
  815. bool is_autocompleting_getters = GLOBAL_GET("debug/gdscript/completion/autocomplete_setters_and_getters").booleanize();
  816. ClassDB::get_method_list(type, &methods, false, !is_autocompleting_getters);
  817. for (List<MethodInfo>::Element *E = methods.front(); E; E = E->next()) {
  818. if (E->get().name.begins_with("_")) {
  819. continue;
  820. }
  821. ScriptCodeCompletionOption option(E->get().name, ScriptCodeCompletionOption::KIND_FUNCTION);
  822. if (E->get().arguments.size()) {
  823. option.insert_text += "(";
  824. } else {
  825. option.insert_text += "()";
  826. }
  827. r_result.insert(option.display, option);
  828. }
  829. }
  830. return;
  831. } break;
  832. case GDScriptParser::DataType::BUILTIN: {
  833. Callable::CallError err;
  834. Variant tmp;
  835. Variant::construct(base_type.builtin_type, tmp, nullptr, 0, err);
  836. if (err.error != Callable::CallError::CALL_OK) {
  837. return;
  838. }
  839. if (!p_only_functions) {
  840. List<PropertyInfo> members;
  841. if (p_base.value.get_type() != Variant::NIL) {
  842. p_base.value.get_property_list(&members);
  843. } else {
  844. tmp.get_property_list(&members);
  845. }
  846. for (List<PropertyInfo>::Element *E = members.front(); E; E = E->next()) {
  847. if (String(E->get().name).find("/") == -1) {
  848. ScriptCodeCompletionOption option(E->get().name, ScriptCodeCompletionOption::KIND_MEMBER);
  849. r_result.insert(option.display, option);
  850. }
  851. }
  852. }
  853. List<MethodInfo> methods;
  854. tmp.get_method_list(&methods);
  855. for (const List<MethodInfo>::Element *E = methods.front(); E; E = E->next()) {
  856. ScriptCodeCompletionOption option(E->get().name, ScriptCodeCompletionOption::KIND_FUNCTION);
  857. if (E->get().arguments.size()) {
  858. option.insert_text += "(";
  859. } else {
  860. option.insert_text += "()";
  861. }
  862. r_result.insert(option.display, option);
  863. }
  864. return;
  865. } break;
  866. default: {
  867. return;
  868. } break;
  869. }
  870. }
  871. }
  872. static void _find_identifiers(GDScriptParser::CompletionContext &p_context, bool p_only_functions, Map<String, ScriptCodeCompletionOption> &r_result, int p_recursion_depth) {
  873. if (!p_only_functions && p_context.current_suite) {
  874. // This includes function parameters, since they are also locals.
  875. _find_identifiers_in_suite(p_context.current_suite, r_result);
  876. }
  877. if (p_context.current_class) {
  878. _find_identifiers_in_class(p_context.current_class, p_only_functions, (!p_context.current_function || p_context.current_function->is_static), false, r_result, p_recursion_depth + 1);
  879. }
  880. List<StringName> functions;
  881. GDScriptUtilityFunctions::get_function_list(&functions);
  882. for (const List<StringName>::Element *E = functions.front(); E; E = E->next()) {
  883. MethodInfo function = GDScriptUtilityFunctions::get_function_info(E->get());
  884. ScriptCodeCompletionOption option(String(E->get()), ScriptCodeCompletionOption::KIND_FUNCTION);
  885. if (function.arguments.size() || (function.flags & METHOD_FLAG_VARARG)) {
  886. option.insert_text += "(";
  887. } else {
  888. option.insert_text += "()";
  889. }
  890. r_result.insert(option.display, option);
  891. }
  892. if (p_only_functions) {
  893. return;
  894. }
  895. static const char *_type_names[Variant::VARIANT_MAX] = {
  896. "null", "bool", "int", "float", "String", "StringName", "Vector2", "Vector2i", "Rect2", "Rect2i", "Vector3", "Vector3i", "Transform2D", "Plane", "Quat", "AABB", "Basis", "Transform",
  897. "Color", "NodePath", "RID", "Signal", "Callable", "Object", "Dictionary", "Array", "PackedByteArray", "PackedInt32Array", "PackedInt64Array", "PackedFloat32Array", "PackedFloat64Array", "PackedStringArray",
  898. "PackedVector2Array", "PackedVector3Array", "PackedColorArray"
  899. };
  900. static_assert((sizeof(_type_names) / sizeof(*_type_names)) == Variant::VARIANT_MAX, "Completion for builtin types is incomplete");
  901. for (int i = 0; i < Variant::VARIANT_MAX; i++) {
  902. ScriptCodeCompletionOption option(_type_names[i], ScriptCodeCompletionOption::KIND_CLASS);
  903. r_result.insert(option.display, option);
  904. }
  905. static const char *_keywords[] = {
  906. "false", "PI", "TAU", "INF", "NAN", "self", "true", "breakpoint", "tool", "super",
  907. "break", "continue", "pass", "return",
  908. nullptr
  909. };
  910. const char **kw = _keywords;
  911. while (*kw) {
  912. ScriptCodeCompletionOption option(*kw, ScriptCodeCompletionOption::KIND_PLAIN_TEXT);
  913. r_result.insert(option.display, option);
  914. kw++;
  915. }
  916. static const char *_keywords_with_space[] = {
  917. "and", "in", "not", "or", "as", "class", "extends", "is", "func", "signal", "await",
  918. "const", "enum", "static", "var", "if", "elif", "else", "for", "match", "while",
  919. nullptr
  920. };
  921. const char **kws = _keywords_with_space;
  922. while (*kws) {
  923. ScriptCodeCompletionOption option(*kws, ScriptCodeCompletionOption::KIND_PLAIN_TEXT);
  924. option.insert_text += " ";
  925. r_result.insert(option.display, option);
  926. kws++;
  927. }
  928. static const char *_keywords_with_args[] = {
  929. "assert", "preload",
  930. nullptr
  931. };
  932. const char **kwa = _keywords_with_args;
  933. while (*kwa) {
  934. ScriptCodeCompletionOption option(*kwa, ScriptCodeCompletionOption::KIND_FUNCTION);
  935. option.insert_text += "(";
  936. r_result.insert(option.display, option);
  937. kwa++;
  938. }
  939. Map<StringName, ProjectSettings::AutoloadInfo> autoloads = ProjectSettings::get_singleton()->get_autoload_list();
  940. for (const Map<StringName, ProjectSettings::AutoloadInfo>::Element *E = autoloads.front(); E != nullptr; E = E->next()) {
  941. if (!E->value().is_singleton) {
  942. continue;
  943. }
  944. ScriptCodeCompletionOption option(E->key(), ScriptCodeCompletionOption::KIND_CONSTANT);
  945. r_result.insert(option.display, option);
  946. }
  947. // Native classes and global constants.
  948. for (const Map<StringName, int>::Element *E = GDScriptLanguage::get_singleton()->get_global_map().front(); E; E = E->next()) {
  949. ScriptCodeCompletionOption option;
  950. if (ClassDB::class_exists(E->key()) || Engine::get_singleton()->has_singleton(E->key())) {
  951. option = ScriptCodeCompletionOption(E->key().operator String(), ScriptCodeCompletionOption::KIND_CLASS);
  952. } else {
  953. option = ScriptCodeCompletionOption(E->key().operator String(), ScriptCodeCompletionOption::KIND_CONSTANT);
  954. }
  955. r_result.insert(option.display, option);
  956. }
  957. }
  958. static GDScriptCompletionIdentifier _type_from_variant(const Variant &p_value) {
  959. GDScriptCompletionIdentifier ci;
  960. ci.value = p_value;
  961. ci.type.is_constant = true;
  962. ci.type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  963. ci.type.kind = GDScriptParser::DataType::BUILTIN;
  964. ci.type.builtin_type = p_value.get_type();
  965. if (ci.type.builtin_type == Variant::OBJECT) {
  966. Object *obj = p_value.operator Object *();
  967. if (!obj) {
  968. return ci;
  969. }
  970. ci.type.native_type = obj->get_class_name();
  971. Ref<Script> scr = p_value;
  972. if (scr.is_valid()) {
  973. ci.type.is_meta_type = true;
  974. } else {
  975. ci.type.is_meta_type = false;
  976. scr = obj->get_script();
  977. }
  978. if (scr.is_valid()) {
  979. ci.type.script_type = scr;
  980. ci.type.kind = GDScriptParser::DataType::SCRIPT;
  981. ci.type.native_type = scr->get_instance_base_type();
  982. } else {
  983. ci.type.kind = GDScriptParser::DataType::NATIVE;
  984. }
  985. }
  986. return ci;
  987. }
  988. static GDScriptCompletionIdentifier _type_from_property(const PropertyInfo &p_property) {
  989. GDScriptCompletionIdentifier ci;
  990. if (p_property.type == Variant::NIL) {
  991. // Variant
  992. return ci;
  993. }
  994. if (p_property.usage & PROPERTY_USAGE_CLASS_IS_ENUM) {
  995. ci.enumeration = p_property.class_name;
  996. }
  997. ci.type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  998. ci.type.builtin_type = p_property.type;
  999. if (p_property.type == Variant::OBJECT) {
  1000. ci.type.kind = GDScriptParser::DataType::NATIVE;
  1001. ci.type.native_type = p_property.class_name == StringName() ? "Object" : p_property.class_name;
  1002. } else {
  1003. ci.type.kind = GDScriptParser::DataType::BUILTIN;
  1004. }
  1005. return ci;
  1006. }
  1007. static bool _guess_identifier_type(GDScriptParser::CompletionContext &p_context, const StringName &p_identifier, GDScriptCompletionIdentifier &r_type);
  1008. static bool _guess_identifier_type_from_base(GDScriptParser::CompletionContext &p_context, const GDScriptCompletionIdentifier &p_base, const StringName &p_identifier, GDScriptCompletionIdentifier &r_type);
  1009. static bool _guess_method_return_type_from_base(GDScriptParser::CompletionContext &p_context, const GDScriptCompletionIdentifier &p_base, const StringName &p_method, GDScriptCompletionIdentifier &r_type);
  1010. static bool _guess_expression_type(GDScriptParser::CompletionContext &p_context, const GDScriptParser::ExpressionNode *p_expression, GDScriptCompletionIdentifier &r_type) {
  1011. bool found = false;
  1012. if (p_expression->is_constant) {
  1013. // Already has a value, so just use that.
  1014. r_type = _type_from_variant(p_expression->reduced_value);
  1015. found = true;
  1016. } else {
  1017. switch (p_expression->type) {
  1018. case GDScriptParser::Node::LITERAL: {
  1019. const GDScriptParser::LiteralNode *literal = static_cast<const GDScriptParser::LiteralNode *>(p_expression);
  1020. r_type = _type_from_variant(literal->value);
  1021. found = true;
  1022. } break;
  1023. case GDScriptParser::Node::SELF: {
  1024. if (p_context.current_class) {
  1025. r_type.type.kind = GDScriptParser::DataType::CLASS;
  1026. r_type.type.type_source = GDScriptParser::DataType::INFERRED;
  1027. r_type.type.is_constant = true;
  1028. r_type.type.class_type = p_context.current_class;
  1029. r_type.value = p_context.base;
  1030. found = true;
  1031. }
  1032. } break;
  1033. case GDScriptParser::Node::IDENTIFIER: {
  1034. const GDScriptParser::IdentifierNode *id = static_cast<const GDScriptParser::IdentifierNode *>(p_expression);
  1035. found = _guess_identifier_type(p_context, id->name, r_type);
  1036. } break;
  1037. case GDScriptParser::Node::DICTIONARY: {
  1038. // Try to recreate the dictionary.
  1039. const GDScriptParser::DictionaryNode *dn = static_cast<const GDScriptParser::DictionaryNode *>(p_expression);
  1040. Dictionary d;
  1041. bool full = true;
  1042. for (int i = 0; i < dn->elements.size(); i++) {
  1043. GDScriptCompletionIdentifier key;
  1044. if (_guess_expression_type(p_context, dn->elements[i].key, key)) {
  1045. if (!key.type.is_constant) {
  1046. full = false;
  1047. break;
  1048. }
  1049. GDScriptCompletionIdentifier value;
  1050. if (_guess_expression_type(p_context, dn->elements[i].value, value)) {
  1051. if (!value.type.is_constant) {
  1052. full = false;
  1053. break;
  1054. }
  1055. d[key.value] = value.value;
  1056. } else {
  1057. full = false;
  1058. break;
  1059. }
  1060. } else {
  1061. full = false;
  1062. break;
  1063. }
  1064. }
  1065. if (full) {
  1066. r_type.value = d;
  1067. r_type.type.is_constant = true;
  1068. }
  1069. r_type.type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  1070. r_type.type.kind = GDScriptParser::DataType::BUILTIN;
  1071. r_type.type.builtin_type = Variant::DICTIONARY;
  1072. found = true;
  1073. } break;
  1074. case GDScriptParser::Node::ARRAY: {
  1075. // Try to recreate the array
  1076. const GDScriptParser::ArrayNode *an = static_cast<const GDScriptParser::ArrayNode *>(p_expression);
  1077. Array a;
  1078. bool full = true;
  1079. a.resize(an->elements.size());
  1080. for (int i = 0; i < an->elements.size(); i++) {
  1081. GDScriptCompletionIdentifier value;
  1082. if (_guess_expression_type(p_context, an->elements[i], value)) {
  1083. if (value.type.is_constant) {
  1084. a[i] = value.value;
  1085. } else {
  1086. full = false;
  1087. break;
  1088. }
  1089. } else {
  1090. full = false;
  1091. break;
  1092. }
  1093. }
  1094. if (full) {
  1095. // If not fully constant, setting this value is detrimental to the inference.
  1096. r_type.value = a;
  1097. }
  1098. r_type.type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  1099. r_type.type.kind = GDScriptParser::DataType::BUILTIN;
  1100. r_type.type.builtin_type = Variant::ARRAY;
  1101. found = true;
  1102. } break;
  1103. case GDScriptParser::Node::CAST: {
  1104. const GDScriptParser::CastNode *cn = static_cast<const GDScriptParser::CastNode *>(p_expression);
  1105. GDScriptCompletionIdentifier value;
  1106. if (_guess_expression_type(p_context, cn->operand, r_type)) {
  1107. r_type.type = cn->get_datatype();
  1108. found = true;
  1109. }
  1110. } break;
  1111. case GDScriptParser::Node::CALL: {
  1112. const GDScriptParser::CallNode *call = static_cast<const GDScriptParser::CallNode *>(p_expression);
  1113. if (GDScriptParser::get_builtin_type(call->function_name) < Variant::VARIANT_MAX) {
  1114. r_type.type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  1115. r_type.type.kind = GDScriptParser::DataType::BUILTIN;
  1116. r_type.type.builtin_type = GDScriptParser::get_builtin_type(call->function_name);
  1117. found = true;
  1118. break;
  1119. } else if (GDScriptUtilityFunctions::function_exists(call->function_name)) {
  1120. MethodInfo mi = GDScriptUtilityFunctions::get_function_info(call->function_name);
  1121. r_type = _type_from_property(mi.return_val);
  1122. found = true;
  1123. break;
  1124. } else {
  1125. GDScriptParser::CompletionContext c = p_context;
  1126. c.current_line = call->start_line;
  1127. GDScriptParser::Node::Type callee_type = call->get_callee_type();
  1128. GDScriptCompletionIdentifier base;
  1129. if (callee_type == GDScriptParser::Node::IDENTIFIER || call->is_super) {
  1130. // Simple call, so base is 'self'.
  1131. if (p_context.current_class) {
  1132. base.type.kind = GDScriptParser::DataType::CLASS;
  1133. base.type.type_source = GDScriptParser::DataType::INFERRED;
  1134. base.type.is_constant = true;
  1135. base.type.class_type = p_context.current_class;
  1136. base.value = p_context.base;
  1137. } else {
  1138. break;
  1139. }
  1140. } else if (callee_type == GDScriptParser::Node::SUBSCRIPT && static_cast<const GDScriptParser::SubscriptNode *>(call->callee)->is_attribute) {
  1141. if (!_guess_expression_type(c, static_cast<const GDScriptParser::SubscriptNode *>(call->callee)->base, base)) {
  1142. found = false;
  1143. break;
  1144. }
  1145. } else {
  1146. break;
  1147. }
  1148. // Try call if constant methods with constant arguments
  1149. if (base.type.is_constant && base.value.get_type() == Variant::OBJECT) {
  1150. GDScriptParser::DataType native_type = base.type;
  1151. while (native_type.kind == GDScriptParser::DataType::CLASS) {
  1152. native_type = native_type.class_type->base_type;
  1153. }
  1154. while (native_type.kind == GDScriptParser::DataType::SCRIPT) {
  1155. if (native_type.script_type.is_valid()) {
  1156. Ref<Script> parent = native_type.script_type->get_base_script();
  1157. if (parent.is_valid()) {
  1158. native_type.script_type = parent;
  1159. } else {
  1160. native_type.kind = GDScriptParser::DataType::NATIVE;
  1161. native_type.native_type = native_type.script_type->get_instance_base_type();
  1162. if (!ClassDB::class_exists(native_type.native_type)) {
  1163. native_type.native_type = String("_") + native_type.native_type;
  1164. if (!ClassDB::class_exists(native_type.native_type)) {
  1165. native_type.kind = GDScriptParser::DataType::UNRESOLVED;
  1166. }
  1167. }
  1168. }
  1169. }
  1170. }
  1171. if (native_type.kind == GDScriptParser::DataType::NATIVE) {
  1172. MethodBind *mb = ClassDB::get_method(native_type.native_type, call->function_name);
  1173. if (mb && mb->is_const()) {
  1174. bool all_is_const = true;
  1175. Vector<Variant> args;
  1176. GDScriptParser::CompletionContext c2 = p_context;
  1177. c2.current_line = call->start_line;
  1178. for (int i = 0; all_is_const && i < call->arguments.size(); i++) {
  1179. GDScriptCompletionIdentifier arg;
  1180. if (!call->arguments[i]->is_constant) {
  1181. all_is_const = false;
  1182. }
  1183. }
  1184. Object *baseptr = base.value;
  1185. if (all_is_const && String(call->function_name) == "get_node" && ClassDB::is_parent_class(native_type.native_type, "Node") && args.size()) {
  1186. String arg1 = args[0];
  1187. if (arg1.begins_with("/root/")) {
  1188. String which = arg1.get_slice("/", 2);
  1189. if (which != "") {
  1190. // Try singletons first
  1191. if (GDScriptLanguage::get_singleton()->get_named_globals_map().has(which)) {
  1192. r_type = _type_from_variant(GDScriptLanguage::get_singleton()->get_named_globals_map()[which]);
  1193. found = true;
  1194. } else {
  1195. Map<StringName, ProjectSettings::AutoloadInfo> autoloads = ProjectSettings::get_singleton()->get_autoload_list();
  1196. for (Map<StringName, ProjectSettings::AutoloadInfo>::Element *E = autoloads.front(); E; E = E->next()) {
  1197. String name = E->key();
  1198. if (name == which) {
  1199. String script = E->value().path;
  1200. if (!script.begins_with("res://")) {
  1201. script = "res://" + script;
  1202. }
  1203. if (!script.ends_with(".gd")) {
  1204. //not a script, try find the script anyway,
  1205. //may have some success
  1206. script = script.get_basename() + ".gd";
  1207. }
  1208. if (FileAccess::exists(script)) {
  1209. Error err = OK;
  1210. Ref<GDScriptParserRef> parser = GDScriptCache::get_parser(script, GDScriptParserRef::INTERFACE_SOLVED, err);
  1211. if (err == OK) {
  1212. r_type.type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  1213. r_type.type.script_path = script;
  1214. r_type.type.class_type = parser->get_parser()->get_tree();
  1215. r_type.type.is_constant = false;
  1216. r_type.type.kind = GDScriptParser::DataType::CLASS;
  1217. r_type.value = Variant();
  1218. p_context.dependent_parsers.push_back(parser);
  1219. found = true;
  1220. }
  1221. }
  1222. break;
  1223. }
  1224. }
  1225. }
  1226. }
  1227. }
  1228. }
  1229. if (!found && all_is_const && baseptr) {
  1230. Vector<const Variant *> argptr;
  1231. for (int i = 0; i < args.size(); i++) {
  1232. argptr.push_back(&args[i]);
  1233. }
  1234. Callable::CallError ce;
  1235. Variant ret = mb->call(baseptr, (const Variant **)argptr.ptr(), argptr.size(), ce);
  1236. if (ce.error == Callable::CallError::CALL_OK && ret.get_type() != Variant::NIL) {
  1237. if (ret.get_type() != Variant::OBJECT || ret.operator Object *() != nullptr) {
  1238. r_type = _type_from_variant(ret);
  1239. found = true;
  1240. }
  1241. }
  1242. }
  1243. }
  1244. }
  1245. }
  1246. if (!found) {
  1247. found = _guess_method_return_type_from_base(c, base, call->function_name, r_type);
  1248. }
  1249. }
  1250. } break;
  1251. case GDScriptParser::Node::SUBSCRIPT: {
  1252. const GDScriptParser::SubscriptNode *subscript = static_cast<const GDScriptParser::SubscriptNode *>(p_expression);
  1253. if (subscript->is_attribute) {
  1254. GDScriptParser::CompletionContext c = p_context;
  1255. c.current_line = subscript->start_line;
  1256. GDScriptCompletionIdentifier base;
  1257. if (!_guess_expression_type(c, subscript->base, base)) {
  1258. found = false;
  1259. break;
  1260. }
  1261. if (base.value.get_type() == Variant::DICTIONARY && base.value.operator Dictionary().has(String(subscript->attribute->name))) {
  1262. Variant value = base.value.operator Dictionary()[String(subscript->attribute->name)];
  1263. r_type = _type_from_variant(value);
  1264. found = true;
  1265. break;
  1266. }
  1267. const GDScriptParser::DictionaryNode *dn = nullptr;
  1268. if (subscript->base->type == GDScriptParser::Node::DICTIONARY) {
  1269. dn = static_cast<const GDScriptParser::DictionaryNode *>(subscript->base);
  1270. } else if (base.assigned_expression && base.assigned_expression->type == GDScriptParser::Node::DICTIONARY) {
  1271. dn = static_cast<const GDScriptParser::DictionaryNode *>(base.assigned_expression);
  1272. }
  1273. if (dn) {
  1274. for (int i = 0; i < dn->elements.size(); i++) {
  1275. GDScriptCompletionIdentifier key;
  1276. if (!_guess_expression_type(c, dn->elements[i].key, key)) {
  1277. continue;
  1278. }
  1279. if (key.value == String(subscript->attribute->name)) {
  1280. r_type.assigned_expression = dn->elements[i].value;
  1281. found = _guess_expression_type(c, dn->elements[i].value, r_type);
  1282. break;
  1283. }
  1284. }
  1285. }
  1286. if (!found) {
  1287. found = _guess_identifier_type_from_base(c, base, subscript->attribute->name, r_type);
  1288. }
  1289. } else {
  1290. if (subscript->index == nullptr) {
  1291. found = false;
  1292. break;
  1293. }
  1294. GDScriptParser::CompletionContext c = p_context;
  1295. c.current_line = subscript->start_line;
  1296. GDScriptCompletionIdentifier base;
  1297. if (!_guess_expression_type(c, subscript->base, base)) {
  1298. found = false;
  1299. break;
  1300. }
  1301. GDScriptCompletionIdentifier index;
  1302. if (!_guess_expression_type(c, subscript->index, index)) {
  1303. found = false;
  1304. break;
  1305. }
  1306. if (base.value.in(index.value)) {
  1307. Variant value = base.value.get(index.value);
  1308. r_type = _type_from_variant(value);
  1309. found = true;
  1310. break;
  1311. }
  1312. // Look if it is a dictionary node.
  1313. const GDScriptParser::DictionaryNode *dn = nullptr;
  1314. if (subscript->base->type == GDScriptParser::Node::DICTIONARY) {
  1315. dn = static_cast<const GDScriptParser::DictionaryNode *>(subscript->base);
  1316. } else if (base.assigned_expression && base.assigned_expression->type == GDScriptParser::Node::DICTIONARY) {
  1317. dn = static_cast<const GDScriptParser::DictionaryNode *>(base.assigned_expression);
  1318. }
  1319. if (dn) {
  1320. for (int i = 0; i < dn->elements.size(); i++) {
  1321. GDScriptCompletionIdentifier key;
  1322. if (!_guess_expression_type(c, dn->elements[i].key, key)) {
  1323. continue;
  1324. }
  1325. if (key.value == index.value) {
  1326. r_type.assigned_expression = dn->elements[i].value;
  1327. found = _guess_expression_type(p_context, dn->elements[i].value, r_type);
  1328. break;
  1329. }
  1330. }
  1331. }
  1332. // Look if it is an array node.
  1333. if (!found && index.value.is_num()) {
  1334. int idx = index.value;
  1335. const GDScriptParser::ArrayNode *an = nullptr;
  1336. if (subscript->base->type == GDScriptParser::Node::ARRAY) {
  1337. an = static_cast<const GDScriptParser::ArrayNode *>(subscript->base);
  1338. } else if (base.assigned_expression && base.assigned_expression->type == GDScriptParser::Node::ARRAY) {
  1339. an = static_cast<const GDScriptParser::ArrayNode *>(base.assigned_expression);
  1340. }
  1341. if (an && idx >= 0 && an->elements.size() > idx) {
  1342. r_type.assigned_expression = an->elements[idx];
  1343. found = _guess_expression_type(c, an->elements[idx], r_type);
  1344. break;
  1345. }
  1346. }
  1347. // Look for valid indexing in other types
  1348. if (!found && (index.value.get_type() == Variant::STRING || index.value.get_type() == Variant::NODE_PATH)) {
  1349. StringName id = index.value;
  1350. found = _guess_identifier_type_from_base(c, base, id, r_type);
  1351. } else if (!found && index.type.kind == GDScriptParser::DataType::BUILTIN) {
  1352. Callable::CallError err;
  1353. Variant base_val;
  1354. Variant::construct(base.type.builtin_type, base_val, nullptr, 0, err);
  1355. bool valid = false;
  1356. Variant res = base_val.get(index.value, &valid);
  1357. if (valid) {
  1358. r_type = _type_from_variant(res);
  1359. r_type.value = Variant();
  1360. r_type.type.is_constant = false;
  1361. found = true;
  1362. }
  1363. }
  1364. }
  1365. } break;
  1366. case GDScriptParser::Node::BINARY_OPERATOR: {
  1367. const GDScriptParser::BinaryOpNode *op = static_cast<const GDScriptParser::BinaryOpNode *>(p_expression);
  1368. if (op->variant_op == Variant::OP_MAX) {
  1369. break;
  1370. }
  1371. GDScriptParser::CompletionContext context = p_context;
  1372. context.current_line = op->start_line;
  1373. GDScriptCompletionIdentifier p1;
  1374. GDScriptCompletionIdentifier p2;
  1375. if (!_guess_expression_type(context, op->left_operand, p1)) {
  1376. found = false;
  1377. break;
  1378. }
  1379. if (!_guess_expression_type(context, op->right_operand, p2)) {
  1380. found = false;
  1381. break;
  1382. }
  1383. Callable::CallError ce;
  1384. bool v1_use_value = p1.value.get_type() != Variant::NIL && p1.value.get_type() != Variant::OBJECT;
  1385. Variant d1;
  1386. Variant::construct(p1.type.builtin_type, d1, nullptr, 0, ce);
  1387. Variant d2;
  1388. Variant::construct(p2.type.builtin_type, d2, nullptr, 0, ce);
  1389. Variant v1 = (v1_use_value) ? p1.value : d1;
  1390. bool v2_use_value = p2.value.get_type() != Variant::NIL && p2.value.get_type() != Variant::OBJECT;
  1391. Variant v2 = (v2_use_value) ? p2.value : d2;
  1392. // avoid potential invalid ops
  1393. if ((op->variant_op == Variant::OP_DIVIDE || op->variant_op == Variant::OP_MODULE) && v2.get_type() == Variant::INT) {
  1394. v2 = 1;
  1395. v2_use_value = false;
  1396. }
  1397. if (op->variant_op == Variant::OP_DIVIDE && v2.get_type() == Variant::FLOAT) {
  1398. v2 = 1.0;
  1399. v2_use_value = false;
  1400. }
  1401. Variant res;
  1402. bool valid;
  1403. Variant::evaluate(op->variant_op, v1, v2, res, valid);
  1404. if (!valid) {
  1405. found = false;
  1406. break;
  1407. }
  1408. r_type = _type_from_variant(res);
  1409. if (!v1_use_value || !v2_use_value) {
  1410. r_type.value = Variant();
  1411. r_type.type.is_constant = false;
  1412. }
  1413. found = true;
  1414. } break;
  1415. default:
  1416. break;
  1417. }
  1418. }
  1419. // It may have found a null, but that's never useful
  1420. if (found && r_type.type.kind == GDScriptParser::DataType::BUILTIN && r_type.type.builtin_type == Variant::NIL) {
  1421. found = false;
  1422. }
  1423. // Check type hint last. For collections we want chance to get the actual value first
  1424. // This way we can detect types from the content of dictionaries and arrays
  1425. if (!found && p_expression->get_datatype().is_hard_type()) {
  1426. r_type.type = p_expression->get_datatype();
  1427. if (!r_type.assigned_expression) {
  1428. r_type.assigned_expression = p_expression;
  1429. }
  1430. found = true;
  1431. }
  1432. return found;
  1433. }
  1434. static bool _guess_identifier_type(GDScriptParser::CompletionContext &p_context, const StringName &p_identifier, GDScriptCompletionIdentifier &r_type) {
  1435. // Look in blocks first.
  1436. int last_assign_line = -1;
  1437. const GDScriptParser::ExpressionNode *last_assigned_expression = nullptr;
  1438. GDScriptParser::DataType id_type;
  1439. GDScriptParser::SuiteNode *suite = p_context.current_suite;
  1440. bool is_function_parameter = false;
  1441. if (suite) {
  1442. if (suite->has_local(p_identifier)) {
  1443. const GDScriptParser::SuiteNode::Local &local = suite->get_local(p_identifier);
  1444. id_type = local.get_datatype();
  1445. // Check initializer as the first assignment.
  1446. switch (local.type) {
  1447. case GDScriptParser::SuiteNode::Local::VARIABLE:
  1448. if (local.variable->initializer) {
  1449. last_assign_line = local.variable->initializer->end_line;
  1450. last_assigned_expression = local.variable->initializer;
  1451. }
  1452. break;
  1453. case GDScriptParser::SuiteNode::Local::CONSTANT:
  1454. if (local.constant->initializer) {
  1455. last_assign_line = local.constant->initializer->end_line;
  1456. last_assigned_expression = local.constant->initializer;
  1457. }
  1458. break;
  1459. case GDScriptParser::SuiteNode::Local::PARAMETER:
  1460. if (local.parameter->default_value) {
  1461. last_assign_line = local.parameter->default_value->end_line;
  1462. last_assigned_expression = local.parameter->default_value;
  1463. }
  1464. is_function_parameter = true;
  1465. break;
  1466. default:
  1467. break;
  1468. }
  1469. }
  1470. }
  1471. while (suite) {
  1472. for (int i = 0; i < suite->statements.size(); i++) {
  1473. if (suite->statements[i]->start_line > p_context.current_line) {
  1474. break;
  1475. }
  1476. switch (suite->statements[i]->type) {
  1477. case GDScriptParser::Node::ASSIGNMENT: {
  1478. const GDScriptParser::AssignmentNode *assign = static_cast<const GDScriptParser::AssignmentNode *>(suite->statements[i]);
  1479. if (assign->end_line > last_assign_line && assign->assignee && assign->assigned_value && assign->assignee->type == GDScriptParser::Node::IDENTIFIER) {
  1480. const GDScriptParser::IdentifierNode *id = static_cast<const GDScriptParser::IdentifierNode *>(assign->assignee);
  1481. if (id->name == p_identifier) {
  1482. last_assign_line = assign->assigned_value->end_line;
  1483. last_assigned_expression = assign->assigned_value;
  1484. }
  1485. }
  1486. } break;
  1487. default:
  1488. // TODO: Check sub blocks (control flow statements) as they might also reassign stuff.
  1489. break;
  1490. }
  1491. }
  1492. if (suite->parent_if && suite->parent_if->condition && suite->parent_if->condition->type == GDScriptParser::Node::BINARY_OPERATOR && static_cast<const GDScriptParser::BinaryOpNode *>(suite->parent_if->condition)->operation == GDScriptParser::BinaryOpNode::OP_TYPE_TEST) {
  1493. // Operator `is` used, check if identifier is in there! this helps resolve in blocks that are (if (identifier is value)): which are very common..
  1494. // Super dirty hack, but very useful.
  1495. // Credit: Zylann.
  1496. // TODO: this could be hacked to detect ANDed conditions too...
  1497. const GDScriptParser::BinaryOpNode *op = static_cast<const GDScriptParser::BinaryOpNode *>(suite->parent_if->condition);
  1498. if (op->left_operand && op->right_operand && op->left_operand->type == GDScriptParser::Node::IDENTIFIER && static_cast<const GDScriptParser::IdentifierNode *>(op->left_operand)->name == p_identifier) {
  1499. // Bingo.
  1500. GDScriptParser::CompletionContext c = p_context;
  1501. c.current_line = op->left_operand->start_line;
  1502. c.current_suite = suite;
  1503. GDScriptCompletionIdentifier is_type;
  1504. if (_guess_expression_type(c, op->right_operand, is_type)) {
  1505. id_type = is_type.type;
  1506. id_type.is_meta_type = false;
  1507. if (last_assign_line < c.current_line) {
  1508. // Override last assignment.
  1509. last_assign_line = c.current_line;
  1510. last_assigned_expression = nullptr;
  1511. }
  1512. }
  1513. }
  1514. }
  1515. suite = suite->parent_block;
  1516. }
  1517. if (last_assigned_expression && last_assign_line != p_context.current_line) {
  1518. GDScriptParser::CompletionContext c = p_context;
  1519. c.current_line = last_assign_line;
  1520. r_type.assigned_expression = last_assigned_expression;
  1521. if (_guess_expression_type(c, last_assigned_expression, r_type)) {
  1522. return true;
  1523. }
  1524. }
  1525. if (is_function_parameter && p_context.current_function && p_context.current_class) {
  1526. // Check if it's override of native function, then we can assume the type from the signature.
  1527. GDScriptParser::DataType base_type = p_context.current_class->base_type;
  1528. while (base_type.is_set()) {
  1529. switch (base_type.kind) {
  1530. case GDScriptParser::DataType::CLASS:
  1531. if (base_type.class_type->has_function(p_context.current_function->identifier->name)) {
  1532. GDScriptParser::FunctionNode *parent_function = base_type.class_type->get_member(p_context.current_function->identifier->name).function;
  1533. const GDScriptParser::ParameterNode *parameter = parent_function->parameters[parent_function->parameters_indices[p_identifier]];
  1534. if ((!id_type.is_set() || id_type.is_variant()) && parameter->get_datatype().is_hard_type()) {
  1535. id_type = parameter->get_datatype();
  1536. }
  1537. if (parameter->default_value) {
  1538. GDScriptParser::CompletionContext c = p_context;
  1539. c.current_function = parent_function;
  1540. c.current_class = base_type.class_type;
  1541. c.base = nullptr;
  1542. if (_guess_expression_type(c, parameter->default_value, r_type)) {
  1543. return true;
  1544. }
  1545. }
  1546. }
  1547. base_type = base_type.class_type->base_type;
  1548. break;
  1549. case GDScriptParser::DataType::NATIVE: {
  1550. if (id_type.is_set() && !id_type.is_variant()) {
  1551. base_type = GDScriptParser::DataType();
  1552. break;
  1553. }
  1554. StringName real_native = GDScriptParser::get_real_class_name(base_type.native_type);
  1555. MethodInfo info;
  1556. if (ClassDB::get_method_info(real_native, p_context.current_function->identifier->name, &info)) {
  1557. for (const List<PropertyInfo>::Element *E = info.arguments.front(); E; E = E->next()) {
  1558. if (E->get().name == p_identifier) {
  1559. r_type = _type_from_property(E->get());
  1560. return true;
  1561. }
  1562. }
  1563. }
  1564. base_type = GDScriptParser::DataType();
  1565. } break;
  1566. default:
  1567. break;
  1568. }
  1569. }
  1570. }
  1571. if (id_type.is_set() && !id_type.is_variant()) {
  1572. r_type.type = id_type;
  1573. return true;
  1574. }
  1575. // Check current class (including inheritance).
  1576. if (p_context.current_class) {
  1577. GDScriptCompletionIdentifier base;
  1578. base.value = p_context.base;
  1579. base.type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  1580. base.type.kind = GDScriptParser::DataType::CLASS;
  1581. base.type.class_type = p_context.current_class;
  1582. base.type.is_meta_type = p_context.current_function && p_context.current_function->is_static;
  1583. if (_guess_identifier_type_from_base(p_context, base, p_identifier, r_type)) {
  1584. return true;
  1585. }
  1586. }
  1587. // Check global scripts.
  1588. if (ScriptServer::is_global_class(p_identifier)) {
  1589. String script = ScriptServer::get_global_class_path(p_identifier);
  1590. if (script.to_lower().ends_with(".gd")) {
  1591. Error err = OK;
  1592. Ref<GDScriptParserRef> parser = GDScriptCache::get_parser(script, GDScriptParserRef::INTERFACE_SOLVED, err);
  1593. if (err == OK) {
  1594. r_type.type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  1595. r_type.type.script_path = script;
  1596. r_type.type.class_type = parser->get_parser()->get_tree();
  1597. r_type.type.is_constant = false;
  1598. r_type.type.kind = GDScriptParser::DataType::CLASS;
  1599. r_type.value = Variant();
  1600. p_context.dependent_parsers.push_back(parser);
  1601. return true;
  1602. }
  1603. } else {
  1604. Ref<Script> scr = ResourceLoader::load(ScriptServer::get_global_class_path(p_identifier));
  1605. if (scr.is_valid()) {
  1606. r_type = _type_from_variant(scr);
  1607. r_type.type.is_meta_type = true;
  1608. return true;
  1609. }
  1610. }
  1611. return false;
  1612. }
  1613. // Check autoloads.
  1614. if (ProjectSettings::get_singleton()->has_autoload(p_identifier)) {
  1615. r_type = _type_from_variant(GDScriptLanguage::get_singleton()->get_named_globals_map()[p_identifier]);
  1616. return true;
  1617. }
  1618. // Check ClassDB.
  1619. StringName class_name = GDScriptParser::get_real_class_name(p_identifier);
  1620. if (ClassDB::class_exists(class_name) && ClassDB::is_class_exposed(class_name)) {
  1621. r_type.type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  1622. r_type.type.kind = GDScriptParser::DataType::NATIVE;
  1623. r_type.type.native_type = p_identifier;
  1624. r_type.type.is_constant = true;
  1625. r_type.type.is_meta_type = !Engine::get_singleton()->has_singleton(class_name);
  1626. r_type.value = Variant();
  1627. }
  1628. return false;
  1629. }
  1630. static bool _guess_identifier_type_from_base(GDScriptParser::CompletionContext &p_context, const GDScriptCompletionIdentifier &p_base, const StringName &p_identifier, GDScriptCompletionIdentifier &r_type) {
  1631. GDScriptParser::DataType base_type = p_base.type;
  1632. bool is_static = base_type.is_meta_type;
  1633. while (base_type.is_set()) {
  1634. switch (base_type.kind) {
  1635. case GDScriptParser::DataType::CLASS:
  1636. if (base_type.class_type->has_member(p_identifier)) {
  1637. const GDScriptParser::ClassNode::Member &member = base_type.class_type->get_member(p_identifier);
  1638. switch (member.type) {
  1639. case GDScriptParser::ClassNode::Member::CONSTANT:
  1640. r_type.type = member.constant->get_datatype();
  1641. if (member.constant->initializer && member.constant->initializer->is_constant) {
  1642. r_type.value = member.constant->initializer->reduced_value;
  1643. }
  1644. return true;
  1645. case GDScriptParser::ClassNode::Member::VARIABLE:
  1646. if (!is_static) {
  1647. if (member.variable->initializer) {
  1648. const GDScriptParser::ExpressionNode *init = member.variable->initializer;
  1649. if (init->is_constant) {
  1650. r_type.value = init->reduced_value;
  1651. r_type = _type_from_variant(init->reduced_value);
  1652. return true;
  1653. } else if (init->start_line == p_context.current_line) {
  1654. return false;
  1655. } else if (_guess_expression_type(p_context, init, r_type)) {
  1656. return true;
  1657. } else if (init->get_datatype().is_set() && !init->get_datatype().is_variant()) {
  1658. r_type.type = init->get_datatype();
  1659. return true;
  1660. }
  1661. } else if (member.variable->get_datatype().is_set() && !member.variable->get_datatype().is_variant()) {
  1662. r_type.type = member.variable->get_datatype();
  1663. return true;
  1664. }
  1665. }
  1666. // TODO: Check assignments in constructor.
  1667. return false;
  1668. case GDScriptParser::ClassNode::Member::ENUM:
  1669. r_type.type = member.m_enum->get_datatype();
  1670. r_type.enumeration = member.m_enum->identifier->name;
  1671. return true;
  1672. case GDScriptParser::ClassNode::Member::ENUM_VALUE:
  1673. r_type = _type_from_variant(member.enum_value.value);
  1674. return true;
  1675. case GDScriptParser::ClassNode::Member::SIGNAL:
  1676. r_type.type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  1677. r_type.type.kind = GDScriptParser::DataType::BUILTIN;
  1678. r_type.type.builtin_type = Variant::SIGNAL;
  1679. return true;
  1680. case GDScriptParser::ClassNode::Member::FUNCTION:
  1681. if (is_static && !member.function->is_static) {
  1682. return false;
  1683. }
  1684. r_type.type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  1685. r_type.type.kind = GDScriptParser::DataType::BUILTIN;
  1686. r_type.type.builtin_type = Variant::CALLABLE;
  1687. return true;
  1688. case GDScriptParser::ClassNode::Member::CLASS:
  1689. r_type.type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  1690. r_type.type.kind = GDScriptParser::DataType::CLASS;
  1691. r_type.type.class_type = member.m_class;
  1692. return true;
  1693. case GDScriptParser::ClassNode::Member::UNDEFINED:
  1694. return false; // Unreachable.
  1695. }
  1696. return false;
  1697. }
  1698. base_type = base_type.class_type->base_type;
  1699. break;
  1700. case GDScriptParser::DataType::SCRIPT: {
  1701. Ref<Script> scr = base_type.script_type;
  1702. if (scr.is_valid()) {
  1703. Map<StringName, Variant> constants;
  1704. scr->get_constants(&constants);
  1705. if (constants.has(p_identifier)) {
  1706. r_type = _type_from_variant(constants[p_identifier]);
  1707. return true;
  1708. }
  1709. if (!is_static) {
  1710. List<PropertyInfo> members;
  1711. scr->get_script_property_list(&members);
  1712. for (const List<PropertyInfo>::Element *E = members.front(); E; E = E->next()) {
  1713. const PropertyInfo &prop = E->get();
  1714. if (prop.name == p_identifier) {
  1715. r_type = _type_from_property(prop);
  1716. return true;
  1717. }
  1718. }
  1719. }
  1720. Ref<Script> parent = scr->get_base_script();
  1721. if (parent.is_valid()) {
  1722. base_type.script_type = parent;
  1723. } else {
  1724. base_type.kind = GDScriptParser::DataType::NATIVE;
  1725. base_type.native_type = scr->get_instance_base_type();
  1726. }
  1727. } else {
  1728. return false;
  1729. }
  1730. } break;
  1731. case GDScriptParser::DataType::NATIVE: {
  1732. StringName class_name = GDScriptParser::get_real_class_name(base_type.native_type);
  1733. if (!ClassDB::class_exists(class_name)) {
  1734. return false;
  1735. }
  1736. // Skip constants since they're all integers. Type does not matter because int has no members.
  1737. PropertyInfo prop;
  1738. if (ClassDB::get_property_info(class_name, p_identifier, &prop)) {
  1739. StringName getter = ClassDB::get_property_getter(class_name, p_identifier);
  1740. if (getter != StringName()) {
  1741. MethodBind *g = ClassDB::get_method(class_name, getter);
  1742. if (g) {
  1743. r_type = _type_from_property(g->get_return_info());
  1744. return true;
  1745. }
  1746. } else {
  1747. r_type = _type_from_property(prop);
  1748. return true;
  1749. }
  1750. }
  1751. return false;
  1752. } break;
  1753. case GDScriptParser::DataType::BUILTIN: {
  1754. Callable::CallError err;
  1755. Variant tmp;
  1756. Variant::construct(base_type.builtin_type, tmp, nullptr, 0, err);
  1757. if (err.error != Callable::CallError::CALL_OK) {
  1758. return false;
  1759. }
  1760. bool valid = false;
  1761. Variant res = tmp.get(p_identifier, &valid);
  1762. if (valid) {
  1763. r_type = _type_from_variant(res);
  1764. r_type.value = Variant();
  1765. r_type.type.is_constant = false;
  1766. return true;
  1767. }
  1768. return false;
  1769. } break;
  1770. default: {
  1771. return false;
  1772. } break;
  1773. }
  1774. }
  1775. return false;
  1776. }
  1777. static void _find_last_return_in_block(GDScriptParser::CompletionContext &p_context, int &r_last_return_line, const GDScriptParser::ExpressionNode **r_last_returned_value) {
  1778. if (!p_context.current_suite) {
  1779. return;
  1780. }
  1781. for (int i = 0; i < p_context.current_suite->statements.size(); i++) {
  1782. if (p_context.current_suite->statements[i]->start_line < r_last_return_line) {
  1783. break;
  1784. }
  1785. GDScriptParser::CompletionContext c = p_context;
  1786. switch (p_context.current_suite->statements[i]->type) {
  1787. case GDScriptParser::Node::FOR:
  1788. c.current_suite = static_cast<const GDScriptParser::ForNode *>(p_context.current_suite->statements[i])->loop;
  1789. _find_last_return_in_block(c, r_last_return_line, r_last_returned_value);
  1790. break;
  1791. case GDScriptParser::Node::WHILE:
  1792. c.current_suite = static_cast<const GDScriptParser::WhileNode *>(p_context.current_suite->statements[i])->loop;
  1793. _find_last_return_in_block(c, r_last_return_line, r_last_returned_value);
  1794. break;
  1795. case GDScriptParser::Node::IF: {
  1796. const GDScriptParser::IfNode *_if = static_cast<const GDScriptParser::IfNode *>(p_context.current_suite->statements[i]);
  1797. c.current_suite = _if->true_block;
  1798. _find_last_return_in_block(c, r_last_return_line, r_last_returned_value);
  1799. if (_if->false_block) {
  1800. c.current_suite = _if->false_block;
  1801. _find_last_return_in_block(c, r_last_return_line, r_last_returned_value);
  1802. }
  1803. } break;
  1804. case GDScriptParser::Node::MATCH: {
  1805. const GDScriptParser::MatchNode *match = static_cast<const GDScriptParser::MatchNode *>(p_context.current_suite->statements[i]);
  1806. for (int j = 0; j < match->branches.size(); j++) {
  1807. c.current_suite = match->branches[j]->block;
  1808. _find_last_return_in_block(c, r_last_return_line, r_last_returned_value);
  1809. }
  1810. } break;
  1811. case GDScriptParser::Node::RETURN: {
  1812. const GDScriptParser::ReturnNode *ret = static_cast<const GDScriptParser::ReturnNode *>(p_context.current_suite->statements[i]);
  1813. if (ret->return_value) {
  1814. if (ret->start_line > r_last_return_line) {
  1815. r_last_return_line = ret->start_line;
  1816. *r_last_returned_value = ret->return_value;
  1817. }
  1818. }
  1819. } break;
  1820. default:
  1821. break;
  1822. }
  1823. }
  1824. }
  1825. static bool _guess_method_return_type_from_base(GDScriptParser::CompletionContext &p_context, const GDScriptCompletionIdentifier &p_base, const StringName &p_method, GDScriptCompletionIdentifier &r_type) {
  1826. GDScriptParser::DataType base_type = p_base.type;
  1827. bool is_static = base_type.is_meta_type;
  1828. if (is_static && p_method == "new") {
  1829. r_type.type = base_type;
  1830. r_type.type.is_meta_type = false;
  1831. r_type.type.is_constant = false;
  1832. return true;
  1833. }
  1834. while (base_type.is_set() && !base_type.is_variant()) {
  1835. switch (base_type.kind) {
  1836. case GDScriptParser::DataType::CLASS:
  1837. if (base_type.class_type->has_function(p_method)) {
  1838. const GDScriptParser::FunctionNode *method = base_type.class_type->get_member(p_method).function;
  1839. if (!is_static || method->is_static) {
  1840. int last_return_line = -1;
  1841. const GDScriptParser::ExpressionNode *last_returned_value = nullptr;
  1842. GDScriptParser::CompletionContext c = p_context;
  1843. c.current_class = base_type.class_type;
  1844. c.current_function = const_cast<GDScriptParser::FunctionNode *>(method);
  1845. c.current_suite = method->body;
  1846. _find_last_return_in_block(c, last_return_line, &last_returned_value);
  1847. if (last_returned_value) {
  1848. c.current_line = c.current_suite->end_line;
  1849. if (_guess_expression_type(c, last_returned_value, r_type)) {
  1850. return true;
  1851. }
  1852. if (method->get_datatype().is_set() && !method->get_datatype().is_variant()) {
  1853. r_type.type = method->get_datatype();
  1854. return true;
  1855. }
  1856. }
  1857. }
  1858. }
  1859. base_type = base_type.class_type->base_type;
  1860. break;
  1861. case GDScriptParser::DataType::SCRIPT: {
  1862. Ref<Script> scr = base_type.script_type;
  1863. if (scr.is_valid()) {
  1864. List<MethodInfo> methods;
  1865. scr->get_script_method_list(&methods);
  1866. for (List<MethodInfo>::Element *E = methods.front(); E; E = E->next()) {
  1867. MethodInfo &mi = E->get();
  1868. if (mi.name == p_method) {
  1869. r_type = _type_from_property(mi.return_val);
  1870. return true;
  1871. }
  1872. }
  1873. Ref<Script> base_script = scr->get_base_script();
  1874. if (base_script.is_valid()) {
  1875. base_type.script_type = base_script;
  1876. } else {
  1877. base_type.kind = GDScriptParser::DataType::NATIVE;
  1878. base_type.native_type = scr->get_instance_base_type();
  1879. }
  1880. } else {
  1881. return false;
  1882. }
  1883. } break;
  1884. case GDScriptParser::DataType::NATIVE: {
  1885. StringName native = GDScriptParser::get_real_class_name(base_type.native_type);
  1886. if (!ClassDB::class_exists(native)) {
  1887. return false;
  1888. }
  1889. MethodBind *mb = ClassDB::get_method(native, p_method);
  1890. if (mb) {
  1891. r_type = _type_from_property(mb->get_return_info());
  1892. return true;
  1893. }
  1894. return false;
  1895. } break;
  1896. case GDScriptParser::DataType::BUILTIN: {
  1897. Callable::CallError err;
  1898. Variant tmp;
  1899. Variant::construct(base_type.builtin_type, tmp, nullptr, 0, err);
  1900. if (err.error != Callable::CallError::CALL_OK) {
  1901. return false;
  1902. }
  1903. List<MethodInfo> methods;
  1904. tmp.get_method_list(&methods);
  1905. for (List<MethodInfo>::Element *E = methods.front(); E; E = E->next()) {
  1906. MethodInfo &mi = E->get();
  1907. if (mi.name == p_method) {
  1908. r_type = _type_from_property(mi.return_val);
  1909. return true;
  1910. }
  1911. }
  1912. return false;
  1913. } break;
  1914. default: {
  1915. return false;
  1916. }
  1917. }
  1918. }
  1919. return false;
  1920. }
  1921. static void _find_enumeration_candidates(GDScriptParser::CompletionContext &p_context, const String &p_enum_hint, Map<String, ScriptCodeCompletionOption> &r_result) {
  1922. if (p_enum_hint.find(".") == -1) {
  1923. // Global constant or in the current class.
  1924. StringName current_enum = p_enum_hint;
  1925. 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) {
  1926. const GDScriptParser::EnumNode *_enum = p_context.current_class->get_member(current_enum).m_enum;
  1927. for (int i = 0; i < _enum->values.size(); i++) {
  1928. ScriptCodeCompletionOption option(_enum->values[i].identifier->name, ScriptCodeCompletionOption::KIND_ENUM);
  1929. r_result.insert(option.display, option);
  1930. }
  1931. } else {
  1932. for (int i = 0; i < CoreConstants::get_global_constant_count(); i++) {
  1933. if (CoreConstants::get_global_constant_enum(i) == current_enum) {
  1934. ScriptCodeCompletionOption option(CoreConstants::get_global_constant_name(i), ScriptCodeCompletionOption::KIND_ENUM);
  1935. r_result.insert(option.display, option);
  1936. }
  1937. }
  1938. }
  1939. } else {
  1940. String class_name = p_enum_hint.get_slice(".", 0);
  1941. String enum_name = p_enum_hint.get_slice(".", 1);
  1942. if (!ClassDB::class_exists(class_name)) {
  1943. return;
  1944. }
  1945. List<StringName> enum_constants;
  1946. ClassDB::get_enum_constants(class_name, enum_name, &enum_constants);
  1947. for (List<StringName>::Element *E = enum_constants.front(); E; E = E->next()) {
  1948. String candidate = class_name + "." + E->get();
  1949. ScriptCodeCompletionOption option(candidate, ScriptCodeCompletionOption::KIND_ENUM);
  1950. r_result.insert(option.display, option);
  1951. }
  1952. }
  1953. }
  1954. static void _find_call_arguments(GDScriptParser::CompletionContext &p_context, const GDScriptCompletionIdentifier &p_base, const StringName &p_method, int p_argidx, bool p_static, Map<String, ScriptCodeCompletionOption> &r_result, String &r_arghint) {
  1955. Variant base = p_base.value;
  1956. GDScriptParser::DataType base_type = p_base.type;
  1957. const String quote_style = EDITOR_DEF("text_editor/completion/use_single_quotes", false) ? "'" : "\"";
  1958. while (base_type.is_set() && !base_type.is_variant()) {
  1959. switch (base_type.kind) {
  1960. case GDScriptParser::DataType::CLASS: {
  1961. if (base_type.class_type->has_member(p_method)) {
  1962. const GDScriptParser::ClassNode::Member &member = base_type.class_type->get_member(p_method);
  1963. if (member.type == GDScriptParser::ClassNode::Member::FUNCTION) {
  1964. r_arghint = _make_arguments_hint(member.function, p_argidx);
  1965. return;
  1966. }
  1967. }
  1968. base_type = base_type.class_type->base_type;
  1969. } break;
  1970. case GDScriptParser::DataType::NATIVE: {
  1971. StringName class_name = GDScriptParser::get_real_class_name(base_type.native_type);
  1972. if (!ClassDB::class_exists(class_name)) {
  1973. base_type.kind = GDScriptParser::DataType::UNRESOLVED;
  1974. break;
  1975. }
  1976. MethodInfo info;
  1977. int method_args = 0;
  1978. if (ClassDB::get_method_info(class_name, p_method, &info)) {
  1979. method_args = info.arguments.size();
  1980. if (base.get_type() == Variant::OBJECT) {
  1981. Object *obj = base.operator Object *();
  1982. if (obj) {
  1983. List<String> options;
  1984. obj->get_argument_options(p_method, p_argidx, &options);
  1985. for (List<String>::Element *F = options.front(); F; F = F->next()) {
  1986. ScriptCodeCompletionOption option(F->get(), ScriptCodeCompletionOption::KIND_FUNCTION);
  1987. r_result.insert(option.display, option);
  1988. }
  1989. }
  1990. }
  1991. if (p_argidx < method_args) {
  1992. PropertyInfo arg_info = info.arguments[p_argidx];
  1993. if (arg_info.usage & PROPERTY_USAGE_CLASS_IS_ENUM) {
  1994. _find_enumeration_candidates(p_context, arg_info.class_name, r_result);
  1995. }
  1996. }
  1997. r_arghint = _make_arguments_hint(info, p_argidx);
  1998. }
  1999. if (p_argidx == 0 && ClassDB::is_parent_class(class_name, "Node") && (p_method == "get_node" || p_method == "has_node")) {
  2000. // Get autoloads
  2001. List<PropertyInfo> props;
  2002. ProjectSettings::get_singleton()->get_property_list(&props);
  2003. for (List<PropertyInfo>::Element *E = props.front(); E; E = E->next()) {
  2004. String s = E->get().name;
  2005. if (!s.begins_with("autoload/")) {
  2006. continue;
  2007. }
  2008. String name = s.get_slice("/", 1);
  2009. ScriptCodeCompletionOption option("/root/" + name, ScriptCodeCompletionOption::KIND_NODE_PATH);
  2010. option.insert_text = quote_style + option.display + quote_style;
  2011. r_result.insert(option.display, option);
  2012. }
  2013. }
  2014. if (p_argidx == 0 && method_args > 0 && ClassDB::is_parent_class(class_name, "InputEvent") && p_method.operator String().find("action") != -1) {
  2015. // Get input actions
  2016. List<PropertyInfo> props;
  2017. ProjectSettings::get_singleton()->get_property_list(&props);
  2018. for (List<PropertyInfo>::Element *E = props.front(); E; E = E->next()) {
  2019. String s = E->get().name;
  2020. if (!s.begins_with("input/")) {
  2021. continue;
  2022. }
  2023. String name = s.get_slice("/", 1);
  2024. ScriptCodeCompletionOption option(name, ScriptCodeCompletionOption::KIND_CONSTANT);
  2025. option.insert_text = quote_style + option.display + quote_style;
  2026. r_result.insert(option.display, option);
  2027. }
  2028. }
  2029. base_type.kind = GDScriptParser::DataType::UNRESOLVED;
  2030. } break;
  2031. case GDScriptParser::DataType::BUILTIN: {
  2032. if (base.get_type() == Variant::NIL) {
  2033. Callable::CallError err;
  2034. Variant::construct(base_type.builtin_type, base, nullptr, 0, err);
  2035. if (err.error != Callable::CallError::CALL_OK) {
  2036. return;
  2037. }
  2038. }
  2039. List<MethodInfo> methods;
  2040. base.get_method_list(&methods);
  2041. for (List<MethodInfo>::Element *E = methods.front(); E; E = E->next()) {
  2042. if (E->get().name == p_method) {
  2043. r_arghint = _make_arguments_hint(E->get(), p_argidx);
  2044. return;
  2045. }
  2046. }
  2047. base_type.kind = GDScriptParser::DataType::UNRESOLVED;
  2048. } break;
  2049. default: {
  2050. base_type.kind = GDScriptParser::DataType::UNRESOLVED;
  2051. } break;
  2052. }
  2053. }
  2054. }
  2055. static void _find_call_arguments(GDScriptParser::CompletionContext &p_context, const GDScriptParser::Node *p_call, int p_argidx, Map<String, ScriptCodeCompletionOption> &r_result, bool &r_forced, String &r_arghint) {
  2056. const String quote_style = EDITOR_DEF("text_editor/completion/use_single_quotes", false) ? "'" : "\"";
  2057. if (p_call->type == GDScriptParser::Node::PRELOAD) {
  2058. if (p_argidx == 0 && bool(EditorSettings::get_singleton()->get("text_editor/completion/complete_file_paths"))) {
  2059. _get_directory_contents(EditorFileSystem::get_singleton()->get_filesystem(), r_result);
  2060. }
  2061. MethodInfo mi(PropertyInfo(Variant::OBJECT, "resource", PROPERTY_HINT_RESOURCE_TYPE, "Resource"), "preload", PropertyInfo(Variant::STRING, "path"));
  2062. r_arghint = _make_arguments_hint(mi, p_argidx);
  2063. return;
  2064. } else if (p_call->type != GDScriptParser::Node::CALL) {
  2065. return;
  2066. }
  2067. Variant base;
  2068. GDScriptParser::DataType base_type;
  2069. bool _static = false;
  2070. const GDScriptParser::CallNode *call = static_cast<const GDScriptParser::CallNode *>(p_call);
  2071. GDScriptParser::Node::Type callee_type = call->get_callee_type();
  2072. GDScriptCompletionIdentifier connect_base;
  2073. if (GDScriptUtilityFunctions::function_exists(call->function_name)) {
  2074. MethodInfo info = GDScriptUtilityFunctions::get_function_info(call->function_name);
  2075. r_arghint = _make_arguments_hint(info, p_argidx);
  2076. return;
  2077. } else if (GDScriptParser::get_builtin_type(call->function_name) < Variant::VARIANT_MAX) {
  2078. // Complete constructor
  2079. List<MethodInfo> constructors;
  2080. Variant::get_constructor_list(GDScriptParser::get_builtin_type(call->function_name), &constructors);
  2081. int i = 0;
  2082. for (List<MethodInfo>::Element *E = constructors.front(); E; E = E->next()) {
  2083. if (p_argidx >= E->get().arguments.size()) {
  2084. continue;
  2085. }
  2086. if (i > 0) {
  2087. r_arghint += "\n";
  2088. }
  2089. r_arghint += _make_arguments_hint(E->get(), p_argidx);
  2090. i++;
  2091. }
  2092. return;
  2093. } else if (call->is_super || callee_type == GDScriptParser::Node::IDENTIFIER) {
  2094. base = p_context.base;
  2095. if (p_context.current_class) {
  2096. base_type = p_context.current_class->get_datatype();
  2097. _static = !p_context.current_function || p_context.current_function->is_static;
  2098. }
  2099. } else if (callee_type == GDScriptParser::Node::SUBSCRIPT) {
  2100. const GDScriptParser::SubscriptNode *subscript = static_cast<const GDScriptParser::SubscriptNode *>(call->callee);
  2101. if (subscript->is_attribute) {
  2102. GDScriptCompletionIdentifier ci;
  2103. if (_guess_expression_type(p_context, subscript->base, ci)) {
  2104. base_type = ci.type;
  2105. base = ci.value;
  2106. } else {
  2107. return;
  2108. }
  2109. _static = base_type.is_meta_type;
  2110. }
  2111. } else {
  2112. return;
  2113. }
  2114. GDScriptCompletionIdentifier ci;
  2115. ci.type = base_type;
  2116. ci.value = base;
  2117. _find_call_arguments(p_context, ci, call->function_name, p_argidx, _static, r_result, r_arghint);
  2118. r_forced = r_result.size() > 0;
  2119. }
  2120. Error GDScriptLanguage::complete_code(const String &p_code, const String &p_path, Object *p_owner, List<ScriptCodeCompletionOption> *r_options, bool &r_forced, String &r_call_hint) {
  2121. const String quote_style = EDITOR_DEF("text_editor/completion/use_single_quotes", false) ? "'" : "\"";
  2122. GDScriptParser parser;
  2123. GDScriptAnalyzer analyzer(&parser);
  2124. parser.parse(p_code, p_path, true);
  2125. analyzer.analyze();
  2126. r_forced = false;
  2127. Map<String, ScriptCodeCompletionOption> options;
  2128. GDScriptParser::CompletionContext completion_context = parser.get_completion_context();
  2129. completion_context.base = p_owner;
  2130. bool is_function = false;
  2131. switch (completion_context.type) {
  2132. case GDScriptParser::COMPLETION_NONE:
  2133. break;
  2134. case GDScriptParser::COMPLETION_ANNOTATION: {
  2135. List<MethodInfo> annotations;
  2136. parser.get_annotation_list(&annotations);
  2137. for (const List<MethodInfo>::Element *E = annotations.front(); E != nullptr; E = E->next()) {
  2138. ScriptCodeCompletionOption option(E->get().name.substr(1), ScriptCodeCompletionOption::KIND_PLAIN_TEXT);
  2139. if (E->get().arguments.size() > 0) {
  2140. option.insert_text += "(";
  2141. }
  2142. options.insert(option.display, option);
  2143. }
  2144. r_forced = true;
  2145. } break;
  2146. case GDScriptParser::COMPLETION_ANNOTATION_ARGUMENTS: {
  2147. if (completion_context.node == nullptr || completion_context.node->type != GDScriptParser::Node::ANNOTATION) {
  2148. break;
  2149. }
  2150. const GDScriptParser::AnnotationNode *annotation = static_cast<const GDScriptParser::AnnotationNode *>(completion_context.node);
  2151. _find_annotation_arguments(annotation, completion_context.current_argument, quote_style, options);
  2152. r_forced = true;
  2153. } break;
  2154. case GDScriptParser::COMPLETION_BUILT_IN_TYPE_CONSTANT: {
  2155. List<StringName> constants;
  2156. Variant::get_constants_for_type(completion_context.builtin_type, &constants);
  2157. for (const List<StringName>::Element *E = constants.front(); E != nullptr; E = E->next()) {
  2158. ScriptCodeCompletionOption option(E->get(), ScriptCodeCompletionOption::KIND_CONSTANT);
  2159. bool valid = false;
  2160. Variant default_value = Variant::get_constant_value(completion_context.builtin_type, E->get(), &valid);
  2161. if (valid) {
  2162. option.default_value = default_value;
  2163. }
  2164. options.insert(option.display, option);
  2165. }
  2166. } break;
  2167. case GDScriptParser::COMPLETION_INHERIT_TYPE: {
  2168. _list_available_types(true, completion_context, options);
  2169. r_forced = true;
  2170. } break;
  2171. case GDScriptParser::COMPLETION_TYPE_NAME_OR_VOID: {
  2172. ScriptCodeCompletionOption option("void", ScriptCodeCompletionOption::KIND_PLAIN_TEXT);
  2173. options.insert(option.display, option);
  2174. }
  2175. [[fallthrough]];
  2176. case GDScriptParser::COMPLETION_TYPE_NAME: {
  2177. _list_available_types(false, completion_context, options);
  2178. r_forced = true;
  2179. } break;
  2180. case GDScriptParser::COMPLETION_PROPERTY_DECLARATION_OR_TYPE: {
  2181. _list_available_types(false, completion_context, options);
  2182. ScriptCodeCompletionOption get("get", ScriptCodeCompletionOption::KIND_PLAIN_TEXT);
  2183. options.insert(get.display, get);
  2184. ScriptCodeCompletionOption set("set", ScriptCodeCompletionOption::KIND_PLAIN_TEXT);
  2185. options.insert(set.display, set);
  2186. r_forced = true;
  2187. } break;
  2188. case GDScriptParser::COMPLETION_PROPERTY_DECLARATION: {
  2189. ScriptCodeCompletionOption get("get", ScriptCodeCompletionOption::KIND_PLAIN_TEXT);
  2190. options.insert(get.display, get);
  2191. ScriptCodeCompletionOption set("set", ScriptCodeCompletionOption::KIND_PLAIN_TEXT);
  2192. options.insert(set.display, set);
  2193. r_forced = true;
  2194. } break;
  2195. case GDScriptParser::COMPLETION_PROPERTY_METHOD: {
  2196. if (!completion_context.current_class) {
  2197. break;
  2198. }
  2199. for (int i = 0; i < completion_context.current_class->members.size(); i++) {
  2200. const GDScriptParser::ClassNode::Member &member = completion_context.current_class->members[i];
  2201. if (member.type != GDScriptParser::ClassNode::Member::FUNCTION) {
  2202. continue;
  2203. }
  2204. if (member.function->is_static) {
  2205. continue;
  2206. }
  2207. ScriptCodeCompletionOption option(member.function->identifier->name, ScriptCodeCompletionOption::KIND_FUNCTION);
  2208. options.insert(option.display, option);
  2209. }
  2210. r_forced = true;
  2211. } break;
  2212. case GDScriptParser::COMPLETION_ASSIGN: {
  2213. GDScriptCompletionIdentifier type;
  2214. if (!completion_context.node || completion_context.node->type != GDScriptParser::Node::ASSIGNMENT) {
  2215. break;
  2216. }
  2217. if (!_guess_expression_type(completion_context, static_cast<const GDScriptParser::AssignmentNode *>(completion_context.node)->assignee, type)) {
  2218. _find_identifiers(completion_context, false, options, 0);
  2219. r_forced = true;
  2220. break;
  2221. }
  2222. if (!type.enumeration.is_empty()) {
  2223. _find_enumeration_candidates(completion_context, type.enumeration, options);
  2224. r_forced = options.size() > 0;
  2225. } else {
  2226. _find_identifiers(completion_context, false, options, 0);
  2227. r_forced = true;
  2228. }
  2229. } break;
  2230. case GDScriptParser::COMPLETION_METHOD:
  2231. is_function = true;
  2232. [[fallthrough]];
  2233. case GDScriptParser::COMPLETION_IDENTIFIER: {
  2234. _find_identifiers(completion_context, is_function, options, 0);
  2235. } break;
  2236. case GDScriptParser::COMPLETION_ATTRIBUTE_METHOD:
  2237. is_function = true;
  2238. [[fallthrough]];
  2239. case GDScriptParser::COMPLETION_ATTRIBUTE: {
  2240. r_forced = true;
  2241. const GDScriptParser::SubscriptNode *attr = static_cast<const GDScriptParser::SubscriptNode *>(completion_context.node);
  2242. if (attr->base) {
  2243. GDScriptCompletionIdentifier base;
  2244. if (!_guess_expression_type(completion_context, attr->base, base)) {
  2245. break;
  2246. }
  2247. _find_identifiers_in_base(base, is_function, options, 0);
  2248. }
  2249. } break;
  2250. case GDScriptParser::COMPLETION_SUBSCRIPT: {
  2251. const GDScriptParser::SubscriptNode *subscript = static_cast<const GDScriptParser::SubscriptNode *>(completion_context.node);
  2252. GDScriptCompletionIdentifier base;
  2253. if (!_guess_expression_type(completion_context, subscript->base, base)) {
  2254. break;
  2255. }
  2256. GDScriptParser::CompletionContext c = completion_context;
  2257. c.current_function = nullptr;
  2258. c.current_suite = nullptr;
  2259. c.base = base.value.get_type() == Variant::OBJECT ? base.value.operator Object *() : nullptr;
  2260. if (base.type.kind == GDScriptParser::DataType::CLASS) {
  2261. c.current_class = base.type.class_type;
  2262. } else {
  2263. c.current_class = nullptr;
  2264. }
  2265. _find_identifiers_in_base(base, false, options, 0);
  2266. } break;
  2267. case GDScriptParser::COMPLETION_TYPE_ATTRIBUTE: {
  2268. if (!completion_context.current_class) {
  2269. break;
  2270. }
  2271. const GDScriptParser::TypeNode *type = static_cast<const GDScriptParser::TypeNode *>(completion_context.node);
  2272. bool found = true;
  2273. GDScriptCompletionIdentifier base;
  2274. base.type.kind = GDScriptParser::DataType::CLASS;
  2275. base.type.type_source = GDScriptParser::DataType::INFERRED;
  2276. base.type.is_constant = true;
  2277. base.type.class_type = completion_context.current_class;
  2278. base.value = completion_context.base;
  2279. for (int i = 0; i < completion_context.current_argument; i++) {
  2280. GDScriptCompletionIdentifier ci;
  2281. if (!_guess_identifier_type_from_base(completion_context, base, type->type_chain[i]->name, ci)) {
  2282. found = false;
  2283. break;
  2284. }
  2285. base = ci;
  2286. }
  2287. // TODO: Improve this to only list types.
  2288. if (found) {
  2289. _find_identifiers_in_base(base, false, options, 0);
  2290. }
  2291. r_forced = true;
  2292. } break;
  2293. case GDScriptParser::COMPLETION_RESOURCE_PATH: {
  2294. if (EditorSettings::get_singleton()->get("text_editor/completion/complete_file_paths")) {
  2295. _get_directory_contents(EditorFileSystem::get_singleton()->get_filesystem(), options);
  2296. r_forced = true;
  2297. }
  2298. } break;
  2299. case GDScriptParser::COMPLETION_CALL_ARGUMENTS: {
  2300. if (!completion_context.node) {
  2301. break;
  2302. }
  2303. _find_call_arguments(completion_context, completion_context.node, completion_context.current_argument, options, r_forced, r_call_hint);
  2304. } break;
  2305. case GDScriptParser::COMPLETION_OVERRIDE_METHOD: {
  2306. GDScriptParser::DataType native_type = completion_context.current_class->base_type;
  2307. while (native_type.is_set() && native_type.kind != GDScriptParser::DataType::NATIVE) {
  2308. switch (native_type.kind) {
  2309. case GDScriptParser::DataType::CLASS: {
  2310. native_type = native_type.class_type->base_type;
  2311. } break;
  2312. default: {
  2313. native_type.kind = GDScriptParser::DataType::UNRESOLVED;
  2314. } break;
  2315. }
  2316. }
  2317. if (!native_type.is_set()) {
  2318. break;
  2319. }
  2320. StringName class_name = GDScriptParser::get_real_class_name(native_type.native_type);
  2321. if (!ClassDB::class_exists(class_name)) {
  2322. break;
  2323. }
  2324. bool use_type_hint = EditorSettings::get_singleton()->get_setting("text_editor/completion/add_type_hints").operator bool();
  2325. List<MethodInfo> virtual_methods;
  2326. ClassDB::get_virtual_methods(class_name, &virtual_methods);
  2327. for (List<MethodInfo>::Element *E = virtual_methods.front(); E; E = E->next()) {
  2328. MethodInfo &mi = E->get();
  2329. String method_hint = mi.name;
  2330. if (method_hint.find(":") != -1) {
  2331. method_hint = method_hint.get_slice(":", 0);
  2332. }
  2333. method_hint += "(";
  2334. if (mi.arguments.size()) {
  2335. for (int i = 0; i < mi.arguments.size(); i++) {
  2336. if (i > 0) {
  2337. method_hint += ", ";
  2338. }
  2339. String arg = mi.arguments[i].name;
  2340. if (arg.find(":") != -1) {
  2341. arg = arg.substr(0, arg.find(":"));
  2342. }
  2343. method_hint += arg;
  2344. if (use_type_hint && mi.arguments[i].type != Variant::NIL) {
  2345. method_hint += ": ";
  2346. if (mi.arguments[i].type == Variant::OBJECT && mi.arguments[i].class_name != StringName()) {
  2347. method_hint += mi.arguments[i].class_name.operator String();
  2348. } else {
  2349. method_hint += Variant::get_type_name(mi.arguments[i].type);
  2350. }
  2351. }
  2352. }
  2353. }
  2354. method_hint += ")";
  2355. if (use_type_hint && (mi.return_val.type != Variant::NIL || !(mi.return_val.usage & PROPERTY_USAGE_NIL_IS_VARIANT))) {
  2356. method_hint += " -> ";
  2357. if (mi.return_val.type == Variant::NIL) {
  2358. method_hint += "void";
  2359. } else if (mi.return_val.type == Variant::OBJECT && mi.return_val.class_name != StringName()) {
  2360. method_hint += mi.return_val.class_name.operator String();
  2361. } else {
  2362. method_hint += Variant::get_type_name(mi.return_val.type);
  2363. }
  2364. }
  2365. method_hint += ":";
  2366. ScriptCodeCompletionOption option(method_hint, ScriptCodeCompletionOption::KIND_FUNCTION);
  2367. options.insert(option.display, option);
  2368. }
  2369. } break;
  2370. case GDScriptParser::COMPLETION_GET_NODE: {
  2371. if (p_owner) {
  2372. List<String> opts;
  2373. p_owner->get_argument_options("get_node", 0, &opts);
  2374. for (List<String>::Element *E = opts.front(); E; E = E->next()) {
  2375. String opt = E->get().strip_edges();
  2376. if (opt.is_quoted()) {
  2377. r_forced = true;
  2378. String idopt = opt.unquote();
  2379. if (idopt.replace("/", "_").is_valid_identifier()) {
  2380. ScriptCodeCompletionOption option(idopt, ScriptCodeCompletionOption::KIND_NODE_PATH);
  2381. options.insert(option.display, option);
  2382. } else {
  2383. ScriptCodeCompletionOption option(opt, ScriptCodeCompletionOption::KIND_NODE_PATH);
  2384. options.insert(option.display, option);
  2385. }
  2386. }
  2387. }
  2388. // Get autoloads.
  2389. Map<StringName, ProjectSettings::AutoloadInfo> autoloads = ProjectSettings::get_singleton()->get_autoload_list();
  2390. for (Map<StringName, ProjectSettings::AutoloadInfo>::Element *E = autoloads.front(); E; E = E->next()) {
  2391. String name = E->key();
  2392. ScriptCodeCompletionOption option(quote_style + "/root/" + name + quote_style, ScriptCodeCompletionOption::KIND_NODE_PATH);
  2393. options.insert(option.display, option);
  2394. }
  2395. }
  2396. } break;
  2397. case GDScriptParser::COMPLETION_SUPER_METHOD: {
  2398. if (!completion_context.current_class) {
  2399. break;
  2400. }
  2401. _find_identifiers_in_class(completion_context.current_class, true, false, true, options, 0);
  2402. } break;
  2403. }
  2404. for (Map<String, ScriptCodeCompletionOption>::Element *E = options.front(); E; E = E->next()) {
  2405. r_options->push_back(E->get());
  2406. }
  2407. return OK;
  2408. }
  2409. #else
  2410. Error GDScriptLanguage::complete_code(const String &p_code, const String &p_path, Object *p_owner, List<ScriptCodeCompletionOption> *r_options, bool &r_forced, String &r_call_hint) {
  2411. return OK;
  2412. }
  2413. #endif
  2414. //////// END COMPLETION //////////
  2415. String GDScriptLanguage::_get_indentation() const {
  2416. #ifdef TOOLS_ENABLED
  2417. if (Engine::get_singleton()->is_editor_hint()) {
  2418. bool use_space_indentation = EDITOR_DEF("text_editor/indent/type", false);
  2419. if (use_space_indentation) {
  2420. int indent_size = EDITOR_DEF("text_editor/indent/size", 4);
  2421. String space_indent = "";
  2422. for (int i = 0; i < indent_size; i++) {
  2423. space_indent += " ";
  2424. }
  2425. return space_indent;
  2426. }
  2427. }
  2428. #endif
  2429. return "\t";
  2430. }
  2431. void GDScriptLanguage::auto_indent_code(String &p_code, int p_from_line, int p_to_line) const {
  2432. String indent = _get_indentation();
  2433. Vector<String> lines = p_code.split("\n");
  2434. List<int> indent_stack;
  2435. for (int i = 0; i < lines.size(); i++) {
  2436. String l = lines[i];
  2437. int tc = 0;
  2438. for (int j = 0; j < l.length(); j++) {
  2439. if (l[j] == ' ' || l[j] == '\t') {
  2440. tc++;
  2441. } else {
  2442. break;
  2443. }
  2444. }
  2445. String st = l.substr(tc, l.length()).strip_edges();
  2446. if (st == "" || st.begins_with("#")) {
  2447. continue; //ignore!
  2448. }
  2449. int ilevel = 0;
  2450. if (indent_stack.size()) {
  2451. ilevel = indent_stack.back()->get();
  2452. }
  2453. if (tc > ilevel) {
  2454. indent_stack.push_back(tc);
  2455. } else if (tc < ilevel) {
  2456. while (indent_stack.size() && indent_stack.back()->get() > tc) {
  2457. indent_stack.pop_back();
  2458. }
  2459. if (indent_stack.size() && indent_stack.back()->get() != tc) {
  2460. indent_stack.push_back(tc); //this is not right but gets the job done
  2461. }
  2462. }
  2463. if (i >= p_from_line) {
  2464. l = "";
  2465. for (int j = 0; j < indent_stack.size(); j++) {
  2466. l += indent;
  2467. }
  2468. l += st;
  2469. } else if (i > p_to_line) {
  2470. break;
  2471. }
  2472. lines.write[i] = l;
  2473. }
  2474. p_code = "";
  2475. for (int i = 0; i < lines.size(); i++) {
  2476. if (i > 0) {
  2477. p_code += "\n";
  2478. }
  2479. p_code += lines[i];
  2480. }
  2481. }
  2482. #ifdef TOOLS_ENABLED
  2483. static Error _lookup_symbol_from_base(const GDScriptParser::DataType &p_base, const String &p_symbol, bool p_is_function, GDScriptLanguage::LookupResult &r_result) {
  2484. GDScriptParser::DataType base_type = p_base;
  2485. while (base_type.is_set()) {
  2486. switch (base_type.kind) {
  2487. case GDScriptParser::DataType::CLASS: {
  2488. if (base_type.class_type) {
  2489. if (base_type.class_type->has_member(p_symbol)) {
  2490. r_result.type = ScriptLanguage::LookupResult::RESULT_SCRIPT_LOCATION;
  2491. r_result.location = base_type.class_type->get_member(p_symbol).get_line();
  2492. return OK;
  2493. }
  2494. base_type = base_type.class_type->base_type;
  2495. }
  2496. } break;
  2497. case GDScriptParser::DataType::SCRIPT: {
  2498. Ref<Script> scr = base_type.script_type;
  2499. if (scr.is_valid()) {
  2500. int line = scr->get_member_line(p_symbol);
  2501. if (line >= 0) {
  2502. r_result.type = ScriptLanguage::LookupResult::RESULT_SCRIPT_LOCATION;
  2503. r_result.location = line;
  2504. r_result.script = scr;
  2505. return OK;
  2506. }
  2507. Ref<Script> base_script = scr->get_base_script();
  2508. if (base_script.is_valid()) {
  2509. base_type.script_type = base_script;
  2510. } else {
  2511. base_type.kind = GDScriptParser::DataType::NATIVE;
  2512. base_type.native_type = scr->get_instance_base_type();
  2513. }
  2514. } else {
  2515. base_type.kind = GDScriptParser::DataType::UNRESOLVED;
  2516. }
  2517. } break;
  2518. case GDScriptParser::DataType::NATIVE: {
  2519. StringName class_name = GDScriptParser::get_real_class_name(base_type.native_type);
  2520. if (!ClassDB::class_exists(class_name)) {
  2521. base_type.kind = GDScriptParser::DataType::UNRESOLVED;
  2522. break;
  2523. }
  2524. if (ClassDB::has_method(class_name, p_symbol, true)) {
  2525. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_METHOD;
  2526. r_result.class_name = base_type.native_type;
  2527. r_result.class_member = p_symbol;
  2528. return OK;
  2529. }
  2530. List<MethodInfo> virtual_methods;
  2531. ClassDB::get_virtual_methods(class_name, &virtual_methods, true);
  2532. for (List<MethodInfo>::Element *E = virtual_methods.front(); E; E = E->next()) {
  2533. if (E->get().name == p_symbol) {
  2534. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_METHOD;
  2535. r_result.class_name = base_type.native_type;
  2536. r_result.class_member = p_symbol;
  2537. return OK;
  2538. }
  2539. }
  2540. StringName enum_name = ClassDB::get_integer_constant_enum(class_name, p_symbol, true);
  2541. if (enum_name != StringName()) {
  2542. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_ENUM;
  2543. r_result.class_name = base_type.native_type;
  2544. r_result.class_member = enum_name;
  2545. return OK;
  2546. }
  2547. List<String> constants;
  2548. ClassDB::get_integer_constant_list(class_name, &constants, true);
  2549. for (List<String>::Element *E = constants.front(); E; E = E->next()) {
  2550. if (E->get() == p_symbol) {
  2551. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_CONSTANT;
  2552. r_result.class_name = base_type.native_type;
  2553. r_result.class_member = p_symbol;
  2554. return OK;
  2555. }
  2556. }
  2557. if (ClassDB::has_property(class_name, p_symbol, true)) {
  2558. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_PROPERTY;
  2559. r_result.class_name = base_type.native_type;
  2560. r_result.class_member = p_symbol;
  2561. return OK;
  2562. }
  2563. StringName parent = ClassDB::get_parent_class(class_name);
  2564. if (parent != StringName()) {
  2565. if (String(parent).begins_with("_")) {
  2566. base_type.native_type = String(parent).right(1);
  2567. } else {
  2568. base_type.native_type = parent;
  2569. }
  2570. } else {
  2571. base_type.kind = GDScriptParser::DataType::UNRESOLVED;
  2572. }
  2573. } break;
  2574. case GDScriptParser::DataType::BUILTIN: {
  2575. base_type.kind = GDScriptParser::DataType::UNRESOLVED;
  2576. if (Variant::has_constant(base_type.builtin_type, p_symbol)) {
  2577. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_CONSTANT;
  2578. r_result.class_name = Variant::get_type_name(base_type.builtin_type);
  2579. r_result.class_member = p_symbol;
  2580. return OK;
  2581. }
  2582. Variant v;
  2583. REF v_ref;
  2584. if (base_type.builtin_type == Variant::OBJECT) {
  2585. v_ref.instance();
  2586. v = v_ref;
  2587. } else {
  2588. Callable::CallError err;
  2589. Variant::construct(base_type.builtin_type, v, nullptr, 0, err);
  2590. if (err.error != Callable::CallError::CALL_OK) {
  2591. break;
  2592. }
  2593. }
  2594. if (v.has_method(p_symbol)) {
  2595. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_METHOD;
  2596. r_result.class_name = Variant::get_type_name(base_type.builtin_type);
  2597. r_result.class_member = p_symbol;
  2598. return OK;
  2599. }
  2600. bool valid = false;
  2601. v.get(p_symbol, &valid);
  2602. if (valid) {
  2603. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_PROPERTY;
  2604. r_result.class_name = Variant::get_type_name(base_type.builtin_type);
  2605. r_result.class_member = p_symbol;
  2606. return OK;
  2607. }
  2608. } break;
  2609. default: {
  2610. base_type.kind = GDScriptParser::DataType::UNRESOLVED;
  2611. } break;
  2612. }
  2613. }
  2614. return ERR_CANT_RESOLVE;
  2615. }
  2616. Error GDScriptLanguage::lookup_code(const String &p_code, const String &p_symbol, const String &p_path, Object *p_owner, LookupResult &r_result) {
  2617. //before parsing, try the usual stuff
  2618. if (ClassDB::class_exists(p_symbol)) {
  2619. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS;
  2620. r_result.class_name = p_symbol;
  2621. return OK;
  2622. } else {
  2623. String under_prefix = "_" + p_symbol;
  2624. if (ClassDB::class_exists(under_prefix)) {
  2625. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS;
  2626. r_result.class_name = p_symbol;
  2627. return OK;
  2628. }
  2629. }
  2630. for (int i = 0; i < Variant::VARIANT_MAX; i++) {
  2631. Variant::Type t = Variant::Type(i);
  2632. if (Variant::get_type_name(t) == p_symbol) {
  2633. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS;
  2634. r_result.class_name = Variant::get_type_name(t);
  2635. return OK;
  2636. }
  2637. }
  2638. if (GDScriptUtilityFunctions::function_exists(p_symbol)) {
  2639. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_METHOD;
  2640. r_result.class_name = "@GDScript";
  2641. r_result.class_member = p_symbol;
  2642. return OK;
  2643. }
  2644. if ("PI" == p_symbol || "TAU" == p_symbol || "INF" == p_symbol || "NAN" == p_symbol) {
  2645. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_CONSTANT;
  2646. r_result.class_name = "@GDScript";
  2647. r_result.class_member = p_symbol;
  2648. return OK;
  2649. }
  2650. GDScriptParser parser;
  2651. parser.parse(p_code, p_path, true);
  2652. GDScriptAnalyzer analyzer(&parser);
  2653. analyzer.analyze();
  2654. GDScriptParser::CompletionContext context = parser.get_completion_context();
  2655. if (context.current_class && context.current_class->extends.size() > 0) {
  2656. bool success = false;
  2657. ClassDB::get_integer_constant(context.current_class->extends[0], p_symbol, &success);
  2658. if (success) {
  2659. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_CONSTANT;
  2660. r_result.class_name = context.current_class->extends[0];
  2661. r_result.class_member = p_symbol;
  2662. return OK;
  2663. }
  2664. }
  2665. bool is_function = false;
  2666. switch (context.type) {
  2667. case GDScriptParser::COMPLETION_BUILT_IN_TYPE_CONSTANT: {
  2668. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_CONSTANT;
  2669. r_result.class_name = Variant::get_type_name(context.builtin_type);
  2670. r_result.class_member = p_symbol;
  2671. return OK;
  2672. } break;
  2673. case GDScriptParser::COMPLETION_SUPER_METHOD:
  2674. case GDScriptParser::COMPLETION_METHOD: {
  2675. is_function = true;
  2676. [[fallthrough]];
  2677. }
  2678. case GDScriptParser::COMPLETION_IDENTIFIER: {
  2679. GDScriptParser::DataType base_type;
  2680. if (context.current_class) {
  2681. if (context.type != GDScriptParser::COMPLETION_SUPER_METHOD) {
  2682. base_type = context.current_class->get_datatype();
  2683. } else {
  2684. base_type = context.current_class->base_type;
  2685. }
  2686. } else {
  2687. break;
  2688. }
  2689. if (!is_function && context.current_suite) {
  2690. // Lookup local variables.
  2691. const GDScriptParser::SuiteNode *suite = context.current_suite;
  2692. while (suite) {
  2693. if (suite->has_local(p_symbol)) {
  2694. r_result.type = ScriptLanguage::LookupResult::RESULT_SCRIPT_LOCATION;
  2695. r_result.location = suite->get_local(p_symbol).start_line;
  2696. return OK;
  2697. }
  2698. suite = suite->parent_block;
  2699. }
  2700. }
  2701. if (_lookup_symbol_from_base(base_type, p_symbol, is_function, r_result) == OK) {
  2702. return OK;
  2703. }
  2704. if (!is_function) {
  2705. // Guess in autoloads as singletons.
  2706. if (ProjectSettings::get_singleton()->has_autoload(p_symbol)) {
  2707. const ProjectSettings::AutoloadInfo &singleton = ProjectSettings::get_singleton()->get_autoload(p_symbol);
  2708. if (singleton.is_singleton) {
  2709. String script = singleton.path;
  2710. if (!script.ends_with(".gd")) {
  2711. // Not a script, try find the script anyway,
  2712. // may have some success.
  2713. script = script.get_basename() + ".gd";
  2714. }
  2715. if (FileAccess::exists(script)) {
  2716. r_result.type = ScriptLanguage::LookupResult::RESULT_SCRIPT_LOCATION;
  2717. r_result.location = 0;
  2718. r_result.script = ResourceLoader::load(script);
  2719. return OK;
  2720. }
  2721. }
  2722. }
  2723. // Global.
  2724. Map<StringName, int> classes = GDScriptLanguage::get_singleton()->get_global_map();
  2725. if (classes.has(p_symbol)) {
  2726. Variant value = GDScriptLanguage::get_singleton()->get_global_array()[classes[p_symbol]];
  2727. if (value.get_type() == Variant::OBJECT) {
  2728. Object *obj = value;
  2729. if (obj) {
  2730. if (Object::cast_to<GDScriptNativeClass>(obj)) {
  2731. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS;
  2732. r_result.class_name = Object::cast_to<GDScriptNativeClass>(obj)->get_name();
  2733. } else {
  2734. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS;
  2735. r_result.class_name = obj->get_class();
  2736. }
  2737. // proxy class remove the underscore.
  2738. if (r_result.class_name.begins_with("_")) {
  2739. r_result.class_name = r_result.class_name.right(1);
  2740. }
  2741. return OK;
  2742. }
  2743. } else {
  2744. /*
  2745. // Because get_integer_constant_enum and get_integer_constant don't work on @GlobalScope
  2746. // We cannot determine the exact nature of the identifier here
  2747. // Otherwise these codes would work
  2748. StringName enumName = ClassDB::get_integer_constant_enum("@GlobalScope", p_symbol, true);
  2749. if (enumName != NULL) {
  2750. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_ENUM;
  2751. r_result.class_name = "@GlobalScope";
  2752. r_result.class_member = enumName;
  2753. return OK;
  2754. }
  2755. else {
  2756. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_CONSTANT;
  2757. r_result.class_name = "@GlobalScope";
  2758. r_result.class_member = p_symbol;
  2759. return OK;
  2760. }*/
  2761. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_TBD_GLOBALSCOPE;
  2762. r_result.class_name = "@GlobalScope";
  2763. r_result.class_member = p_symbol;
  2764. return OK;
  2765. }
  2766. }
  2767. }
  2768. } break;
  2769. case GDScriptParser::COMPLETION_ATTRIBUTE_METHOD: {
  2770. is_function = true;
  2771. [[fallthrough]];
  2772. }
  2773. case GDScriptParser::COMPLETION_ATTRIBUTE: {
  2774. if (context.node->type != GDScriptParser::Node::SUBSCRIPT) {
  2775. break;
  2776. }
  2777. const GDScriptParser::SubscriptNode *subscript = static_cast<const GDScriptParser::SubscriptNode *>(context.node);
  2778. if (!subscript->is_attribute) {
  2779. break;
  2780. }
  2781. GDScriptCompletionIdentifier base;
  2782. if (!_guess_expression_type(context, subscript->base, base)) {
  2783. break;
  2784. }
  2785. if (_lookup_symbol_from_base(base.type, p_symbol, is_function, r_result) == OK) {
  2786. return OK;
  2787. }
  2788. } break;
  2789. case GDScriptParser::COMPLETION_OVERRIDE_METHOD: {
  2790. GDScriptParser::DataType base_type = context.current_class->base_type;
  2791. if (_lookup_symbol_from_base(base_type, p_symbol, true, r_result) == OK) {
  2792. return OK;
  2793. }
  2794. } break;
  2795. case GDScriptParser::COMPLETION_TYPE_NAME_OR_VOID:
  2796. case GDScriptParser::COMPLETION_TYPE_NAME: {
  2797. GDScriptParser::DataType base_type = context.current_class->get_datatype();
  2798. if (_lookup_symbol_from_base(base_type, p_symbol, false, r_result) == OK) {
  2799. return OK;
  2800. }
  2801. } break;
  2802. default: {
  2803. }
  2804. }
  2805. return ERR_CANT_RESOLVE;
  2806. }
  2807. #endif