2
0

gdscript_editor.cpp 110 KB

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