gd_editor.cpp 83 KB

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