gdscript_editor.cpp 118 KB

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