gdscript_editor.cpp 118 KB

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