gd_editor.cpp 86 KB

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