gd_editor.cpp 87 KB

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