gdscript_editor.cpp 117 KB

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