gdscript_editor.cpp 90 KB

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