gd_editor.cpp 69 KB

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