gdscript_editor.cpp 109 KB

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