gdscript_editor.cpp 109 KB

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