gdscript_editor.cpp 121 KB

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