gdscript_editor.cpp 92 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072
  1. /*************************************************************************/
  2. /* gdscript_editor.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "gdscript.h"
  31. #include "core/engine.h"
  32. #include "editor/editor_settings.h"
  33. #include "gdscript_compiler.h"
  34. #include "global_constants.h"
  35. #include "os/file_access.h"
  36. #ifdef TOOLS_ENABLED
  37. #include "editor/editor_file_system.h"
  38. #include "editor/editor_settings.h"
  39. #include "engine.h"
  40. #endif
  41. void GDScriptLanguage::get_comment_delimiters(List<String> *p_delimiters) const {
  42. p_delimiters->push_back("#");
  43. p_delimiters->push_back("\"\"\" \"\"\"");
  44. }
  45. void GDScriptLanguage::get_string_delimiters(List<String> *p_delimiters) const {
  46. p_delimiters->push_back("\" \"");
  47. p_delimiters->push_back("' '");
  48. }
  49. Ref<Script> GDScriptLanguage::get_template(const String &p_class_name, const String &p_base_class_name) const {
  50. String _template = String() +
  51. "extends %BASE%\n\n" +
  52. "# class member variables go here, for example:\n" +
  53. "# var a = 2\n" +
  54. "# var b = \"textvar\"\n\n" +
  55. "func _ready():\n" +
  56. "%TS%# Called when the node is added to the scene for the first time.\n" +
  57. "%TS%# Initialization here.\n" +
  58. "%TS%pass\n\n" +
  59. "#func _process(delta):\n" +
  60. "#%TS%# Called every frame. Delta is time since last frame.\n" +
  61. "#%TS%# Update game logic here.\n" +
  62. "#%TS%pass\n";
  63. _template = _template.replace("%BASE%", p_base_class_name);
  64. _template = _template.replace("%TS%", _get_indentation());
  65. Ref<GDScript> script;
  66. script.instance();
  67. script->set_source_code(_template);
  68. return script;
  69. }
  70. bool GDScriptLanguage::is_using_templates() {
  71. return true;
  72. }
  73. void GDScriptLanguage::make_template(const String &p_class_name, const String &p_base_class_name, Ref<Script> &p_script) {
  74. String src = p_script->get_source_code();
  75. src = src.replace("%BASE%", p_base_class_name);
  76. src = src.replace("%TS%", _get_indentation());
  77. p_script->set_source_code(src);
  78. }
  79. 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) const {
  80. GDScriptParser parser;
  81. Error err = parser.parse(p_script, p_path.get_base_dir(), true, p_path);
  82. if (err) {
  83. r_line_error = parser.get_error_line();
  84. r_col_error = parser.get_error_column();
  85. r_test_error = parser.get_error();
  86. return false;
  87. } else {
  88. const GDScriptParser::Node *root = parser.get_parse_tree();
  89. ERR_FAIL_COND_V(root->type != GDScriptParser::Node::TYPE_CLASS, false);
  90. const GDScriptParser::ClassNode *cl = static_cast<const GDScriptParser::ClassNode *>(root);
  91. Map<int, String> funcs;
  92. for (int i = 0; i < cl->functions.size(); i++) {
  93. funcs[cl->functions[i]->line] = cl->functions[i]->name;
  94. }
  95. for (int i = 0; i < cl->static_functions.size(); i++) {
  96. funcs[cl->static_functions[i]->line] = cl->static_functions[i]->name;
  97. }
  98. for (int i = 0; i < cl->subclasses.size(); i++) {
  99. for (int j = 0; j < cl->subclasses[i]->functions.size(); j++) {
  100. funcs[cl->subclasses[i]->functions[j]->line] = String(cl->subclasses[i]->name) + "." + String(cl->subclasses[i]->functions[j]->name);
  101. }
  102. }
  103. for (Map<int, String>::Element *E = funcs.front(); E; E = E->next()) {
  104. r_functions->push_back(E->get() + ":" + itos(E->key()));
  105. }
  106. }
  107. return true;
  108. }
  109. bool GDScriptLanguage::has_named_classes() const {
  110. return false;
  111. }
  112. bool GDScriptLanguage::supports_builtin_mode() const {
  113. return true;
  114. }
  115. int GDScriptLanguage::find_function(const String &p_function, const String &p_code) const {
  116. GDScriptTokenizerText tokenizer;
  117. tokenizer.set_code(p_code);
  118. int indent = 0;
  119. while (tokenizer.get_token() != GDScriptTokenizer::TK_EOF && tokenizer.get_token() != GDScriptTokenizer::TK_ERROR) {
  120. if (tokenizer.get_token() == GDScriptTokenizer::TK_NEWLINE) {
  121. indent = tokenizer.get_token_line_indent();
  122. }
  123. //print_line("TOKEN: "+String(GDScriptTokenizer::get_token_name(tokenizer.get_token())));
  124. if (indent == 0 && tokenizer.get_token() == GDScriptTokenizer::TK_PR_FUNCTION && tokenizer.get_token(1) == GDScriptTokenizer::TK_IDENTIFIER) {
  125. String identifier = tokenizer.get_token_identifier(1);
  126. if (identifier == p_function) {
  127. return tokenizer.get_token_line();
  128. }
  129. }
  130. tokenizer.advance();
  131. //print_line("NEXT: "+String(GDScriptTokenizer::get_token_name(tokenizer.get_token())));
  132. }
  133. return -1;
  134. }
  135. Script *GDScriptLanguage::create_script() const {
  136. return memnew(GDScript);
  137. }
  138. /* DEBUGGER FUNCTIONS */
  139. bool GDScriptLanguage::debug_break_parse(const String &p_file, int p_line, const String &p_error) {
  140. //break because of parse error
  141. if (ScriptDebugger::get_singleton() && Thread::get_caller_id() == Thread::get_main_id()) {
  142. _debug_parse_err_line = p_line;
  143. _debug_parse_err_file = p_file;
  144. _debug_error = p_error;
  145. ScriptDebugger::get_singleton()->debug(this, false);
  146. return true;
  147. } else {
  148. return false;
  149. }
  150. }
  151. bool GDScriptLanguage::debug_break(const String &p_error, bool p_allow_continue) {
  152. if (ScriptDebugger::get_singleton() && Thread::get_caller_id() == Thread::get_main_id()) {
  153. _debug_parse_err_line = -1;
  154. _debug_parse_err_file = "";
  155. _debug_error = p_error;
  156. ScriptDebugger::get_singleton()->debug(this, p_allow_continue);
  157. return true;
  158. } else {
  159. return false;
  160. }
  161. }
  162. String GDScriptLanguage::debug_get_error() const {
  163. return _debug_error;
  164. }
  165. int GDScriptLanguage::debug_get_stack_level_count() const {
  166. if (_debug_parse_err_line >= 0)
  167. return 1;
  168. return _debug_call_stack_pos;
  169. }
  170. int GDScriptLanguage::debug_get_stack_level_line(int p_level) const {
  171. if (_debug_parse_err_line >= 0)
  172. return _debug_parse_err_line;
  173. ERR_FAIL_INDEX_V(p_level, _debug_call_stack_pos, -1);
  174. int l = _debug_call_stack_pos - p_level - 1;
  175. return *(_call_stack[l].line);
  176. }
  177. String GDScriptLanguage::debug_get_stack_level_function(int p_level) const {
  178. if (_debug_parse_err_line >= 0)
  179. return "";
  180. ERR_FAIL_INDEX_V(p_level, _debug_call_stack_pos, "");
  181. int l = _debug_call_stack_pos - p_level - 1;
  182. return _call_stack[l].function->get_name();
  183. }
  184. String GDScriptLanguage::debug_get_stack_level_source(int p_level) const {
  185. if (_debug_parse_err_line >= 0)
  186. return _debug_parse_err_file;
  187. ERR_FAIL_INDEX_V(p_level, _debug_call_stack_pos, "");
  188. int l = _debug_call_stack_pos - p_level - 1;
  189. return _call_stack[l].function->get_source();
  190. }
  191. 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) {
  192. if (_debug_parse_err_line >= 0)
  193. return;
  194. ERR_FAIL_INDEX(p_level, _debug_call_stack_pos);
  195. int l = _debug_call_stack_pos - p_level - 1;
  196. GDScriptFunction *f = _call_stack[l].function;
  197. List<Pair<StringName, int> > locals;
  198. f->debug_get_stack_member_state(*_call_stack[l].line, &locals);
  199. for (List<Pair<StringName, int> >::Element *E = locals.front(); E; E = E->next()) {
  200. p_locals->push_back(E->get().first);
  201. p_values->push_back(_call_stack[l].stack[E->get().second]);
  202. }
  203. }
  204. 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) {
  205. if (_debug_parse_err_line >= 0)
  206. return;
  207. ERR_FAIL_INDEX(p_level, _debug_call_stack_pos);
  208. int l = _debug_call_stack_pos - p_level - 1;
  209. GDScriptInstance *instance = _call_stack[l].instance;
  210. if (!instance)
  211. return;
  212. Ref<GDScript> script = instance->get_script();
  213. ERR_FAIL_COND(script.is_null());
  214. const Map<StringName, GDScript::MemberInfo> &mi = script->debug_get_member_indices();
  215. for (const Map<StringName, GDScript::MemberInfo>::Element *E = mi.front(); E; E = E->next()) {
  216. p_members->push_back(E->key());
  217. p_values->push_back(instance->debug_get_member_by_index(E->get().index));
  218. }
  219. }
  220. ScriptInstance *GDScriptLanguage::debug_get_stack_level_instance(int p_level) {
  221. ERR_FAIL_COND_V(_debug_parse_err_line >= 0, NULL);
  222. ERR_FAIL_INDEX_V(p_level, _debug_call_stack_pos, NULL);
  223. int l = _debug_call_stack_pos - p_level - 1;
  224. ScriptInstance *instance = _call_stack[l].instance;
  225. return instance;
  226. }
  227. void GDScriptLanguage::debug_get_globals(List<String> *p_globals, List<Variant> *p_values, int p_max_subitems, int p_max_depth) {
  228. const Map<StringName, int> &name_idx = GDScriptLanguage::get_singleton()->get_global_map();
  229. const Variant *globals = GDScriptLanguage::get_singleton()->get_global_array();
  230. List<Pair<String, Variant> > cinfo;
  231. get_public_constants(&cinfo);
  232. for (const Map<StringName, int>::Element *E = name_idx.front(); E; E = E->next()) {
  233. if (ClassDB::class_exists(E->key()) || Engine::get_singleton()->has_singleton(E->key()))
  234. continue;
  235. bool is_script_constant = false;
  236. for (List<Pair<String, Variant> >::Element *CE = cinfo.front(); CE; CE = CE->next()) {
  237. if (CE->get().first == E->key()) {
  238. is_script_constant = true;
  239. break;
  240. }
  241. }
  242. if (is_script_constant)
  243. continue;
  244. const Variant &var = globals[E->value()];
  245. if (Object *obj = var) {
  246. if (Object::cast_to<GDScriptNativeClass>(obj))
  247. continue;
  248. }
  249. bool skip = false;
  250. for (int i = 0; i < GlobalConstants::get_global_constant_count(); i++) {
  251. if (E->key() == GlobalConstants::get_global_constant_name(i)) {
  252. skip = true;
  253. break;
  254. }
  255. }
  256. if (skip)
  257. continue;
  258. p_globals->push_back(E->key());
  259. p_values->push_back(var);
  260. }
  261. }
  262. String GDScriptLanguage::debug_parse_stack_level_expression(int p_level, const String &p_expression, int p_max_subitems, int p_max_depth) {
  263. if (_debug_parse_err_line >= 0)
  264. return "";
  265. return "";
  266. }
  267. void GDScriptLanguage::get_recognized_extensions(List<String> *p_extensions) const {
  268. p_extensions->push_back("gd");
  269. }
  270. void GDScriptLanguage::get_public_functions(List<MethodInfo> *p_functions) const {
  271. for (int i = 0; i < GDScriptFunctions::FUNC_MAX; i++) {
  272. p_functions->push_back(GDScriptFunctions::get_info(GDScriptFunctions::Function(i)));
  273. }
  274. //not really "functions", but..
  275. {
  276. MethodInfo mi;
  277. mi.name = "preload";
  278. mi.arguments.push_back(PropertyInfo(Variant::STRING, "path"));
  279. mi.return_val = PropertyInfo(Variant::OBJECT, "", PROPERTY_HINT_RESOURCE_TYPE, "Resource");
  280. p_functions->push_back(mi);
  281. }
  282. {
  283. MethodInfo mi;
  284. mi.name = "yield";
  285. mi.arguments.push_back(PropertyInfo(Variant::OBJECT, "object"));
  286. mi.arguments.push_back(PropertyInfo(Variant::STRING, "signal"));
  287. mi.default_arguments.push_back(Variant());
  288. mi.default_arguments.push_back(String());
  289. mi.return_val = PropertyInfo(Variant::OBJECT, "", PROPERTY_HINT_RESOURCE_TYPE, "GDScriptFunctionState");
  290. p_functions->push_back(mi);
  291. }
  292. {
  293. MethodInfo mi;
  294. mi.name = "assert";
  295. mi.return_val.type = Variant::NIL;
  296. mi.arguments.push_back(PropertyInfo(Variant::BOOL, "condition"));
  297. p_functions->push_back(mi);
  298. }
  299. }
  300. void GDScriptLanguage::get_public_constants(List<Pair<String, Variant> > *p_constants) const {
  301. Pair<String, Variant> pi;
  302. pi.first = "PI";
  303. pi.second = Math_PI;
  304. p_constants->push_back(pi);
  305. Pair<String, Variant> tau;
  306. tau.first = "TAU";
  307. tau.second = Math_TAU;
  308. p_constants->push_back(tau);
  309. Pair<String, Variant> infinity;
  310. infinity.first = "INF";
  311. infinity.second = Math_INF;
  312. p_constants->push_back(infinity);
  313. Pair<String, Variant> nan;
  314. nan.first = "NAN";
  315. nan.second = Math_NAN;
  316. p_constants->push_back(nan);
  317. }
  318. String GDScriptLanguage::make_function(const String &p_class, const String &p_name, const PoolStringArray &p_args) const {
  319. String s = "func " + p_name + "(";
  320. if (p_args.size()) {
  321. for (int i = 0; i < p_args.size(); i++) {
  322. if (i > 0)
  323. s += ", ";
  324. s += p_args[i].get_slice(":", 0);
  325. }
  326. }
  327. s += "):\n" + _get_indentation() + "pass # Replace with function body.\n";
  328. return s;
  329. }
  330. #if defined(DEBUG_METHODS_ENABLED) && defined(TOOLS_ENABLED)
  331. struct GDScriptCompletionIdentifier {
  332. String enumeration;
  333. StringName obj_type;
  334. Ref<GDScript> script;
  335. Variant::Type type;
  336. Variant value; //im case there is a value, also return it
  337. GDScriptCompletionIdentifier() :
  338. type(Variant::NIL) {}
  339. };
  340. static GDScriptCompletionIdentifier _get_type_from_variant(const Variant &p_variant, bool p_allow_gdnative_class = false) {
  341. GDScriptCompletionIdentifier t;
  342. t.type = p_variant.get_type();
  343. t.value = p_variant;
  344. if (p_variant.get_type() == Variant::OBJECT) {
  345. Object *obj = p_variant;
  346. if (obj) {
  347. if (p_allow_gdnative_class && Object::cast_to<GDScriptNativeClass>(obj)) {
  348. t.obj_type = Object::cast_to<GDScriptNativeClass>(obj)->get_name();
  349. t.value = Variant();
  350. } else {
  351. t.obj_type = obj->get_class();
  352. }
  353. }
  354. }
  355. return t;
  356. }
  357. static GDScriptCompletionIdentifier _get_type_from_pinfo(const PropertyInfo &p_info) {
  358. GDScriptCompletionIdentifier t;
  359. t.type = p_info.type;
  360. if (p_info.hint == PROPERTY_HINT_RESOURCE_TYPE) {
  361. t.obj_type = p_info.hint_string;
  362. }
  363. return t;
  364. }
  365. struct GDScriptCompletionContext {
  366. const GDScriptParser::ClassNode *_class;
  367. const GDScriptParser::FunctionNode *function;
  368. const GDScriptParser::BlockNode *block;
  369. Object *base;
  370. String base_path;
  371. };
  372. static Ref<Reference> _get_parent_class(GDScriptCompletionContext &context) {
  373. if (context._class->extends_used) {
  374. //do inheritance
  375. String path = context._class->extends_file;
  376. Ref<GDScript> script;
  377. Ref<GDScriptNativeClass> native;
  378. if (path != "") {
  379. //path (and optionally subclasses)
  380. if (path.is_rel_path()) {
  381. path = context.base_path.plus_file(path);
  382. }
  383. if (ScriptCodeCompletionCache::get_singleton())
  384. script = ScriptCodeCompletionCache::get_singleton()->get_cached_resource(path);
  385. else
  386. script = ResourceLoader::load(path);
  387. if (script.is_null()) {
  388. return REF();
  389. }
  390. if (!script->is_valid()) {
  391. return REF();
  392. }
  393. //print_line("EXTENDS PATH: "+path+" script is "+itos(script.is_valid())+" indices is "+itos(script->member_indices.size())+" valid? "+itos(script->valid));
  394. if (context._class->extends_class.size()) {
  395. for (int i = 0; i < context._class->extends_class.size(); i++) {
  396. String sub = context._class->extends_class[i];
  397. if (script->get_subclasses().has(sub)) {
  398. script = script->get_subclasses()[sub];
  399. } else {
  400. return REF();
  401. }
  402. }
  403. }
  404. if (script.is_valid())
  405. return script;
  406. } else {
  407. if (context._class->extends_class.size() == 0) {
  408. ERR_PRINT("BUG");
  409. return REF();
  410. }
  411. String base = context._class->extends_class[0];
  412. if (context._class->extends_class.size() > 1) {
  413. return REF();
  414. }
  415. //if not found, try engine classes
  416. if (!GDScriptLanguage::get_singleton()->get_global_map().has(base)) {
  417. return REF();
  418. }
  419. int base_idx = GDScriptLanguage::get_singleton()->get_global_map()[base];
  420. native = GDScriptLanguage::get_singleton()->get_global_array()[base_idx];
  421. return native;
  422. }
  423. }
  424. return Ref<Reference>();
  425. }
  426. static GDScriptCompletionIdentifier _get_native_class(GDScriptCompletionContext &context) {
  427. GDScriptCompletionIdentifier id;
  428. REF pc = _get_parent_class(context);
  429. if (!pc.is_valid()) {
  430. return id;
  431. }
  432. Ref<GDScriptNativeClass> nc = pc;
  433. Ref<GDScript> s = pc;
  434. if (s.is_null() && nc.is_null()) {
  435. return id;
  436. }
  437. while (!s.is_null()) {
  438. nc = s->get_native();
  439. s = s->get_base();
  440. }
  441. if (nc.is_null()) {
  442. return id;
  443. }
  444. id.type = Variant::OBJECT;
  445. if (context.base)
  446. id.value = context.base;
  447. id.obj_type = nc->get_name();
  448. return id;
  449. }
  450. static bool _guess_identifier_type(GDScriptCompletionContext &context, int p_line, const StringName &p_identifier, GDScriptCompletionIdentifier &r_type, bool p_for_indexing);
  451. static bool _guess_expression_type(GDScriptCompletionContext &context, const GDScriptParser::Node *p_node, int p_line, GDScriptCompletionIdentifier &r_type, bool p_for_indexing = false) {
  452. if (p_node->type == GDScriptParser::Node::TYPE_CONSTANT) {
  453. const GDScriptParser::ConstantNode *cn = static_cast<const GDScriptParser::ConstantNode *>(p_node);
  454. r_type = _get_type_from_variant(cn->value);
  455. return true;
  456. } else if (p_node->type == GDScriptParser::Node::TYPE_DICTIONARY) {
  457. r_type.type = Variant::DICTIONARY;
  458. //what the heck, fill it anyway
  459. const GDScriptParser::DictionaryNode *an = static_cast<const GDScriptParser::DictionaryNode *>(p_node);
  460. Dictionary d;
  461. for (int i = 0; i < an->elements.size(); i++) {
  462. GDScriptCompletionIdentifier k;
  463. if (_guess_expression_type(context, an->elements[i].key, p_line, k) && k.value.get_type() != Variant::NIL) {
  464. GDScriptCompletionIdentifier v;
  465. if (_guess_expression_type(context, an->elements[i].value, p_line, v)) {
  466. d[k.value] = v.value;
  467. }
  468. }
  469. }
  470. r_type.value = d;
  471. return true;
  472. } else if (p_node->type == GDScriptParser::Node::TYPE_ARRAY) {
  473. r_type.type = Variant::ARRAY;
  474. //what the heck, fill it anyway
  475. const GDScriptParser::ArrayNode *an = static_cast<const GDScriptParser::ArrayNode *>(p_node);
  476. Array arr;
  477. arr.resize(an->elements.size());
  478. for (int i = 0; i < an->elements.size(); i++) {
  479. GDScriptCompletionIdentifier ci;
  480. if (_guess_expression_type(context, an->elements[i], p_line, ci)) {
  481. arr[i] = ci.value;
  482. }
  483. }
  484. r_type.value = arr;
  485. return true;
  486. } else if (p_node->type == GDScriptParser::Node::TYPE_BUILT_IN_FUNCTION) {
  487. MethodInfo mi = GDScriptFunctions::get_info(static_cast<const GDScriptParser::BuiltInFunctionNode *>(p_node)->function);
  488. r_type = _get_type_from_pinfo(mi.return_val);
  489. return true;
  490. } else if (p_node->type == GDScriptParser::Node::TYPE_IDENTIFIER) {
  491. return _guess_identifier_type(context, p_line - 1, static_cast<const GDScriptParser::IdentifierNode *>(p_node)->name, r_type, p_for_indexing);
  492. } else if (p_node->type == GDScriptParser::Node::TYPE_SELF) {
  493. //eeh...
  494. r_type = _get_native_class(context);
  495. return r_type.type != Variant::NIL;
  496. } else if (p_node->type == GDScriptParser::Node::TYPE_OPERATOR) {
  497. const GDScriptParser::OperatorNode *op = static_cast<const GDScriptParser::OperatorNode *>(p_node);
  498. if (op->op == GDScriptParser::OperatorNode::OP_CALL) {
  499. if (op->arguments[0]->type == GDScriptParser::Node::TYPE_TYPE) {
  500. const GDScriptParser::TypeNode *tn = static_cast<const GDScriptParser::TypeNode *>(op->arguments[0]);
  501. r_type.type = tn->vtype;
  502. return true;
  503. } else if (op->arguments[0]->type == GDScriptParser::Node::TYPE_BUILT_IN_FUNCTION) {
  504. const GDScriptParser::BuiltInFunctionNode *bin = static_cast<const GDScriptParser::BuiltInFunctionNode *>(op->arguments[0]);
  505. return _guess_expression_type(context, bin, p_line, r_type);
  506. } else if (op->arguments.size() > 1 && op->arguments[1]->type == GDScriptParser::Node::TYPE_IDENTIFIER) {
  507. StringName id = static_cast<const GDScriptParser::IdentifierNode *>(op->arguments[1])->name;
  508. if (op->arguments[0]->type == GDScriptParser::Node::TYPE_IDENTIFIER && String(id) == "new") {
  509. //shortcut
  510. StringName identifier = static_cast<const GDScriptParser::IdentifierNode *>(op->arguments[0])->name;
  511. if (ClassDB::class_exists(identifier)) {
  512. r_type.type = Variant::OBJECT;
  513. r_type.value = Variant();
  514. r_type.obj_type = identifier;
  515. return true;
  516. }
  517. }
  518. GDScriptCompletionIdentifier base;
  519. if (!_guess_expression_type(context, op->arguments[0], p_line, base))
  520. return false;
  521. if (base.type == Variant::OBJECT) {
  522. if (id.operator String() == "new" && base.value.get_type() == Variant::OBJECT) {
  523. Object *obj = base.value;
  524. if (obj && Object::cast_to<GDScriptNativeClass>(obj)) {
  525. GDScriptNativeClass *gdnc = Object::cast_to<GDScriptNativeClass>(obj);
  526. r_type.type = Variant::OBJECT;
  527. r_type.value = Variant();
  528. r_type.obj_type = gdnc->get_name();
  529. return true;
  530. } else {
  531. if (base.obj_type != StringName()) {
  532. r_type.type = Variant::OBJECT;
  533. r_type.value = Variant();
  534. r_type.obj_type = base.obj_type;
  535. return true;
  536. }
  537. }
  538. }
  539. if (ClassDB::has_method(base.obj_type, id)) {
  540. #ifdef TOOLS_ENABLED
  541. MethodBind *mb = ClassDB::get_method(base.obj_type, id);
  542. PropertyInfo pi = mb->get_return_info();
  543. //try calling the function if constant and all args are constant, should not crash..
  544. Object *baseptr = base.value;
  545. if (mb->is_const() && pi.type == Variant::OBJECT) {
  546. bool all_valid = true;
  547. Vector<Variant> args;
  548. for (int i = 2; i < op->arguments.size(); i++) {
  549. GDScriptCompletionIdentifier arg;
  550. if (_guess_expression_type(context, op->arguments[i], p_line, arg)) {
  551. if (arg.value.get_type() != Variant::NIL && arg.value.get_type() != Variant::OBJECT) { // calling with object seems dangerous, i don' t know
  552. args.push_back(arg.value);
  553. } else {
  554. all_valid = false;
  555. break;
  556. }
  557. } else {
  558. all_valid = false;
  559. }
  560. }
  561. if (all_valid && String(id) == "get_node" && ClassDB::is_parent_class(base.obj_type, "Node") && args.size()) {
  562. String arg1 = args[0];
  563. if (arg1.begins_with("/root/")) {
  564. String which = arg1.get_slice("/", 2);
  565. if (which != "") {
  566. List<PropertyInfo> props;
  567. ProjectSettings::get_singleton()->get_property_list(&props);
  568. //print_line("find singleton");
  569. for (List<PropertyInfo>::Element *E = props.front(); E; E = E->next()) {
  570. String s = E->get().name;
  571. if (!s.begins_with("autoload/"))
  572. continue;
  573. //print_line("found "+s);
  574. String name = s.get_slice("/", 1);
  575. //print_line("name: "+name+", which: "+which);
  576. if (name == which) {
  577. String script = ProjectSettings::get_singleton()->get(s);
  578. if (!script.begins_with("res://")) {
  579. script = "res://" + script;
  580. }
  581. if (!script.ends_with(".gd")) {
  582. //not a script, try find the script anyway,
  583. //may have some success
  584. script = script.get_basename() + ".gd";
  585. }
  586. if (FileAccess::exists(script)) {
  587. //print_line("is a script");
  588. Ref<Script> scr;
  589. if (ScriptCodeCompletionCache::get_singleton())
  590. scr = ScriptCodeCompletionCache::get_singleton()->get_cached_resource(script);
  591. else
  592. scr = ResourceLoader::load(script);
  593. r_type.obj_type = "Node";
  594. r_type.type = Variant::OBJECT;
  595. r_type.script = scr;
  596. r_type.value = Variant();
  597. return true;
  598. }
  599. }
  600. }
  601. }
  602. }
  603. }
  604. if (baseptr) {
  605. if (all_valid) {
  606. Vector<const Variant *> argptr;
  607. for (int i = 0; i < args.size(); i++) {
  608. argptr.push_back(&args[i]);
  609. }
  610. Variant::CallError ce;
  611. Variant ret = mb->call(baseptr, (const Variant **)argptr.ptr(), argptr.size(), ce);
  612. if (ce.error == Variant::CallError::CALL_OK && ret.get_type() != Variant::NIL) {
  613. if (ret.get_type() != Variant::OBJECT || ret.operator Object *() != NULL) {
  614. r_type = _get_type_from_variant(ret);
  615. return true;
  616. }
  617. }
  618. }
  619. }
  620. }
  621. r_type.type = pi.type;
  622. if (pi.hint == PROPERTY_HINT_RESOURCE_TYPE) {
  623. r_type.obj_type = pi.hint_string;
  624. }
  625. return true;
  626. #else
  627. return false;
  628. #endif
  629. } else {
  630. return false;
  631. }
  632. } else {
  633. //method for some variant..
  634. Variant::CallError ce;
  635. Variant v = Variant::construct(base.type, NULL, 0, ce);
  636. List<MethodInfo> mi;
  637. v.get_method_list(&mi);
  638. for (List<MethodInfo>::Element *E = mi.front(); E; E = E->next()) {
  639. if (!E->get().name.begins_with("_") && E->get().name == id.operator String()) {
  640. MethodInfo mi = E->get();
  641. r_type.type = mi.return_val.type;
  642. if (mi.return_val.hint == PROPERTY_HINT_RESOURCE_TYPE) {
  643. r_type.obj_type = mi.return_val.hint_string;
  644. }
  645. return true;
  646. }
  647. }
  648. }
  649. }
  650. } else if (op->op == GDScriptParser::OperatorNode::OP_INDEX || op->op == GDScriptParser::OperatorNode::OP_INDEX_NAMED) {
  651. GDScriptCompletionIdentifier p1;
  652. GDScriptCompletionIdentifier p2;
  653. if (op->op == GDScriptParser::OperatorNode::OP_INDEX_NAMED) {
  654. if (op->arguments[1]->type == GDScriptParser::Node::TYPE_IDENTIFIER) {
  655. String id = static_cast<const GDScriptParser::IdentifierNode *>(op->arguments[1])->name;
  656. p2.type = Variant::STRING;
  657. p2.value = id;
  658. }
  659. } else {
  660. if (op->arguments[1]) {
  661. if (!_guess_expression_type(context, op->arguments[1], p_line, p2)) {
  662. return false;
  663. }
  664. }
  665. }
  666. if (op->arguments[0]->type == GDScriptParser::Node::TYPE_ARRAY) {
  667. const GDScriptParser::ArrayNode *an = static_cast<const GDScriptParser::ArrayNode *>(op->arguments[0]);
  668. if (p2.value.is_num()) {
  669. int index = p2.value;
  670. if (index < 0 || index >= an->elements.size())
  671. return false;
  672. return _guess_expression_type(context, an->elements[index], p_line, r_type);
  673. }
  674. } else if (op->arguments[0]->type == GDScriptParser::Node::TYPE_DICTIONARY) {
  675. const GDScriptParser::DictionaryNode *dn = static_cast<const GDScriptParser::DictionaryNode *>(op->arguments[0]);
  676. if (p2.value.get_type() == Variant::NIL)
  677. return false;
  678. for (int i = 0; i < dn->elements.size(); i++) {
  679. GDScriptCompletionIdentifier k;
  680. if (!_guess_expression_type(context, dn->elements[i].key, p_line, k)) {
  681. return false;
  682. }
  683. if (k.value.get_type() == Variant::NIL)
  684. return false;
  685. if (k.value == p2.value) {
  686. return _guess_expression_type(context, dn->elements[i].value, p_line, r_type);
  687. }
  688. }
  689. } else {
  690. if (op->arguments[0]) {
  691. if (!_guess_expression_type(context, op->arguments[0], p_line, p1)) {
  692. return false;
  693. }
  694. }
  695. if (p1.value.get_type() == Variant::OBJECT) {
  696. //??
  697. if (p1.obj_type != StringName() && p2.type == Variant::STRING) {
  698. StringName base_type = p1.obj_type;
  699. if (p1.obj_type == "GDScriptNativeClass") {
  700. //native enum
  701. Ref<GDScriptNativeClass> gdn = p1.value;
  702. if (gdn.is_valid()) {
  703. base_type = gdn->get_name();
  704. }
  705. }
  706. StringName index = p2.value;
  707. bool valid;
  708. Variant::Type t = ClassDB::get_property_type(base_type, index, &valid);
  709. if (t != Variant::NIL && valid) {
  710. r_type.type = t;
  711. if (t == Variant::INT || t == Variant::OBJECT) {
  712. //check for enum!
  713. #if defined(DEBUG_METHODS_ENABLED) && defined(TOOLS_ENABLED)
  714. StringName getter = ClassDB::get_property_getter(base_type, index);
  715. if (getter != StringName()) {
  716. MethodBind *mb = ClassDB::get_method(base_type, getter);
  717. if (mb) {
  718. PropertyInfo rt = mb->get_return_info();
  719. if ((rt.usage & PROPERTY_USAGE_CLASS_IS_ENUM) && t == Variant::INT) {
  720. r_type.enumeration = rt.class_name;
  721. } else if (t == Variant::OBJECT) {
  722. r_type.obj_type = rt.class_name;
  723. }
  724. }
  725. }
  726. #endif
  727. }
  728. return true;
  729. }
  730. }
  731. } else if (p1.value.get_type() != Variant::NIL) {
  732. bool valid;
  733. Variant ret = p1.value.get(p2.value, &valid);
  734. if (valid) {
  735. r_type = _get_type_from_variant(ret);
  736. return true;
  737. }
  738. } else {
  739. if (p1.type != Variant::NIL) {
  740. Variant::CallError ce;
  741. Variant base = Variant::construct(p1.type, NULL, 0, ce);
  742. bool valid;
  743. Variant ret = base.get(p2.value, &valid);
  744. if (valid) {
  745. r_type = _get_type_from_variant(ret);
  746. return true;
  747. }
  748. }
  749. }
  750. }
  751. } else {
  752. Variant::Operator vop = Variant::OP_MAX;
  753. switch (op->op) {
  754. case GDScriptParser::OperatorNode::OP_ADD: vop = Variant::OP_ADD; break;
  755. case GDScriptParser::OperatorNode::OP_SUB: vop = Variant::OP_SUBTRACT; break;
  756. case GDScriptParser::OperatorNode::OP_MUL: vop = Variant::OP_MULTIPLY; break;
  757. case GDScriptParser::OperatorNode::OP_DIV: vop = Variant::OP_DIVIDE; break;
  758. case GDScriptParser::OperatorNode::OP_MOD: vop = Variant::OP_MODULE; break;
  759. case GDScriptParser::OperatorNode::OP_SHIFT_LEFT: vop = Variant::OP_SHIFT_LEFT; break;
  760. case GDScriptParser::OperatorNode::OP_SHIFT_RIGHT: vop = Variant::OP_SHIFT_RIGHT; break;
  761. case GDScriptParser::OperatorNode::OP_BIT_AND: vop = Variant::OP_BIT_AND; break;
  762. case GDScriptParser::OperatorNode::OP_BIT_OR: vop = Variant::OP_BIT_OR; break;
  763. case GDScriptParser::OperatorNode::OP_BIT_XOR: vop = Variant::OP_BIT_XOR; break;
  764. default: {}
  765. }
  766. if (vop == Variant::OP_MAX)
  767. return false;
  768. GDScriptCompletionIdentifier p1;
  769. GDScriptCompletionIdentifier p2;
  770. if (op->arguments[0]) {
  771. if (!_guess_expression_type(context, op->arguments[0], p_line, p1)) {
  772. return false;
  773. }
  774. }
  775. if (op->arguments.size() > 1) {
  776. if (!_guess_expression_type(context, op->arguments[1], p_line, p2)) {
  777. return false;
  778. }
  779. }
  780. Variant::CallError ce;
  781. bool v1_use_value = p1.value.get_type() != Variant::NIL && p1.value.get_type() != Variant::OBJECT;
  782. Variant v1 = (v1_use_value) ? p1.value : Variant::construct(p1.type, NULL, 0, ce);
  783. bool v2_use_value = p2.value.get_type() != Variant::NIL && p2.value.get_type() != Variant::OBJECT;
  784. Variant v2 = (v2_use_value) ? p2.value : Variant::construct(p2.type, NULL, 0, ce);
  785. // avoid potential invalid ops
  786. if ((vop == Variant::OP_DIVIDE || vop == Variant::OP_MODULE) && v2.get_type() == Variant::INT) {
  787. v2 = 1;
  788. v2_use_value = false;
  789. }
  790. if (vop == Variant::OP_DIVIDE && v2.get_type() == Variant::REAL) {
  791. v2 = 1.0;
  792. v2_use_value = false;
  793. }
  794. Variant r;
  795. bool valid;
  796. Variant::evaluate(vop, v1, v2, r, valid);
  797. if (!valid)
  798. return false;
  799. r_type.type = r.get_type();
  800. if (v1_use_value && v2_use_value)
  801. r_type.value = r;
  802. return true;
  803. }
  804. }
  805. return false;
  806. }
  807. static bool _guess_identifier_type_in_block(GDScriptCompletionContext &context, int p_line, const StringName &p_identifier, GDScriptCompletionIdentifier &r_type) {
  808. if (context.block->if_condition && context.block->if_condition->type == GDScriptParser::Node::TYPE_OPERATOR && static_cast<const GDScriptParser::OperatorNode *>(context.block->if_condition)->op == GDScriptParser::OperatorNode::OP_IS) {
  809. //is used, check if identifier is in there! this helps resolve in blocks that are (if (identifier is value)): which are very common..
  810. //super dirty hack, but very useful
  811. //credit: Zylann
  812. //TODO: this could be hacked to detect ANDed conditions too..
  813. const GDScriptParser::OperatorNode *op = static_cast<const GDScriptParser::OperatorNode *>(context.block->if_condition);
  814. if (op->arguments[0]->type == GDScriptParser::Node::TYPE_IDENTIFIER && static_cast<const GDScriptParser::IdentifierNode *>(op->arguments[0])->name == p_identifier) {
  815. //bingo
  816. if (_guess_expression_type(context, op->arguments[1], op->line, r_type)) {
  817. return true;
  818. }
  819. }
  820. }
  821. GDScriptCompletionIdentifier gdi = _get_native_class(context);
  822. if (gdi.obj_type != StringName()) {
  823. bool valid;
  824. Variant::Type t = ClassDB::get_property_type(gdi.obj_type, p_identifier, &valid);
  825. if (t != Variant::NIL && valid) {
  826. r_type.type = t;
  827. if (t == Variant::INT) {
  828. //check for enum!
  829. #if defined(DEBUG_METHODS_ENABLED) && defined(TOOLS_ENABLED)
  830. StringName getter = ClassDB::get_property_getter(gdi.obj_type, p_identifier);
  831. if (getter != StringName()) {
  832. MethodBind *mb = ClassDB::get_method(gdi.obj_type, getter);
  833. if (mb) {
  834. PropertyInfo rt = mb->get_return_info();
  835. if (rt.usage & PROPERTY_USAGE_CLASS_IS_ENUM) {
  836. r_type.enumeration = rt.class_name;
  837. }
  838. }
  839. }
  840. #endif
  841. }
  842. return true;
  843. }
  844. }
  845. const GDScriptParser::Node *last_assign = NULL;
  846. int last_assign_line = -1;
  847. for (int i = 0; i < context.block->statements.size(); i++) {
  848. if (context.block->statements[i]->line > p_line)
  849. continue;
  850. if (context.block->statements[i]->type == GDScriptParser::BlockNode::TYPE_LOCAL_VAR) {
  851. const GDScriptParser::LocalVarNode *lv = static_cast<const GDScriptParser::LocalVarNode *>(context.block->statements[i]);
  852. if (lv->assign && lv->name == p_identifier) {
  853. last_assign = lv->assign;
  854. last_assign_line = context.block->statements[i]->line;
  855. }
  856. }
  857. if (context.block->statements[i]->type == GDScriptParser::BlockNode::TYPE_OPERATOR) {
  858. const GDScriptParser::OperatorNode *op = static_cast<const GDScriptParser::OperatorNode *>(context.block->statements[i]);
  859. if (op->op == GDScriptParser::OperatorNode::OP_ASSIGN) {
  860. if (op->arguments.size() && op->arguments[0]->type == GDScriptParser::Node::TYPE_IDENTIFIER) {
  861. const GDScriptParser::IdentifierNode *id = static_cast<const GDScriptParser::IdentifierNode *>(op->arguments[0]);
  862. if (id->name == p_identifier) {
  863. last_assign = op->arguments[1];
  864. last_assign_line = context.block->statements[i]->line;
  865. }
  866. }
  867. }
  868. }
  869. }
  870. //use the last assignment, (then backwards?)
  871. if (last_assign && last_assign_line != p_line) {
  872. return _guess_expression_type(context, last_assign, last_assign_line, r_type);
  873. }
  874. return false;
  875. }
  876. static bool _guess_identifier_from_assignment_in_function(GDScriptCompletionContext &context, int p_src_line, const StringName &p_identifier, const StringName &p_function, GDScriptCompletionIdentifier &r_type) {
  877. const GDScriptParser::FunctionNode *func = NULL;
  878. for (int i = 0; i < context._class->functions.size(); i++) {
  879. if (context._class->functions[i]->name == p_function) {
  880. func = context._class->functions[i];
  881. break;
  882. }
  883. }
  884. if (!func)
  885. return false;
  886. for (int i = 0; i < func->body->statements.size(); i++) {
  887. if (func->body->statements[i]->line == p_src_line) {
  888. break;
  889. }
  890. if (func->body->statements[i]->type == GDScriptParser::BlockNode::TYPE_OPERATOR) {
  891. const GDScriptParser::OperatorNode *op = static_cast<const GDScriptParser::OperatorNode *>(func->body->statements[i]);
  892. if (op->op == GDScriptParser::OperatorNode::OP_ASSIGN) {
  893. if (op->arguments.size() && op->arguments[0]->type == GDScriptParser::Node::TYPE_IDENTIFIER) {
  894. const GDScriptParser::IdentifierNode *id = static_cast<const GDScriptParser::IdentifierNode *>(op->arguments[0]);
  895. if (id->name == p_identifier) {
  896. return _guess_expression_type(context, op->arguments[1], func->body->statements[i]->line, r_type);
  897. }
  898. }
  899. }
  900. }
  901. }
  902. return false;
  903. }
  904. static bool _guess_identifier_type(GDScriptCompletionContext &context, int p_line, const StringName &p_identifier, GDScriptCompletionIdentifier &r_type, bool p_for_indexing) {
  905. //go to block first
  906. const GDScriptParser::BlockNode *block = context.block;
  907. while (block) {
  908. GDScriptCompletionContext c = context;
  909. c.block = block;
  910. if (_guess_identifier_type_in_block(c, p_line, p_identifier, r_type)) {
  911. return true;
  912. }
  913. block = block->parent_block;
  914. }
  915. //guess from argument if virtual
  916. if (context.function && context.function->name != StringName()) {
  917. int argindex = -1;
  918. for (int i = 0; i < context.function->arguments.size(); i++) {
  919. if (context.function->arguments[i] == p_identifier) {
  920. argindex = i;
  921. break;
  922. }
  923. }
  924. if (argindex != -1) {
  925. GDScriptCompletionIdentifier id = _get_native_class(context);
  926. if (id.type == Variant::OBJECT && id.obj_type != StringName()) {
  927. //this kinda sucks but meh
  928. List<MethodInfo> vmethods;
  929. ClassDB::get_virtual_methods(id.obj_type, &vmethods);
  930. for (List<MethodInfo>::Element *E = vmethods.front(); E; E = E->next()) {
  931. if (E->get().name == context.function->name && argindex < E->get().arguments.size()) {
  932. PropertyInfo arg = E->get().arguments[argindex];
  933. int scp = String(arg.name).find(":");
  934. if (scp != -1) {
  935. r_type.type = Variant::OBJECT;
  936. r_type.obj_type = String(arg.name).substr(scp + 1, String(arg.name).length());
  937. return true;
  938. } else {
  939. r_type.type = arg.type;
  940. if (arg.hint == PROPERTY_HINT_RESOURCE_TYPE)
  941. r_type.obj_type = arg.hint_string;
  942. return true;
  943. }
  944. }
  945. }
  946. }
  947. }
  948. }
  949. //guess type in constant
  950. for (int i = 0; i < context._class->constant_expressions.size(); i++) {
  951. if (context._class->constant_expressions[i].identifier == p_identifier) {
  952. ERR_FAIL_COND_V(context._class->constant_expressions[i].expression->type != GDScriptParser::Node::TYPE_CONSTANT, false);
  953. r_type = _get_type_from_variant(static_cast<const GDScriptParser::ConstantNode *>(context._class->constant_expressions[i].expression)->value);
  954. return true;
  955. }
  956. }
  957. if (!(context.function && context.function->_static)) {
  958. for (int i = 0; i < context._class->variables.size(); i++) {
  959. if (context._class->variables[i].identifier == p_identifier) {
  960. if (context._class->variables[i]._export.type != Variant::NIL) {
  961. r_type = _get_type_from_pinfo(context._class->variables[i]._export);
  962. return true;
  963. } else if (context._class->variables[i].expression) {
  964. if (p_line <= context._class->variables[i].line)
  965. return false;
  966. bool rtype = _guess_expression_type(context, context._class->variables[i].expression, context._class->variables[i].line, r_type);
  967. if (rtype && r_type.type != Variant::NIL)
  968. return true;
  969. //return _guess_expression_type(context,context._class->variables[i].expression,context._class->variables[i].line,r_type);
  970. }
  971. //try to guess from assignment in constructor or _ready
  972. if (_guess_identifier_from_assignment_in_function(context, p_line + 1, p_identifier, "_ready", r_type))
  973. return true;
  974. if (_guess_identifier_from_assignment_in_function(context, p_line + 1, p_identifier, "_enter_tree", r_type))
  975. return true;
  976. if (_guess_identifier_from_assignment_in_function(context, p_line + 1, p_identifier, "_init", r_type))
  977. return true;
  978. return false;
  979. }
  980. }
  981. }
  982. //autoloads as singletons
  983. List<PropertyInfo> props;
  984. ProjectSettings::get_singleton()->get_property_list(&props);
  985. for (List<PropertyInfo>::Element *E = props.front(); E; E = E->next()) {
  986. String s = E->get().name;
  987. if (!s.begins_with("autoload/"))
  988. continue;
  989. String name = s.get_slice("/", 1);
  990. if (name == String(p_identifier)) {
  991. String path = ProjectSettings::get_singleton()->get(s);
  992. if (path.begins_with("*")) {
  993. String script = path.substr(1, path.length());
  994. if (!script.ends_with(".gd")) {
  995. //not a script, try find the script anyway,
  996. //may have some success
  997. script = script.get_basename() + ".gd";
  998. }
  999. if (FileAccess::exists(script)) {
  1000. //print_line("is a script");
  1001. Ref<Script> scr;
  1002. if (ScriptCodeCompletionCache::get_singleton())
  1003. scr = ScriptCodeCompletionCache::get_singleton()->get_cached_resource(script);
  1004. else
  1005. scr = ResourceLoader::load(script);
  1006. r_type.obj_type = "Node";
  1007. r_type.type = Variant::OBJECT;
  1008. r_type.script = scr;
  1009. r_type.value = Variant();
  1010. return true;
  1011. }
  1012. }
  1013. }
  1014. }
  1015. //global
  1016. for (Map<StringName, int>::Element *E = GDScriptLanguage::get_singleton()->get_global_map().front(); E; E = E->next()) {
  1017. if (E->key() == p_identifier) {
  1018. r_type = _get_type_from_variant(GDScriptLanguage::get_singleton()->get_global_array()[E->get()], !p_for_indexing);
  1019. return true;
  1020. }
  1021. }
  1022. return false;
  1023. }
  1024. static void _find_identifiers_in_block(GDScriptCompletionContext &context, int p_line, bool p_only_functions, Set<String> &result) {
  1025. if (p_only_functions)
  1026. return;
  1027. for (int i = 0; i < context.block->statements.size(); i++) {
  1028. GDScriptParser::Node *statement = context.block->statements[i];
  1029. if (statement->line > p_line)
  1030. continue;
  1031. GDScriptParser::BlockNode::Type statementType = statement->type;
  1032. if (statementType == GDScriptParser::BlockNode::TYPE_LOCAL_VAR) {
  1033. const GDScriptParser::LocalVarNode *lv = static_cast<const GDScriptParser::LocalVarNode *>(statement);
  1034. result.insert(lv->name.operator String());
  1035. } else if (statementType == GDScriptParser::BlockNode::TYPE_CONTROL_FLOW) {
  1036. const GDScriptParser::ControlFlowNode *cf = static_cast<const GDScriptParser::ControlFlowNode *>(statement);
  1037. if (cf->cf_type == GDScriptParser::ControlFlowNode::CF_FOR) {
  1038. const GDScriptParser::IdentifierNode *id = static_cast<const GDScriptParser::IdentifierNode *>(cf->arguments[0]);
  1039. result.insert(id->name.operator String());
  1040. }
  1041. }
  1042. }
  1043. }
  1044. static void _find_identifiers_in_class(GDScriptCompletionContext &context, bool p_static, bool p_only_functions, Set<String> &result) {
  1045. if (!p_static && !p_only_functions) {
  1046. for (int i = 0; i < context._class->variables.size(); i++) {
  1047. result.insert(context._class->variables[i].identifier);
  1048. }
  1049. }
  1050. if (!p_only_functions) {
  1051. for (int i = 0; i < context._class->constant_expressions.size(); i++) {
  1052. result.insert(context._class->constant_expressions[i].identifier);
  1053. }
  1054. for (int i = 0; i < context._class->subclasses.size(); i++) {
  1055. result.insert(context._class->subclasses[i]->name);
  1056. }
  1057. }
  1058. for (int i = 0; i < context._class->static_functions.size(); i++) {
  1059. if (context._class->static_functions[i]->arguments.size())
  1060. result.insert(context._class->static_functions[i]->name.operator String() + "(");
  1061. else
  1062. result.insert(context._class->static_functions[i]->name.operator String() + "()");
  1063. }
  1064. if (!p_static) {
  1065. for (int i = 0; i < context._class->functions.size(); i++) {
  1066. if (context._class->functions[i]->arguments.size())
  1067. result.insert(context._class->functions[i]->name.operator String() + "(");
  1068. else
  1069. result.insert(context._class->functions[i]->name.operator String() + "()");
  1070. }
  1071. }
  1072. //globals
  1073. Ref<Reference> base = _get_parent_class(context);
  1074. while (true) {
  1075. Ref<GDScript> script = base;
  1076. Ref<GDScriptNativeClass> nc = base;
  1077. if (script.is_valid()) {
  1078. if (!p_static && !p_only_functions) {
  1079. for (const Set<StringName>::Element *E = script->get_members().front(); E; E = E->next()) {
  1080. result.insert(E->get().operator String());
  1081. }
  1082. }
  1083. if (!p_only_functions) {
  1084. for (const Map<StringName, Variant>::Element *E = script->get_constants().front(); E; E = E->next()) {
  1085. result.insert(E->key().operator String());
  1086. }
  1087. }
  1088. for (const Map<StringName, GDScriptFunction *>::Element *E = script->get_member_functions().front(); E; E = E->next()) {
  1089. if (!p_static || E->get()->is_static()) {
  1090. if (E->get()->get_argument_count())
  1091. result.insert(E->key().operator String() + "(");
  1092. else
  1093. result.insert(E->key().operator String() + "()");
  1094. }
  1095. }
  1096. if (!p_only_functions) {
  1097. for (const Map<StringName, Ref<GDScript> >::Element *E = script->get_subclasses().front(); E; E = E->next()) {
  1098. result.insert(E->key().operator String());
  1099. }
  1100. }
  1101. base = script->get_base();
  1102. if (base.is_null())
  1103. base = script->get_native();
  1104. } else if (nc.is_valid()) {
  1105. StringName type = nc->get_name();
  1106. if (!p_only_functions) {
  1107. List<String> constants;
  1108. ClassDB::get_integer_constant_list(type, &constants);
  1109. for (List<String>::Element *E = constants.front(); E; E = E->next()) {
  1110. result.insert(E->get());
  1111. }
  1112. List<PropertyInfo> pinfo;
  1113. ClassDB::get_property_list(type, &pinfo);
  1114. for (List<PropertyInfo>::Element *E = pinfo.front(); E; E = E->next()) {
  1115. if (E->get().usage & (PROPERTY_USAGE_GROUP | PROPERTY_USAGE_CATEGORY))
  1116. continue;
  1117. if (String(E->get().name).find("/") != -1)
  1118. continue;
  1119. result.insert(E->get().name);
  1120. }
  1121. }
  1122. List<MethodInfo> methods;
  1123. ClassDB::get_method_list(type, &methods, false, true);
  1124. for (List<MethodInfo>::Element *E = methods.front(); E; E = E->next()) {
  1125. if (E->get().name.begins_with("_"))
  1126. continue;
  1127. if (E->get().arguments.size())
  1128. result.insert(E->get().name + "(");
  1129. else
  1130. result.insert(E->get().name + "()");
  1131. }
  1132. break;
  1133. } else
  1134. break;
  1135. }
  1136. }
  1137. static void _find_identifiers(GDScriptCompletionContext &context, int p_line, bool p_only_functions, Set<String> &result) {
  1138. const GDScriptParser::BlockNode *block = context.block;
  1139. if (context.function) {
  1140. const GDScriptParser::FunctionNode *f = context.function;
  1141. for (int i = 0; i < f->arguments.size(); i++) {
  1142. result.insert(f->arguments[i].operator String());
  1143. }
  1144. }
  1145. while (block) {
  1146. GDScriptCompletionContext c = context;
  1147. c.block = block;
  1148. _find_identifiers_in_block(c, p_line, p_only_functions, result);
  1149. block = block->parent_block;
  1150. }
  1151. const GDScriptParser::ClassNode *clss = context._class;
  1152. bool _static = context.function && context.function->_static;
  1153. while (clss) {
  1154. GDScriptCompletionContext c = context;
  1155. c._class = clss;
  1156. c.block = NULL;
  1157. c.function = NULL;
  1158. _find_identifiers_in_class(c, _static, p_only_functions, result);
  1159. clss = clss->owner;
  1160. }
  1161. for (int i = 0; i < GDScriptFunctions::FUNC_MAX; i++) {
  1162. result.insert(GDScriptFunctions::get_func_name(GDScriptFunctions::Function(i)));
  1163. }
  1164. static const char *_type_names[Variant::VARIANT_MAX] = {
  1165. "null", "bool", "int", "float", "String", "Vector2", "Rect2", "Vector3", "Transform2D", "Plane", "Quat", "AABB", "Basis", "Transform",
  1166. "Color", "NodePath", "RID", "Object", "Dictionary", "Array", "PoolByteArray", "PoolIntArray", "PoolRealArray", "PoolStringArray",
  1167. "PoolVector2Array", "PoolVector3Array", "PoolColorArray"
  1168. };
  1169. for (int i = 0; i < Variant::VARIANT_MAX; i++) {
  1170. result.insert(_type_names[i]);
  1171. }
  1172. List<String> reserved_words;
  1173. GDScriptLanguage::get_singleton()->get_reserved_words(&reserved_words);
  1174. for (List<String>::Element *E = reserved_words.front(); E; E = E->next()) {
  1175. result.insert(E->get());
  1176. }
  1177. //autoload singletons
  1178. List<PropertyInfo> props;
  1179. ProjectSettings::get_singleton()->get_property_list(&props);
  1180. for (List<PropertyInfo>::Element *E = props.front(); E; E = E->next()) {
  1181. String s = E->get().name;
  1182. if (!s.begins_with("autoload/"))
  1183. continue;
  1184. String name = s.get_slice("/", 1);
  1185. String path = ProjectSettings::get_singleton()->get(s);
  1186. if (path.begins_with("*")) {
  1187. result.insert(name);
  1188. }
  1189. }
  1190. for (const Map<StringName, int>::Element *E = GDScriptLanguage::get_singleton()->get_global_map().front(); E; E = E->next()) {
  1191. result.insert(E->key().operator String());
  1192. }
  1193. }
  1194. static String _get_visual_datatype(const PropertyInfo &p_info, bool p_isarg = true) {
  1195. String n = p_info.name;
  1196. int idx = n.find(":");
  1197. if (idx != -1) {
  1198. return n.substr(idx + 1, n.length());
  1199. }
  1200. if (p_info.type == Variant::OBJECT && p_info.hint == PROPERTY_HINT_RESOURCE_TYPE)
  1201. return p_info.hint_string;
  1202. if (p_info.type == Variant::NIL) {
  1203. if (p_isarg)
  1204. return "var";
  1205. else
  1206. return "void";
  1207. }
  1208. return Variant::get_type_name(p_info.type);
  1209. }
  1210. static void _make_function_hint(const GDScriptParser::FunctionNode *p_func, int p_argidx, String &arghint) {
  1211. arghint = "func " + p_func->name + "(";
  1212. for (int i = 0; i < p_func->arguments.size(); i++) {
  1213. if (i > 0)
  1214. arghint += ", ";
  1215. else
  1216. arghint += " ";
  1217. if (i == p_argidx) {
  1218. arghint += String::chr(0xFFFF);
  1219. }
  1220. arghint += p_func->arguments[i].operator String();
  1221. int deffrom = p_func->arguments.size() - p_func->default_values.size();
  1222. if (i >= deffrom) {
  1223. int defidx = deffrom - i;
  1224. if (defidx >= 0 && defidx < p_func->default_values.size()) {
  1225. if (p_func->default_values[defidx]->type == GDScriptParser::Node::TYPE_OPERATOR) {
  1226. const GDScriptParser::OperatorNode *op = static_cast<const GDScriptParser::OperatorNode *>(p_func->default_values[defidx]);
  1227. if (op->op == GDScriptParser::OperatorNode::OP_ASSIGN) {
  1228. const GDScriptParser::ConstantNode *cn = static_cast<const GDScriptParser::ConstantNode *>(op->arguments[1]);
  1229. arghint += "=" + cn->value.get_construct_string();
  1230. }
  1231. } else {
  1232. }
  1233. }
  1234. }
  1235. if (i == p_argidx) {
  1236. arghint += String::chr(0xFFFF);
  1237. }
  1238. }
  1239. if (p_func->arguments.size() > 0)
  1240. arghint += " ";
  1241. arghint += ")";
  1242. }
  1243. void get_directory_contents(EditorFileSystemDirectory *p_dir, Set<String> &r_list) {
  1244. for (int i = 0; i < p_dir->get_subdir_count(); i++) {
  1245. get_directory_contents(p_dir->get_subdir(i), r_list);
  1246. }
  1247. for (int i = 0; i < p_dir->get_file_count(); i++) {
  1248. r_list.insert("\"" + p_dir->get_file_path(i) + "\"");
  1249. }
  1250. }
  1251. static void _find_type_arguments(GDScriptCompletionContext &context, const GDScriptParser::Node *p_node, int p_line, const StringName &p_method, const GDScriptCompletionIdentifier &id, int p_argidx, Set<String> &result, bool &r_forced, String &arghint) {
  1252. //print_line("find type arguments?");
  1253. if (id.type == Variant::OBJECT && id.obj_type != StringName()) {
  1254. MethodBind *m = ClassDB::get_method(id.obj_type, p_method);
  1255. if (!m) {
  1256. //not in static method, see script
  1257. //print_line("not in static: "+String(p_method));
  1258. Ref<GDScript> on_script;
  1259. if (id.value.get_type()) {
  1260. Object *obj = id.value;
  1261. GDScript *scr = Object::cast_to<GDScript>(obj);
  1262. if (scr) {
  1263. while (scr) {
  1264. for (const Map<StringName, GDScriptFunction *>::Element *E = scr->get_member_functions().front(); E; E = E->next()) {
  1265. if (E->get()->is_static() && p_method == E->get()->get_name()) {
  1266. arghint = "static func " + String(p_method) + "(";
  1267. for (int i = 0; i < E->get()->get_argument_count(); i++) {
  1268. if (i > 0)
  1269. arghint += ", ";
  1270. else
  1271. arghint += " ";
  1272. if (i == p_argidx) {
  1273. arghint += String::chr(0xFFFF);
  1274. }
  1275. arghint += "var " + E->get()->get_argument_name(i);
  1276. int deffrom = E->get()->get_argument_count() - E->get()->get_default_argument_count();
  1277. if (i >= deffrom) {
  1278. int defidx = deffrom - i;
  1279. if (defidx >= 0 && defidx < E->get()->get_default_argument_count()) {
  1280. arghint += "=" + E->get()->get_default_argument(defidx).get_construct_string();
  1281. }
  1282. }
  1283. if (i == p_argidx) {
  1284. arghint += String::chr(0xFFFF);
  1285. }
  1286. }
  1287. arghint += ")";
  1288. return; //found
  1289. }
  1290. }
  1291. if (scr->get_base().is_valid())
  1292. scr = scr->get_base().ptr();
  1293. else
  1294. scr = NULL;
  1295. }
  1296. } else {
  1297. if (obj) {
  1298. on_script = obj->get_script();
  1299. }
  1300. }
  1301. }
  1302. //print_line("but it has a script?");
  1303. if (!on_script.is_valid() && id.script.is_valid()) {
  1304. //print_line("yes");
  1305. on_script = id.script;
  1306. }
  1307. if (on_script.is_valid()) {
  1308. GDScript *scr = on_script.ptr();
  1309. if (scr) {
  1310. while (scr) {
  1311. String code = scr->get_source_code();
  1312. //print_line("has source code!");
  1313. if (code != "") {
  1314. //if there is code, parse it. This way is slower but updates in real-time
  1315. GDScriptParser p;
  1316. //Error parse(const String& p_code, const String& p_base_path="", bool p_just_validate=false,const String& p_self_path="",bool p_for_completion=false);
  1317. Error err = p.parse(scr->get_source_code(), scr->get_path().get_base_dir(), true, "", false);
  1318. if (err == OK) {
  1319. //print_line("checking the functions...");
  1320. //only if ok, otherwise use what is cached on the script
  1321. //GDScriptParser::ClassNode *base = p.
  1322. const GDScriptParser::Node *root = p.get_parse_tree();
  1323. ERR_FAIL_COND(root->type != GDScriptParser::Node::TYPE_CLASS);
  1324. const GDScriptParser::ClassNode *cl = static_cast<const GDScriptParser::ClassNode *>(root);
  1325. const GDScriptParser::FunctionNode *func = NULL;
  1326. bool st = false;
  1327. for (int i = 0; i < cl->functions.size(); i++) {
  1328. //print_line(String(cl->functions[i]->name)+" vs "+String(p_method));
  1329. if (cl->functions[i]->name == p_method) {
  1330. func = cl->functions[i];
  1331. }
  1332. }
  1333. for (int i = 0; i < cl->static_functions.size(); i++) {
  1334. //print_line(String(cl->static_functions[i]->name)+" vs "+String(p_method));
  1335. if (cl->static_functions[i]->name == p_method) {
  1336. func = cl->static_functions[i];
  1337. st = true;
  1338. }
  1339. }
  1340. if (func) {
  1341. arghint = "func " + String(p_method) + "(";
  1342. if (st)
  1343. arghint = "static " + arghint;
  1344. for (int i = 0; i < func->arguments.size(); i++) {
  1345. if (i > 0)
  1346. arghint += ", ";
  1347. else
  1348. arghint += " ";
  1349. if (i == p_argidx) {
  1350. arghint += String::chr(0xFFFF);
  1351. }
  1352. arghint += "var " + String(func->arguments[i]);
  1353. int deffrom = func->arguments.size() - func->default_values.size();
  1354. if (i >= deffrom) {
  1355. int defidx = deffrom - i;
  1356. if (defidx >= 0 && defidx < func->default_values.size() && func->default_values[defidx]->type == GDScriptParser::Node::TYPE_OPERATOR) {
  1357. const GDScriptParser::OperatorNode *op = static_cast<const GDScriptParser::OperatorNode *>(func->default_values[defidx]);
  1358. if (op->op == GDScriptParser::OperatorNode::OP_ASSIGN) {
  1359. const GDScriptParser::ConstantNode *cn = static_cast<const GDScriptParser::ConstantNode *>(op->arguments[1]);
  1360. arghint += "=" + cn->value.get_construct_string();
  1361. }
  1362. }
  1363. }
  1364. if (i == p_argidx) {
  1365. arghint += String::chr(0xFFFF);
  1366. }
  1367. }
  1368. arghint += " )";
  1369. return;
  1370. }
  1371. } else {
  1372. //print_line("failed parsing?");
  1373. code = "";
  1374. }
  1375. }
  1376. if (code == "") {
  1377. for (const Map<StringName, GDScriptFunction *>::Element *E = scr->get_member_functions().front(); E; E = E->next()) {
  1378. if (p_method == E->get()->get_name()) {
  1379. arghint = "func " + String(p_method) + "(";
  1380. for (int i = 0; i < E->get()->get_argument_count(); i++) {
  1381. if (i > 0)
  1382. arghint += ", ";
  1383. else
  1384. arghint += " ";
  1385. if (i == p_argidx) {
  1386. arghint += String::chr(0xFFFF);
  1387. }
  1388. arghint += "var " + E->get()->get_argument_name(i);
  1389. int deffrom = E->get()->get_argument_count() - E->get()->get_default_argument_count();
  1390. if (i >= deffrom) {
  1391. int defidx = deffrom - i;
  1392. if (defidx >= 0 && defidx < E->get()->get_default_argument_count()) {
  1393. arghint += "=" + E->get()->get_default_argument(defidx).get_construct_string();
  1394. }
  1395. }
  1396. if (i == p_argidx) {
  1397. arghint += String::chr(0xFFFF);
  1398. }
  1399. }
  1400. arghint += ")";
  1401. return; //found
  1402. }
  1403. }
  1404. }
  1405. if (scr->get_base().is_valid())
  1406. scr = scr->get_base().ptr();
  1407. else
  1408. scr = NULL;
  1409. }
  1410. }
  1411. }
  1412. } else {
  1413. //regular method
  1414. #if defined(DEBUG_METHODS_ENABLED) && defined(TOOLS_ENABLED)
  1415. if (p_argidx < m->get_argument_count()) {
  1416. PropertyInfo pi = m->get_argument_info(p_argidx);
  1417. if (pi.usage & PROPERTY_USAGE_CLASS_IS_ENUM) {
  1418. String enumeration = pi.class_name;
  1419. if (enumeration.find(".") != -1) {
  1420. //class constant
  1421. List<StringName> constants;
  1422. String cls = enumeration.get_slice(".", 0);
  1423. String enm = enumeration.get_slice(".", 1);
  1424. ClassDB::get_enum_constants(cls, enm, &constants);
  1425. //constants.sort_custom<StringName::AlphCompare>();
  1426. for (List<StringName>::Element *E = constants.front(); E; E = E->next()) {
  1427. String add = cls + "." + E->get();
  1428. result.insert(add);
  1429. r_forced = true;
  1430. }
  1431. } else {
  1432. //global constant
  1433. StringName current_enum = enumeration;
  1434. for (int i = 0; i < GlobalConstants::get_global_constant_count(); i++) {
  1435. if (GlobalConstants::get_global_constant_enum(i) == current_enum) {
  1436. result.insert(GlobalConstants::get_global_constant_name(i));
  1437. r_forced = true;
  1438. }
  1439. }
  1440. //global
  1441. }
  1442. }
  1443. }
  1444. #endif
  1445. if (p_method.operator String() == "connect" || (p_method.operator String() == "emit_signal" && p_argidx == 0)) {
  1446. if (p_argidx == 0) {
  1447. List<MethodInfo> sigs;
  1448. ClassDB::get_signal_list(id.obj_type, &sigs);
  1449. if (id.script.is_valid()) {
  1450. id.script->get_script_signal_list(&sigs);
  1451. } else if (id.value.get_type() == Variant::OBJECT) {
  1452. Object *obj = id.value;
  1453. if (obj && !obj->get_script().is_null()) {
  1454. Ref<Script> scr = obj->get_script();
  1455. if (scr.is_valid()) {
  1456. scr->get_script_signal_list(&sigs);
  1457. }
  1458. }
  1459. }
  1460. for (List<MethodInfo>::Element *E = sigs.front(); E; E = E->next()) {
  1461. result.insert("\"" + E->get().name + "\"");
  1462. r_forced = true;
  1463. }
  1464. } else if (p_argidx == 2) {
  1465. if (context._class) {
  1466. for (int i = 0; i < context._class->functions.size(); i++) {
  1467. result.insert("\"" + context._class->functions[i]->name + "\"");
  1468. r_forced = true;
  1469. }
  1470. }
  1471. }
  1472. /*if (p_argidx==2) {
  1473. ERR_FAIL_COND(p_node->type!=GDScriptParser::Node::TYPE_OPERATOR);
  1474. const GDScriptParser::OperatorNode *op=static_cast<const GDScriptParser::OperatorNode *>(p_node);
  1475. if (op->arguments.size()>)
  1476. }*/
  1477. } else {
  1478. if (p_argidx == 0 && (String(p_method) == "get_node" || String(p_method) == "has_node") && ClassDB::is_parent_class(id.obj_type, "Node")) {
  1479. List<PropertyInfo> props;
  1480. ProjectSettings::get_singleton()->get_property_list(&props);
  1481. for (List<PropertyInfo>::Element *E = props.front(); E; E = E->next()) {
  1482. String s = E->get().name;
  1483. if (!s.begins_with("autoload/"))
  1484. continue;
  1485. //print_line("found "+s);
  1486. String name = s.get_slice("/", 1);
  1487. result.insert("\"/root/" + name + "\"");
  1488. r_forced = true;
  1489. }
  1490. }
  1491. Object *obj = id.value;
  1492. if (obj) {
  1493. List<String> options;
  1494. obj->get_argument_options(p_method, p_argidx, &options);
  1495. for (List<String>::Element *E = options.front(); E; E = E->next()) {
  1496. result.insert(E->get());
  1497. r_forced = true;
  1498. }
  1499. }
  1500. }
  1501. arghint = _get_visual_datatype(m->get_return_info(), false) + " " + p_method.operator String() + String("(");
  1502. for (int i = 0; i < m->get_argument_count(); i++) {
  1503. if (i > 0)
  1504. arghint += ", ";
  1505. else
  1506. arghint += " ";
  1507. if (i == p_argidx) {
  1508. arghint += String::chr(0xFFFF);
  1509. }
  1510. String n = m->get_argument_info(i).name;
  1511. int dp = n.find(":");
  1512. if (dp != -1)
  1513. n = n.substr(0, dp);
  1514. arghint += _get_visual_datatype(m->get_argument_info(i)) + " " + n;
  1515. int deffrom = m->get_argument_count() - m->get_default_argument_count();
  1516. if (i >= deffrom) {
  1517. int defidx = i - deffrom;
  1518. if (defidx >= 0 && defidx < m->get_default_argument_count()) {
  1519. Variant v = m->get_default_argument(i);
  1520. arghint += "=" + v.get_construct_string();
  1521. }
  1522. }
  1523. if (i == p_argidx) {
  1524. arghint += String::chr(0xFFFF);
  1525. }
  1526. }
  1527. if (m->get_argument_count() > 0)
  1528. arghint += " ";
  1529. arghint += ")";
  1530. }
  1531. }
  1532. }
  1533. static void _find_call_arguments(GDScriptCompletionContext &context, const GDScriptParser::Node *p_node, int p_line, int p_argidx, Set<String> &result, bool &r_forced, String &arghint) {
  1534. if (!p_node || p_node->type != GDScriptParser::Node::TYPE_OPERATOR) {
  1535. return;
  1536. }
  1537. const GDScriptParser::OperatorNode *op = static_cast<const GDScriptParser::OperatorNode *>(p_node);
  1538. if (op->op != GDScriptParser::OperatorNode::OP_CALL) {
  1539. return;
  1540. }
  1541. if (op->arguments[0]->type == GDScriptParser::Node::TYPE_BUILT_IN_FUNCTION) {
  1542. //complete built-in function
  1543. const GDScriptParser::BuiltInFunctionNode *fn = static_cast<const GDScriptParser::BuiltInFunctionNode *>(op->arguments[0]);
  1544. MethodInfo mi = GDScriptFunctions::get_info(fn->function);
  1545. if (mi.name == "load" && bool(EditorSettings::get_singleton()->get("text_editor/completion/complete_file_paths"))) {
  1546. get_directory_contents(EditorFileSystem::get_singleton()->get_filesystem(), result);
  1547. }
  1548. arghint = _get_visual_datatype(mi.return_val, false) + " " + GDScriptFunctions::get_func_name(fn->function) + String("(");
  1549. for (int i = 0; i < mi.arguments.size(); i++) {
  1550. if (i > 0)
  1551. arghint += ", ";
  1552. else
  1553. arghint += " ";
  1554. if (i == p_argidx || ((mi.flags & METHOD_FLAG_VARARG) && i > p_argidx)) {
  1555. arghint += String::chr(0xFFFF);
  1556. }
  1557. arghint += _get_visual_datatype(mi.arguments[i]) + " " + mi.arguments[i].name;
  1558. if (i == p_argidx || ((mi.flags & METHOD_FLAG_VARARG) && i > p_argidx)) {
  1559. arghint += String::chr(0xFFFF);
  1560. }
  1561. }
  1562. if (mi.arguments.size() > 0)
  1563. arghint += " ";
  1564. arghint += ")";
  1565. } else if (op->arguments[0]->type == GDScriptParser::Node::TYPE_TYPE) {
  1566. //complete constructor
  1567. const GDScriptParser::TypeNode *tn = static_cast<const GDScriptParser::TypeNode *>(op->arguments[0]);
  1568. List<MethodInfo> mil;
  1569. Variant::get_constructor_list(tn->vtype, &mil);
  1570. for (List<MethodInfo>::Element *E = mil.front(); E; E = E->next()) {
  1571. MethodInfo mi = E->get();
  1572. if (mi.arguments.size() == 0)
  1573. continue;
  1574. if (E->prev())
  1575. arghint += "\n";
  1576. arghint += Variant::get_type_name(tn->vtype) + " " + Variant::get_type_name(tn->vtype) + String("(");
  1577. for (int i = 0; i < mi.arguments.size(); i++) {
  1578. if (i > 0)
  1579. arghint += ", ";
  1580. else
  1581. arghint += " ";
  1582. if (i == p_argidx) {
  1583. arghint += String::chr(0xFFFF);
  1584. }
  1585. arghint += _get_visual_datatype(mi.arguments[i]) + " " + mi.arguments[i].name;
  1586. if (i == p_argidx) {
  1587. arghint += String::chr(0xFFFF);
  1588. }
  1589. }
  1590. if (mi.arguments.size() > 0)
  1591. arghint += " ";
  1592. arghint += ")";
  1593. }
  1594. } else if (op->arguments.size() >= 2 && op->arguments[1]->type == GDScriptParser::Node::TYPE_IDENTIFIER) {
  1595. //make sure identifier exists...
  1596. const GDScriptParser::IdentifierNode *id = static_cast<const GDScriptParser::IdentifierNode *>(op->arguments[1]);
  1597. if (op->arguments[0]->type == GDScriptParser::Node::TYPE_SELF) {
  1598. //self, look up
  1599. for (int i = 0; i < context._class->static_functions.size(); i++) {
  1600. if (context._class->static_functions[i]->name == id->name) {
  1601. _make_function_hint(context._class->static_functions[i], p_argidx, arghint);
  1602. return;
  1603. }
  1604. }
  1605. if (context.function && !context.function->_static) {
  1606. for (int i = 0; i < context._class->functions.size(); i++) {
  1607. if (context._class->functions[i]->name == id->name) {
  1608. _make_function_hint(context._class->functions[i], p_argidx, arghint);
  1609. return;
  1610. }
  1611. }
  1612. }
  1613. Ref<Reference> base = _get_parent_class(context);
  1614. while (true) {
  1615. Ref<GDScript> script = base;
  1616. Ref<GDScriptNativeClass> nc = base;
  1617. if (script.is_valid()) {
  1618. for (const Map<StringName, GDScriptFunction *>::Element *E = script->get_member_functions().front(); E; E = E->next()) {
  1619. if (E->key() == id->name) {
  1620. if (context.function && context.function->_static && !E->get()->is_static())
  1621. continue;
  1622. arghint = "func " + id->name.operator String() + String("(");
  1623. for (int i = 0; i < E->get()->get_argument_count(); i++) {
  1624. if (i > 0)
  1625. arghint += ", ";
  1626. else
  1627. arghint += " ";
  1628. if (i == p_argidx) {
  1629. arghint += String::chr(0xFFFF);
  1630. }
  1631. arghint += E->get()->get_argument_name(i);
  1632. int deffrom = E->get()->get_argument_count() - E->get()->get_default_argument_count();
  1633. if (i >= deffrom) {
  1634. int defidx = deffrom - i;
  1635. if (defidx >= 0 && defidx < E->get()->get_default_argument_count()) {
  1636. arghint += "=" + E->get()->get_default_argument(defidx).get_construct_string();
  1637. }
  1638. }
  1639. if (i == p_argidx) {
  1640. arghint += String::chr(0xFFFF);
  1641. }
  1642. }
  1643. if (E->get()->get_argument_count() > 0)
  1644. arghint += " ";
  1645. arghint += ")";
  1646. return;
  1647. }
  1648. }
  1649. base = script->get_base();
  1650. if (base.is_null())
  1651. base = script->get_native();
  1652. } else if (nc.is_valid()) {
  1653. if (!(context.function && context.function->_static)) {
  1654. GDScriptCompletionIdentifier ci;
  1655. ci.type = Variant::OBJECT;
  1656. ci.obj_type = nc->get_name();
  1657. if (!context._class->owner)
  1658. ci.value = context.base;
  1659. _find_type_arguments(context, p_node, p_line, id->name, ci, p_argidx, result, r_forced, arghint);
  1660. //guess type..
  1661. /*
  1662. List<MethodInfo> methods;
  1663. ClassDB::get_method_list(type,&methods);
  1664. for(List<MethodInfo>::Element *E=methods.front();E;E=E->next()) {
  1665. if (E->get().arguments.size())
  1666. result.insert(E->get().name+"(");
  1667. else
  1668. result.insert(E->get().name+"()");
  1669. }*/
  1670. }
  1671. break;
  1672. } else
  1673. break;
  1674. }
  1675. } else {
  1676. //indexed lookup
  1677. GDScriptCompletionIdentifier ci;
  1678. if (_guess_expression_type(context, op->arguments[0], p_line, ci)) {
  1679. _find_type_arguments(context, p_node, p_line, id->name, ci, p_argidx, result, r_forced, arghint);
  1680. return;
  1681. }
  1682. }
  1683. }
  1684. }
  1685. Error GDScriptLanguage::complete_code(const String &p_code, const String &p_base_path, Object *p_owner, List<String> *r_options, bool &r_forced, String &r_call_hint) {
  1686. GDScriptParser p;
  1687. p.parse(p_code, p_base_path, false, "", true);
  1688. bool isfunction = false;
  1689. Set<String> options;
  1690. r_forced = false;
  1691. GDScriptCompletionContext context;
  1692. context._class = p.get_completion_class();
  1693. context.block = p.get_completion_block();
  1694. context.function = p.get_completion_function();
  1695. context.base = p_owner;
  1696. context.base_path = p_base_path;
  1697. switch (p.get_completion_type()) {
  1698. case GDScriptParser::COMPLETION_NONE: {
  1699. } break;
  1700. case GDScriptParser::COMPLETION_BUILT_IN_TYPE_CONSTANT: {
  1701. List<StringName> constants;
  1702. Variant::get_numeric_constants_for_type(p.get_completion_built_in_constant(), &constants);
  1703. for (List<StringName>::Element *E = constants.front(); E; E = E->next()) {
  1704. options.insert(E->get().operator String());
  1705. }
  1706. } break;
  1707. case GDScriptParser::COMPLETION_FUNCTION:
  1708. isfunction = true;
  1709. case GDScriptParser::COMPLETION_IDENTIFIER: {
  1710. _find_identifiers(context, p.get_completion_line(), isfunction, options);
  1711. } break;
  1712. case GDScriptParser::COMPLETION_PARENT_FUNCTION: {
  1713. } break;
  1714. case GDScriptParser::COMPLETION_GET_NODE: {
  1715. if (p_owner) {
  1716. List<String> opts;
  1717. p_owner->get_argument_options("get_node", 0, &opts);
  1718. for (List<String>::Element *E = opts.front(); E; E = E->next()) {
  1719. String opt = E->get().strip_edges();
  1720. if (opt.is_quoted()) {
  1721. r_forced = true;
  1722. String idopt = opt.unquote();
  1723. if (idopt.replace("/", "_").is_valid_identifier()) {
  1724. options.insert(idopt);
  1725. } else {
  1726. options.insert(opt);
  1727. }
  1728. }
  1729. }
  1730. }
  1731. } break;
  1732. case GDScriptParser::COMPLETION_METHOD:
  1733. isfunction = true;
  1734. case GDScriptParser::COMPLETION_INDEX: {
  1735. const GDScriptParser::Node *node = p.get_completion_node();
  1736. if (node->type != GDScriptParser::Node::TYPE_OPERATOR)
  1737. break;
  1738. GDScriptCompletionIdentifier t;
  1739. if (_guess_expression_type(context, static_cast<const GDScriptParser::OperatorNode *>(node)->arguments[0], p.get_completion_line(), t, true)) {
  1740. if (t.type == Variant::OBJECT && t.obj_type == "GDScriptNativeClass") {
  1741. //native enum
  1742. Ref<GDScriptNativeClass> gdn = t.value;
  1743. if (gdn.is_valid()) {
  1744. StringName cn = gdn->get_name();
  1745. List<String> cnames;
  1746. ClassDB::get_integer_constant_list(cn, &cnames);
  1747. for (List<String>::Element *E = cnames.front(); E; E = E->next()) {
  1748. options.insert(E->get());
  1749. }
  1750. List<PropertyInfo> pinfo;
  1751. ClassDB::get_property_list(cn, &pinfo);
  1752. for (List<PropertyInfo>::Element *E = pinfo.front(); E; E = E->next()) {
  1753. if (E->get().usage & (PROPERTY_USAGE_GROUP | PROPERTY_USAGE_CATEGORY))
  1754. continue;
  1755. if (String(E->get().name).find("/") != -1)
  1756. continue;
  1757. options.insert(E->get().name);
  1758. }
  1759. }
  1760. } else if (t.type == Variant::OBJECT && t.obj_type != StringName()) {
  1761. Ref<GDScript> on_script;
  1762. if (t.value.get_type()) {
  1763. Object *obj = t.value;
  1764. GDScript *scr = Object::cast_to<GDScript>(obj);
  1765. if (scr) {
  1766. while (scr) {
  1767. if (!isfunction) {
  1768. for (const Map<StringName, Variant>::Element *E = scr->get_constants().front(); E; E = E->next()) {
  1769. options.insert(E->key());
  1770. }
  1771. }
  1772. for (const Map<StringName, GDScriptFunction *>::Element *E = scr->get_member_functions().front(); E; E = E->next()) {
  1773. if (E->get()->is_static())
  1774. options.insert(E->key());
  1775. }
  1776. if (scr->get_base().is_valid())
  1777. scr = scr->get_base().ptr();
  1778. else
  1779. scr = NULL;
  1780. }
  1781. } else {
  1782. if (obj) {
  1783. on_script = obj->get_script();
  1784. }
  1785. }
  1786. }
  1787. if (!on_script.is_valid() && t.script.is_valid()) {
  1788. on_script = t.script;
  1789. }
  1790. if (on_script.is_valid()) {
  1791. GDScript *scr = on_script.ptr();
  1792. if (scr) {
  1793. while (scr) {
  1794. String code = scr->get_source_code();
  1795. if (code != "") {
  1796. //if there is code, parse it. This way is slower but updates in real-time
  1797. GDScriptParser p;
  1798. Error err = p.parse(scr->get_source_code(), scr->get_path().get_base_dir(), true, "", false);
  1799. if (err == OK) {
  1800. //only if ok, otherwise use what is cached on the script
  1801. //GDScriptParser::ClassNode *base = p.
  1802. const GDScriptParser::Node *root = p.get_parse_tree();
  1803. ERR_FAIL_COND_V(root->type != GDScriptParser::Node::TYPE_CLASS, ERR_PARSE_ERROR);
  1804. const GDScriptParser::ClassNode *cl = static_cast<const GDScriptParser::ClassNode *>(root);
  1805. for (int i = 0; i < cl->functions.size(); i++) {
  1806. if (cl->functions[i]->arguments.size())
  1807. options.insert(String(cl->functions[i]->name) + "(");
  1808. else
  1809. options.insert(String(cl->functions[i]->name) + "()");
  1810. }
  1811. for (int i = 0; i < cl->static_functions.size(); i++) {
  1812. if (cl->static_functions[i]->arguments.size())
  1813. options.insert(String(cl->static_functions[i]->name) + "(");
  1814. else
  1815. options.insert(String(cl->static_functions[i]->name) + "()");
  1816. }
  1817. if (!isfunction) {
  1818. for (int i = 0; i < cl->variables.size(); i++) {
  1819. options.insert(String(cl->variables[i].identifier));
  1820. }
  1821. for (int i = 0; i < cl->constant_expressions.size(); i++) {
  1822. options.insert(String(cl->constant_expressions[i].identifier));
  1823. }
  1824. }
  1825. } else {
  1826. code = ""; //well, then no code
  1827. }
  1828. }
  1829. if (code == "") {
  1830. //use class directly, no code was found
  1831. if (!isfunction) {
  1832. for (const Map<StringName, Variant>::Element *E = scr->get_constants().front(); E; E = E->next()) {
  1833. options.insert(E->key());
  1834. }
  1835. }
  1836. for (const Map<StringName, GDScriptFunction *>::Element *E = scr->get_member_functions().front(); E; E = E->next()) {
  1837. if (E->get()->get_argument_count())
  1838. options.insert(String(E->key()) + "()");
  1839. else
  1840. options.insert(String(E->key()) + "(");
  1841. }
  1842. for (const Set<StringName>::Element *E = scr->get_members().front(); E; E = E->next()) {
  1843. options.insert(E->get());
  1844. }
  1845. }
  1846. if (scr->get_base().is_valid())
  1847. scr = scr->get_base().ptr();
  1848. else
  1849. scr = NULL;
  1850. }
  1851. }
  1852. }
  1853. if (!isfunction) {
  1854. ClassDB::get_integer_constant_list(t.obj_type, r_options);
  1855. List<PropertyInfo> pinfo;
  1856. ClassDB::get_property_list(t.obj_type, &pinfo);
  1857. for (List<PropertyInfo>::Element *E = pinfo.front(); E; E = E->next()) {
  1858. if (E->get().usage & (PROPERTY_USAGE_GROUP | PROPERTY_USAGE_CATEGORY))
  1859. continue;
  1860. if (String(E->get().name).find("/") != -1)
  1861. continue;
  1862. r_options->push_back(E->get().name);
  1863. }
  1864. }
  1865. List<MethodInfo> mi;
  1866. ClassDB::get_method_list(t.obj_type, &mi, false, true);
  1867. for (List<MethodInfo>::Element *E = mi.front(); E; E = E->next()) {
  1868. if (E->get().name.begins_with("_"))
  1869. continue;
  1870. if (E->get().arguments.size())
  1871. options.insert(E->get().name + "(");
  1872. else
  1873. options.insert(E->get().name + "()");
  1874. }
  1875. } else {
  1876. //check InputEvent hint
  1877. {
  1878. if (t.value.get_type() == Variant::NIL) {
  1879. Variant::CallError ce;
  1880. t.value = Variant::construct(t.type, NULL, 0, ce);
  1881. }
  1882. if (!isfunction) {
  1883. List<PropertyInfo> pl;
  1884. t.value.get_property_list(&pl);
  1885. for (List<PropertyInfo>::Element *E = pl.front(); E; E = E->next()) {
  1886. if (String(E->get().name).find("/") == -1)
  1887. options.insert(E->get().name);
  1888. }
  1889. }
  1890. List<MethodInfo> mi;
  1891. t.value.get_method_list(&mi);
  1892. for (List<MethodInfo>::Element *E = mi.front(); E; E = E->next()) {
  1893. if (E->get().arguments.size())
  1894. options.insert(E->get().name + "(");
  1895. else
  1896. options.insert(E->get().name + "()");
  1897. }
  1898. }
  1899. }
  1900. }
  1901. } break;
  1902. case GDScriptParser::COMPLETION_CALL_ARGUMENTS: {
  1903. _find_call_arguments(context, p.get_completion_node(), p.get_completion_line(), p.get_completion_argument_index(), options, r_forced, r_call_hint);
  1904. } break;
  1905. case GDScriptParser::COMPLETION_VIRTUAL_FUNC: {
  1906. GDScriptCompletionIdentifier cid = _get_native_class(context);
  1907. if (cid.obj_type != StringName()) {
  1908. List<MethodInfo> vm;
  1909. ClassDB::get_virtual_methods(cid.obj_type, &vm);
  1910. for (List<MethodInfo>::Element *E = vm.front(); E; E = E->next()) {
  1911. MethodInfo &mi = E->get();
  1912. String m = mi.name;
  1913. if (m.find(":") != -1)
  1914. m = m.substr(0, m.find(":"));
  1915. m += "(";
  1916. if (mi.arguments.size()) {
  1917. for (int i = 0; i < mi.arguments.size(); i++) {
  1918. if (i > 0)
  1919. m += ", ";
  1920. String n = mi.arguments[i].name;
  1921. if (n.find(":") != -1)
  1922. n = n.substr(0, n.find(":"));
  1923. m += n;
  1924. }
  1925. }
  1926. m += "):";
  1927. options.insert(m);
  1928. }
  1929. }
  1930. } break;
  1931. case GDScriptParser::COMPLETION_YIELD: {
  1932. const GDScriptParser::Node *node = p.get_completion_node();
  1933. GDScriptCompletionIdentifier t;
  1934. if (!_guess_expression_type(context, node, p.get_completion_line(), t))
  1935. break;
  1936. if (t.type == Variant::OBJECT && t.obj_type != StringName()) {
  1937. List<MethodInfo> sigs;
  1938. ClassDB::get_signal_list(t.obj_type, &sigs);
  1939. for (List<MethodInfo>::Element *E = sigs.front(); E; E = E->next()) {
  1940. options.insert("\"" + E->get().name + "\"");
  1941. r_forced = true;
  1942. }
  1943. }
  1944. } break;
  1945. case GDScriptParser::COMPLETION_RESOURCE_PATH: {
  1946. if (EditorSettings::get_singleton()->get("text_editor/completion/complete_file_paths")) {
  1947. get_directory_contents(EditorFileSystem::get_singleton()->get_filesystem(), options);
  1948. r_forced = true;
  1949. }
  1950. } break;
  1951. case GDScriptParser::COMPLETION_ASSIGN: {
  1952. #if defined(DEBUG_METHODS_ENABLED) && defined(TOOLS_ENABLED)
  1953. GDScriptCompletionIdentifier ci;
  1954. if (_guess_expression_type(context, p.get_completion_node(), p.get_completion_line(), ci)) {
  1955. String enumeration = ci.enumeration;
  1956. if (enumeration.find(".") != -1) {
  1957. //class constant
  1958. List<StringName> constants;
  1959. String cls = enumeration.get_slice(".", 0);
  1960. String enm = enumeration.get_slice(".", 1);
  1961. ClassDB::get_enum_constants(cls, enm, &constants);
  1962. //constants.sort_custom<StringName::AlphCompare>();
  1963. for (List<StringName>::Element *E = constants.front(); E; E = E->next()) {
  1964. String add = cls + "." + E->get();
  1965. r_options->push_back(add);
  1966. r_forced = true;
  1967. }
  1968. } else {
  1969. //global constant
  1970. StringName current_enum = enumeration;
  1971. for (int i = 0; i < GlobalConstants::get_global_constant_count(); i++) {
  1972. if (GlobalConstants::get_global_constant_enum(i) == current_enum) {
  1973. r_options->push_back(GlobalConstants::get_global_constant_name(i));
  1974. r_forced = true;
  1975. }
  1976. }
  1977. //global
  1978. }
  1979. }
  1980. #endif
  1981. } break;
  1982. }
  1983. for (Set<String>::Element *E = options.front(); E; E = E->next()) {
  1984. r_options->push_back(E->get());
  1985. }
  1986. return OK;
  1987. }
  1988. #else
  1989. Error GDScriptLanguage::complete_code(const String &p_code, const String &p_base_path, Object *p_owner, List<String> *r_options, bool &r_forced, String &r_call_hint) {
  1990. return OK;
  1991. }
  1992. #endif
  1993. String GDScriptLanguage::_get_indentation() const {
  1994. #ifdef TOOLS_ENABLED
  1995. if (Engine::get_singleton()->is_editor_hint()) {
  1996. bool use_space_indentation = EDITOR_DEF("text_editor/indent/type", 0);
  1997. if (use_space_indentation) {
  1998. int indent_size = EDITOR_DEF("text_editor/indent/size", 4);
  1999. String space_indent = "";
  2000. for (int i = 0; i < indent_size; i++) {
  2001. space_indent += " ";
  2002. }
  2003. return space_indent;
  2004. }
  2005. }
  2006. #endif
  2007. return "\t";
  2008. }
  2009. void GDScriptLanguage::auto_indent_code(String &p_code, int p_from_line, int p_to_line) const {
  2010. String indent = _get_indentation();
  2011. Vector<String> lines = p_code.split("\n");
  2012. List<int> indent_stack;
  2013. for (int i = 0; i < lines.size(); i++) {
  2014. String l = lines[i];
  2015. int tc = 0;
  2016. for (int j = 0; j < l.length(); j++) {
  2017. if (l[j] == ' ' || l[j] == '\t') {
  2018. tc++;
  2019. } else {
  2020. break;
  2021. }
  2022. }
  2023. String st = l.substr(tc, l.length()).strip_edges();
  2024. if (st == "" || st.begins_with("#"))
  2025. continue; //ignore!
  2026. int ilevel = 0;
  2027. if (indent_stack.size()) {
  2028. ilevel = indent_stack.back()->get();
  2029. }
  2030. if (tc > ilevel) {
  2031. indent_stack.push_back(tc);
  2032. } else if (tc < ilevel) {
  2033. while (indent_stack.size() && indent_stack.back()->get() > tc) {
  2034. indent_stack.pop_back();
  2035. }
  2036. if (indent_stack.size() && indent_stack.back()->get() != tc)
  2037. indent_stack.push_back(tc); //this is not right but gets the job done
  2038. }
  2039. if (i >= p_from_line) {
  2040. l = "";
  2041. for (int j = 0; j < indent_stack.size(); j++) {
  2042. l += indent;
  2043. }
  2044. l += st;
  2045. } else if (i > p_to_line) {
  2046. break;
  2047. }
  2048. //print_line(itos(indent_stack.size())+","+itos(tc)+": "+l);
  2049. lines[i] = l;
  2050. }
  2051. p_code = "";
  2052. for (int i = 0; i < lines.size(); i++) {
  2053. if (i > 0)
  2054. p_code += "\n";
  2055. p_code += lines[i];
  2056. }
  2057. }
  2058. #ifdef TOOLS_ENABLED
  2059. Error GDScriptLanguage::lookup_code(const String &p_code, const String &p_symbol, const String &p_base_path, Object *p_owner, LookupResult &r_result) {
  2060. //before parsing, try the usual stuff
  2061. if (ClassDB::class_exists(p_symbol)) {
  2062. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS;
  2063. r_result.class_name = p_symbol;
  2064. return OK;
  2065. }
  2066. for (int i = 0; i < Variant::VARIANT_MAX; i++) {
  2067. Variant::Type t = Variant::Type(i);
  2068. if (Variant::get_type_name(t) == p_symbol) {
  2069. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS;
  2070. r_result.class_name = Variant::get_type_name(t);
  2071. return OK;
  2072. }
  2073. }
  2074. for (int i = 0; i < GDScriptFunctions::FUNC_MAX; i++) {
  2075. if (GDScriptFunctions::get_func_name(GDScriptFunctions::Function(i)) == p_symbol) {
  2076. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_METHOD;
  2077. r_result.class_name = "@GDScript";
  2078. r_result.class_member = p_symbol;
  2079. return OK;
  2080. }
  2081. }
  2082. if ("PI" == p_symbol || "TAU" == p_symbol || "INF" == p_symbol || "NAN" == p_symbol) {
  2083. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_CONSTANT;
  2084. r_result.class_name = "@GDScript";
  2085. r_result.class_member = p_symbol;
  2086. return OK;
  2087. }
  2088. GDScriptParser p;
  2089. p.parse(p_code, p_base_path, false, "", true);
  2090. if (p.get_completion_type() == GDScriptParser::COMPLETION_NONE)
  2091. return ERR_CANT_RESOLVE;
  2092. GDScriptCompletionContext context;
  2093. context._class = p.get_completion_class();
  2094. context.block = p.get_completion_block();
  2095. context.function = p.get_completion_function();
  2096. context.base = p_owner;
  2097. context.base_path = p_base_path;
  2098. if (context._class && context._class->extends_class.size() > 0) {
  2099. bool success = false;
  2100. ClassDB::get_integer_constant(context._class->extends_class[0], p_symbol, &success);
  2101. if (success) {
  2102. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_CONSTANT;
  2103. r_result.class_name = context._class->extends_class[0];
  2104. r_result.class_member = p_symbol;
  2105. return OK;
  2106. }
  2107. }
  2108. bool isfunction = false;
  2109. switch (p.get_completion_type()) {
  2110. case GDScriptParser::COMPLETION_GET_NODE:
  2111. case GDScriptParser::COMPLETION_NONE: {
  2112. } break;
  2113. case GDScriptParser::COMPLETION_BUILT_IN_TYPE_CONSTANT: {
  2114. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_CONSTANT;
  2115. r_result.class_name = Variant::get_type_name(p.get_completion_built_in_constant());
  2116. r_result.class_member = p_symbol;
  2117. return OK;
  2118. } break;
  2119. case GDScriptParser::COMPLETION_FUNCTION: {
  2120. if (context._class && context._class->functions.size()) {
  2121. for (int i = 0; i < context._class->functions.size(); i++) {
  2122. if (context._class->functions[i]->name == p_symbol) {
  2123. r_result.type = ScriptLanguage::LookupResult::RESULT_SCRIPT_LOCATION;
  2124. r_result.location = context._class->functions[i]->line;
  2125. return OK;
  2126. }
  2127. }
  2128. }
  2129. Ref<GDScript> parent = _get_parent_class(context);
  2130. while (parent.is_valid()) {
  2131. int line = parent->get_member_line(p_symbol);
  2132. if (line >= 0) {
  2133. r_result.type = ScriptLanguage::LookupResult::RESULT_SCRIPT_LOCATION;
  2134. r_result.location = line;
  2135. r_result.script = parent;
  2136. return OK;
  2137. }
  2138. parent = parent->get_base();
  2139. }
  2140. GDScriptCompletionIdentifier identifier = _get_native_class(context);
  2141. print_line("identifier: " + String(identifier.obj_type));
  2142. if (ClassDB::has_method(identifier.obj_type, p_symbol)) {
  2143. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_METHOD;
  2144. r_result.class_name = identifier.obj_type;
  2145. r_result.class_member = p_symbol;
  2146. return OK;
  2147. }
  2148. } break;
  2149. case GDScriptParser::COMPLETION_IDENTIFIER: {
  2150. //check if a function
  2151. if (p.get_completion_identifier_is_function()) {
  2152. if (context._class && context._class->functions.size()) {
  2153. for (int i = 0; i < context._class->functions.size(); i++) {
  2154. if (context._class->functions[i]->name == p_symbol) {
  2155. r_result.type = ScriptLanguage::LookupResult::RESULT_SCRIPT_LOCATION;
  2156. r_result.location = context._class->functions[i]->line;
  2157. return OK;
  2158. }
  2159. }
  2160. }
  2161. Ref<GDScript> parent = _get_parent_class(context);
  2162. while (parent.is_valid()) {
  2163. int line = parent->get_member_line(p_symbol);
  2164. if (line >= 0) {
  2165. r_result.type = ScriptLanguage::LookupResult::RESULT_SCRIPT_LOCATION;
  2166. r_result.location = line;
  2167. r_result.script = parent;
  2168. return OK;
  2169. }
  2170. parent = parent->get_base();
  2171. }
  2172. GDScriptCompletionIdentifier identifier = _get_native_class(context);
  2173. if (ClassDB::has_method(identifier.obj_type, p_symbol)) {
  2174. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_METHOD;
  2175. r_result.class_name = identifier.obj_type;
  2176. r_result.class_member = p_symbol;
  2177. return OK;
  2178. }
  2179. } else {
  2180. GDScriptCompletionIdentifier gdi = _get_native_class(context);
  2181. if (gdi.obj_type != StringName()) {
  2182. bool valid;
  2183. Variant::Type t = ClassDB::get_property_type(gdi.obj_type, p_symbol, &valid);
  2184. if (t != Variant::NIL && valid) {
  2185. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_PROPERTY;
  2186. r_result.class_name = gdi.obj_type;
  2187. r_result.class_member = p_symbol;
  2188. return OK;
  2189. }
  2190. }
  2191. const GDScriptParser::BlockNode *block = context.block;
  2192. //search in blocks going up (local var?)
  2193. while (block) {
  2194. for (int i = 0; i < block->statements.size(); i++) {
  2195. if (block->statements[i]->line > p.get_completion_line())
  2196. continue;
  2197. if (block->statements[i]->type == GDScriptParser::BlockNode::TYPE_LOCAL_VAR) {
  2198. const GDScriptParser::LocalVarNode *lv = static_cast<const GDScriptParser::LocalVarNode *>(block->statements[i]);
  2199. if (lv->assign && lv->name == p_symbol) {
  2200. r_result.type = ScriptLanguage::LookupResult::RESULT_SCRIPT_LOCATION;
  2201. r_result.location = block->statements[i]->line;
  2202. return OK;
  2203. }
  2204. }
  2205. }
  2206. block = block->parent_block;
  2207. }
  2208. //guess from function arguments
  2209. if (context.function && context.function->name != StringName()) {
  2210. for (int i = 0; i < context.function->arguments.size(); i++) {
  2211. if (context.function->arguments[i] == p_symbol) {
  2212. r_result.type = ScriptLanguage::LookupResult::RESULT_SCRIPT_LOCATION;
  2213. r_result.location = context.function->line;
  2214. return OK;
  2215. }
  2216. }
  2217. }
  2218. //guess in class constants
  2219. for (int i = 0; i < context._class->constant_expressions.size(); i++) {
  2220. if (context._class->constant_expressions[i].identifier == p_symbol) {
  2221. r_result.type = ScriptLanguage::LookupResult::RESULT_SCRIPT_LOCATION;
  2222. r_result.location = context._class->constant_expressions[i].expression->line;
  2223. return OK;
  2224. }
  2225. }
  2226. //guess in class variables
  2227. if (!(context.function && context.function->_static)) {
  2228. for (int i = 0; i < context._class->variables.size(); i++) {
  2229. if (context._class->variables[i].identifier == p_symbol) {
  2230. r_result.type = ScriptLanguage::LookupResult::RESULT_SCRIPT_LOCATION;
  2231. r_result.location = context._class->variables[i].line;
  2232. return OK;
  2233. }
  2234. }
  2235. }
  2236. //guess in autoloads as singletons
  2237. List<PropertyInfo> props;
  2238. ProjectSettings::get_singleton()->get_property_list(&props);
  2239. for (List<PropertyInfo>::Element *E = props.front(); E; E = E->next()) {
  2240. String s = E->get().name;
  2241. if (!s.begins_with("autoload/"))
  2242. continue;
  2243. String name = s.get_slice("/", 1);
  2244. if (name == String(p_symbol)) {
  2245. String path = ProjectSettings::get_singleton()->get(s);
  2246. if (path.begins_with("*")) {
  2247. String script = path.substr(1, path.length());
  2248. if (!script.ends_with(".gd")) {
  2249. //not a script, try find the script anyway,
  2250. //may have some success
  2251. script = script.get_basename() + ".gd";
  2252. }
  2253. if (FileAccess::exists(script)) {
  2254. r_result.type = ScriptLanguage::LookupResult::RESULT_SCRIPT_LOCATION;
  2255. r_result.location = 0;
  2256. r_result.script = ResourceLoader::load(script);
  2257. return OK;
  2258. }
  2259. }
  2260. }
  2261. }
  2262. //global
  2263. Map<StringName, int> classes = GDScriptLanguage::get_singleton()->get_global_map();
  2264. if (classes.has(p_symbol)) {
  2265. Variant value = GDScriptLanguage::get_singleton()->get_global_array()[classes[p_symbol]];
  2266. if (value.get_type() == Variant::OBJECT) {
  2267. Object *obj = value;
  2268. if (obj) {
  2269. if (Object::cast_to<GDScriptNativeClass>(obj)) {
  2270. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS;
  2271. r_result.class_name = Object::cast_to<GDScriptNativeClass>(obj)->get_name();
  2272. } else {
  2273. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS;
  2274. r_result.class_name = obj->get_class();
  2275. }
  2276. // proxy class remove the underscore.
  2277. if (r_result.class_name.begins_with("_")) {
  2278. r_result.class_name = r_result.class_name.right(1);
  2279. }
  2280. return OK;
  2281. }
  2282. } else {
  2283. /*
  2284. // Because get_integer_constant_enum and get_integer_constant dont work on @GlobalScope
  2285. // We cannot determine the exact nature of the identifier here
  2286. // Otherwise these codes would work
  2287. StringName enumName = ClassDB::get_integer_constant_enum("@GlobalScope", p_symbol, true);
  2288. if (enumName != NULL) {
  2289. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_ENUM;
  2290. r_result.class_name = "@GlobalScope";
  2291. r_result.class_member = enumName;
  2292. return OK;
  2293. }
  2294. else {
  2295. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_CONSTANT;
  2296. r_result.class_name = "@GlobalScope";
  2297. r_result.class_member = p_symbol;
  2298. return OK;
  2299. }*/
  2300. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_TBD_GLOBALSCOPE;
  2301. r_result.class_name = "@GlobalScope";
  2302. r_result.class_member = p_symbol;
  2303. return OK;
  2304. }
  2305. }
  2306. }
  2307. } break;
  2308. case GDScriptParser::COMPLETION_PARENT_FUNCTION: {
  2309. } break;
  2310. case GDScriptParser::COMPLETION_METHOD:
  2311. isfunction = true;
  2312. case GDScriptParser::COMPLETION_INDEX: {
  2313. const GDScriptParser::Node *node = p.get_completion_node();
  2314. if (node->type != GDScriptParser::Node::TYPE_OPERATOR)
  2315. break;
  2316. GDScriptCompletionIdentifier t;
  2317. if (_guess_expression_type(context, static_cast<const GDScriptParser::OperatorNode *>(node)->arguments[0], p.get_completion_line(), t)) {
  2318. if (t.type == Variant::OBJECT && t.obj_type == "GDScriptNativeClass") {
  2319. //native enum
  2320. Ref<GDScriptNativeClass> gdn = t.value;
  2321. if (gdn.is_valid()) {
  2322. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_CONSTANT;
  2323. r_result.class_name = gdn->get_name();
  2324. r_result.class_member = p_symbol;
  2325. return OK;
  2326. }
  2327. } else if (t.type == Variant::OBJECT && t.obj_type != StringName()) {
  2328. Ref<GDScript> on_script;
  2329. if (t.value.get_type()) {
  2330. Object *obj = t.value;
  2331. if (obj) {
  2332. on_script = obj->get_script();
  2333. if (on_script.is_valid()) {
  2334. int loc = on_script->get_member_line(p_symbol);
  2335. if (loc >= 0) {
  2336. r_result.script = on_script;
  2337. r_result.type = ScriptLanguage::LookupResult::RESULT_SCRIPT_LOCATION;
  2338. r_result.location = loc;
  2339. return OK;
  2340. }
  2341. }
  2342. }
  2343. }
  2344. if (ClassDB::has_method(t.obj_type, p_symbol)) {
  2345. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_METHOD;
  2346. r_result.class_name = t.obj_type;
  2347. r_result.class_member = p_symbol;
  2348. return OK;
  2349. }
  2350. StringName enumName = ClassDB::get_integer_constant_enum(t.obj_type, p_symbol, true);
  2351. if (enumName != StringName()) {
  2352. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_ENUM;
  2353. r_result.class_name = t.obj_type;
  2354. r_result.class_member = enumName;
  2355. return OK;
  2356. }
  2357. bool success;
  2358. ClassDB::get_integer_constant(t.obj_type, p_symbol, &success);
  2359. if (success) {
  2360. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_CONSTANT;
  2361. r_result.class_name = t.obj_type;
  2362. r_result.class_member = p_symbol;
  2363. return OK;
  2364. }
  2365. ClassDB::get_property_type(t.obj_type, p_symbol, &success);
  2366. if (success) {
  2367. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_PROPERTY;
  2368. r_result.class_name = t.obj_type;
  2369. r_result.class_member = p_symbol;
  2370. return OK;
  2371. }
  2372. } else {
  2373. Variant::CallError ce;
  2374. Variant v = Variant::construct(t.type, NULL, 0, ce);
  2375. bool valid;
  2376. v.get_numeric_constant_value(t.type, p_symbol, &valid);
  2377. if (valid) {
  2378. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_CONSTANT;
  2379. r_result.class_name = Variant::get_type_name(t.type);
  2380. r_result.class_member = p_symbol;
  2381. return OK;
  2382. }
  2383. //todo check all inputevent types for property
  2384. v.get(p_symbol, &valid);
  2385. if (valid) {
  2386. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_PROPERTY;
  2387. r_result.class_name = Variant::get_type_name(t.type);
  2388. r_result.class_member = p_symbol;
  2389. return OK;
  2390. }
  2391. if (v.has_method(p_symbol)) {
  2392. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_METHOD;
  2393. r_result.class_name = Variant::get_type_name(t.type);
  2394. r_result.class_member = p_symbol;
  2395. return OK;
  2396. }
  2397. }
  2398. }
  2399. } break;
  2400. case GDScriptParser::COMPLETION_CALL_ARGUMENTS: {
  2401. return ERR_CANT_RESOLVE;
  2402. } break;
  2403. case GDScriptParser::COMPLETION_VIRTUAL_FUNC: {
  2404. GDScriptCompletionIdentifier cid = _get_native_class(context);
  2405. if (cid.obj_type != StringName()) {
  2406. List<MethodInfo> vm;
  2407. ClassDB::get_virtual_methods(cid.obj_type, &vm);
  2408. for (List<MethodInfo>::Element *E = vm.front(); E; E = E->next()) {
  2409. if (p_symbol == E->get().name) {
  2410. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_METHOD;
  2411. r_result.class_name = cid.obj_type;
  2412. r_result.class_member = p_symbol;
  2413. return OK;
  2414. }
  2415. }
  2416. }
  2417. } break;
  2418. case GDScriptParser::COMPLETION_YIELD: {
  2419. return ERR_CANT_RESOLVE;
  2420. } break;
  2421. }
  2422. return ERR_CANT_RESOLVE;
  2423. }
  2424. #endif