gdscript_editor.cpp 122 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570
  1. /*************************************************************************/
  2. /* gdscript_editor.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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/engine.h"
  32. #include "core/global_constants.h"
  33. #include "core/os/file_access.h"
  34. #include "gdscript_compiler.h"
  35. #ifdef TOOLS_ENABLED
  36. #include "editor/editor_file_system.h"
  37. #include "editor/editor_settings.h"
  38. #endif
  39. void GDScriptLanguage::get_comment_delimiters(List<String> *p_delimiters) const {
  40. p_delimiters->push_back("#");
  41. }
  42. void GDScriptLanguage::get_string_delimiters(List<String> *p_delimiters) const {
  43. p_delimiters->push_back("\"\"\" \"\"\"");
  44. p_delimiters->push_back("\" \"");
  45. p_delimiters->push_back("' '");
  46. }
  47. String GDScriptLanguage::_get_processed_template(const String &p_template, const String &p_base_class_name) const {
  48. String processed_template = p_template;
  49. #ifdef TOOLS_ENABLED
  50. if (EDITOR_DEF("text_editor/completion/add_type_hints", false)) {
  51. processed_template = processed_template.replace("%INT_TYPE%", ": int");
  52. processed_template = processed_template.replace("%STRING_TYPE%", ": String");
  53. processed_template = processed_template.replace("%FLOAT_TYPE%", ": float");
  54. processed_template = processed_template.replace("%VOID_RETURN%", " -> void");
  55. } else {
  56. processed_template = processed_template.replace("%INT_TYPE%", "");
  57. processed_template = processed_template.replace("%STRING_TYPE%", "");
  58. processed_template = processed_template.replace("%FLOAT_TYPE%", "");
  59. processed_template = processed_template.replace("%VOID_RETURN%", "");
  60. }
  61. #else
  62. processed_template = processed_template.replace("%INT_TYPE%", "");
  63. processed_template = processed_template.replace("%STRING_TYPE%", "");
  64. processed_template = processed_template.replace("%FLOAT_TYPE%", "");
  65. processed_template = processed_template.replace("%VOID_RETURN%", "");
  66. #endif
  67. processed_template = processed_template.replace("%BASE%", p_base_class_name);
  68. processed_template = processed_template.replace("%TS%", _get_indentation());
  69. return processed_template;
  70. }
  71. Ref<Script> GDScriptLanguage::get_template(const String &p_class_name, const String &p_base_class_name) const {
  72. String _template = "extends %BASE%\n"
  73. "\n"
  74. "\n"
  75. "# Declare member variables here. Examples:\n"
  76. "# var a%INT_TYPE% = 2\n"
  77. "# var b%STRING_TYPE% = \"text\"\n"
  78. "\n"
  79. "\n"
  80. "# Called when the node enters the scene tree for the first time.\n"
  81. "func _ready()%VOID_RETURN%:\n"
  82. "%TS%pass # Replace with function body.\n"
  83. "\n"
  84. "\n"
  85. "# Called every frame. 'delta' is the elapsed time since the previous frame.\n"
  86. "#func _process(delta%FLOAT_TYPE%)%VOID_RETURN%:\n"
  87. "#%TS%pass\n";
  88. _template = _get_processed_template(_template, p_base_class_name);
  89. Ref<GDScript> script;
  90. script.instance();
  91. script->set_source_code(_template);
  92. return script;
  93. }
  94. bool GDScriptLanguage::is_using_templates() {
  95. return true;
  96. }
  97. void GDScriptLanguage::make_template(const String &p_class_name, const String &p_base_class_name, Ref<Script> &p_script) {
  98. String _template = _get_processed_template(p_script->get_source_code(), p_base_class_name);
  99. p_script->set_source_code(_template);
  100. }
  101. 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 {
  102. GDScriptParser parser;
  103. Error err = parser.parse(p_script, p_path.get_base_dir(), true, p_path, false, r_safe_lines);
  104. #ifdef DEBUG_ENABLED
  105. if (r_warnings) {
  106. for (const List<GDScriptWarning>::Element *E = parser.get_warnings().front(); E; E = E->next()) {
  107. const GDScriptWarning &warn = E->get();
  108. ScriptLanguage::Warning w;
  109. w.line = warn.line;
  110. w.code = (int)warn.code;
  111. w.string_code = GDScriptWarning::get_name_from_code(warn.code);
  112. w.message = warn.get_message();
  113. r_warnings->push_back(w);
  114. }
  115. }
  116. #endif
  117. if (err) {
  118. r_line_error = parser.get_error_line();
  119. r_col_error = parser.get_error_column();
  120. r_test_error = parser.get_error();
  121. return false;
  122. } else {
  123. const GDScriptParser::Node *root = parser.get_parse_tree();
  124. ERR_FAIL_COND_V(root->type != GDScriptParser::Node::TYPE_CLASS, false);
  125. const GDScriptParser::ClassNode *cl = static_cast<const GDScriptParser::ClassNode *>(root);
  126. Map<int, String> funcs;
  127. for (int i = 0; i < cl->functions.size(); i++) {
  128. funcs[cl->functions[i]->line] = cl->functions[i]->name;
  129. }
  130. for (int i = 0; i < cl->static_functions.size(); i++) {
  131. funcs[cl->static_functions[i]->line] = cl->static_functions[i]->name;
  132. }
  133. for (int i = 0; i < cl->subclasses.size(); i++) {
  134. for (int j = 0; j < cl->subclasses[i]->functions.size(); j++) {
  135. funcs[cl->subclasses[i]->functions[j]->line] = String(cl->subclasses[i]->name) + "." + cl->subclasses[i]->functions[j]->name;
  136. }
  137. for (int j = 0; j < cl->subclasses[i]->static_functions.size(); j++) {
  138. funcs[cl->subclasses[i]->static_functions[j]->line] = String(cl->subclasses[i]->name) + "." + cl->subclasses[i]->static_functions[j]->name;
  139. }
  140. }
  141. for (Map<int, String>::Element *E = funcs.front(); E; E = E->next()) {
  142. r_functions->push_back(E->get() + ":" + itos(E->key()));
  143. }
  144. }
  145. return true;
  146. }
  147. bool GDScriptLanguage::has_named_classes() const {
  148. return false;
  149. }
  150. bool GDScriptLanguage::supports_builtin_mode() const {
  151. return true;
  152. }
  153. int GDScriptLanguage::find_function(const String &p_function, const String &p_code) const {
  154. GDScriptTokenizerText tokenizer;
  155. tokenizer.set_code(p_code);
  156. int indent = 0;
  157. while (tokenizer.get_token() != GDScriptTokenizer::TK_EOF && tokenizer.get_token() != GDScriptTokenizer::TK_ERROR) {
  158. if (tokenizer.get_token() == GDScriptTokenizer::TK_NEWLINE) {
  159. indent = tokenizer.get_token_line_indent();
  160. }
  161. if (indent == 0 && tokenizer.get_token() == GDScriptTokenizer::TK_PR_FUNCTION && tokenizer.get_token(1) == GDScriptTokenizer::TK_IDENTIFIER) {
  162. String identifier = tokenizer.get_token_identifier(1);
  163. if (identifier == p_function) {
  164. return tokenizer.get_token_line();
  165. }
  166. }
  167. tokenizer.advance();
  168. }
  169. return -1;
  170. }
  171. Script *GDScriptLanguage::create_script() const {
  172. return memnew(GDScript);
  173. }
  174. /* DEBUGGER FUNCTIONS */
  175. bool GDScriptLanguage::debug_break_parse(const String &p_file, int p_line, const String &p_error) {
  176. //break because of parse error
  177. if (ScriptDebugger::get_singleton() && Thread::get_caller_id() == Thread::get_main_id()) {
  178. _debug_parse_err_line = p_line;
  179. _debug_parse_err_file = p_file;
  180. _debug_error = p_error;
  181. ScriptDebugger::get_singleton()->debug(this, false, true);
  182. return true;
  183. } else {
  184. return false;
  185. }
  186. }
  187. bool GDScriptLanguage::debug_break(const String &p_error, bool p_allow_continue) {
  188. if (ScriptDebugger::get_singleton() && Thread::get_caller_id() == Thread::get_main_id()) {
  189. _debug_parse_err_line = -1;
  190. _debug_parse_err_file = "";
  191. _debug_error = p_error;
  192. bool is_error_breakpoint = p_error != "Breakpoint";
  193. ScriptDebugger::get_singleton()->debug(this, p_allow_continue, is_error_breakpoint);
  194. return true;
  195. } else {
  196. return false;
  197. }
  198. }
  199. String GDScriptLanguage::debug_get_error() const {
  200. return _debug_error;
  201. }
  202. int GDScriptLanguage::debug_get_stack_level_count() const {
  203. if (_debug_parse_err_line >= 0) {
  204. return 1;
  205. }
  206. return _debug_call_stack_pos;
  207. }
  208. int GDScriptLanguage::debug_get_stack_level_line(int p_level) const {
  209. if (_debug_parse_err_line >= 0) {
  210. return _debug_parse_err_line;
  211. }
  212. ERR_FAIL_INDEX_V(p_level, _debug_call_stack_pos, -1);
  213. int l = _debug_call_stack_pos - p_level - 1;
  214. return *(_call_stack[l].line);
  215. }
  216. String GDScriptLanguage::debug_get_stack_level_function(int p_level) const {
  217. if (_debug_parse_err_line >= 0) {
  218. return "";
  219. }
  220. ERR_FAIL_INDEX_V(p_level, _debug_call_stack_pos, "");
  221. int l = _debug_call_stack_pos - p_level - 1;
  222. return _call_stack[l].function->get_name();
  223. }
  224. String GDScriptLanguage::debug_get_stack_level_source(int p_level) const {
  225. if (_debug_parse_err_line >= 0) {
  226. return _debug_parse_err_file;
  227. }
  228. ERR_FAIL_INDEX_V(p_level, _debug_call_stack_pos, "");
  229. int l = _debug_call_stack_pos - p_level - 1;
  230. return _call_stack[l].function->get_source();
  231. }
  232. 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) {
  233. if (_debug_parse_err_line >= 0) {
  234. return;
  235. }
  236. ERR_FAIL_INDEX(p_level, _debug_call_stack_pos);
  237. int l = _debug_call_stack_pos - p_level - 1;
  238. GDScriptFunction *f = _call_stack[l].function;
  239. List<Pair<StringName, int>> locals;
  240. f->debug_get_stack_member_state(*_call_stack[l].line, &locals);
  241. for (List<Pair<StringName, int>>::Element *E = locals.front(); E; E = E->next()) {
  242. p_locals->push_back(E->get().first);
  243. p_values->push_back(_call_stack[l].stack[E->get().second]);
  244. }
  245. }
  246. 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) {
  247. if (_debug_parse_err_line >= 0) {
  248. return;
  249. }
  250. ERR_FAIL_INDEX(p_level, _debug_call_stack_pos);
  251. int l = _debug_call_stack_pos - p_level - 1;
  252. GDScriptInstance *instance = _call_stack[l].instance;
  253. if (!instance) {
  254. return;
  255. }
  256. Ref<GDScript> script = instance->get_script();
  257. ERR_FAIL_COND(script.is_null());
  258. const Map<StringName, GDScript::MemberInfo> &mi = script->debug_get_member_indices();
  259. for (const Map<StringName, GDScript::MemberInfo>::Element *E = mi.front(); E; E = E->next()) {
  260. p_members->push_back(E->key());
  261. p_values->push_back(instance->debug_get_member_by_index(E->get().index));
  262. }
  263. }
  264. ScriptInstance *GDScriptLanguage::debug_get_stack_level_instance(int p_level) {
  265. if (_debug_parse_err_line >= 0) {
  266. return nullptr;
  267. }
  268. ERR_FAIL_INDEX_V(p_level, _debug_call_stack_pos, nullptr);
  269. int l = _debug_call_stack_pos - p_level - 1;
  270. ScriptInstance *instance = _call_stack[l].instance;
  271. return instance;
  272. }
  273. void GDScriptLanguage::debug_get_globals(List<String> *p_globals, List<Variant> *p_values, int p_max_subitems, int p_max_depth) {
  274. const Map<StringName, int> &name_idx = GDScriptLanguage::get_singleton()->get_global_map();
  275. const Variant *globals = GDScriptLanguage::get_singleton()->get_global_array();
  276. List<Pair<String, Variant>> cinfo;
  277. get_public_constants(&cinfo);
  278. for (const Map<StringName, int>::Element *E = name_idx.front(); E; E = E->next()) {
  279. if (ClassDB::class_exists(E->key()) || Engine::get_singleton()->has_singleton(E->key())) {
  280. continue;
  281. }
  282. bool is_script_constant = false;
  283. for (List<Pair<String, Variant>>::Element *CE = cinfo.front(); CE; CE = CE->next()) {
  284. if (CE->get().first == E->key()) {
  285. is_script_constant = true;
  286. break;
  287. }
  288. }
  289. if (is_script_constant) {
  290. continue;
  291. }
  292. const Variant &var = globals[E->value()];
  293. if (Object *obj = var) {
  294. if (Object::cast_to<GDScriptNativeClass>(obj)) {
  295. continue;
  296. }
  297. }
  298. bool skip = false;
  299. for (int i = 0; i < GlobalConstants::get_global_constant_count(); i++) {
  300. if (E->key() == GlobalConstants::get_global_constant_name(i)) {
  301. skip = true;
  302. break;
  303. }
  304. }
  305. if (skip) {
  306. continue;
  307. }
  308. p_globals->push_back(E->key());
  309. p_values->push_back(var);
  310. }
  311. }
  312. String GDScriptLanguage::debug_parse_stack_level_expression(int p_level, const String &p_expression, int p_max_subitems, int p_max_depth) {
  313. return "";
  314. }
  315. void GDScriptLanguage::get_recognized_extensions(List<String> *p_extensions) const {
  316. p_extensions->push_back("gd");
  317. }
  318. void GDScriptLanguage::get_public_functions(List<MethodInfo> *p_functions) const {
  319. for (int i = 0; i < GDScriptFunctions::FUNC_MAX; i++) {
  320. p_functions->push_back(GDScriptFunctions::get_info(GDScriptFunctions::Function(i)));
  321. }
  322. //not really "functions", but..
  323. {
  324. MethodInfo mi;
  325. mi.name = "preload";
  326. mi.arguments.push_back(PropertyInfo(Variant::STRING, "path"));
  327. mi.return_val = PropertyInfo(Variant::OBJECT, "", PROPERTY_HINT_RESOURCE_TYPE, "Resource");
  328. p_functions->push_back(mi);
  329. }
  330. {
  331. MethodInfo mi;
  332. mi.name = "yield";
  333. mi.arguments.push_back(PropertyInfo(Variant::OBJECT, "object"));
  334. mi.arguments.push_back(PropertyInfo(Variant::STRING, "signal"));
  335. mi.default_arguments.push_back(Variant());
  336. mi.default_arguments.push_back(String());
  337. mi.return_val = PropertyInfo(Variant::OBJECT, "", PROPERTY_HINT_RESOURCE_TYPE, "GDScriptFunctionState");
  338. p_functions->push_back(mi);
  339. }
  340. {
  341. MethodInfo mi;
  342. mi.name = "assert";
  343. mi.return_val.type = Variant::NIL;
  344. mi.arguments.push_back(PropertyInfo(Variant::BOOL, "condition"));
  345. mi.arguments.push_back(PropertyInfo(Variant::STRING, "message"));
  346. mi.default_arguments.push_back(String());
  347. p_functions->push_back(mi);
  348. }
  349. }
  350. void GDScriptLanguage::get_public_constants(List<Pair<String, Variant>> *p_constants) const {
  351. Pair<String, Variant> pi;
  352. pi.first = "PI";
  353. pi.second = Math_PI;
  354. p_constants->push_back(pi);
  355. Pair<String, Variant> tau;
  356. tau.first = "TAU";
  357. tau.second = Math_TAU;
  358. p_constants->push_back(tau);
  359. Pair<String, Variant> infinity;
  360. infinity.first = "INF";
  361. infinity.second = Math_INF;
  362. p_constants->push_back(infinity);
  363. Pair<String, Variant> nan;
  364. nan.first = "NAN";
  365. nan.second = Math_NAN;
  366. p_constants->push_back(nan);
  367. }
  368. String GDScriptLanguage::make_function(const String &p_class, const String &p_name, const PoolStringArray &p_args) const {
  369. #ifdef TOOLS_ENABLED
  370. bool th = EditorSettings::get_singleton()->get_setting("text_editor/completion/add_type_hints");
  371. #else
  372. bool th = false;
  373. #endif
  374. String s = "func " + p_name + "(";
  375. if (p_args.size()) {
  376. for (int i = 0; i < p_args.size(); i++) {
  377. if (i > 0) {
  378. s += ", ";
  379. }
  380. s += p_args[i].get_slice(":", 0);
  381. if (th) {
  382. String type = p_args[i].get_slice(":", 1);
  383. if (!type.empty() && type != "var") {
  384. s += ": " + type;
  385. }
  386. }
  387. }
  388. }
  389. s += String(")") + (th ? " -> void" : "") + ":\n" + _get_indentation() + "pass # Replace with function body.\n";
  390. return s;
  391. }
  392. //////// COMPLETION //////////
  393. #if defined(DEBUG_METHODS_ENABLED) && defined(TOOLS_ENABLED)
  394. struct GDScriptCompletionContext {
  395. const GDScriptParser::ClassNode *_class;
  396. const GDScriptParser::FunctionNode *function;
  397. const GDScriptParser::BlockNode *block;
  398. Object *base;
  399. String base_path;
  400. int line;
  401. uint32_t depth;
  402. GDScriptCompletionContext() :
  403. _class(nullptr),
  404. function(nullptr),
  405. block(nullptr),
  406. base(nullptr),
  407. line(0),
  408. depth(0) {}
  409. };
  410. struct GDScriptCompletionIdentifier {
  411. GDScriptParser::DataType type;
  412. String enumeration;
  413. Variant value;
  414. const GDScriptParser::Node *assigned_expression;
  415. GDScriptCompletionIdentifier() :
  416. assigned_expression(nullptr) {}
  417. };
  418. static void _get_directory_contents(EditorFileSystemDirectory *p_dir, Map<String, ScriptCodeCompletionOption> &r_list, String p_ends_with = "") {
  419. const String quote_style = EDITOR_GET("text_editor/completion/use_single_quotes") ? "'" : "\"";
  420. for (int i = 0; i < p_dir->get_file_count(); i++) {
  421. ScriptCodeCompletionOption option(p_dir->get_file_path(i), ScriptCodeCompletionOption::KIND_FILE_PATH);
  422. option.insert_text = quote_style + option.display + quote_style;
  423. if (!p_ends_with.empty()) {
  424. if (option.display.ends_with(p_ends_with)) {
  425. r_list.insert(option.display, option);
  426. }
  427. } else {
  428. r_list.insert(option.display, option);
  429. }
  430. }
  431. for (int i = 0; i < p_dir->get_subdir_count(); i++) {
  432. _get_directory_contents(p_dir->get_subdir(i), r_list, p_ends_with);
  433. }
  434. }
  435. static String _get_visual_datatype(const PropertyInfo &p_info, bool p_isarg = true) {
  436. if (p_info.usage & PROPERTY_USAGE_CLASS_IS_ENUM) {
  437. String enum_name = p_info.class_name;
  438. if (enum_name.find(".") == -1) {
  439. return enum_name;
  440. }
  441. return enum_name.get_slice(".", 1);
  442. }
  443. String n = p_info.name;
  444. int idx = n.find(":");
  445. if (idx != -1) {
  446. return n.substr(idx + 1, n.length());
  447. }
  448. if (p_info.type == Variant::OBJECT) {
  449. if (p_info.hint == PROPERTY_HINT_RESOURCE_TYPE) {
  450. return p_info.hint_string;
  451. } else {
  452. return p_info.class_name.operator String();
  453. }
  454. }
  455. if (p_info.type == Variant::NIL) {
  456. if (p_isarg || (p_info.usage & PROPERTY_USAGE_NIL_IS_VARIANT)) {
  457. return "Variant";
  458. } else {
  459. return "void";
  460. }
  461. }
  462. return Variant::get_type_name(p_info.type);
  463. }
  464. static GDScriptCompletionIdentifier _type_from_variant(const Variant &p_value) {
  465. GDScriptCompletionIdentifier ci;
  466. ci.value = p_value;
  467. ci.type.is_constant = true;
  468. ci.type.has_type = true;
  469. ci.type.kind = GDScriptParser::DataType::BUILTIN;
  470. ci.type.builtin_type = p_value.get_type();
  471. if (ci.type.builtin_type == Variant::OBJECT) {
  472. Object *obj = p_value.operator Object *();
  473. if (!obj) {
  474. return ci;
  475. }
  476. ci.type.native_type = obj->get_class_name();
  477. Ref<Script> scr = p_value;
  478. if (scr.is_valid()) {
  479. ci.type.is_meta_type = true;
  480. } else {
  481. ci.type.is_meta_type = false;
  482. scr = obj->get_script();
  483. }
  484. if (scr.is_valid()) {
  485. ci.type.script_type = scr;
  486. Ref<GDScript> gds = scr;
  487. if (gds.is_valid()) {
  488. ci.type.kind = GDScriptParser::DataType::GDSCRIPT;
  489. } else {
  490. ci.type.kind = GDScriptParser::DataType::SCRIPT;
  491. }
  492. ci.type.native_type = scr->get_instance_base_type();
  493. } else {
  494. ci.type.kind = GDScriptParser::DataType::NATIVE;
  495. }
  496. }
  497. return ci;
  498. }
  499. static GDScriptCompletionIdentifier _type_from_property(const PropertyInfo &p_property) {
  500. GDScriptCompletionIdentifier ci;
  501. if (p_property.type == Variant::NIL) {
  502. // Variant
  503. return ci;
  504. }
  505. if (p_property.usage & PROPERTY_USAGE_CLASS_IS_ENUM) {
  506. ci.enumeration = p_property.class_name;
  507. }
  508. ci.type.has_type = true;
  509. ci.type.builtin_type = p_property.type;
  510. if (p_property.type == Variant::OBJECT) {
  511. ci.type.kind = GDScriptParser::DataType::NATIVE;
  512. ci.type.native_type = p_property.class_name == StringName() ? "Object" : p_property.class_name;
  513. } else {
  514. ci.type.kind = GDScriptParser::DataType::BUILTIN;
  515. }
  516. return ci;
  517. }
  518. static GDScriptCompletionIdentifier _type_from_gdtype(const GDScriptDataType &p_gdtype) {
  519. GDScriptCompletionIdentifier ci;
  520. if (!p_gdtype.has_type) {
  521. return ci;
  522. }
  523. ci.type.has_type = true;
  524. ci.type.builtin_type = p_gdtype.builtin_type;
  525. ci.type.native_type = p_gdtype.native_type;
  526. ci.type.script_type = Ref<Script>(p_gdtype.script_type);
  527. switch (p_gdtype.kind) {
  528. case GDScriptDataType::UNINITIALIZED: {
  529. ERR_PRINT("Uninitialized completion. Please report a bug.");
  530. } break;
  531. case GDScriptDataType::BUILTIN: {
  532. ci.type.kind = GDScriptParser::DataType::BUILTIN;
  533. } break;
  534. case GDScriptDataType::NATIVE: {
  535. ci.type.kind = GDScriptParser::DataType::NATIVE;
  536. } break;
  537. case GDScriptDataType::GDSCRIPT: {
  538. ci.type.kind = GDScriptParser::DataType::GDSCRIPT;
  539. } break;
  540. case GDScriptDataType::SCRIPT: {
  541. ci.type.kind = GDScriptParser::DataType::SCRIPT;
  542. } break;
  543. }
  544. return ci;
  545. }
  546. static bool _guess_identifier_type(GDScriptCompletionContext &p_context, const StringName &p_identifier, GDScriptCompletionIdentifier &r_type);
  547. static bool _guess_identifier_type_from_base(GDScriptCompletionContext &p_context, const GDScriptCompletionIdentifier &p_base, const StringName &p_identifier, GDScriptCompletionIdentifier &r_type);
  548. static bool _guess_method_return_type_from_base(GDScriptCompletionContext &p_context, const GDScriptCompletionIdentifier &p_base, const StringName &p_method, GDScriptCompletionIdentifier &r_type);
  549. static bool _guess_expression_type(GDScriptCompletionContext &p_context, const GDScriptParser::Node *p_expression, GDScriptCompletionIdentifier &r_type) {
  550. bool found = false;
  551. if (++p_context.depth > 100) {
  552. print_error("Maximum _guess_expression_type depth limit reached. Please file a bugreport.");
  553. return false;
  554. }
  555. switch (p_expression->type) {
  556. case GDScriptParser::Node::TYPE_CONSTANT: {
  557. const GDScriptParser::ConstantNode *cn = static_cast<const GDScriptParser::ConstantNode *>(p_expression);
  558. r_type = _type_from_variant(cn->value);
  559. found = true;
  560. } break;
  561. case GDScriptParser::Node::TYPE_SELF: {
  562. if (p_context._class) {
  563. r_type.type.has_type = true;
  564. r_type.type.kind = GDScriptParser::DataType::CLASS;
  565. r_type.type.class_type = const_cast<GDScriptParser::ClassNode *>(p_context._class);
  566. r_type.type.is_constant = true;
  567. r_type.value = p_context.base;
  568. found = true;
  569. }
  570. } break;
  571. case GDScriptParser::Node::TYPE_IDENTIFIER: {
  572. const GDScriptParser::IdentifierNode *id = static_cast<const GDScriptParser::IdentifierNode *>(p_expression);
  573. found = _guess_identifier_type(p_context, id->name, r_type);
  574. } break;
  575. case GDScriptParser::Node::TYPE_DICTIONARY: {
  576. // Try to recreate the dictionary
  577. const GDScriptParser::DictionaryNode *dn = static_cast<const GDScriptParser::DictionaryNode *>(p_expression);
  578. Dictionary d;
  579. bool full = true;
  580. for (int i = 0; i < dn->elements.size(); i++) {
  581. GDScriptCompletionIdentifier key;
  582. if (_guess_expression_type(p_context, dn->elements[i].key, key)) {
  583. GDScriptCompletionIdentifier value;
  584. if (_guess_expression_type(p_context, dn->elements[i].value, value)) {
  585. if (!value.type.is_constant) {
  586. full = false;
  587. break;
  588. }
  589. d[key.value] = value.value;
  590. } else {
  591. full = false;
  592. break;
  593. }
  594. } else {
  595. full = false;
  596. break;
  597. }
  598. }
  599. if (full) {
  600. // If not fully constant, setting this value is detrimental to the inference
  601. r_type.value = d;
  602. r_type.type.is_constant = true;
  603. }
  604. r_type.type.has_type = true;
  605. r_type.type.kind = GDScriptParser::DataType::BUILTIN;
  606. r_type.type.builtin_type = Variant::DICTIONARY;
  607. } break;
  608. case GDScriptParser::Node::TYPE_ARRAY: {
  609. // Try to recreate the array
  610. const GDScriptParser::ArrayNode *an = static_cast<const GDScriptParser::ArrayNode *>(p_expression);
  611. Array a;
  612. bool full = true;
  613. a.resize(an->elements.size());
  614. for (int i = 0; i < an->elements.size(); i++) {
  615. GDScriptCompletionIdentifier value;
  616. if (_guess_expression_type(p_context, an->elements[i], value)) {
  617. a[i] = value.value;
  618. } else {
  619. full = false;
  620. break;
  621. }
  622. }
  623. if (full) {
  624. // If not fully constant, setting this value is detrimental to the inference
  625. r_type.value = a;
  626. }
  627. r_type.type.has_type = true;
  628. r_type.type.kind = GDScriptParser::DataType::BUILTIN;
  629. r_type.type.builtin_type = Variant::ARRAY;
  630. } break;
  631. case GDScriptParser::Node::TYPE_CAST: {
  632. const GDScriptParser::CastNode *cn = static_cast<const GDScriptParser::CastNode *>(p_expression);
  633. GDScriptCompletionIdentifier value;
  634. if (_guess_expression_type(p_context, cn->source_node, r_type)) {
  635. r_type.type = cn->get_datatype();
  636. found = true;
  637. }
  638. } break;
  639. case GDScriptParser::Node::TYPE_OPERATOR: {
  640. const GDScriptParser::OperatorNode *op = static_cast<const GDScriptParser::OperatorNode *>(p_expression);
  641. switch (op->op) {
  642. case GDScriptParser::OperatorNode::OP_CALL: {
  643. if (op->arguments[0]->type == GDScriptParser::Node::TYPE_TYPE) {
  644. const GDScriptParser::TypeNode *tn = static_cast<const GDScriptParser::TypeNode *>(op->arguments[0]);
  645. r_type.type.has_type = true;
  646. r_type.type.kind = GDScriptParser::DataType::BUILTIN;
  647. r_type.type.builtin_type = tn->vtype;
  648. found = true;
  649. break;
  650. } else if (op->arguments[0]->type == GDScriptParser::Node::TYPE_BUILT_IN_FUNCTION) {
  651. const GDScriptParser::BuiltInFunctionNode *bin = static_cast<const GDScriptParser::BuiltInFunctionNode *>(op->arguments[0]);
  652. MethodInfo mi = GDScriptFunctions::get_info(bin->function);
  653. r_type = _type_from_property(mi.return_val);
  654. found = true;
  655. break;
  656. } else if (op->arguments.size() >= 2 && op->arguments[1]->type == GDScriptParser::Node::TYPE_IDENTIFIER) {
  657. StringName id = static_cast<const GDScriptParser::IdentifierNode *>(op->arguments[1])->name;
  658. GDScriptCompletionContext c = p_context;
  659. c.line = op->line;
  660. GDScriptCompletionIdentifier base;
  661. if (!_guess_expression_type(c, op->arguments[0], base)) {
  662. found = false;
  663. break;
  664. }
  665. // Try call if constant methods with constant arguments
  666. if (base.type.is_constant && base.value.get_type() == Variant::OBJECT) {
  667. GDScriptParser::DataType native_type = base.type;
  668. while (native_type.kind == GDScriptParser::DataType::CLASS) {
  669. native_type = native_type.class_type->base_type;
  670. }
  671. while (native_type.kind == GDScriptParser::DataType::GDSCRIPT || native_type.kind == GDScriptParser::DataType::SCRIPT) {
  672. if (native_type.script_type.is_valid()) {
  673. Ref<Script> parent = native_type.script_type->get_base_script();
  674. if (parent.is_valid()) {
  675. native_type.script_type = parent;
  676. } else {
  677. native_type.kind = GDScriptParser::DataType::NATIVE;
  678. native_type.native_type = native_type.script_type->get_instance_base_type();
  679. if (!ClassDB::class_exists(native_type.native_type)) {
  680. native_type.native_type = String("_") + native_type.native_type;
  681. if (!ClassDB::class_exists(native_type.native_type)) {
  682. native_type.has_type = false;
  683. }
  684. }
  685. }
  686. }
  687. }
  688. if (native_type.has_type && native_type.kind == GDScriptParser::DataType::NATIVE) {
  689. MethodBind *mb = ClassDB::get_method(native_type.native_type, id);
  690. if (mb && mb->is_const()) {
  691. bool all_is_const = true;
  692. Vector<Variant> args;
  693. GDScriptCompletionContext c2 = p_context;
  694. c2.line = op->line;
  695. for (int i = 2; all_is_const && i < op->arguments.size(); i++) {
  696. GDScriptCompletionIdentifier arg;
  697. if (_guess_expression_type(c2, op->arguments[i], arg)) {
  698. if (arg.type.has_type && arg.type.is_constant && arg.value.get_type() != Variant::OBJECT) {
  699. args.push_back(arg.value);
  700. } else {
  701. all_is_const = false;
  702. }
  703. } else {
  704. all_is_const = false;
  705. }
  706. }
  707. Object *baseptr = base.value;
  708. if (all_is_const && String(id) == "get_node" && ClassDB::is_parent_class(native_type.native_type, "Node") && args.size()) {
  709. String arg1 = args[0];
  710. if (arg1.begins_with("/root/")) {
  711. String which = arg1.get_slice("/", 2);
  712. if (which != "") {
  713. // Try singletons first
  714. if (GDScriptLanguage::get_singleton()->get_named_globals_map().has(which)) {
  715. r_type = _type_from_variant(GDScriptLanguage::get_singleton()->get_named_globals_map()[which]);
  716. found = true;
  717. } else {
  718. List<PropertyInfo> props;
  719. ProjectSettings::get_singleton()->get_property_list(&props);
  720. for (List<PropertyInfo>::Element *E = props.front(); E; E = E->next()) {
  721. String s = E->get().name;
  722. if (!s.begins_with("autoload/")) {
  723. continue;
  724. }
  725. String name = s.get_slice("/", 1);
  726. if (name == which) {
  727. String script = ProjectSettings::get_singleton()->get(s);
  728. if (script.begins_with("*")) {
  729. script = script.right(1);
  730. }
  731. if (!script.begins_with("res://")) {
  732. script = "res://" + script;
  733. }
  734. if (!script.ends_with(".gd")) {
  735. //not a script, try find the script anyway,
  736. //may have some success
  737. script = script.get_basename() + ".gd";
  738. }
  739. if (FileAccess::exists(script)) {
  740. Ref<Script> scr;
  741. if (ScriptCodeCompletionCache::get_singleton()) {
  742. scr = ScriptCodeCompletionCache::get_singleton()->get_cached_resource(script);
  743. } else {
  744. scr = ResourceLoader::load(script);
  745. }
  746. if (scr.is_valid()) {
  747. r_type.type.has_type = true;
  748. r_type.type.script_type = scr;
  749. r_type.type.is_constant = false;
  750. Ref<GDScript> gds = scr;
  751. if (gds.is_valid()) {
  752. r_type.type.kind = GDScriptParser::DataType::GDSCRIPT;
  753. } else {
  754. r_type.type.kind = GDScriptParser::DataType::SCRIPT;
  755. }
  756. r_type.value = Variant();
  757. found = true;
  758. }
  759. }
  760. break;
  761. }
  762. }
  763. }
  764. }
  765. }
  766. }
  767. if (!found && all_is_const && baseptr) {
  768. Vector<const Variant *> argptr;
  769. for (int i = 0; i < args.size(); i++) {
  770. argptr.push_back(&args[i]);
  771. }
  772. Variant::CallError ce;
  773. Variant ret = mb->call(baseptr, (const Variant **)argptr.ptr(), argptr.size(), ce);
  774. if (ce.error == Variant::CallError::CALL_OK && ret.get_type() != Variant::NIL) {
  775. if (ret.get_type() != Variant::OBJECT || ret.operator Object *() != nullptr) {
  776. r_type = _type_from_variant(ret);
  777. found = true;
  778. }
  779. }
  780. }
  781. }
  782. }
  783. }
  784. if (!found) {
  785. found = _guess_method_return_type_from_base(c, base, id, r_type);
  786. }
  787. }
  788. } break;
  789. case GDScriptParser::OperatorNode::OP_PARENT_CALL: {
  790. if (!p_context._class || !op->arguments.size() || op->arguments[0]->type != GDScriptParser::Node::TYPE_IDENTIFIER) {
  791. break;
  792. }
  793. StringName id = static_cast<const GDScriptParser::IdentifierNode *>(op->arguments[0])->name;
  794. GDScriptCompletionIdentifier base;
  795. base.value = p_context.base;
  796. base.type = p_context._class->base_type;
  797. GDScriptCompletionContext c = p_context;
  798. c.line = op->line;
  799. found = _guess_method_return_type_from_base(c, base, id, r_type);
  800. } break;
  801. case GDScriptParser::OperatorNode::OP_INDEX_NAMED: {
  802. if (op->arguments.size() < 2 || op->arguments[1]->type != GDScriptParser::Node::TYPE_IDENTIFIER) {
  803. found = false;
  804. break;
  805. }
  806. const GDScriptParser::IdentifierNode *id = static_cast<const GDScriptParser::IdentifierNode *>(op->arguments[1]);
  807. GDScriptCompletionContext c = p_context;
  808. c.line = op->line;
  809. GDScriptCompletionIdentifier base;
  810. if (!_guess_expression_type(c, op->arguments[0], base)) {
  811. found = false;
  812. break;
  813. }
  814. if (base.value.get_type() == Variant::DICTIONARY && base.value.operator Dictionary().has(String(id->name))) {
  815. Variant value = base.value.operator Dictionary()[String(id->name)];
  816. r_type = _type_from_variant(value);
  817. found = true;
  818. break;
  819. }
  820. const GDScriptParser::DictionaryNode *dn = nullptr;
  821. if (op->arguments[0]->type == GDScriptParser::Node::TYPE_DICTIONARY) {
  822. dn = static_cast<const GDScriptParser::DictionaryNode *>(op->arguments[0]);
  823. } else if (base.assigned_expression && base.assigned_expression->type == GDScriptParser::Node::TYPE_DICTIONARY) {
  824. dn = static_cast<const GDScriptParser::DictionaryNode *>(base.assigned_expression);
  825. }
  826. if (dn) {
  827. for (int i = 0; i < dn->elements.size(); i++) {
  828. GDScriptCompletionIdentifier key;
  829. if (!_guess_expression_type(c, dn->elements[i].key, key)) {
  830. continue;
  831. }
  832. if (key.value == String(id->name)) {
  833. r_type.assigned_expression = dn->elements[i].value;
  834. found = _guess_expression_type(c, dn->elements[i].value, r_type);
  835. break;
  836. }
  837. }
  838. }
  839. if (!found) {
  840. found = _guess_identifier_type_from_base(c, base, id->name, r_type);
  841. }
  842. } break;
  843. case GDScriptParser::OperatorNode::OP_INDEX: {
  844. if (op->arguments.size() < 2) {
  845. found = false;
  846. break;
  847. }
  848. GDScriptCompletionContext c = p_context;
  849. c.line = op->line;
  850. GDScriptCompletionIdentifier base;
  851. if (!_guess_expression_type(c, op->arguments[0], base)) {
  852. found = false;
  853. break;
  854. }
  855. GDScriptCompletionIdentifier index;
  856. if (!_guess_expression_type(c, op->arguments[1], index)) {
  857. found = false;
  858. break;
  859. }
  860. if (base.value.in(index.value)) {
  861. Variant value = base.value.get(index.value);
  862. r_type = _type_from_variant(value);
  863. found = true;
  864. break;
  865. }
  866. // Look if it is a dictionary node
  867. const GDScriptParser::DictionaryNode *dn = nullptr;
  868. if (op->arguments[0]->type == GDScriptParser::Node::TYPE_DICTIONARY) {
  869. dn = static_cast<const GDScriptParser::DictionaryNode *>(op->arguments[0]);
  870. } else if (base.assigned_expression && base.assigned_expression->type == GDScriptParser::Node::TYPE_DICTIONARY) {
  871. dn = static_cast<const GDScriptParser::DictionaryNode *>(base.assigned_expression);
  872. }
  873. if (dn) {
  874. for (int i = 0; i < dn->elements.size(); i++) {
  875. GDScriptCompletionIdentifier key;
  876. if (!_guess_expression_type(c, dn->elements[i].key, key)) {
  877. continue;
  878. }
  879. if (key.value == index.value) {
  880. r_type.assigned_expression = dn->elements[i].value;
  881. found = _guess_expression_type(p_context, dn->elements[i].value, r_type);
  882. break;
  883. }
  884. }
  885. }
  886. // Look if it is an array node
  887. if (!found && index.value.is_num()) {
  888. int idx = index.value;
  889. const GDScriptParser::ArrayNode *an = nullptr;
  890. if (op->arguments[0]->type == GDScriptParser::Node::TYPE_ARRAY) {
  891. an = static_cast<const GDScriptParser::ArrayNode *>(op->arguments[0]);
  892. } else if (base.assigned_expression && base.assigned_expression->type == GDScriptParser::Node::TYPE_ARRAY) {
  893. an = static_cast<const GDScriptParser::ArrayNode *>(base.assigned_expression);
  894. }
  895. if (an && idx >= 0 && an->elements.size() > idx) {
  896. r_type.assigned_expression = an->elements[idx];
  897. found = _guess_expression_type(c, an->elements[idx], r_type);
  898. break;
  899. }
  900. }
  901. // Look for valid indexing in other types
  902. if (!found && (index.value.get_type() == Variant::STRING || index.value.get_type() == Variant::NODE_PATH)) {
  903. StringName id = index.value;
  904. found = _guess_identifier_type_from_base(c, base, id, r_type);
  905. } else if (!found && index.type.kind == GDScriptParser::DataType::BUILTIN) {
  906. Variant::CallError err;
  907. Variant base_val = Variant::construct(base.type.builtin_type, nullptr, 0, err);
  908. bool valid = false;
  909. Variant res = base_val.get(index.value, &valid);
  910. if (valid) {
  911. r_type = _type_from_variant(res);
  912. r_type.value = Variant();
  913. r_type.type.is_constant = false;
  914. found = true;
  915. }
  916. }
  917. } break;
  918. default: {
  919. if (op->arguments.size() < 2) {
  920. found = false;
  921. break;
  922. }
  923. Variant::Operator vop = Variant::OP_MAX;
  924. switch (op->op) {
  925. case GDScriptParser::OperatorNode::OP_ADD:
  926. vop = Variant::OP_ADD;
  927. break;
  928. case GDScriptParser::OperatorNode::OP_SUB:
  929. vop = Variant::OP_SUBTRACT;
  930. break;
  931. case GDScriptParser::OperatorNode::OP_MUL:
  932. vop = Variant::OP_MULTIPLY;
  933. break;
  934. case GDScriptParser::OperatorNode::OP_DIV:
  935. vop = Variant::OP_DIVIDE;
  936. break;
  937. case GDScriptParser::OperatorNode::OP_MOD:
  938. vop = Variant::OP_MODULE;
  939. break;
  940. case GDScriptParser::OperatorNode::OP_SHIFT_LEFT:
  941. vop = Variant::OP_SHIFT_LEFT;
  942. break;
  943. case GDScriptParser::OperatorNode::OP_SHIFT_RIGHT:
  944. vop = Variant::OP_SHIFT_RIGHT;
  945. break;
  946. case GDScriptParser::OperatorNode::OP_BIT_AND:
  947. vop = Variant::OP_BIT_AND;
  948. break;
  949. case GDScriptParser::OperatorNode::OP_BIT_OR:
  950. vop = Variant::OP_BIT_OR;
  951. break;
  952. case GDScriptParser::OperatorNode::OP_BIT_XOR:
  953. vop = Variant::OP_BIT_XOR;
  954. break;
  955. default: {
  956. }
  957. }
  958. if (vop == Variant::OP_MAX) {
  959. break;
  960. }
  961. GDScriptCompletionContext context = p_context;
  962. context.line = op->line;
  963. GDScriptCompletionIdentifier p1;
  964. GDScriptCompletionIdentifier p2;
  965. if (!_guess_expression_type(context, op->arguments[0], p1)) {
  966. found = false;
  967. break;
  968. }
  969. if (!_guess_expression_type(context, op->arguments[1], p2)) {
  970. found = false;
  971. break;
  972. }
  973. Variant::CallError ce;
  974. bool v1_use_value = p1.value.get_type() != Variant::NIL && p1.value.get_type() != Variant::OBJECT;
  975. Variant v1 = (v1_use_value) ? p1.value : Variant::construct(p1.type.builtin_type, nullptr, 0, ce);
  976. bool v2_use_value = p2.value.get_type() != Variant::NIL && p2.value.get_type() != Variant::OBJECT;
  977. Variant v2 = (v2_use_value) ? p2.value : Variant::construct(p2.type.builtin_type, nullptr, 0, ce);
  978. // avoid potential invalid ops
  979. if ((vop == Variant::OP_DIVIDE || vop == Variant::OP_MODULE) && v2.get_type() == Variant::INT) {
  980. v2 = 1;
  981. v2_use_value = false;
  982. }
  983. if (vop == Variant::OP_DIVIDE && v2.get_type() == Variant::REAL) {
  984. v2 = 1.0;
  985. v2_use_value = false;
  986. }
  987. Variant res;
  988. bool valid;
  989. Variant::evaluate(vop, v1, v2, res, valid);
  990. if (!valid) {
  991. found = false;
  992. break;
  993. }
  994. r_type = _type_from_variant(res);
  995. if (!v1_use_value || !v2_use_value) {
  996. r_type.value = Variant();
  997. r_type.type.is_constant = false;
  998. }
  999. found = true;
  1000. } break;
  1001. }
  1002. } break;
  1003. default: {
  1004. }
  1005. }
  1006. // It may have found a null, but that's never useful
  1007. if (found && r_type.type.has_type && r_type.type.kind == GDScriptParser::DataType::BUILTIN && r_type.type.builtin_type == Variant::NIL) {
  1008. found = false;
  1009. }
  1010. // Check type hint last. For collections we want chance to get the actual value first
  1011. // This way we can detect types from the content of dictionaries and arrays
  1012. if (!found && p_expression->get_datatype().has_type) {
  1013. r_type.type = p_expression->get_datatype();
  1014. if (!r_type.assigned_expression) {
  1015. r_type.assigned_expression = p_expression;
  1016. }
  1017. found = true;
  1018. }
  1019. return found;
  1020. }
  1021. static bool _guess_identifier_type(GDScriptCompletionContext &p_context, const StringName &p_identifier, GDScriptCompletionIdentifier &r_type) {
  1022. // Look in blocks first
  1023. const GDScriptParser::BlockNode *blk = p_context.block;
  1024. int last_assign_line = -1;
  1025. const GDScriptParser::Node *last_assigned_expression = nullptr;
  1026. GDScriptParser::DataType var_type;
  1027. while (blk) {
  1028. if (blk->variables.has(p_identifier)) {
  1029. if (blk->variables[p_identifier]->line > p_context.line) {
  1030. return false;
  1031. }
  1032. var_type = blk->variables[p_identifier]->datatype;
  1033. if (!last_assigned_expression && blk->variables[p_identifier]->assign && blk->variables[p_identifier]->assign->type == GDScriptParser::Node::TYPE_OPERATOR) {
  1034. const GDScriptParser::OperatorNode *op = static_cast<const GDScriptParser::OperatorNode *>(blk->variables[p_identifier]->assign);
  1035. if (op->op == GDScriptParser::OperatorNode::OP_ASSIGN && op->arguments.size() >= 2) {
  1036. last_assign_line = op->line;
  1037. last_assigned_expression = op->arguments[1];
  1038. }
  1039. }
  1040. }
  1041. for (int z = 0; z < blk->statements.size(); z++) {
  1042. const GDScriptParser::Node *expr = blk->statements[z];
  1043. if (expr->line > p_context.line || expr->type != GDScriptParser::Node::TYPE_OPERATOR) {
  1044. continue;
  1045. }
  1046. const GDScriptParser::OperatorNode *op = static_cast<const GDScriptParser::OperatorNode *>(expr);
  1047. if (op->op != GDScriptParser::OperatorNode::OP_ASSIGN || op->line < last_assign_line) {
  1048. continue;
  1049. }
  1050. if (op->arguments.size() >= 2 && op->arguments[0]->type == GDScriptParser::Node::TYPE_IDENTIFIER) {
  1051. const GDScriptParser::IdentifierNode *id = static_cast<const GDScriptParser::IdentifierNode *>(op->arguments[0]);
  1052. if (id->name == p_identifier) {
  1053. last_assign_line = op->line;
  1054. last_assigned_expression = op->arguments[1];
  1055. }
  1056. }
  1057. }
  1058. if (blk->if_condition && blk->if_condition->type == GDScriptParser::Node::TYPE_OPERATOR && static_cast<const GDScriptParser::OperatorNode *>(blk->if_condition)->op == GDScriptParser::OperatorNode::OP_IS) {
  1059. //is used, check if identifier is in there! this helps resolve in blocks that are (if (identifier is value)): which are very common..
  1060. //super dirty hack, but very useful
  1061. //credit: Zylann
  1062. //TODO: this could be hacked to detect ANDed conditions too..
  1063. const GDScriptParser::OperatorNode *op = static_cast<const GDScriptParser::OperatorNode *>(blk->if_condition);
  1064. if (op->arguments[0]->type == GDScriptParser::Node::TYPE_IDENTIFIER && static_cast<const GDScriptParser::IdentifierNode *>(op->arguments[0])->name == p_identifier) {
  1065. //bingo
  1066. GDScriptCompletionContext c = p_context;
  1067. c.line = op->line;
  1068. c.block = blk;
  1069. if (_guess_expression_type(p_context, op->arguments[1], r_type)) {
  1070. r_type.type.is_meta_type = false; // Right-hand of `is` will be a meta type, but the left-hand value is not
  1071. // Not an assignment, it shouldn't carry any value
  1072. r_type.value = Variant();
  1073. r_type.assigned_expression = nullptr;
  1074. return true;
  1075. }
  1076. }
  1077. }
  1078. blk = blk->parent_block;
  1079. }
  1080. if (last_assigned_expression && last_assign_line != p_context.line) {
  1081. GDScriptCompletionContext c = p_context;
  1082. c.line = last_assign_line;
  1083. r_type.assigned_expression = last_assigned_expression;
  1084. if (_guess_expression_type(c, last_assigned_expression, r_type)) {
  1085. if (var_type.has_type) {
  1086. r_type.type = var_type;
  1087. }
  1088. return true;
  1089. }
  1090. }
  1091. if (var_type.has_type) {
  1092. r_type.type = var_type;
  1093. return true;
  1094. }
  1095. if (p_context.function) {
  1096. for (int i = 0; i < p_context.function->arguments.size(); i++) {
  1097. if (p_context.function->arguments[i] == p_identifier) {
  1098. if (p_context.function->argument_types[i].has_type) {
  1099. r_type.type = p_context.function->argument_types[i];
  1100. return true;
  1101. }
  1102. int def_from = p_context.function->arguments.size() - p_context.function->default_values.size();
  1103. if (i >= def_from) {
  1104. int def_idx = i - def_from;
  1105. if (p_context.function->default_values[def_idx]->type == GDScriptParser::Node::TYPE_OPERATOR) {
  1106. const GDScriptParser::OperatorNode *op = static_cast<const GDScriptParser::OperatorNode *>(p_context.function->default_values[def_idx]);
  1107. if (op->arguments.size() < 2) {
  1108. return false;
  1109. }
  1110. GDScriptCompletionContext c = p_context;
  1111. c.function = nullptr;
  1112. c.block = nullptr;
  1113. return _guess_expression_type(c, op->arguments[1], r_type);
  1114. }
  1115. }
  1116. break;
  1117. }
  1118. }
  1119. GDScriptParser::DataType base_type = p_context._class->base_type;
  1120. while (base_type.has_type) {
  1121. switch (base_type.kind) {
  1122. case GDScriptParser::DataType::GDSCRIPT: {
  1123. Ref<GDScript> gds = base_type.script_type;
  1124. if (gds.is_valid() && gds->has_method(p_context.function->name)) {
  1125. GDScriptFunction *func = gds->get_member_functions()[p_context.function->name];
  1126. if (func) {
  1127. for (int i = 0; i < func->get_argument_count(); i++) {
  1128. if (func->get_argument_name(i) == p_identifier) {
  1129. r_type = _type_from_gdtype(func->get_argument_type(i));
  1130. return true;
  1131. }
  1132. }
  1133. }
  1134. Ref<GDScript> base_gds = gds->get_base_script();
  1135. if (base_gds.is_valid()) {
  1136. base_type.kind = GDScriptParser::DataType::GDSCRIPT;
  1137. base_type.script_type = base_gds;
  1138. } else {
  1139. base_type.kind = GDScriptParser::DataType::NATIVE;
  1140. base_type.native_type = gds->get_instance_base_type();
  1141. }
  1142. } else {
  1143. base_type.kind = GDScriptParser::DataType::NATIVE;
  1144. base_type.native_type = gds->get_instance_base_type();
  1145. }
  1146. } break;
  1147. case GDScriptParser::DataType::NATIVE: {
  1148. List<MethodInfo> methods;
  1149. ClassDB::get_method_list(base_type.native_type, &methods);
  1150. ClassDB::get_virtual_methods(base_type.native_type, &methods);
  1151. for (List<MethodInfo>::Element *E = methods.front(); E; E = E->next()) {
  1152. if (E->get().name == p_context.function->name) {
  1153. MethodInfo &mi = E->get();
  1154. for (List<PropertyInfo>::Element *F = mi.arguments.front(); F; F = F->next()) {
  1155. if (F->get().name == p_identifier) {
  1156. r_type = _type_from_property(F->get());
  1157. return true;
  1158. }
  1159. }
  1160. }
  1161. }
  1162. base_type.has_type = false;
  1163. } break;
  1164. default: {
  1165. base_type.has_type = false;
  1166. } break;
  1167. }
  1168. }
  1169. }
  1170. // Check current class (including inheritance)
  1171. if (p_context._class) {
  1172. GDScriptCompletionIdentifier context_base;
  1173. context_base.value = p_context.base;
  1174. context_base.type.has_type = true;
  1175. context_base.type.kind = GDScriptParser::DataType::CLASS;
  1176. context_base.type.class_type = const_cast<GDScriptParser::ClassNode *>(p_context._class);
  1177. context_base.type.is_meta_type = p_context.function && p_context.function->_static;
  1178. if (_guess_identifier_type_from_base(p_context, context_base, p_identifier, r_type)) {
  1179. return true;
  1180. }
  1181. }
  1182. // Check named scripts
  1183. if (ScriptServer::is_global_class(p_identifier)) {
  1184. Ref<Script> scr = ResourceLoader::load(ScriptServer::get_global_class_path(p_identifier));
  1185. if (scr.is_valid()) {
  1186. r_type = _type_from_variant(scr);
  1187. r_type.type.is_meta_type = true;
  1188. return true;
  1189. }
  1190. return false;
  1191. }
  1192. for (int i = 0; i < 2; i++) {
  1193. StringName target_id;
  1194. switch (i) {
  1195. case 0:
  1196. // Check ClassDB
  1197. target_id = p_identifier;
  1198. break;
  1199. case 1:
  1200. // ClassDB again for underscore-prefixed classes
  1201. target_id = String("_") + p_identifier;
  1202. break;
  1203. }
  1204. if (ClassDB::class_exists(target_id)) {
  1205. r_type.type.has_type = true;
  1206. r_type.type.kind = GDScriptParser::DataType::NATIVE;
  1207. r_type.type.native_type = target_id;
  1208. if (Engine::get_singleton()->has_singleton(target_id)) {
  1209. r_type.type.is_meta_type = false;
  1210. r_type.value = Engine::get_singleton()->get_singleton_object(target_id);
  1211. } else {
  1212. r_type.type.is_meta_type = true;
  1213. const Map<StringName, int>::Element *target_elem = GDScriptLanguage::get_singleton()->get_global_map().find(target_id);
  1214. // Check because classes like EditorNode are in ClassDB by now, but unknown to GDScript
  1215. if (!target_elem) {
  1216. return false;
  1217. }
  1218. int idx = target_elem->get();
  1219. r_type.value = GDScriptLanguage::get_singleton()->get_global_array()[idx];
  1220. }
  1221. return true;
  1222. }
  1223. }
  1224. // Check autoload singletons
  1225. if (GDScriptLanguage::get_singleton()->get_named_globals_map().has(p_identifier)) {
  1226. r_type = _type_from_variant(GDScriptLanguage::get_singleton()->get_named_globals_map()[p_identifier]);
  1227. return true;
  1228. }
  1229. return false;
  1230. }
  1231. static bool _guess_identifier_type_from_base(GDScriptCompletionContext &p_context, const GDScriptCompletionIdentifier &p_base, const StringName &p_identifier, GDScriptCompletionIdentifier &r_type) {
  1232. GDScriptParser::DataType base_type = p_base.type;
  1233. bool _static = base_type.is_meta_type;
  1234. while (base_type.has_type) {
  1235. switch (base_type.kind) {
  1236. case GDScriptParser::DataType::CLASS: {
  1237. if (base_type.class_type->constant_expressions.has(p_identifier)) {
  1238. GDScriptParser::ClassNode::Constant c = base_type.class_type->constant_expressions[p_identifier];
  1239. r_type.type = c.type;
  1240. if (c.expression->type == GDScriptParser::Node::TYPE_CONSTANT) {
  1241. r_type.value = static_cast<const GDScriptParser::ConstantNode *>(c.expression)->value;
  1242. }
  1243. return true;
  1244. }
  1245. if (!_static) {
  1246. for (int i = 0; i < base_type.class_type->variables.size(); i++) {
  1247. GDScriptParser::ClassNode::Member m = base_type.class_type->variables[i];
  1248. if (m.identifier == p_identifier) {
  1249. if (m.expression) {
  1250. if (p_context.line == m.expression->line) {
  1251. // Variable used in the same expression
  1252. return false;
  1253. }
  1254. if (_guess_expression_type(p_context, m.expression, r_type)) {
  1255. return true;
  1256. }
  1257. if (m.expression->get_datatype().has_type) {
  1258. r_type.type = m.expression->get_datatype();
  1259. return true;
  1260. }
  1261. }
  1262. if (m.data_type.has_type) {
  1263. r_type.type = m.data_type;
  1264. return true;
  1265. }
  1266. return false;
  1267. }
  1268. }
  1269. }
  1270. base_type = base_type.class_type->base_type;
  1271. } break;
  1272. case GDScriptParser::DataType::GDSCRIPT: {
  1273. Ref<GDScript> gds = base_type.script_type;
  1274. if (gds.is_valid()) {
  1275. if (gds->get_constants().has(p_identifier)) {
  1276. r_type = _type_from_variant(gds->get_constants()[p_identifier]);
  1277. return true;
  1278. }
  1279. if (!_static) {
  1280. const Set<StringName>::Element *m = gds->get_members().find(p_identifier);
  1281. if (m) {
  1282. r_type = _type_from_gdtype(gds->get_member_type(p_identifier));
  1283. return true;
  1284. }
  1285. }
  1286. Ref<GDScript> parent = gds->get_base_script();
  1287. if (parent.is_valid()) {
  1288. base_type.script_type = parent;
  1289. } else {
  1290. base_type.kind = GDScriptParser::DataType::NATIVE;
  1291. base_type.native_type = gds->get_instance_base_type();
  1292. }
  1293. } else {
  1294. return false;
  1295. }
  1296. } break;
  1297. case GDScriptParser::DataType::SCRIPT: {
  1298. Ref<Script> scr = base_type.script_type;
  1299. if (scr.is_valid()) {
  1300. Map<StringName, Variant> constants;
  1301. scr->get_constants(&constants);
  1302. if (constants.has(p_identifier)) {
  1303. r_type = _type_from_variant(constants[p_identifier]);
  1304. return true;
  1305. }
  1306. if (!_static) {
  1307. List<PropertyInfo> members;
  1308. scr->get_script_property_list(&members);
  1309. for (const List<PropertyInfo>::Element *E = members.front(); E; E = E->next()) {
  1310. const PropertyInfo &prop = E->get();
  1311. if (prop.name == p_identifier) {
  1312. r_type = _type_from_property(prop);
  1313. return true;
  1314. }
  1315. }
  1316. }
  1317. Ref<Script> parent = scr->get_base_script();
  1318. if (parent.is_valid()) {
  1319. base_type.script_type = parent;
  1320. } else {
  1321. base_type.kind = GDScriptParser::DataType::NATIVE;
  1322. base_type.native_type = scr->get_instance_base_type();
  1323. }
  1324. } else {
  1325. return false;
  1326. }
  1327. } break;
  1328. case GDScriptParser::DataType::NATIVE: {
  1329. StringName class_name = base_type.native_type;
  1330. if (!ClassDB::class_exists(class_name)) {
  1331. class_name = String("_") + class_name;
  1332. if (!ClassDB::class_exists(class_name)) {
  1333. return false;
  1334. }
  1335. }
  1336. // Skip constants since they're all integers. Type does not matter because int has no members
  1337. List<PropertyInfo> props;
  1338. ClassDB::get_property_list(class_name, &props);
  1339. for (const List<PropertyInfo>::Element *E = props.front(); E; E = E->next()) {
  1340. const PropertyInfo &prop = E->get();
  1341. // Ignore groups and categories in code completion.
  1342. if (prop.usage & (PROPERTY_USAGE_GROUP | PROPERTY_USAGE_CATEGORY)) {
  1343. continue;
  1344. }
  1345. if (prop.name == p_identifier) {
  1346. StringName getter = ClassDB::get_property_getter(class_name, p_identifier);
  1347. if (getter != StringName()) {
  1348. MethodBind *g = ClassDB::get_method(class_name, getter);
  1349. if (g) {
  1350. r_type = _type_from_property(g->get_return_info());
  1351. return true;
  1352. }
  1353. } else {
  1354. r_type = _type_from_property(prop);
  1355. return true;
  1356. }
  1357. break;
  1358. }
  1359. }
  1360. return false;
  1361. } break;
  1362. case GDScriptParser::DataType::BUILTIN: {
  1363. Variant::CallError err;
  1364. Variant tmp = Variant::construct(base_type.builtin_type, nullptr, 0, err);
  1365. if (err.error != Variant::CallError::CALL_OK) {
  1366. return false;
  1367. }
  1368. bool valid = false;
  1369. Variant res = tmp.get(p_identifier, &valid);
  1370. if (valid) {
  1371. r_type = _type_from_variant(res);
  1372. r_type.value = Variant();
  1373. r_type.type.is_constant = false;
  1374. return true;
  1375. }
  1376. return false;
  1377. } break;
  1378. default: {
  1379. return false;
  1380. } break;
  1381. }
  1382. }
  1383. return false;
  1384. }
  1385. static bool _find_last_return_in_block(const GDScriptCompletionContext &p_context, int &r_last_return_line, const GDScriptParser::Node **r_last_returned_value) {
  1386. if (!p_context.block) {
  1387. return false;
  1388. }
  1389. for (int i = 0; i < p_context.block->statements.size(); i++) {
  1390. if (p_context.block->statements[i]->line < r_last_return_line) {
  1391. continue;
  1392. }
  1393. if (p_context.block->statements[i]->type != GDScriptParser::Node::TYPE_CONTROL_FLOW) {
  1394. continue;
  1395. }
  1396. const GDScriptParser::ControlFlowNode *cf = static_cast<const GDScriptParser::ControlFlowNode *>(p_context.block->statements[i]);
  1397. if (cf->cf_type == GDScriptParser::ControlFlowNode::CF_RETURN && cf->arguments.size() > 0) {
  1398. if (cf->line > r_last_return_line) {
  1399. r_last_return_line = cf->line;
  1400. *r_last_returned_value = cf->arguments[0];
  1401. }
  1402. }
  1403. }
  1404. // Recurse into subblocks
  1405. for (int i = 0; i < p_context.block->sub_blocks.size(); i++) {
  1406. GDScriptCompletionContext c = p_context;
  1407. c.block = p_context.block->sub_blocks[i];
  1408. _find_last_return_in_block(c, r_last_return_line, r_last_returned_value);
  1409. }
  1410. return false;
  1411. }
  1412. static bool _guess_method_return_type_from_base(GDScriptCompletionContext &p_context, const GDScriptCompletionIdentifier &p_base, const StringName &p_method, GDScriptCompletionIdentifier &r_type) {
  1413. GDScriptParser::DataType base_type = p_base.type;
  1414. bool _static = base_type.is_meta_type;
  1415. if (_static && p_method == "new") {
  1416. r_type.type = base_type;
  1417. r_type.type.is_meta_type = false;
  1418. r_type.type.is_constant = false;
  1419. return true;
  1420. }
  1421. while (base_type.has_type) {
  1422. switch (base_type.kind) {
  1423. case GDScriptParser::DataType::CLASS: {
  1424. if (!base_type.class_type) {
  1425. base_type.has_type = false;
  1426. break;
  1427. }
  1428. for (int i = 0; i < base_type.class_type->static_functions.size(); i++) {
  1429. if (base_type.class_type->static_functions[i]->name == p_method) {
  1430. int last_return_line = -1;
  1431. const GDScriptParser::Node *last_returned_value = nullptr;
  1432. GDScriptCompletionContext c = p_context;
  1433. c._class = base_type.class_type;
  1434. c.function = base_type.class_type->static_functions[i];
  1435. c.block = c.function->body;
  1436. _find_last_return_in_block(c, last_return_line, &last_returned_value);
  1437. if (last_returned_value) {
  1438. c.line = c.block->end_line;
  1439. return _guess_expression_type(c, last_returned_value, r_type);
  1440. }
  1441. }
  1442. }
  1443. if (!_static) {
  1444. for (int i = 0; i < base_type.class_type->functions.size(); i++) {
  1445. if (base_type.class_type->functions[i]->name == p_method) {
  1446. int last_return_line = -1;
  1447. const GDScriptParser::Node *last_returned_value = nullptr;
  1448. GDScriptCompletionContext c = p_context;
  1449. c._class = base_type.class_type;
  1450. c.function = base_type.class_type->functions[i];
  1451. c.block = c.function->body;
  1452. _find_last_return_in_block(c, last_return_line, &last_returned_value);
  1453. if (last_returned_value) {
  1454. c.line = c.block->end_line;
  1455. return _guess_expression_type(c, last_returned_value, r_type);
  1456. }
  1457. }
  1458. }
  1459. }
  1460. base_type = base_type.class_type->base_type;
  1461. } break;
  1462. case GDScriptParser::DataType::GDSCRIPT: {
  1463. Ref<GDScript> gds = base_type.script_type;
  1464. if (gds.is_valid()) {
  1465. if (gds->get_member_functions().has(p_method)) {
  1466. r_type = _type_from_gdtype(gds->get_member_functions()[p_method]->get_return_type());
  1467. return true;
  1468. }
  1469. Ref<GDScript> base_script = gds->get_base_script();
  1470. if (base_script.is_valid()) {
  1471. base_type.script_type = base_script;
  1472. } else {
  1473. base_type.kind = GDScriptParser::DataType::NATIVE;
  1474. base_type.native_type = gds->get_instance_base_type();
  1475. }
  1476. } else {
  1477. return false;
  1478. }
  1479. } break;
  1480. case GDScriptParser::DataType::SCRIPT: {
  1481. Ref<Script> scr = base_type.script_type;
  1482. if (scr.is_valid()) {
  1483. List<MethodInfo> methods;
  1484. scr->get_script_method_list(&methods);
  1485. for (List<MethodInfo>::Element *E = methods.front(); E; E = E->next()) {
  1486. MethodInfo &mi = E->get();
  1487. if (mi.name == p_method) {
  1488. r_type = _type_from_property(mi.return_val);
  1489. return true;
  1490. }
  1491. }
  1492. Ref<Script> base_script = scr->get_base_script();
  1493. if (base_script.is_valid()) {
  1494. base_type.script_type = base_script;
  1495. } else {
  1496. base_type.kind = GDScriptParser::DataType::NATIVE;
  1497. base_type.native_type = scr->get_instance_base_type();
  1498. }
  1499. } else {
  1500. return false;
  1501. }
  1502. } break;
  1503. case GDScriptParser::DataType::NATIVE: {
  1504. StringName native = base_type.native_type;
  1505. if (!ClassDB::class_exists(native)) {
  1506. native = String("_") + native;
  1507. if (!ClassDB::class_exists(native)) {
  1508. return false;
  1509. }
  1510. }
  1511. MethodBind *mb = ClassDB::get_method(native, p_method);
  1512. if (mb) {
  1513. r_type = _type_from_property(mb->get_return_info());
  1514. return true;
  1515. }
  1516. return false;
  1517. } break;
  1518. case GDScriptParser::DataType::BUILTIN: {
  1519. Variant::CallError err;
  1520. Variant tmp = Variant::construct(base_type.builtin_type, nullptr, 0, err);
  1521. if (err.error != Variant::CallError::CALL_OK) {
  1522. return false;
  1523. }
  1524. List<MethodInfo> methods;
  1525. tmp.get_method_list(&methods);
  1526. for (List<MethodInfo>::Element *E = methods.front(); E; E = E->next()) {
  1527. MethodInfo &mi = E->get();
  1528. if (mi.name == p_method) {
  1529. r_type = _type_from_property(mi.return_val);
  1530. return true;
  1531. }
  1532. }
  1533. return false;
  1534. } break;
  1535. default: {
  1536. return false;
  1537. }
  1538. }
  1539. }
  1540. return false;
  1541. }
  1542. static String _make_arguments_hint(const MethodInfo &p_info, int p_arg_idx) {
  1543. String arghint = _get_visual_datatype(p_info.return_val, false) + " " + p_info.name + "(";
  1544. int def_args = p_info.arguments.size() - p_info.default_arguments.size();
  1545. int i = 0;
  1546. for (const List<PropertyInfo>::Element *E = p_info.arguments.front(); E; E = E->next()) {
  1547. if (i > 0) {
  1548. arghint += ", ";
  1549. }
  1550. if (i == p_arg_idx) {
  1551. arghint += String::chr(0xFFFF);
  1552. }
  1553. arghint += E->get().name + ": " + _get_visual_datatype(E->get(), true);
  1554. if (i - def_args >= 0) {
  1555. arghint += String(" = ") + p_info.default_arguments[i - def_args].get_construct_string();
  1556. }
  1557. if (i == p_arg_idx) {
  1558. arghint += String::chr(0xFFFF);
  1559. }
  1560. i++;
  1561. }
  1562. if (p_info.flags & METHOD_FLAG_VARARG) {
  1563. if (p_info.arguments.size() > 0) {
  1564. arghint += ", ";
  1565. }
  1566. if (p_arg_idx >= p_info.arguments.size()) {
  1567. arghint += String::chr(0xFFFF);
  1568. }
  1569. arghint += "...";
  1570. if (p_arg_idx >= p_info.arguments.size()) {
  1571. arghint += String::chr(0xFFFF);
  1572. }
  1573. }
  1574. arghint += ")";
  1575. return arghint;
  1576. }
  1577. static String _make_arguments_hint(const GDScriptParser::FunctionNode *p_function, int p_arg_idx) {
  1578. String arghint = p_function->return_type.to_string() + " " + p_function->name.operator String() + "(";
  1579. int def_args = p_function->arguments.size() - p_function->default_values.size();
  1580. for (int i = 0; i < p_function->arguments.size(); i++) {
  1581. if (i > 0) {
  1582. arghint += ", ";
  1583. }
  1584. if (i == p_arg_idx) {
  1585. arghint += String::chr(0xFFFF);
  1586. }
  1587. arghint += p_function->arguments[i].operator String() + ": " + p_function->argument_types[i].to_string();
  1588. if (i - def_args >= 0) {
  1589. String def_val = "<unknown>";
  1590. if (p_function->default_values[i - def_args] && p_function->default_values[i - def_args]->type == GDScriptParser::Node::TYPE_OPERATOR) {
  1591. const GDScriptParser::OperatorNode *assign = static_cast<const GDScriptParser::OperatorNode *>(p_function->default_values[i - def_args]);
  1592. if (assign->arguments.size() >= 2) {
  1593. if (assign->arguments[1]->type == GDScriptParser::Node::TYPE_CONSTANT) {
  1594. const GDScriptParser::ConstantNode *cn = static_cast<const GDScriptParser::ConstantNode *>(assign->arguments[1]);
  1595. def_val = cn->value.get_construct_string();
  1596. } else if (assign->arguments[1]->type == GDScriptParser::Node::TYPE_IDENTIFIER) {
  1597. const GDScriptParser::IdentifierNode *id = static_cast<const GDScriptParser::IdentifierNode *>(assign->arguments[1]);
  1598. def_val = id->name.operator String();
  1599. }
  1600. }
  1601. }
  1602. arghint += " = " + def_val;
  1603. }
  1604. if (i == p_arg_idx) {
  1605. arghint += String::chr(0xFFFF);
  1606. }
  1607. }
  1608. arghint += ")";
  1609. return arghint;
  1610. }
  1611. static void _find_enumeration_candidates(const String p_enum_hint, Map<String, ScriptCodeCompletionOption> &r_result) {
  1612. if (p_enum_hint.find(".") == -1) {
  1613. // Global constant
  1614. StringName current_enum = p_enum_hint;
  1615. for (int i = 0; i < GlobalConstants::get_global_constant_count(); i++) {
  1616. if (GlobalConstants::get_global_constant_enum(i) == current_enum) {
  1617. ScriptCodeCompletionOption option(GlobalConstants::get_global_constant_name(i), ScriptCodeCompletionOption::KIND_ENUM);
  1618. r_result.insert(option.display, option);
  1619. }
  1620. }
  1621. } else {
  1622. String class_name = p_enum_hint.get_slice(".", 0);
  1623. String enum_name = p_enum_hint.get_slice(".", 1);
  1624. if (!ClassDB::class_exists(class_name)) {
  1625. return;
  1626. }
  1627. List<StringName> enum_constants;
  1628. ClassDB::get_enum_constants(class_name, enum_name, &enum_constants);
  1629. for (List<StringName>::Element *E = enum_constants.front(); E; E = E->next()) {
  1630. String candidate = class_name + "." + E->get();
  1631. if (candidate[0] == '_') { // Trim leading underscore.
  1632. candidate = candidate.substr(1);
  1633. }
  1634. ScriptCodeCompletionOption option(candidate, ScriptCodeCompletionOption::KIND_ENUM);
  1635. r_result.insert(option.display, option);
  1636. }
  1637. }
  1638. }
  1639. static void _find_identifiers_in_block(const GDScriptCompletionContext &p_context, Map<String, ScriptCodeCompletionOption> &r_result) {
  1640. for (Map<StringName, GDScriptParser::LocalVarNode *>::Element *E = p_context.block->variables.front(); E; E = E->next()) {
  1641. if (E->get()->line < p_context.line) {
  1642. ScriptCodeCompletionOption option(E->key().operator String(), ScriptCodeCompletionOption::KIND_VARIABLE);
  1643. r_result.insert(option.display, option);
  1644. }
  1645. }
  1646. if (p_context.block->parent_block) {
  1647. GDScriptCompletionContext c = p_context;
  1648. c.block = p_context.block->parent_block;
  1649. _find_identifiers_in_block(c, r_result);
  1650. }
  1651. }
  1652. static void _find_identifiers_in_base(const GDScriptCompletionContext &p_context, const GDScriptCompletionIdentifier &p_base, bool p_only_functions, Map<String, ScriptCodeCompletionOption> &r_result);
  1653. static void _find_identifiers_in_class(const GDScriptCompletionContext &p_context, bool p_static, bool p_only_functions, bool p_parent_only, Map<String, ScriptCodeCompletionOption> &r_result) {
  1654. if (!p_parent_only) {
  1655. if (!p_static && !p_only_functions) {
  1656. for (int i = 0; i < p_context._class->variables.size(); i++) {
  1657. ScriptCodeCompletionOption option(p_context._class->variables[i].identifier, ScriptCodeCompletionOption::KIND_MEMBER);
  1658. r_result.insert(option.display, option);
  1659. }
  1660. }
  1661. if (!p_only_functions) {
  1662. for (Map<StringName, GDScriptParser::ClassNode::Constant>::Element *E = p_context._class->constant_expressions.front(); E; E = E->next()) {
  1663. ScriptCodeCompletionOption option(E->key(), ScriptCodeCompletionOption::KIND_CONSTANT);
  1664. if (E->get().expression && E->get().expression->type == GDScriptParser::Node::TYPE_CONSTANT) {
  1665. GDScriptParser::ConstantNode *cnode = (GDScriptParser::ConstantNode *)E->get().expression;
  1666. if (cnode) {
  1667. option.default_value = cnode->value;
  1668. }
  1669. }
  1670. r_result.insert(option.display, option);
  1671. }
  1672. for (int i = 0; i < p_context._class->subclasses.size(); i++) {
  1673. ScriptCodeCompletionOption option(p_context._class->subclasses[i]->name, ScriptCodeCompletionOption::KIND_CLASS);
  1674. r_result.insert(option.display, option);
  1675. }
  1676. }
  1677. for (int i = 0; i < p_context._class->static_functions.size(); i++) {
  1678. ScriptCodeCompletionOption option(p_context._class->static_functions[i]->name.operator String(), ScriptCodeCompletionOption::KIND_FUNCTION);
  1679. if (p_context._class->static_functions[i]->arguments.size()) {
  1680. option.insert_text += "(";
  1681. } else {
  1682. option.insert_text += "()";
  1683. }
  1684. r_result.insert(option.display, option);
  1685. }
  1686. if (!p_static) {
  1687. for (int i = 0; i < p_context._class->functions.size(); i++) {
  1688. ScriptCodeCompletionOption option(p_context._class->functions[i]->name.operator String(), ScriptCodeCompletionOption::KIND_FUNCTION);
  1689. if (p_context._class->functions[i]->arguments.size()) {
  1690. option.insert_text += "(";
  1691. } else {
  1692. option.insert_text += "()";
  1693. }
  1694. r_result.insert(option.display, option);
  1695. }
  1696. }
  1697. }
  1698. // Parents
  1699. GDScriptCompletionIdentifier base_type;
  1700. base_type.type = p_context._class->base_type;
  1701. base_type.type.is_meta_type = p_static;
  1702. base_type.value = p_context.base;
  1703. GDScriptCompletionContext c = p_context;
  1704. c.block = nullptr;
  1705. c.function = nullptr;
  1706. _find_identifiers_in_base(c, base_type, p_only_functions, r_result);
  1707. }
  1708. static void _find_identifiers_in_base(const GDScriptCompletionContext &p_context, const GDScriptCompletionIdentifier &p_base, bool p_only_functions, Map<String, ScriptCodeCompletionOption> &r_result) {
  1709. GDScriptParser::DataType base_type = p_base.type;
  1710. bool _static = base_type.is_meta_type;
  1711. if (_static && base_type.kind != GDScriptParser::DataType::BUILTIN) {
  1712. ScriptCodeCompletionOption option("new", ScriptCodeCompletionOption::KIND_FUNCTION);
  1713. option.insert_text += "(";
  1714. r_result.insert(option.display, option);
  1715. }
  1716. while (base_type.has_type) {
  1717. switch (base_type.kind) {
  1718. case GDScriptParser::DataType::CLASS: {
  1719. GDScriptCompletionContext c = p_context;
  1720. c._class = base_type.class_type;
  1721. c.block = nullptr;
  1722. c.function = nullptr;
  1723. _find_identifiers_in_class(c, _static, p_only_functions, false, r_result);
  1724. base_type = base_type.class_type->base_type;
  1725. } break;
  1726. case GDScriptParser::DataType::GDSCRIPT: {
  1727. Ref<GDScript> script = base_type.script_type;
  1728. if (script.is_valid()) {
  1729. if (!_static && !p_only_functions) {
  1730. if (p_context.base && p_context.base->get_script_instance()) {
  1731. List<PropertyInfo> members;
  1732. p_context.base->get_script_instance()->get_property_list(&members);
  1733. for (List<PropertyInfo>::Element *E = members.front(); E; E = E->next()) {
  1734. ScriptCodeCompletionOption option(E->get().name, ScriptCodeCompletionOption::KIND_MEMBER);
  1735. r_result.insert(option.display, option);
  1736. }
  1737. }
  1738. for (const Set<StringName>::Element *E = script->get_members().front(); E; E = E->next()) {
  1739. ScriptCodeCompletionOption option(E->get().operator String(), ScriptCodeCompletionOption::KIND_MEMBER);
  1740. r_result.insert(option.display, option);
  1741. }
  1742. }
  1743. if (!p_only_functions) {
  1744. for (const Map<StringName, Variant>::Element *E = script->get_constants().front(); E; E = E->next()) {
  1745. ScriptCodeCompletionOption option(E->key().operator String(), ScriptCodeCompletionOption::KIND_CONSTANT);
  1746. r_result.insert(option.display, option);
  1747. }
  1748. }
  1749. for (const Map<StringName, GDScriptFunction *>::Element *E = script->get_member_functions().front(); E; E = E->next()) {
  1750. if (!_static || E->get()->is_static()) {
  1751. ScriptCodeCompletionOption option(E->key().operator String(), ScriptCodeCompletionOption::KIND_FUNCTION);
  1752. if (E->get()->get_argument_count()) {
  1753. option.insert_text += "(";
  1754. } else {
  1755. option.insert_text += "()";
  1756. }
  1757. r_result.insert(option.display, option);
  1758. }
  1759. }
  1760. if (!p_only_functions) {
  1761. for (const Map<StringName, Ref<GDScript>>::Element *E = script->get_subclasses().front(); E; E = E->next()) {
  1762. ScriptCodeCompletionOption option(E->key().operator String(), ScriptCodeCompletionOption::KIND_CLASS);
  1763. r_result.insert(option.display, option);
  1764. }
  1765. }
  1766. base_type = GDScriptParser::DataType();
  1767. if (script->get_base().is_valid()) {
  1768. base_type.has_type = true;
  1769. base_type.kind = GDScriptParser::DataType::GDSCRIPT;
  1770. base_type.script_type = script->get_base();
  1771. } else {
  1772. base_type.has_type = script->get_instance_base_type() != StringName();
  1773. base_type.kind = GDScriptParser::DataType::NATIVE;
  1774. base_type.native_type = script->get_instance_base_type();
  1775. }
  1776. } else {
  1777. return;
  1778. }
  1779. } break;
  1780. case GDScriptParser::DataType::SCRIPT: {
  1781. Ref<Script> scr = base_type.script_type;
  1782. if (scr.is_valid()) {
  1783. if (!_static && !p_only_functions) {
  1784. List<PropertyInfo> members;
  1785. scr->get_script_property_list(&members);
  1786. for (List<PropertyInfo>::Element *E = members.front(); E; E = E->next()) {
  1787. ScriptCodeCompletionOption option(E->get().name, ScriptCodeCompletionOption::KIND_MEMBER);
  1788. r_result.insert(option.display, option);
  1789. }
  1790. }
  1791. if (!p_only_functions) {
  1792. Map<StringName, Variant> constants;
  1793. scr->get_constants(&constants);
  1794. for (Map<StringName, Variant>::Element *E = constants.front(); E; E = E->next()) {
  1795. ScriptCodeCompletionOption option(E->key().operator String(), ScriptCodeCompletionOption::KIND_CONSTANT);
  1796. r_result.insert(option.display, option);
  1797. }
  1798. }
  1799. List<MethodInfo> methods;
  1800. scr->get_script_method_list(&methods);
  1801. for (List<MethodInfo>::Element *E = methods.front(); E; E = E->next()) {
  1802. ScriptCodeCompletionOption option(E->get().name, ScriptCodeCompletionOption::KIND_FUNCTION);
  1803. if (E->get().arguments.size()) {
  1804. option.insert_text += "(";
  1805. } else {
  1806. option.insert_text += "()";
  1807. }
  1808. r_result.insert(option.display, option);
  1809. }
  1810. Ref<Script> base_script = scr->get_base_script();
  1811. if (base_script.is_valid()) {
  1812. base_type.script_type = base_script;
  1813. } else {
  1814. base_type.kind = GDScriptParser::DataType::NATIVE;
  1815. base_type.native_type = scr->get_instance_base_type();
  1816. }
  1817. } else {
  1818. return;
  1819. }
  1820. } break;
  1821. case GDScriptParser::DataType::NATIVE: {
  1822. StringName type = base_type.native_type;
  1823. if (!ClassDB::class_exists(type)) {
  1824. type = String("_") + type;
  1825. if (!ClassDB::class_exists(type)) {
  1826. return;
  1827. }
  1828. }
  1829. if (!p_only_functions) {
  1830. List<String> constants;
  1831. ClassDB::get_integer_constant_list(type, &constants);
  1832. for (List<String>::Element *E = constants.front(); E; E = E->next()) {
  1833. ScriptCodeCompletionOption option(E->get(), ScriptCodeCompletionOption::KIND_CONSTANT);
  1834. r_result.insert(option.display, option);
  1835. }
  1836. if (!_static) {
  1837. List<PropertyInfo> pinfo;
  1838. ClassDB::get_property_list(type, &pinfo);
  1839. for (List<PropertyInfo>::Element *E = pinfo.front(); E; E = E->next()) {
  1840. if (E->get().usage & (PROPERTY_USAGE_GROUP | PROPERTY_USAGE_CATEGORY)) {
  1841. continue;
  1842. }
  1843. if (E->get().name.find("/") != -1) {
  1844. continue;
  1845. }
  1846. ScriptCodeCompletionOption option(E->get().name, ScriptCodeCompletionOption::KIND_MEMBER);
  1847. r_result.insert(option.display, option);
  1848. }
  1849. }
  1850. }
  1851. if (!_static) {
  1852. List<MethodInfo> methods;
  1853. bool is_autocompleting_getters = GLOBAL_GET("debug/gdscript/completion/autocomplete_setters_and_getters").booleanize();
  1854. ClassDB::get_method_list(type, &methods, false, !is_autocompleting_getters);
  1855. for (List<MethodInfo>::Element *E = methods.front(); E; E = E->next()) {
  1856. if (E->get().name.begins_with("_")) {
  1857. continue;
  1858. }
  1859. ScriptCodeCompletionOption option(E->get().name, ScriptCodeCompletionOption::KIND_FUNCTION);
  1860. if (E->get().arguments.size()) {
  1861. option.insert_text += "(";
  1862. } else {
  1863. option.insert_text += "()";
  1864. }
  1865. r_result.insert(option.display, option);
  1866. }
  1867. }
  1868. return;
  1869. } break;
  1870. case GDScriptParser::DataType::BUILTIN: {
  1871. Variant::CallError err;
  1872. Variant tmp = Variant::construct(base_type.builtin_type, nullptr, 0, err);
  1873. if (err.error != Variant::CallError::CALL_OK) {
  1874. return;
  1875. }
  1876. if (!p_only_functions) {
  1877. List<PropertyInfo> members;
  1878. if (p_base.value.get_type() != Variant::NIL) {
  1879. p_base.value.get_property_list(&members);
  1880. } else {
  1881. tmp.get_property_list(&members);
  1882. }
  1883. for (List<PropertyInfo>::Element *E = members.front(); E; E = E->next()) {
  1884. if (String(E->get().name).find("/") == -1) {
  1885. ScriptCodeCompletionOption option(E->get().name, ScriptCodeCompletionOption::KIND_MEMBER);
  1886. r_result.insert(option.display, option);
  1887. }
  1888. }
  1889. }
  1890. List<MethodInfo> methods;
  1891. tmp.get_method_list(&methods);
  1892. for (List<MethodInfo>::Element *E = methods.front(); E; E = E->next()) {
  1893. ScriptCodeCompletionOption option(E->get().name, ScriptCodeCompletionOption::KIND_FUNCTION);
  1894. if (E->get().arguments.size()) {
  1895. option.insert_text += "(";
  1896. } else {
  1897. option.insert_text += "()";
  1898. }
  1899. r_result.insert(option.display, option);
  1900. }
  1901. return;
  1902. } break;
  1903. default: {
  1904. return;
  1905. } break;
  1906. }
  1907. }
  1908. }
  1909. static void _find_identifiers(const GDScriptCompletionContext &p_context, bool p_only_functions, Map<String, ScriptCodeCompletionOption> &r_result) {
  1910. const GDScriptParser::BlockNode *block = p_context.block;
  1911. if (p_context.function) {
  1912. const GDScriptParser::FunctionNode *f = p_context.function;
  1913. for (int i = 0; i < f->arguments.size(); i++) {
  1914. ScriptCodeCompletionOption option(f->arguments[i].operator String(), ScriptCodeCompletionOption::KIND_PLAIN_TEXT);
  1915. r_result.insert(option.display, option);
  1916. }
  1917. }
  1918. if (!p_only_functions && block) {
  1919. GDScriptCompletionContext c = p_context;
  1920. c.block = block;
  1921. _find_identifiers_in_block(c, r_result);
  1922. }
  1923. const GDScriptParser::ClassNode *clss = p_context._class;
  1924. bool _static = p_context.function && p_context.function->_static;
  1925. while (clss) {
  1926. GDScriptCompletionContext c = p_context;
  1927. c._class = clss;
  1928. c.block = nullptr;
  1929. c.function = nullptr;
  1930. _find_identifiers_in_class(c, _static, p_only_functions, false, r_result);
  1931. _static = true;
  1932. clss = clss->owner;
  1933. }
  1934. for (int i = 0; i < GDScriptFunctions::FUNC_MAX; i++) {
  1935. MethodInfo mi = GDScriptFunctions::get_info(GDScriptFunctions::Function(i));
  1936. ScriptCodeCompletionOption option(String(GDScriptFunctions::get_func_name(GDScriptFunctions::Function(i))), ScriptCodeCompletionOption::KIND_FUNCTION);
  1937. if (mi.arguments.size() || (mi.flags & METHOD_FLAG_VARARG)) {
  1938. option.insert_text += "(";
  1939. } else {
  1940. option.insert_text += "()";
  1941. }
  1942. r_result.insert(option.display, option);
  1943. }
  1944. static const char *_type_names[Variant::VARIANT_MAX] = {
  1945. "null", "bool", "int", "float", "String", "Vector2", "Rect2", "Vector3", "Transform2D", "Plane", "Quat", "AABB", "Basis", "Transform",
  1946. "Color", "NodePath", "RID", "Object", "Dictionary", "Array", "PoolByteArray", "PoolIntArray", "PoolRealArray", "PoolStringArray",
  1947. "PoolVector2Array", "PoolVector3Array", "PoolColorArray"
  1948. };
  1949. for (int i = 0; i < Variant::VARIANT_MAX; i++) {
  1950. ScriptCodeCompletionOption option(_type_names[i], ScriptCodeCompletionOption::KIND_CLASS);
  1951. r_result.insert(option.display, option);
  1952. }
  1953. static const char *_keywords[] = {
  1954. "and", "in", "not", "or", "false", "PI", "TAU", "INF", "NAN", "self", "true", "as", "assert",
  1955. "breakpoint", "class", "extends", "is", "func", "preload", "setget", "signal", "tool", "yield",
  1956. "const", "enum", "export", "onready", "static", "var", "break", "continue", "if", "elif",
  1957. "else", "for", "pass", "return", "match", "while", "remote", "sync", "master", "puppet", "slave",
  1958. "remotesync", "mastersync", "puppetsync",
  1959. nullptr
  1960. };
  1961. const char **kw = _keywords;
  1962. while (*kw) {
  1963. ScriptCodeCompletionOption option(*kw, ScriptCodeCompletionOption::KIND_PLAIN_TEXT);
  1964. r_result.insert(option.display, option);
  1965. kw++;
  1966. }
  1967. // Autoload singletons
  1968. List<PropertyInfo> props;
  1969. ProjectSettings::get_singleton()->get_property_list(&props);
  1970. for (List<PropertyInfo>::Element *E = props.front(); E; E = E->next()) {
  1971. String s = E->get().name;
  1972. if (!s.begins_with("autoload/")) {
  1973. continue;
  1974. }
  1975. String path = ProjectSettings::get_singleton()->get(s);
  1976. if (path.begins_with("*")) {
  1977. ScriptCodeCompletionOption option(s.get_slice("/", 1), ScriptCodeCompletionOption::KIND_CONSTANT);
  1978. r_result.insert(option.display, option);
  1979. }
  1980. }
  1981. // Named scripts
  1982. List<StringName> named_scripts;
  1983. ScriptServer::get_global_class_list(&named_scripts);
  1984. for (List<StringName>::Element *E = named_scripts.front(); E; E = E->next()) {
  1985. ScriptCodeCompletionOption option(E->get().operator String(), ScriptCodeCompletionOption::KIND_CLASS);
  1986. r_result.insert(option.display, option);
  1987. }
  1988. // Native classes
  1989. for (const Map<StringName, int>::Element *E = GDScriptLanguage::get_singleton()->get_global_map().front(); E; E = E->next()) {
  1990. ScriptCodeCompletionOption option(E->key().operator String(), ScriptCodeCompletionOption::KIND_CLASS);
  1991. r_result.insert(option.display, option);
  1992. }
  1993. }
  1994. static void _find_call_arguments(const GDScriptCompletionContext &p_context, const GDScriptCompletionIdentifier &p_base, const StringName &p_method, int p_argidx, bool p_static, Map<String, ScriptCodeCompletionOption> &r_result, String &r_arghint) {
  1995. Variant base = p_base.value;
  1996. GDScriptParser::DataType base_type = p_base.type;
  1997. const String quote_style = EDITOR_GET("text_editor/completion/use_single_quotes") ? "'" : "\"";
  1998. #define IS_METHOD_SIGNAL(m_method) (m_method == "connect" || m_method == "disconnect" || m_method == "is_connected" || m_method == "emit_signal")
  1999. while (base_type.has_type) {
  2000. switch (base_type.kind) {
  2001. case GDScriptParser::DataType::CLASS: {
  2002. for (int i = 0; i < base_type.class_type->static_functions.size(); i++) {
  2003. if (base_type.class_type->static_functions[i]->name == p_method) {
  2004. r_arghint = _make_arguments_hint(base_type.class_type->static_functions[i], p_argidx);
  2005. return;
  2006. }
  2007. }
  2008. for (int i = 0; i < base_type.class_type->functions.size(); i++) {
  2009. if (base_type.class_type->functions[i]->name == p_method) {
  2010. r_arghint = _make_arguments_hint(base_type.class_type->functions[i], p_argidx);
  2011. return;
  2012. }
  2013. }
  2014. if (IS_METHOD_SIGNAL(p_method) && p_argidx == 0) {
  2015. for (int i = 0; i < base_type.class_type->_signals.size(); i++) {
  2016. ScriptCodeCompletionOption option(base_type.class_type->_signals[i].name.operator String(), ScriptCodeCompletionOption::KIND_SIGNAL);
  2017. option.insert_text = quote_style + option.display + quote_style;
  2018. r_result.insert(option.display, option);
  2019. }
  2020. }
  2021. base_type = base_type.class_type->base_type;
  2022. } break;
  2023. case GDScriptParser::DataType::GDSCRIPT: {
  2024. Ref<GDScript> gds = base_type.script_type;
  2025. if (gds.is_valid()) {
  2026. if (IS_METHOD_SIGNAL(p_method) && p_argidx == 0) {
  2027. List<MethodInfo> signals;
  2028. gds->get_script_signal_list(&signals);
  2029. for (List<MethodInfo>::Element *E = signals.front(); E; E = E->next()) {
  2030. ScriptCodeCompletionOption option(E->get().name, ScriptCodeCompletionOption::KIND_SIGNAL);
  2031. option.insert_text = quote_style + option.display + quote_style;
  2032. r_result.insert(option.display, option);
  2033. }
  2034. }
  2035. Ref<GDScript> base_script = gds->get_base_script();
  2036. if (base_script.is_valid()) {
  2037. base_type.script_type = base_script;
  2038. } else {
  2039. base_type.kind = GDScriptParser::DataType::NATIVE;
  2040. base_type.native_type = gds->get_instance_base_type();
  2041. }
  2042. } else {
  2043. return;
  2044. }
  2045. } break;
  2046. case GDScriptParser::DataType::NATIVE: {
  2047. StringName class_name = base_type.native_type;
  2048. if (!ClassDB::class_exists(class_name)) {
  2049. class_name = String("_") + class_name;
  2050. if (!ClassDB::class_exists(class_name)) {
  2051. base_type.has_type = false;
  2052. break;
  2053. }
  2054. }
  2055. List<MethodInfo> methods;
  2056. ClassDB::get_method_list(class_name, &methods);
  2057. ClassDB::get_virtual_methods(class_name, &methods);
  2058. int method_args = 0;
  2059. for (List<MethodInfo>::Element *E = methods.front(); E; E = E->next()) {
  2060. if (E->get().name == p_method) {
  2061. method_args = E->get().arguments.size();
  2062. if (base.get_type() == Variant::OBJECT) {
  2063. Object *obj = base.operator Object *();
  2064. if (obj) {
  2065. List<String> options;
  2066. obj->get_argument_options(p_method, p_argidx, &options);
  2067. for (List<String>::Element *F = options.front(); F; F = F->next()) {
  2068. ScriptCodeCompletionOption option(F->get(), ScriptCodeCompletionOption::KIND_FUNCTION);
  2069. r_result.insert(option.display, option);
  2070. }
  2071. }
  2072. }
  2073. if (p_argidx < method_args) {
  2074. PropertyInfo arg_info = E->get().arguments[p_argidx];
  2075. if (arg_info.usage & PROPERTY_USAGE_CLASS_IS_ENUM) {
  2076. _find_enumeration_candidates(arg_info.class_name, r_result);
  2077. }
  2078. }
  2079. r_arghint = _make_arguments_hint(E->get(), p_argidx);
  2080. break;
  2081. }
  2082. }
  2083. if (IS_METHOD_SIGNAL(p_method) && p_argidx == 0) {
  2084. List<MethodInfo> signals;
  2085. ClassDB::get_signal_list(class_name, &signals);
  2086. for (List<MethodInfo>::Element *E = signals.front(); E; E = E->next()) {
  2087. ScriptCodeCompletionOption option(E->get().name, ScriptCodeCompletionOption::KIND_SIGNAL);
  2088. option.insert_text = quote_style + option.display + quote_style;
  2089. r_result.insert(option.display, option);
  2090. }
  2091. }
  2092. #undef IS_METHOD_SIGNAL
  2093. if (ClassDB::is_parent_class(class_name, "Node") && (p_method == "get_node" || p_method == "has_node") && p_argidx == 0) {
  2094. // Get autoloads
  2095. List<PropertyInfo> props;
  2096. ProjectSettings::get_singleton()->get_property_list(&props);
  2097. for (List<PropertyInfo>::Element *E = props.front(); E; E = E->next()) {
  2098. String s = E->get().name;
  2099. if (!s.begins_with("autoload/")) {
  2100. continue;
  2101. }
  2102. String name = s.get_slice("/", 1);
  2103. ScriptCodeCompletionOption option("/root/" + name, ScriptCodeCompletionOption::KIND_NODE_PATH);
  2104. option.insert_text = quote_style + option.display + quote_style;
  2105. r_result.insert(option.display, option);
  2106. }
  2107. }
  2108. if (p_argidx == 0 && method_args > 0 && ClassDB::is_parent_class(class_name, "InputEvent") && p_method.operator String().find("action") != -1) {
  2109. // Get input actions
  2110. List<PropertyInfo> props;
  2111. ProjectSettings::get_singleton()->get_property_list(&props);
  2112. for (List<PropertyInfo>::Element *E = props.front(); E; E = E->next()) {
  2113. String s = E->get().name;
  2114. if (!s.begins_with("input/")) {
  2115. continue;
  2116. }
  2117. String name = s.get_slice("/", 1);
  2118. ScriptCodeCompletionOption option(name, ScriptCodeCompletionOption::KIND_CONSTANT);
  2119. option.insert_text = quote_style + option.display + quote_style;
  2120. r_result.insert(option.display, option);
  2121. }
  2122. }
  2123. base_type.has_type = false;
  2124. } break;
  2125. case GDScriptParser::DataType::BUILTIN: {
  2126. if (base.get_type() == Variant::NIL) {
  2127. Variant::CallError err;
  2128. base = Variant::construct(base_type.builtin_type, nullptr, 0, err);
  2129. if (err.error != Variant::CallError::CALL_OK) {
  2130. return;
  2131. }
  2132. }
  2133. List<MethodInfo> methods;
  2134. base.get_method_list(&methods);
  2135. for (List<MethodInfo>::Element *E = methods.front(); E; E = E->next()) {
  2136. if (E->get().name == p_method) {
  2137. r_arghint = _make_arguments_hint(E->get(), p_argidx);
  2138. return;
  2139. }
  2140. }
  2141. base_type.has_type = false;
  2142. } break;
  2143. default: {
  2144. base_type.has_type = false;
  2145. } break;
  2146. }
  2147. }
  2148. }
  2149. static void _find_call_arguments(GDScriptCompletionContext &p_context, const GDScriptParser::Node *p_node, int p_argidx, Map<String, ScriptCodeCompletionOption> &r_result, bool &r_forced, String &r_arghint) {
  2150. const String quote_style = EDITOR_GET("text_editor/completion/use_single_quotes") ? "'" : "\"";
  2151. if (!p_node || p_node->type != GDScriptParser::Node::TYPE_OPERATOR) {
  2152. return;
  2153. }
  2154. Variant base;
  2155. GDScriptParser::DataType base_type;
  2156. StringName function;
  2157. bool _static = false;
  2158. const GDScriptParser::OperatorNode *op = static_cast<const GDScriptParser::OperatorNode *>(p_node);
  2159. GDScriptCompletionIdentifier connect_base;
  2160. if (op->op != GDScriptParser::OperatorNode::OP_CALL && op->op != GDScriptParser::OperatorNode::OP_PARENT_CALL) {
  2161. return;
  2162. }
  2163. if (!op->arguments.size()) {
  2164. return;
  2165. }
  2166. if (op->op == GDScriptParser::OperatorNode::OP_CALL) {
  2167. if (op->arguments[0]->type == GDScriptParser::Node::TYPE_BUILT_IN_FUNCTION) {
  2168. // Complete built-in function
  2169. const GDScriptParser::BuiltInFunctionNode *fn = static_cast<const GDScriptParser::BuiltInFunctionNode *>(op->arguments[0]);
  2170. MethodInfo mi = GDScriptFunctions::get_info(fn->function);
  2171. if ((mi.name == "load" || mi.name == "preload") && bool(EditorSettings::get_singleton()->get("text_editor/completion/complete_file_paths"))) {
  2172. _get_directory_contents(EditorFileSystem::get_singleton()->get_filesystem(), r_result);
  2173. }
  2174. r_arghint = _make_arguments_hint(mi, p_argidx);
  2175. return;
  2176. } else if (op->arguments[0]->type == GDScriptParser::Node::TYPE_TYPE) {
  2177. // Complete constructor
  2178. const GDScriptParser::TypeNode *tn = static_cast<const GDScriptParser::TypeNode *>(op->arguments[0]);
  2179. List<MethodInfo> constructors;
  2180. Variant::get_constructor_list(tn->vtype, &constructors);
  2181. int i = 0;
  2182. for (List<MethodInfo>::Element *E = constructors.front(); E; E = E->next()) {
  2183. if (p_argidx >= E->get().arguments.size()) {
  2184. continue;
  2185. }
  2186. if (i > 0) {
  2187. r_arghint += "\n";
  2188. }
  2189. r_arghint += _make_arguments_hint(E->get(), p_argidx);
  2190. i++;
  2191. }
  2192. return;
  2193. } else if (op->arguments[0]->type == GDScriptParser::Node::TYPE_SELF) {
  2194. if (op->arguments.size() < 2 || op->arguments[1]->type != GDScriptParser::Node::TYPE_IDENTIFIER) {
  2195. return;
  2196. }
  2197. base = p_context.base;
  2198. const GDScriptParser::IdentifierNode *id = static_cast<const GDScriptParser::IdentifierNode *>(op->arguments[1]);
  2199. function = id->name;
  2200. base_type.has_type = true;
  2201. base_type.kind = GDScriptParser::DataType::CLASS;
  2202. base_type.class_type = const_cast<GDScriptParser::ClassNode *>(p_context._class);
  2203. _static = p_context.function && p_context.function->_static;
  2204. if (function == "connect" && op->arguments.size() >= 4) {
  2205. _guess_expression_type(p_context, op->arguments[3], connect_base);
  2206. }
  2207. } else {
  2208. if (op->arguments.size() < 2 || op->arguments[1]->type != GDScriptParser::Node::TYPE_IDENTIFIER) {
  2209. return;
  2210. }
  2211. const GDScriptParser::IdentifierNode *id = static_cast<const GDScriptParser::IdentifierNode *>(op->arguments[1]);
  2212. function = id->name;
  2213. GDScriptCompletionIdentifier ci;
  2214. if (_guess_expression_type(p_context, op->arguments[0], ci)) {
  2215. base_type = ci.type;
  2216. base = ci.value;
  2217. } else {
  2218. return;
  2219. }
  2220. _static = ci.type.is_meta_type;
  2221. if (function == "connect" && op->arguments.size() >= 4) {
  2222. _guess_expression_type(p_context, op->arguments[3], connect_base);
  2223. }
  2224. }
  2225. } else {
  2226. if (!p_context._class || op->arguments.size() < 1 || op->arguments[0]->type != GDScriptParser::Node::TYPE_IDENTIFIER) {
  2227. return;
  2228. }
  2229. base_type.has_type = true;
  2230. base_type.kind = GDScriptParser::DataType::CLASS;
  2231. base_type.class_type = const_cast<GDScriptParser::ClassNode *>(p_context._class);
  2232. base_type.is_meta_type = p_context.function && p_context.function->_static;
  2233. base = p_context.base;
  2234. function = static_cast<const GDScriptParser::IdentifierNode *>(op->arguments[0])->name;
  2235. if (function == "connect" && op->arguments.size() >= 4) {
  2236. _guess_expression_type(p_context, op->arguments[3], connect_base);
  2237. }
  2238. }
  2239. GDScriptCompletionIdentifier ci;
  2240. ci.type = base_type;
  2241. ci.value = base;
  2242. _find_call_arguments(p_context, ci, function, p_argidx, _static, r_result, r_arghint);
  2243. if (function == "connect" && p_argidx == 2) {
  2244. Map<String, ScriptCodeCompletionOption> methods;
  2245. _find_identifiers_in_base(p_context, connect_base, true, methods);
  2246. for (Map<String, ScriptCodeCompletionOption>::Element *E = methods.front(); E; E = E->next()) {
  2247. ScriptCodeCompletionOption &option = E->value();
  2248. option.insert_text = quote_style + option.display + quote_style;
  2249. r_result.insert(option.display, option);
  2250. }
  2251. }
  2252. r_forced = r_result.size() > 0;
  2253. }
  2254. 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) {
  2255. const String quote_style = EDITOR_GET("text_editor/completion/use_single_quotes") ? "'" : "\"";
  2256. GDScriptParser parser;
  2257. parser.parse(p_code, p_path.get_base_dir(), false, p_path, true);
  2258. r_forced = false;
  2259. Map<String, ScriptCodeCompletionOption> options;
  2260. GDScriptCompletionContext context;
  2261. context._class = parser.get_completion_class();
  2262. context.block = parser.get_completion_block();
  2263. context.function = parser.get_completion_function();
  2264. context.line = parser.get_completion_line();
  2265. if (!context._class || context._class->owner == nullptr) {
  2266. context.base = p_owner;
  2267. context.base_path = p_path.get_base_dir();
  2268. }
  2269. bool is_function = false;
  2270. switch (parser.get_completion_type()) {
  2271. case GDScriptParser::COMPLETION_NONE: {
  2272. } break;
  2273. case GDScriptParser::COMPLETION_BUILT_IN_TYPE_CONSTANT: {
  2274. List<StringName> constants;
  2275. Variant::get_constants_for_type(parser.get_completion_built_in_constant(), &constants);
  2276. for (List<StringName>::Element *E = constants.front(); E; E = E->next()) {
  2277. ScriptCodeCompletionOption option(E->get().operator String(), ScriptCodeCompletionOption::KIND_CONSTANT);
  2278. bool valid = false;
  2279. Variant default_value = Variant::get_constant_value(parser.get_completion_built_in_constant(), E->get(), &valid);
  2280. if (valid) {
  2281. option.default_value = default_value;
  2282. }
  2283. options.insert(option.display, option);
  2284. }
  2285. } break;
  2286. case GDScriptParser::COMPLETION_PARENT_FUNCTION: {
  2287. _find_identifiers_in_class(context, !context.function || context.function->_static, true, true, options);
  2288. } break;
  2289. case GDScriptParser::COMPLETION_FUNCTION: {
  2290. is_function = true;
  2291. FALLTHROUGH;
  2292. }
  2293. case GDScriptParser::COMPLETION_IDENTIFIER: {
  2294. _find_identifiers(context, is_function, options);
  2295. } break;
  2296. case GDScriptParser::COMPLETION_EXTENDS: {
  2297. // Native classes.
  2298. List<StringName> class_list;
  2299. ClassDB::get_class_list(&class_list);
  2300. for (int i = 0; i < class_list.size(); i++) {
  2301. ScriptCodeCompletionOption option(class_list[i], ScriptCodeCompletionOption::KIND_CLASS);
  2302. options.insert(option.display, option);
  2303. }
  2304. // GDScript classes.
  2305. if (EditorSettings::get_singleton()->get("text_editor/completion/complete_file_paths")) {
  2306. for (int i = 0; i < ScriptServer::get_language_count(); i++) {
  2307. if (ScriptServer::get_language(i)->get_name() == "GDScript") {
  2308. List<String> extensions;
  2309. ScriptServer::get_language(i)->get_recognized_extensions(&extensions);
  2310. for (List<String>::Element *E = extensions.front(); E; E = E->next()) {
  2311. _get_directory_contents(EditorFileSystem::get_singleton()->get_filesystem(), options, String("." + E->get()));
  2312. }
  2313. }
  2314. }
  2315. r_forced = true;
  2316. }
  2317. // Named Scripts.
  2318. List<StringName> named_scripts;
  2319. ScriptServer::get_global_class_list(&named_scripts);
  2320. for (List<StringName>::Element *E = named_scripts.front(); E; E = E->next()) {
  2321. ScriptCodeCompletionOption option(E->get().operator String(), ScriptCodeCompletionOption::KIND_CLASS);
  2322. options.insert(option.display, option);
  2323. }
  2324. } break;
  2325. case GDScriptParser::COMPLETION_GET_NODE: {
  2326. if (p_owner) {
  2327. List<String> opts;
  2328. p_owner->get_argument_options("get_node", 0, &opts);
  2329. for (List<String>::Element *E = opts.front(); E; E = E->next()) {
  2330. String opt = E->get().strip_edges();
  2331. if (opt.is_quoted()) {
  2332. r_forced = true;
  2333. String idopt = opt.unquote();
  2334. if (idopt.replace("/", "_").is_valid_identifier()) {
  2335. ScriptCodeCompletionOption option(idopt, ScriptCodeCompletionOption::KIND_NODE_PATH);
  2336. options.insert(option.display, option);
  2337. } else {
  2338. ScriptCodeCompletionOption option(opt, ScriptCodeCompletionOption::KIND_NODE_PATH);
  2339. options.insert(option.display, option);
  2340. }
  2341. }
  2342. }
  2343. // Get autoloads
  2344. List<PropertyInfo> props;
  2345. ProjectSettings::get_singleton()->get_property_list(&props);
  2346. for (List<PropertyInfo>::Element *E = props.front(); E; E = E->next()) {
  2347. String s = E->get().name;
  2348. if (!s.begins_with("autoload/")) {
  2349. continue;
  2350. }
  2351. String name = s.get_slice("/", 1);
  2352. ScriptCodeCompletionOption option(quote_style + "/root/" + name + quote_style, ScriptCodeCompletionOption::KIND_NODE_PATH);
  2353. options.insert(option.display, option);
  2354. }
  2355. }
  2356. } break;
  2357. case GDScriptParser::COMPLETION_METHOD: {
  2358. is_function = true;
  2359. FALLTHROUGH;
  2360. }
  2361. case GDScriptParser::COMPLETION_INDEX: {
  2362. const GDScriptParser::Node *node = parser.get_completion_node();
  2363. if (node->type != GDScriptParser::Node::TYPE_OPERATOR) {
  2364. break;
  2365. }
  2366. const GDScriptParser::OperatorNode *op = static_cast<const GDScriptParser::OperatorNode *>(node);
  2367. if (op->arguments.size() < 1) {
  2368. break;
  2369. }
  2370. GDScriptCompletionIdentifier base;
  2371. if (!_guess_expression_type(context, op->arguments[0], base)) {
  2372. break;
  2373. }
  2374. GDScriptCompletionContext c = context;
  2375. c.function = nullptr;
  2376. c.block = nullptr;
  2377. c.base = base.value.get_type() == Variant::OBJECT ? base.value.operator Object *() : nullptr;
  2378. if (base.type.kind == GDScriptParser::DataType::CLASS) {
  2379. c._class = base.type.class_type;
  2380. } else {
  2381. c._class = nullptr;
  2382. }
  2383. _find_identifiers_in_base(c, base, is_function, options);
  2384. } break;
  2385. case GDScriptParser::COMPLETION_CALL_ARGUMENTS: {
  2386. _find_call_arguments(context, parser.get_completion_node(), parser.get_completion_argument_index(), options, r_forced, r_call_hint);
  2387. } break;
  2388. case GDScriptParser::COMPLETION_VIRTUAL_FUNC: {
  2389. GDScriptParser::DataType native_type = context._class->base_type;
  2390. while (native_type.has_type && native_type.kind != GDScriptParser::DataType::NATIVE) {
  2391. switch (native_type.kind) {
  2392. case GDScriptParser::DataType::CLASS: {
  2393. native_type = native_type.class_type->base_type;
  2394. } break;
  2395. case GDScriptParser::DataType::GDSCRIPT: {
  2396. Ref<GDScript> gds = native_type.script_type;
  2397. if (gds.is_valid()) {
  2398. Ref<GDScript> base = gds->get_base_script();
  2399. if (base.is_valid()) {
  2400. native_type.script_type = base;
  2401. } else {
  2402. native_type.native_type = gds->get_instance_base_type();
  2403. native_type.kind = GDScriptParser::DataType::NATIVE;
  2404. }
  2405. } else {
  2406. native_type.has_type = false;
  2407. }
  2408. } break;
  2409. default: {
  2410. native_type.has_type = false;
  2411. } break;
  2412. }
  2413. }
  2414. if (!native_type.has_type) {
  2415. break;
  2416. }
  2417. StringName class_name = native_type.native_type;
  2418. if (!ClassDB::class_exists(class_name)) {
  2419. class_name = String("_") + class_name;
  2420. if (!ClassDB::class_exists(class_name)) {
  2421. break;
  2422. }
  2423. }
  2424. bool use_type_hint = EditorSettings::get_singleton()->get_setting("text_editor/completion/add_type_hints").operator bool();
  2425. List<MethodInfo> virtual_methods;
  2426. ClassDB::get_virtual_methods(class_name, &virtual_methods);
  2427. for (List<MethodInfo>::Element *E = virtual_methods.front(); E; E = E->next()) {
  2428. MethodInfo &mi = E->get();
  2429. String method_hint = mi.name;
  2430. if (method_hint.find(":") != -1) {
  2431. method_hint = method_hint.get_slice(":", 0);
  2432. }
  2433. method_hint += "(";
  2434. if (mi.arguments.size()) {
  2435. for (int i = 0; i < mi.arguments.size(); i++) {
  2436. if (i > 0) {
  2437. method_hint += ", ";
  2438. }
  2439. String arg = mi.arguments[i].name;
  2440. if (arg.find(":") != -1) {
  2441. arg = arg.substr(0, arg.find(":"));
  2442. }
  2443. method_hint += arg;
  2444. if (use_type_hint && mi.arguments[i].type != Variant::NIL) {
  2445. method_hint += ": ";
  2446. if (mi.arguments[i].type == Variant::OBJECT && mi.arguments[i].class_name != StringName()) {
  2447. method_hint += mi.arguments[i].class_name.operator String();
  2448. } else {
  2449. method_hint += Variant::get_type_name(mi.arguments[i].type);
  2450. }
  2451. }
  2452. }
  2453. }
  2454. method_hint += ")";
  2455. if (use_type_hint && (mi.return_val.type != Variant::NIL || !(mi.return_val.usage & PROPERTY_USAGE_NIL_IS_VARIANT))) {
  2456. method_hint += " -> ";
  2457. if (mi.return_val.type == Variant::NIL) {
  2458. method_hint += "void";
  2459. } else if (mi.return_val.type == Variant::OBJECT && mi.return_val.class_name != StringName()) {
  2460. method_hint += mi.return_val.class_name.operator String();
  2461. } else {
  2462. method_hint += Variant::get_type_name(mi.return_val.type);
  2463. }
  2464. }
  2465. method_hint += ":";
  2466. ScriptCodeCompletionOption option(method_hint, ScriptCodeCompletionOption::KIND_FUNCTION);
  2467. options.insert(option.display, option);
  2468. }
  2469. } break;
  2470. case GDScriptParser::COMPLETION_YIELD: {
  2471. const GDScriptParser::Node *node = parser.get_completion_node();
  2472. GDScriptCompletionContext c = context;
  2473. c.line = node->line;
  2474. GDScriptCompletionIdentifier type;
  2475. if (!_guess_expression_type(c, node, type)) {
  2476. break;
  2477. }
  2478. GDScriptParser::DataType base_type = type.type;
  2479. while (base_type.has_type) {
  2480. switch (base_type.kind) {
  2481. case GDScriptParser::DataType::CLASS: {
  2482. for (int i = 0; i < base_type.class_type->_signals.size(); i++) {
  2483. ScriptCodeCompletionOption option(base_type.class_type->_signals[i].name.operator String(), ScriptCodeCompletionOption::KIND_SIGNAL);
  2484. option.insert_text = quote_style + option.display + quote_style;
  2485. options.insert(option.display, option);
  2486. }
  2487. base_type = base_type.class_type->base_type;
  2488. } break;
  2489. case GDScriptParser::DataType::SCRIPT:
  2490. case GDScriptParser::DataType::GDSCRIPT: {
  2491. Ref<Script> scr = base_type.script_type;
  2492. if (scr.is_valid()) {
  2493. List<MethodInfo> signals;
  2494. scr->get_script_signal_list(&signals);
  2495. for (List<MethodInfo>::Element *E = signals.front(); E; E = E->next()) {
  2496. ScriptCodeCompletionOption option(quote_style + E->get().name + quote_style, ScriptCodeCompletionOption::KIND_SIGNAL);
  2497. options.insert(option.display, option);
  2498. }
  2499. Ref<Script> base_script = scr->get_base_script();
  2500. if (base_script.is_valid()) {
  2501. base_type.script_type = base_script;
  2502. } else {
  2503. base_type.kind = GDScriptParser::DataType::NATIVE;
  2504. base_type.native_type = scr->get_instance_base_type();
  2505. }
  2506. } else {
  2507. base_type.has_type = false;
  2508. }
  2509. } break;
  2510. case GDScriptParser::DataType::NATIVE: {
  2511. base_type.has_type = false;
  2512. StringName class_name = base_type.native_type;
  2513. if (!ClassDB::class_exists(class_name)) {
  2514. class_name = String("_") + class_name;
  2515. if (!ClassDB::class_exists(class_name)) {
  2516. break;
  2517. }
  2518. }
  2519. List<MethodInfo> signals;
  2520. ClassDB::get_signal_list(class_name, &signals);
  2521. for (List<MethodInfo>::Element *E = signals.front(); E; E = E->next()) {
  2522. ScriptCodeCompletionOption option(quote_style + E->get().name + quote_style, ScriptCodeCompletionOption::KIND_SIGNAL);
  2523. options.insert(option.display, option);
  2524. }
  2525. } break;
  2526. default: {
  2527. base_type.has_type = false;
  2528. }
  2529. }
  2530. }
  2531. } break;
  2532. case GDScriptParser::COMPLETION_RESOURCE_PATH: {
  2533. if (EditorSettings::get_singleton()->get("text_editor/completion/complete_file_paths")) {
  2534. _get_directory_contents(EditorFileSystem::get_singleton()->get_filesystem(), options);
  2535. r_forced = true;
  2536. }
  2537. } break;
  2538. case GDScriptParser::COMPLETION_ASSIGN: {
  2539. GDScriptCompletionIdentifier type;
  2540. if (!_guess_expression_type(context, parser.get_completion_node(), type)) {
  2541. break;
  2542. }
  2543. if (!type.enumeration.empty()) {
  2544. _find_enumeration_candidates(type.enumeration, options);
  2545. r_forced = options.size() > 0;
  2546. }
  2547. } break;
  2548. case GDScriptParser::COMPLETION_TYPE_HINT: {
  2549. const GDScriptParser::ClassNode *clss = context._class;
  2550. while (clss) {
  2551. for (Map<StringName, GDScriptParser::ClassNode::Constant>::Element *E = clss->constant_expressions.front(); E; E = E->next()) {
  2552. GDScriptCompletionIdentifier constant;
  2553. GDScriptCompletionContext c = context;
  2554. c.function = nullptr;
  2555. c.block = nullptr;
  2556. c.line = E->value().expression->line;
  2557. if (_guess_expression_type(c, E->value().expression, constant)) {
  2558. if (constant.type.has_type && constant.type.is_meta_type) {
  2559. ScriptCodeCompletionOption option(E->key().operator String(), ScriptCodeCompletionOption::KIND_CLASS);
  2560. options.insert(option.display, option);
  2561. }
  2562. }
  2563. }
  2564. for (int i = 0; i < clss->subclasses.size(); i++) {
  2565. if (clss->subclasses[i]->name != StringName()) {
  2566. ScriptCodeCompletionOption option(clss->subclasses[i]->name.operator String(), ScriptCodeCompletionOption::KIND_CLASS);
  2567. options.insert(option.display, option);
  2568. }
  2569. }
  2570. clss = clss->owner;
  2571. for (int i = 0; i < Variant::VARIANT_MAX; i++) {
  2572. ScriptCodeCompletionOption option(Variant::get_type_name((Variant::Type)i), ScriptCodeCompletionOption::KIND_CLASS);
  2573. options.insert(option.display, option);
  2574. }
  2575. List<PropertyInfo> props;
  2576. ProjectSettings::get_singleton()->get_property_list(&props);
  2577. for (List<PropertyInfo>::Element *E = props.front(); E; E = E->next()) {
  2578. String s = E->get().name;
  2579. if (!s.begins_with("autoload/")) {
  2580. continue;
  2581. }
  2582. ScriptCodeCompletionOption option(s.get_slice("/", 1), ScriptCodeCompletionOption::KIND_CLASS);
  2583. options.insert(option.display, option);
  2584. }
  2585. }
  2586. List<StringName> native_classes;
  2587. ClassDB::get_class_list(&native_classes);
  2588. for (List<StringName>::Element *E = native_classes.front(); E; E = E->next()) {
  2589. String class_name = E->get().operator String();
  2590. if (class_name.begins_with("_")) {
  2591. class_name = class_name.right(1);
  2592. }
  2593. if (Engine::get_singleton()->has_singleton(class_name)) {
  2594. continue;
  2595. }
  2596. ScriptCodeCompletionOption option(class_name, ScriptCodeCompletionOption::KIND_CLASS);
  2597. options.insert(option.display, option);
  2598. }
  2599. // Named scripts
  2600. List<StringName> named_scripts;
  2601. ScriptServer::get_global_class_list(&named_scripts);
  2602. for (List<StringName>::Element *E = named_scripts.front(); E; E = E->next()) {
  2603. ScriptCodeCompletionOption option(E->get().operator String(), ScriptCodeCompletionOption::KIND_CLASS);
  2604. options.insert(option.display, option);
  2605. }
  2606. if (parser.get_completion_identifier_is_function()) {
  2607. ScriptCodeCompletionOption option("void", ScriptCodeCompletionOption::KIND_PLAIN_TEXT);
  2608. options.insert(option.display, option);
  2609. }
  2610. r_forced = true;
  2611. } break;
  2612. case GDScriptParser::COMPLETION_TYPE_HINT_INDEX: {
  2613. GDScriptCompletionIdentifier base;
  2614. String index = parser.get_completion_cursor().operator String();
  2615. if (!_guess_identifier_type(context, index.get_slice(".", 0), base)) {
  2616. break;
  2617. }
  2618. GDScriptCompletionContext c = context;
  2619. c._class = nullptr;
  2620. c.function = nullptr;
  2621. c.block = nullptr;
  2622. bool finding = true;
  2623. index = index.right(index.find(".") + 1);
  2624. while (index.find(".") != -1) {
  2625. String id = index.get_slice(".", 0);
  2626. GDScriptCompletionIdentifier sub_base;
  2627. if (!_guess_identifier_type_from_base(c, base, id, sub_base)) {
  2628. finding = false;
  2629. break;
  2630. }
  2631. index = index.right(index.find(".") + 1);
  2632. base = sub_base;
  2633. }
  2634. if (!finding) {
  2635. break;
  2636. }
  2637. GDScriptParser::DataType base_type = base.type;
  2638. while (base_type.has_type) {
  2639. switch (base_type.kind) {
  2640. case GDScriptParser::DataType::CLASS: {
  2641. if (base_type.class_type) {
  2642. for (Map<StringName, GDScriptParser::ClassNode::Constant>::Element *E = base_type.class_type->constant_expressions.front(); E; E = E->next()) {
  2643. GDScriptCompletionIdentifier constant;
  2644. GDScriptCompletionContext c2 = context;
  2645. c2._class = base_type.class_type;
  2646. c2.function = nullptr;
  2647. c2.block = nullptr;
  2648. c2.line = E->value().expression->line;
  2649. if (_guess_expression_type(c2, E->value().expression, constant)) {
  2650. if (constant.type.has_type && constant.type.is_meta_type) {
  2651. ScriptCodeCompletionOption option(E->key().operator String(), ScriptCodeCompletionOption::KIND_CLASS);
  2652. options.insert(option.display, option);
  2653. }
  2654. }
  2655. }
  2656. for (int i = 0; i < base_type.class_type->subclasses.size(); i++) {
  2657. if (base_type.class_type->subclasses[i]->name != StringName()) {
  2658. ScriptCodeCompletionOption option(base_type.class_type->subclasses[i]->name.operator String(), ScriptCodeCompletionOption::KIND_CLASS);
  2659. options.insert(option.display, option);
  2660. }
  2661. }
  2662. base_type = base_type.class_type->base_type;
  2663. } else {
  2664. base_type.has_type = false;
  2665. }
  2666. } break;
  2667. case GDScriptParser::DataType::GDSCRIPT: {
  2668. Ref<GDScript> scr = base_type.script_type;
  2669. if (scr.is_valid()) {
  2670. for (const Map<StringName, Ref<GDScript>>::Element *E = scr->get_subclasses().front(); E; E = E->next()) {
  2671. ScriptCodeCompletionOption option(E->key().operator String(), ScriptCodeCompletionOption::KIND_CLASS);
  2672. options.insert(option.display, option);
  2673. }
  2674. }
  2675. FALLTHROUGH;
  2676. }
  2677. case GDScriptParser::DataType::SCRIPT: {
  2678. Ref<Script> scr = base_type.script_type;
  2679. if (scr.is_valid()) {
  2680. Map<StringName, Variant> constants;
  2681. scr->get_constants(&constants);
  2682. for (Map<StringName, Variant>::Element *E = constants.front(); E; E = E->next()) {
  2683. Ref<Script> const_scr = E->value();
  2684. if (const_scr.is_valid()) {
  2685. ScriptCodeCompletionOption option(E->key().operator String(), ScriptCodeCompletionOption::KIND_CLASS);
  2686. options.insert(option.display, option);
  2687. }
  2688. }
  2689. Ref<Script> base_script = scr->get_base_script();
  2690. if (base_script.is_valid()) {
  2691. base_type.script_type = base_script;
  2692. } else {
  2693. base_type.has_type = false;
  2694. }
  2695. } else {
  2696. base_type.has_type = false;
  2697. }
  2698. } break;
  2699. default: {
  2700. base_type.has_type = false;
  2701. } break;
  2702. }
  2703. }
  2704. r_forced = options.size() > 0;
  2705. } break;
  2706. }
  2707. for (Map<String, ScriptCodeCompletionOption>::Element *E = options.front(); E; E = E->next()) {
  2708. r_options->push_back(E->get());
  2709. }
  2710. return OK;
  2711. }
  2712. #else
  2713. 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) {
  2714. return OK;
  2715. }
  2716. #endif
  2717. //////// END COMPLETION //////////
  2718. String GDScriptLanguage::_get_indentation() const {
  2719. #ifdef TOOLS_ENABLED
  2720. if (Engine::get_singleton()->is_editor_hint()) {
  2721. bool use_space_indentation = EDITOR_GET("text_editor/indent/type");
  2722. if (use_space_indentation) {
  2723. int indent_size = EDITOR_GET("text_editor/indent/size");
  2724. String space_indent = "";
  2725. for (int i = 0; i < indent_size; i++) {
  2726. space_indent += " ";
  2727. }
  2728. return space_indent;
  2729. }
  2730. }
  2731. #endif
  2732. return "\t";
  2733. }
  2734. void GDScriptLanguage::auto_indent_code(String &p_code, int p_from_line, int p_to_line) const {
  2735. String indent = _get_indentation();
  2736. Vector<String> lines = p_code.split("\n");
  2737. List<int> indent_stack;
  2738. for (int i = 0; i < lines.size(); i++) {
  2739. String l = lines[i];
  2740. int tc = 0;
  2741. for (int j = 0; j < l.length(); j++) {
  2742. if (l[j] == ' ' || l[j] == '\t') {
  2743. tc++;
  2744. } else {
  2745. break;
  2746. }
  2747. }
  2748. String st = l.substr(tc, l.length()).strip_edges();
  2749. if (st == "" || st.begins_with("#")) {
  2750. continue; //ignore!
  2751. }
  2752. int ilevel = 0;
  2753. if (indent_stack.size()) {
  2754. ilevel = indent_stack.back()->get();
  2755. }
  2756. if (tc > ilevel) {
  2757. indent_stack.push_back(tc);
  2758. } else if (tc < ilevel) {
  2759. while (indent_stack.size() && indent_stack.back()->get() > tc) {
  2760. indent_stack.pop_back();
  2761. }
  2762. if (indent_stack.size() && indent_stack.back()->get() != tc) {
  2763. indent_stack.push_back(tc); //this is not right but gets the job done
  2764. }
  2765. }
  2766. if (i >= p_from_line) {
  2767. l = "";
  2768. for (int j = 0; j < indent_stack.size(); j++) {
  2769. l += indent;
  2770. }
  2771. l += st;
  2772. } else if (i > p_to_line) {
  2773. break;
  2774. }
  2775. lines.write[i] = l;
  2776. }
  2777. p_code = "";
  2778. for (int i = 0; i < lines.size(); i++) {
  2779. if (i > 0) {
  2780. p_code += "\n";
  2781. }
  2782. p_code += lines[i];
  2783. }
  2784. }
  2785. #ifdef TOOLS_ENABLED
  2786. static Error _lookup_symbol_from_base(const GDScriptParser::DataType &p_base, const String &p_symbol, bool p_is_function, GDScriptLanguage::LookupResult &r_result) {
  2787. GDScriptParser::DataType base_type = p_base;
  2788. while (base_type.has_type) {
  2789. switch (base_type.kind) {
  2790. case GDScriptParser::DataType::CLASS: {
  2791. if (base_type.class_type) {
  2792. if (p_is_function) {
  2793. for (int i = 0; i < base_type.class_type->functions.size(); i++) {
  2794. if (base_type.class_type->functions[i]->name == p_symbol) {
  2795. r_result.type = ScriptLanguage::LookupResult::RESULT_SCRIPT_LOCATION;
  2796. r_result.location = base_type.class_type->functions[i]->line;
  2797. return OK;
  2798. }
  2799. }
  2800. for (int i = 0; i < base_type.class_type->static_functions.size(); i++) {
  2801. if (base_type.class_type->static_functions[i]->name == p_symbol) {
  2802. r_result.type = ScriptLanguage::LookupResult::RESULT_SCRIPT_LOCATION;
  2803. r_result.location = base_type.class_type->static_functions[i]->line;
  2804. return OK;
  2805. }
  2806. }
  2807. } else {
  2808. if (base_type.class_type->constant_expressions.has(p_symbol)) {
  2809. r_result.type = ScriptLanguage::LookupResult::RESULT_SCRIPT_LOCATION;
  2810. r_result.location = base_type.class_type->constant_expressions[p_symbol].expression->line;
  2811. return OK;
  2812. }
  2813. for (int i = 0; i < base_type.class_type->variables.size(); i++) {
  2814. if (base_type.class_type->variables[i].identifier == p_symbol) {
  2815. r_result.type = ScriptLanguage::LookupResult::RESULT_SCRIPT_LOCATION;
  2816. r_result.location = base_type.class_type->variables[i].line;
  2817. return OK;
  2818. }
  2819. }
  2820. for (int i = 0; i < base_type.class_type->subclasses.size(); i++) {
  2821. if (base_type.class_type->subclasses[i]->name == p_symbol) {
  2822. r_result.type = ScriptLanguage::LookupResult::RESULT_SCRIPT_LOCATION;
  2823. r_result.location = base_type.class_type->subclasses[i]->line;
  2824. return OK;
  2825. }
  2826. }
  2827. }
  2828. base_type = base_type.class_type->base_type;
  2829. }
  2830. } break;
  2831. case GDScriptParser::DataType::SCRIPT:
  2832. case GDScriptParser::DataType::GDSCRIPT: {
  2833. Ref<Script> scr = base_type.script_type;
  2834. if (scr.is_valid()) {
  2835. int line = scr->get_member_line(p_symbol);
  2836. if (line >= 0) {
  2837. r_result.type = ScriptLanguage::LookupResult::RESULT_SCRIPT_LOCATION;
  2838. r_result.location = line;
  2839. r_result.script = scr;
  2840. return OK;
  2841. }
  2842. Ref<Script> base_script = scr->get_base_script();
  2843. if (base_script.is_valid()) {
  2844. base_type.script_type = base_script;
  2845. } else {
  2846. base_type.kind = GDScriptParser::DataType::NATIVE;
  2847. base_type.native_type = scr->get_instance_base_type();
  2848. }
  2849. } else {
  2850. base_type.has_type = false;
  2851. }
  2852. } break;
  2853. case GDScriptParser::DataType::NATIVE: {
  2854. StringName class_name = base_type.native_type;
  2855. if (!ClassDB::class_exists(class_name)) {
  2856. class_name = String("_") + class_name;
  2857. if (!ClassDB::class_exists(class_name)) {
  2858. base_type.has_type = false;
  2859. break;
  2860. }
  2861. }
  2862. if (ClassDB::has_method(class_name, p_symbol, true)) {
  2863. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_METHOD;
  2864. r_result.class_name = base_type.native_type;
  2865. r_result.class_member = p_symbol;
  2866. return OK;
  2867. }
  2868. List<MethodInfo> virtual_methods;
  2869. ClassDB::get_virtual_methods(class_name, &virtual_methods, true);
  2870. for (List<MethodInfo>::Element *E = virtual_methods.front(); E; E = E->next()) {
  2871. if (E->get().name == p_symbol) {
  2872. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_METHOD;
  2873. r_result.class_name = base_type.native_type;
  2874. r_result.class_member = p_symbol;
  2875. return OK;
  2876. }
  2877. }
  2878. StringName enum_name = ClassDB::get_integer_constant_enum(class_name, p_symbol, true);
  2879. if (enum_name != StringName()) {
  2880. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_ENUM;
  2881. r_result.class_name = base_type.native_type;
  2882. r_result.class_member = enum_name;
  2883. return OK;
  2884. }
  2885. List<String> constants;
  2886. ClassDB::get_integer_constant_list(class_name, &constants, true);
  2887. for (List<String>::Element *E = constants.front(); E; E = E->next()) {
  2888. if (E->get() == p_symbol) {
  2889. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_CONSTANT;
  2890. r_result.class_name = base_type.native_type;
  2891. r_result.class_member = p_symbol;
  2892. return OK;
  2893. }
  2894. }
  2895. List<PropertyInfo> properties;
  2896. ClassDB::get_property_list(class_name, &properties, true);
  2897. for (List<PropertyInfo>::Element *E = properties.front(); E; E = E->next()) {
  2898. if (E->get().name == p_symbol) {
  2899. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_PROPERTY;
  2900. r_result.class_name = base_type.native_type;
  2901. r_result.class_member = p_symbol;
  2902. return OK;
  2903. }
  2904. }
  2905. StringName parent = ClassDB::get_parent_class(class_name);
  2906. if (parent != StringName()) {
  2907. if (String(parent).begins_with("_")) {
  2908. base_type.native_type = String(parent).right(1);
  2909. } else {
  2910. base_type.native_type = parent;
  2911. }
  2912. } else {
  2913. base_type.has_type = false;
  2914. }
  2915. } break;
  2916. case GDScriptParser::DataType::BUILTIN: {
  2917. base_type.has_type = false;
  2918. if (Variant::has_constant(base_type.builtin_type, p_symbol)) {
  2919. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_CONSTANT;
  2920. r_result.class_name = Variant::get_type_name(base_type.builtin_type);
  2921. r_result.class_member = p_symbol;
  2922. return OK;
  2923. }
  2924. Variant v;
  2925. REF v_ref;
  2926. if (base_type.builtin_type == Variant::OBJECT) {
  2927. v_ref.instance();
  2928. v = v_ref;
  2929. } else {
  2930. Variant::CallError err;
  2931. v = Variant::construct(base_type.builtin_type, nullptr, 0, err);
  2932. if (err.error != Variant::CallError::CALL_OK) {
  2933. break;
  2934. }
  2935. }
  2936. if (v.has_method(p_symbol)) {
  2937. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_METHOD;
  2938. r_result.class_name = Variant::get_type_name(base_type.builtin_type);
  2939. r_result.class_member = p_symbol;
  2940. return OK;
  2941. }
  2942. bool valid = false;
  2943. v.get(p_symbol, &valid);
  2944. if (valid) {
  2945. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_PROPERTY;
  2946. r_result.class_name = Variant::get_type_name(base_type.builtin_type);
  2947. r_result.class_member = p_symbol;
  2948. return OK;
  2949. }
  2950. } break;
  2951. default: {
  2952. base_type.has_type = false;
  2953. } break;
  2954. }
  2955. }
  2956. return ERR_CANT_RESOLVE;
  2957. }
  2958. Error GDScriptLanguage::lookup_code(const String &p_code, const String &p_symbol, const String &p_path, Object *p_owner, LookupResult &r_result) {
  2959. // Before parsing, try the usual stuff.
  2960. if (ClassDB::class_exists(p_symbol)) {
  2961. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS;
  2962. r_result.class_name = p_symbol;
  2963. return OK;
  2964. } else {
  2965. String under_prefix = "_" + p_symbol;
  2966. if (ClassDB::class_exists(under_prefix)) {
  2967. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS;
  2968. r_result.class_name = p_symbol;
  2969. return OK;
  2970. }
  2971. }
  2972. for (int i = 0; i < Variant::VARIANT_MAX; i++) {
  2973. Variant::Type t = Variant::Type(i);
  2974. if (Variant::get_type_name(t) == p_symbol) {
  2975. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS;
  2976. r_result.class_name = Variant::get_type_name(t);
  2977. return OK;
  2978. }
  2979. }
  2980. if ("PI" == p_symbol || "TAU" == p_symbol || "INF" == p_symbol || "NAN" == p_symbol) {
  2981. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_CONSTANT;
  2982. r_result.class_name = "@GDScript";
  2983. r_result.class_member = p_symbol;
  2984. return OK;
  2985. }
  2986. GDScriptParser parser;
  2987. parser.parse(p_code, p_path.get_base_dir(), false, p_path, true);
  2988. if (parser.get_completion_type() == GDScriptParser::COMPLETION_NONE) {
  2989. return ERR_CANT_RESOLVE;
  2990. }
  2991. GDScriptCompletionContext context;
  2992. context._class = parser.get_completion_class();
  2993. context.function = parser.get_completion_function();
  2994. context.block = parser.get_completion_block();
  2995. context.line = parser.get_completion_line();
  2996. context.base = p_owner;
  2997. context.base_path = p_path.get_base_dir();
  2998. if (context._class && context._class->extends_class.size() > 0) {
  2999. bool success = false;
  3000. ClassDB::get_integer_constant(context._class->extends_class[0], p_symbol, &success);
  3001. if (success) {
  3002. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_CONSTANT;
  3003. r_result.class_name = context._class->extends_class[0];
  3004. r_result.class_member = p_symbol;
  3005. return OK;
  3006. }
  3007. }
  3008. bool is_function = false;
  3009. switch (parser.get_completion_type()) {
  3010. case GDScriptParser::COMPLETION_BUILT_IN_TYPE_CONSTANT: {
  3011. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_CONSTANT;
  3012. r_result.class_name = Variant::get_type_name(parser.get_completion_built_in_constant());
  3013. r_result.class_member = p_symbol;
  3014. return OK;
  3015. } break;
  3016. case GDScriptParser::COMPLETION_PARENT_FUNCTION:
  3017. case GDScriptParser::COMPLETION_FUNCTION: {
  3018. is_function = true;
  3019. FALLTHROUGH;
  3020. }
  3021. case GDScriptParser::COMPLETION_IDENTIFIER: {
  3022. if (!is_function) {
  3023. is_function = parser.get_completion_identifier_is_function();
  3024. }
  3025. GDScriptParser::DataType base_type;
  3026. if (context._class) {
  3027. if (parser.get_completion_type() != GDScriptParser::COMPLETION_PARENT_FUNCTION) {
  3028. base_type.has_type = true;
  3029. base_type.kind = GDScriptParser::DataType::CLASS;
  3030. base_type.class_type = const_cast<GDScriptParser::ClassNode *>(context._class);
  3031. } else {
  3032. base_type = context._class->base_type;
  3033. }
  3034. } else {
  3035. break;
  3036. }
  3037. if (!is_function && context.block) {
  3038. // Lookup local variables
  3039. const GDScriptParser::BlockNode *block = context.block;
  3040. while (block) {
  3041. if (block->variables.has(p_symbol)) {
  3042. r_result.type = ScriptLanguage::LookupResult::RESULT_SCRIPT_LOCATION;
  3043. r_result.location = block->variables[p_symbol]->line;
  3044. return OK;
  3045. }
  3046. block = block->parent_block;
  3047. }
  3048. }
  3049. if (context.function && context.function->name != StringName()) {
  3050. // Lookup function arguments
  3051. for (int i = 0; i < context.function->arguments.size(); i++) {
  3052. if (context.function->arguments[i] == p_symbol) {
  3053. r_result.type = ScriptLanguage::LookupResult::RESULT_SCRIPT_LOCATION;
  3054. r_result.location = context.function->line;
  3055. return OK;
  3056. }
  3057. }
  3058. }
  3059. if (_lookup_symbol_from_base(base_type, p_symbol, is_function, r_result) == OK) {
  3060. return OK;
  3061. }
  3062. if (!is_function) {
  3063. // Guess in autoloads as singletons
  3064. List<PropertyInfo> props;
  3065. ProjectSettings::get_singleton()->get_property_list(&props);
  3066. for (List<PropertyInfo>::Element *E = props.front(); E; E = E->next()) {
  3067. String s = E->get().name;
  3068. if (!s.begins_with("autoload/")) {
  3069. continue;
  3070. }
  3071. String name = s.get_slice("/", 1);
  3072. if (name == String(p_symbol)) {
  3073. String path = ProjectSettings::get_singleton()->get(s);
  3074. if (path.begins_with("*")) {
  3075. String script = path.substr(1, path.length());
  3076. if (!script.ends_with(".gd")) {
  3077. // Not a script, try find the script anyway,
  3078. // may have some success
  3079. script = script.get_basename() + ".gd";
  3080. }
  3081. if (FileAccess::exists(script)) {
  3082. r_result.type = ScriptLanguage::LookupResult::RESULT_SCRIPT_LOCATION;
  3083. r_result.location = 0;
  3084. r_result.script = ResourceLoader::load(script);
  3085. return OK;
  3086. }
  3087. }
  3088. }
  3089. }
  3090. // Global
  3091. Map<StringName, int> classes = GDScriptLanguage::get_singleton()->get_global_map();
  3092. if (classes.has(p_symbol)) {
  3093. Variant value = GDScriptLanguage::get_singleton()->get_global_array()[classes[p_symbol]];
  3094. if (value.get_type() == Variant::OBJECT) {
  3095. Object *obj = value;
  3096. if (obj) {
  3097. if (Object::cast_to<GDScriptNativeClass>(obj)) {
  3098. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS;
  3099. r_result.class_name = Object::cast_to<GDScriptNativeClass>(obj)->get_name();
  3100. } else {
  3101. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS;
  3102. r_result.class_name = obj->get_class();
  3103. }
  3104. // proxy class remove the underscore.
  3105. if (r_result.class_name.begins_with("_")) {
  3106. r_result.class_name = r_result.class_name.right(1);
  3107. }
  3108. return OK;
  3109. }
  3110. } else {
  3111. /*
  3112. // Because get_integer_constant_enum and get_integer_constant don't work on @GlobalScope
  3113. // We cannot determine the exact nature of the identifier here
  3114. // Otherwise these codes would work
  3115. StringName enumName = ClassDB::get_integer_constant_enum("@GlobalScope", p_symbol, true);
  3116. if (enumName != NULL) {
  3117. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_ENUM;
  3118. r_result.class_name = "@GlobalScope";
  3119. r_result.class_member = enumName;
  3120. return OK;
  3121. }
  3122. else {
  3123. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_CONSTANT;
  3124. r_result.class_name = "@GlobalScope";
  3125. r_result.class_member = p_symbol;
  3126. return OK;
  3127. }*/
  3128. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_TBD_GLOBALSCOPE;
  3129. r_result.class_name = "@GlobalScope";
  3130. r_result.class_member = p_symbol;
  3131. return OK;
  3132. }
  3133. }
  3134. }
  3135. } break;
  3136. case GDScriptParser::COMPLETION_METHOD: {
  3137. is_function = true;
  3138. FALLTHROUGH;
  3139. }
  3140. case GDScriptParser::COMPLETION_INDEX: {
  3141. const GDScriptParser::Node *node = parser.get_completion_node();
  3142. if (node->type != GDScriptParser::Node::TYPE_OPERATOR) {
  3143. break;
  3144. }
  3145. GDScriptCompletionIdentifier base;
  3146. if (!_guess_expression_type(context, static_cast<const GDScriptParser::OperatorNode *>(node)->arguments[0], base)) {
  3147. break;
  3148. }
  3149. if (_lookup_symbol_from_base(base.type, p_symbol, is_function, r_result) == OK) {
  3150. return OK;
  3151. }
  3152. } break;
  3153. case GDScriptParser::COMPLETION_VIRTUAL_FUNC: {
  3154. GDScriptParser::DataType base_type = context._class->base_type;
  3155. if (_lookup_symbol_from_base(base_type, p_symbol, true, r_result) == OK) {
  3156. return OK;
  3157. }
  3158. } break;
  3159. case GDScriptParser::COMPLETION_TYPE_HINT: {
  3160. GDScriptParser::DataType base_type = context._class->base_type;
  3161. base_type.has_type = true;
  3162. base_type.kind = GDScriptParser::DataType::CLASS;
  3163. base_type.class_type = const_cast<GDScriptParser::ClassNode *>(context._class);
  3164. if (_lookup_symbol_from_base(base_type, p_symbol, false, r_result) == OK) {
  3165. return OK;
  3166. }
  3167. } break;
  3168. default: {
  3169. }
  3170. }
  3171. // Must check GDScript functions after parsing to ensure functions like Array.max() are handled correctly.
  3172. bool function_in_gdscript_functions = false;
  3173. for (int i = 0; i < GDScriptFunctions::FUNC_MAX; i++) {
  3174. if (GDScriptFunctions::get_func_name(GDScriptFunctions::Function(i)) == p_symbol) {
  3175. function_in_gdscript_functions = true;
  3176. break;
  3177. }
  3178. }
  3179. // Need special checks for assert, preload, and yield as they are technically
  3180. // keywords, so are not registered in GDScriptFunctions.
  3181. if (function_in_gdscript_functions || "yield" == p_symbol || "assert" == p_symbol || "preload" == p_symbol) {
  3182. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_METHOD;
  3183. r_result.class_name = "@GDScript";
  3184. r_result.class_member = p_symbol;
  3185. return OK;
  3186. }
  3187. return ERR_CANT_RESOLVE;
  3188. }
  3189. #endif