gd_editor.cpp 84 KB

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