gdscript_editor.cpp 117 KB

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