gd_editor.cpp 69 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392
  1. /*************************************************************************/
  2. /* gd_editor.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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. }
  673. if (vop == Variant::OP_MAX)
  674. return false;
  675. GDCompletionIdentifier p1;
  676. GDCompletionIdentifier p2;
  677. if (op->arguments[0]) {
  678. if (!_guess_expression_type(context, op->arguments[0], p_line, p1)) {
  679. return false;
  680. }
  681. }
  682. if (op->arguments.size() > 1) {
  683. if (!_guess_expression_type(context, op->arguments[1], p_line, p2)) {
  684. return false;
  685. }
  686. }
  687. Variant::CallError ce;
  688. bool v1_use_value = p1.value.get_type() != Variant::NIL && p1.value.get_type() != Variant::OBJECT;
  689. Variant v1 = (v1_use_value) ? p1.value : Variant::construct(p1.type, NULL, 0, ce);
  690. bool v2_use_value = p2.value.get_type() != Variant::NIL && p2.value.get_type() != Variant::OBJECT;
  691. Variant v2 = (v2_use_value) ? p2.value : Variant::construct(p2.type, NULL, 0, ce);
  692. // avoid potential invalid ops
  693. if ((vop == Variant::OP_DIVIDE || vop == Variant::OP_MODULE) && v2.get_type() == Variant::INT) {
  694. v2 = 1;
  695. v2_use_value = false;
  696. }
  697. if (vop == Variant::OP_DIVIDE && v2.get_type() == Variant::REAL) {
  698. v2 = 1.0;
  699. v2_use_value = false;
  700. }
  701. Variant r;
  702. bool valid;
  703. Variant::evaluate(vop, v1, v2, r, valid);
  704. if (!valid)
  705. return false;
  706. r_type.type = r.get_type();
  707. if (v1_use_value && v2_use_value)
  708. r_type.value = r;
  709. return true;
  710. }
  711. }
  712. return false;
  713. }
  714. static bool _guess_identifier_type_in_block(GDCompletionContext &context, int p_line, const StringName &p_identifier, GDCompletionIdentifier &r_type) {
  715. const GDParser::Node *last_assign = NULL;
  716. int last_assign_line = -1;
  717. for (int i = 0; i < context.block->statements.size(); i++) {
  718. if (context.block->statements[i]->line > p_line)
  719. continue;
  720. if (context.block->statements[i]->type == GDParser::BlockNode::TYPE_LOCAL_VAR) {
  721. const GDParser::LocalVarNode *lv = static_cast<const GDParser::LocalVarNode *>(context.block->statements[i]);
  722. if (lv->assign && lv->name == p_identifier) {
  723. last_assign = lv->assign;
  724. last_assign_line = context.block->statements[i]->line;
  725. }
  726. }
  727. if (context.block->statements[i]->type == GDParser::BlockNode::TYPE_OPERATOR) {
  728. const GDParser::OperatorNode *op = static_cast<const GDParser::OperatorNode *>(context.block->statements[i]);
  729. if (op->op == GDParser::OperatorNode::OP_ASSIGN) {
  730. if (op->arguments.size() && op->arguments[0]->type == GDParser::Node::TYPE_IDENTIFIER) {
  731. const GDParser::IdentifierNode *id = static_cast<const GDParser::IdentifierNode *>(op->arguments[0]);
  732. if (id->name == p_identifier) {
  733. last_assign = op->arguments[1];
  734. last_assign_line = context.block->statements[i]->line;
  735. }
  736. }
  737. }
  738. }
  739. }
  740. //use the last assignment, (then backwards?)
  741. if (last_assign && last_assign_line != p_line) {
  742. return _guess_expression_type(context, last_assign, last_assign_line, r_type);
  743. }
  744. return false;
  745. }
  746. 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) {
  747. const GDParser::FunctionNode *func = NULL;
  748. for (int i = 0; i < context._class->functions.size(); i++) {
  749. if (context._class->functions[i]->name == p_function) {
  750. func = context._class->functions[i];
  751. break;
  752. }
  753. }
  754. if (!func)
  755. return false;
  756. for (int i = 0; i < func->body->statements.size(); i++) {
  757. if (func->body->statements[i]->line == p_src_line) {
  758. break;
  759. }
  760. if (func->body->statements[i]->type == GDParser::BlockNode::TYPE_OPERATOR) {
  761. const GDParser::OperatorNode *op = static_cast<const GDParser::OperatorNode *>(func->body->statements[i]);
  762. if (op->op == GDParser::OperatorNode::OP_ASSIGN) {
  763. if (op->arguments.size() && op->arguments[0]->type == GDParser::Node::TYPE_IDENTIFIER) {
  764. const GDParser::IdentifierNode *id = static_cast<const GDParser::IdentifierNode *>(op->arguments[0]);
  765. if (id->name == p_identifier) {
  766. return _guess_expression_type(context, op->arguments[1], func->body->statements[i]->line, r_type);
  767. }
  768. }
  769. }
  770. }
  771. }
  772. return false;
  773. }
  774. static bool _guess_identifier_type(GDCompletionContext &context, int p_line, const StringName &p_identifier, GDCompletionIdentifier &r_type) {
  775. //go to block first
  776. const GDParser::BlockNode *block = context.block;
  777. while (block) {
  778. GDCompletionContext c = context;
  779. c.block = block;
  780. if (_guess_identifier_type_in_block(c, p_line, p_identifier, r_type)) {
  781. return true;
  782. }
  783. block = block->parent_block;
  784. }
  785. //guess from argument if virtual
  786. if (context.function && context.function->name != StringName()) {
  787. int argindex = -1;
  788. for (int i = 0; i < context.function->arguments.size(); i++) {
  789. if (context.function->arguments[i] == p_identifier) {
  790. argindex = i;
  791. break;
  792. }
  793. }
  794. if (argindex != -1) {
  795. GDCompletionIdentifier id = _get_native_class(context);
  796. if (id.type == Variant::OBJECT && id.obj_type != StringName()) {
  797. //this kinda sucks but meh
  798. List<MethodInfo> vmethods;
  799. ObjectTypeDB::get_virtual_methods(id.obj_type, &vmethods);
  800. for (List<MethodInfo>::Element *E = vmethods.front(); E; E = E->next()) {
  801. if (E->get().name == context.function->name && argindex < E->get().arguments.size()) {
  802. PropertyInfo arg = E->get().arguments[argindex];
  803. int scp = arg.name.find(":");
  804. if (scp != -1) {
  805. r_type.type = Variant::OBJECT;
  806. r_type.obj_type = arg.name.substr(scp + 1, arg.name.length());
  807. return true;
  808. } else {
  809. r_type.type = arg.type;
  810. if (arg.hint == PROPERTY_HINT_RESOURCE_TYPE)
  811. r_type.obj_type = arg.hint_string;
  812. return true;
  813. }
  814. }
  815. }
  816. }
  817. }
  818. }
  819. //guess type in constant
  820. for (int i = 0; i < context._class->constant_expressions.size(); i++) {
  821. if (context._class->constant_expressions[i].identifier == p_identifier) {
  822. ERR_FAIL_COND_V(context._class->constant_expressions[i].expression->type != GDParser::Node::TYPE_CONSTANT, false);
  823. r_type = _get_type_from_variant(static_cast<const GDParser::ConstantNode *>(context._class->constant_expressions[i].expression)->value);
  824. return true;
  825. }
  826. }
  827. if (!(context.function && context.function->_static)) {
  828. for (int i = 0; i < context._class->variables.size(); i++) {
  829. if (context._class->variables[i].identifier == p_identifier) {
  830. if (context._class->variables[i]._export.type != Variant::NIL) {
  831. r_type = _get_type_from_pinfo(context._class->variables[i]._export);
  832. return true;
  833. } else if (context._class->variables[i].expression) {
  834. if (p_line <= context._class->variables[i].line)
  835. return false;
  836. bool rtype = _guess_expression_type(context, context._class->variables[i].expression, context._class->variables[i].line, r_type);
  837. if (rtype && r_type.type != Variant::NIL)
  838. return true;
  839. //return _guess_expression_type(context,context._class->variables[i].expression,context._class->variables[i].line,r_type);
  840. }
  841. //try to guess from assignment in construtor or _ready
  842. if (_guess_identifier_from_assignment_in_function(context, p_line + 1, p_identifier, "_ready", r_type))
  843. return true;
  844. if (_guess_identifier_from_assignment_in_function(context, p_line + 1, p_identifier, "_enter_tree", r_type))
  845. return true;
  846. if (_guess_identifier_from_assignment_in_function(context, p_line + 1, p_identifier, "_init", r_type))
  847. return true;
  848. return false;
  849. }
  850. }
  851. }
  852. //autoloads as singletons
  853. List<PropertyInfo> props;
  854. Globals::get_singleton()->get_property_list(&props);
  855. for (List<PropertyInfo>::Element *E = props.front(); E; E = E->next()) {
  856. String s = E->get().name;
  857. if (!s.begins_with("autoload/"))
  858. continue;
  859. String name = s.get_slice("/", 1);
  860. if (name == String(p_identifier)) {
  861. String path = Globals::get_singleton()->get(s);
  862. if (path.begins_with("*")) {
  863. String script = path.substr(1, path.length());
  864. if (!script.ends_with(".gd")) {
  865. //not a script, try find the script anyway,
  866. //may have some success
  867. script = script.basename() + ".gd";
  868. }
  869. if (FileAccess::exists(script)) {
  870. //print_line("is a script");
  871. Ref<Script> scr;
  872. if (ScriptCodeCompletionCache::get_sigleton())
  873. scr = ScriptCodeCompletionCache::get_sigleton()->get_cached_resource(script);
  874. else
  875. scr = ResourceLoader::load(script);
  876. r_type.obj_type = "Node";
  877. r_type.type = Variant::OBJECT;
  878. r_type.script = scr;
  879. r_type.value = Variant();
  880. return true;
  881. }
  882. }
  883. }
  884. }
  885. //global
  886. for (Map<StringName, int>::Element *E = GDScriptLanguage::get_singleton()->get_global_map().front(); E; E = E->next()) {
  887. if (E->key() == p_identifier) {
  888. r_type = _get_type_from_variant(GDScriptLanguage::get_singleton()->get_global_array()[E->get()]);
  889. return true;
  890. }
  891. }
  892. return false;
  893. }
  894. static void _find_identifiers_in_block(GDCompletionContext &context, int p_line, bool p_only_functions, Set<String> &result) {
  895. if (p_only_functions)
  896. return;
  897. for (int i = 0; i < context.block->statements.size(); i++) {
  898. if (context.block->statements[i]->line > p_line)
  899. continue;
  900. if (context.block->statements[i]->type == GDParser::BlockNode::TYPE_LOCAL_VAR) {
  901. const GDParser::LocalVarNode *lv = static_cast<const GDParser::LocalVarNode *>(context.block->statements[i]);
  902. result.insert(lv->name.operator String());
  903. }
  904. }
  905. }
  906. static void _find_identifiers_in_class(GDCompletionContext &context, bool p_static, bool p_only_functions, Set<String> &result) {
  907. if (!p_static && !p_only_functions) {
  908. for (int i = 0; i < context._class->variables.size(); i++) {
  909. result.insert(context._class->variables[i].identifier);
  910. }
  911. }
  912. if (!p_only_functions) {
  913. for (int i = 0; i < context._class->constant_expressions.size(); i++) {
  914. result.insert(context._class->constant_expressions[i].identifier);
  915. }
  916. for (int i = 0; i < context._class->subclasses.size(); i++) {
  917. result.insert(context._class->subclasses[i]->name);
  918. }
  919. }
  920. for (int i = 0; i < context._class->static_functions.size(); i++) {
  921. if (context._class->static_functions[i]->arguments.size())
  922. result.insert(context._class->static_functions[i]->name.operator String() + "(");
  923. else
  924. result.insert(context._class->static_functions[i]->name.operator String() + "()");
  925. }
  926. if (!p_static) {
  927. for (int i = 0; i < context._class->functions.size(); i++) {
  928. if (context._class->functions[i]->arguments.size())
  929. result.insert(context._class->functions[i]->name.operator String() + "(");
  930. else
  931. result.insert(context._class->functions[i]->name.operator String() + "()");
  932. }
  933. }
  934. //globals
  935. Ref<Reference> base = _get_parent_class(context);
  936. while (true) {
  937. Ref<GDScript> script = base;
  938. Ref<GDNativeClass> nc = base;
  939. if (script.is_valid()) {
  940. if (!p_static && !p_only_functions) {
  941. for (const Set<StringName>::Element *E = script->get_members().front(); E; E = E->next()) {
  942. result.insert(E->get().operator String());
  943. }
  944. }
  945. if (!p_only_functions) {
  946. for (const Map<StringName, Variant>::Element *E = script->get_constants().front(); E; E = E->next()) {
  947. result.insert(E->key().operator String());
  948. }
  949. }
  950. for (const Map<StringName, GDFunction *>::Element *E = script->get_member_functions().front(); E; E = E->next()) {
  951. if (!p_static || E->get()->is_static()) {
  952. if (E->get()->get_argument_count())
  953. result.insert(E->key().operator String() + "(");
  954. else
  955. result.insert(E->key().operator String() + "()");
  956. }
  957. }
  958. if (!p_only_functions) {
  959. for (const Map<StringName, Ref<GDScript> >::Element *E = script->get_subclasses().front(); E; E = E->next()) {
  960. result.insert(E->key().operator String());
  961. }
  962. }
  963. base = script->get_base();
  964. if (base.is_null())
  965. base = script->get_native();
  966. } else if (nc.is_valid()) {
  967. if (!p_only_functions) {
  968. StringName type = nc->get_name();
  969. List<String> constants;
  970. ObjectTypeDB::get_integer_constant_list(type, &constants);
  971. for (List<String>::Element *E = constants.front(); E; E = E->next()) {
  972. result.insert(E->get());
  973. }
  974. List<MethodInfo> methods;
  975. ObjectTypeDB::get_method_list(type, &methods);
  976. for (List<MethodInfo>::Element *E = methods.front(); E; E = E->next()) {
  977. if (E->get().name.begins_with("_"))
  978. continue;
  979. if (E->get().arguments.size())
  980. result.insert(E->get().name + "(");
  981. else
  982. result.insert(E->get().name + "()");
  983. }
  984. }
  985. break;
  986. } else
  987. break;
  988. }
  989. }
  990. static void _find_identifiers(GDCompletionContext &context, int p_line, bool p_only_functions, Set<String> &result) {
  991. const GDParser::BlockNode *block = context.block;
  992. if (context.function) {
  993. const GDParser::FunctionNode *f = context.function;
  994. for (int i = 0; i < f->arguments.size(); i++) {
  995. result.insert(f->arguments[i].operator String());
  996. }
  997. }
  998. while (block) {
  999. GDCompletionContext c = context;
  1000. c.block = block;
  1001. _find_identifiers_in_block(c, p_line, p_only_functions, result);
  1002. block = block->parent_block;
  1003. }
  1004. const GDParser::ClassNode *clss = context._class;
  1005. bool _static = context.function && context.function->_static;
  1006. while (clss) {
  1007. GDCompletionContext c = context;
  1008. c._class = clss;
  1009. c.block = NULL;
  1010. c.function = NULL;
  1011. _find_identifiers_in_class(c, _static, p_only_functions, result);
  1012. clss = clss->owner;
  1013. }
  1014. for (int i = 0; i < GDFunctions::FUNC_MAX; i++) {
  1015. result.insert(GDFunctions::get_func_name(GDFunctions::Function(i)));
  1016. }
  1017. static const char *_type_names[Variant::VARIANT_MAX] = {
  1018. "null", "bool", "int", "float", "String", "Vector2", "Rect2", "Vector3", "Matrix32", "Plane", "Quat", "AABB", "Matrix3", "Transform",
  1019. "Color", "Image", "NodePath", "RID", "Object", "InputEvent", "Dictionary", "Array", "RawArray", "IntArray", "FloatArray", "StringArray",
  1020. "Vector2Array", "Vector3Array", "ColorArray"
  1021. };
  1022. for (int i = 0; i < Variant::VARIANT_MAX; i++) {
  1023. result.insert(_type_names[i]);
  1024. }
  1025. //autoload singletons
  1026. List<PropertyInfo> props;
  1027. Globals::get_singleton()->get_property_list(&props);
  1028. for (List<PropertyInfo>::Element *E = props.front(); E; E = E->next()) {
  1029. String s = E->get().name;
  1030. if (!s.begins_with("autoload/"))
  1031. continue;
  1032. String name = s.get_slice("/", 1);
  1033. String path = Globals::get_singleton()->get(s);
  1034. if (path.begins_with("*")) {
  1035. result.insert(name);
  1036. }
  1037. }
  1038. for (const Map<StringName, int>::Element *E = GDScriptLanguage::get_singleton()->get_global_map().front(); E; E = E->next()) {
  1039. result.insert(E->key().operator String());
  1040. }
  1041. }
  1042. static String _get_visual_datatype(const PropertyInfo &p_info, bool p_isarg = true) {
  1043. String n = p_info.name;
  1044. int idx = n.find(":");
  1045. if (idx != -1) {
  1046. return n.substr(idx + 1, n.length());
  1047. }
  1048. if (p_info.type == Variant::OBJECT && p_info.hint == PROPERTY_HINT_RESOURCE_TYPE)
  1049. return p_info.hint_string;
  1050. if (p_info.type == Variant::NIL) {
  1051. if (p_isarg)
  1052. return "var";
  1053. else
  1054. return "void";
  1055. }
  1056. return Variant::get_type_name(p_info.type);
  1057. }
  1058. static void _make_function_hint(const GDParser::FunctionNode *p_func, int p_argidx, String &arghint) {
  1059. arghint = "func " + p_func->name + "(";
  1060. for (int i = 0; i < p_func->arguments.size(); i++) {
  1061. if (i > 0)
  1062. arghint += ", ";
  1063. else
  1064. arghint += " ";
  1065. if (i == p_argidx) {
  1066. arghint += String::chr(0xFFFF);
  1067. }
  1068. arghint += p_func->arguments[i].operator String();
  1069. int deffrom = p_func->arguments.size() - p_func->default_values.size();
  1070. if (i >= deffrom) {
  1071. int defidx = deffrom - i;
  1072. if (defidx >= 0 && defidx < p_func->default_values.size()) {
  1073. if (p_func->default_values[defidx]->type == GDParser::Node::TYPE_OPERATOR) {
  1074. const GDParser::OperatorNode *op = static_cast<const GDParser::OperatorNode *>(p_func->default_values[defidx]);
  1075. if (op->op == GDParser::OperatorNode::OP_ASSIGN) {
  1076. const GDParser::ConstantNode *cn = static_cast<const GDParser::ConstantNode *>(op->arguments[1]);
  1077. arghint += "=" + cn->value.get_construct_string();
  1078. }
  1079. } else {
  1080. }
  1081. }
  1082. }
  1083. if (i == p_argidx) {
  1084. arghint += String::chr(0xFFFF);
  1085. }
  1086. }
  1087. if (p_func->arguments.size() > 0)
  1088. arghint += " ";
  1089. arghint += ")";
  1090. }
  1091. 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) {
  1092. //print_line("find type arguments?");
  1093. if (id.type == Variant::INPUT_EVENT && String(p_method) == "is_action" && p_argidx == 0) {
  1094. List<PropertyInfo> pinfo;
  1095. Globals::get_singleton()->get_property_list(&pinfo);
  1096. for (List<PropertyInfo>::Element *E = pinfo.front(); E; E = E->next()) {
  1097. const PropertyInfo &pi = E->get();
  1098. if (!pi.name.begins_with("input/"))
  1099. continue;
  1100. String name = pi.name.substr(pi.name.find("/") + 1, pi.name.length());
  1101. result.insert("\"" + name + "\"");
  1102. }
  1103. } else if (id.type == Variant::OBJECT && id.obj_type != StringName()) {
  1104. MethodBind *m = ObjectTypeDB::get_method(id.obj_type, p_method);
  1105. if (!m) {
  1106. //not in static method, see script
  1107. //print_line("not in static: "+String(p_method));
  1108. Ref<GDScript> on_script;
  1109. if (id.value.get_type()) {
  1110. Object *obj = id.value;
  1111. if (obj) {
  1112. GDScript *scr = obj->cast_to<GDScript>();
  1113. if (scr) {
  1114. while (scr) {
  1115. for (const Map<StringName, GDFunction *>::Element *E = scr->get_member_functions().front(); E; E = E->next()) {
  1116. if (E->get()->is_static() && p_method == E->get()->get_name()) {
  1117. arghint = "static func " + String(p_method) + "(";
  1118. for (int i = 0; i < E->get()->get_argument_count(); i++) {
  1119. if (i > 0)
  1120. arghint += ", ";
  1121. else
  1122. arghint += " ";
  1123. if (i == p_argidx) {
  1124. arghint += String::chr(0xFFFF);
  1125. }
  1126. arghint += "var " + E->get()->get_argument_name(i);
  1127. int deffrom = E->get()->get_argument_count() - E->get()->get_default_argument_count();
  1128. if (i >= deffrom) {
  1129. int defidx = deffrom - i;
  1130. if (defidx >= 0 && defidx < E->get()->get_default_argument_count()) {
  1131. arghint += "=" + E->get()->get_default_argument(defidx).get_construct_string();
  1132. }
  1133. }
  1134. if (i == p_argidx) {
  1135. arghint += String::chr(0xFFFF);
  1136. }
  1137. }
  1138. arghint += ")";
  1139. return; //found
  1140. }
  1141. }
  1142. if (scr->get_base().is_valid())
  1143. scr = scr->get_base().ptr();
  1144. else
  1145. scr = NULL;
  1146. }
  1147. } else {
  1148. on_script = obj->get_script();
  1149. }
  1150. }
  1151. }
  1152. //print_line("but it has a script?");
  1153. if (!on_script.is_valid() && id.script.is_valid()) {
  1154. //print_line("yes");
  1155. on_script = id.script;
  1156. }
  1157. if (on_script.is_valid()) {
  1158. GDScript *scr = on_script.ptr();
  1159. if (scr) {
  1160. while (scr) {
  1161. String code = scr->get_source_code();
  1162. //print_line("has source code!");
  1163. if (code != "") {
  1164. //if there is code, parse it. This way is slower but updates in real-time
  1165. GDParser p;
  1166. //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);
  1167. Error err = p.parse(scr->get_source_code(), scr->get_path().get_base_dir(), true, "", false);
  1168. if (err == OK) {
  1169. //print_line("checking the functions...");
  1170. //only if ok, otherwise use what is cached on the script
  1171. //GDParser::ClassNode *base = p.
  1172. const GDParser::Node *root = p.get_parse_tree();
  1173. ERR_FAIL_COND(root->type != GDParser::Node::TYPE_CLASS);
  1174. const GDParser::ClassNode *cl = static_cast<const GDParser::ClassNode *>(root);
  1175. const GDParser::FunctionNode *func = NULL;
  1176. bool st = false;
  1177. for (int i = 0; i < cl->functions.size(); i++) {
  1178. //print_line(String(cl->functions[i]->name)+" vs "+String(p_method));
  1179. if (cl->functions[i]->name == p_method) {
  1180. func = cl->functions[i];
  1181. }
  1182. }
  1183. for (int i = 0; i < cl->static_functions.size(); i++) {
  1184. //print_line(String(cl->static_functions[i]->name)+" vs "+String(p_method));
  1185. if (cl->static_functions[i]->name == p_method) {
  1186. func = cl->static_functions[i];
  1187. st = true;
  1188. }
  1189. }
  1190. if (func) {
  1191. arghint = "func " + String(p_method) + "(";
  1192. if (st)
  1193. arghint = "static " + arghint;
  1194. for (int i = 0; i < func->arguments.size(); i++) {
  1195. if (i > 0)
  1196. arghint += ", ";
  1197. else
  1198. arghint += " ";
  1199. if (i == p_argidx) {
  1200. arghint += String::chr(0xFFFF);
  1201. }
  1202. arghint += "var " + String(func->arguments[i]);
  1203. int deffrom = func->arguments.size() - func->default_values.size();
  1204. if (i >= deffrom) {
  1205. int defidx = deffrom - i;
  1206. if (defidx >= 0 && defidx < func->default_values.size() && func->default_values[defidx]->type == GDParser::Node::TYPE_OPERATOR) {
  1207. const GDParser::OperatorNode *op = static_cast<const GDParser::OperatorNode *>(func->default_values[defidx]);
  1208. if (op->op == GDParser::OperatorNode::OP_ASSIGN) {
  1209. const GDParser::ConstantNode *cn = static_cast<const GDParser::ConstantNode *>(op->arguments[1]);
  1210. arghint += "=" + cn->value.get_construct_string();
  1211. }
  1212. }
  1213. }
  1214. if (i == p_argidx) {
  1215. arghint += String::chr(0xFFFF);
  1216. }
  1217. }
  1218. arghint += " )";
  1219. return;
  1220. }
  1221. } else {
  1222. //print_line("failed parsing?");
  1223. code = "";
  1224. }
  1225. }
  1226. if (code == "") {
  1227. for (const Map<StringName, GDFunction *>::Element *E = scr->get_member_functions().front(); E; E = E->next()) {
  1228. if (p_method == E->get()->get_name()) {
  1229. arghint = "func " + String(p_method) + "(";
  1230. for (int i = 0; i < E->get()->get_argument_count(); i++) {
  1231. if (i > 0)
  1232. arghint += ", ";
  1233. else
  1234. arghint += " ";
  1235. if (i == p_argidx) {
  1236. arghint += String::chr(0xFFFF);
  1237. }
  1238. arghint += "var " + E->get()->get_argument_name(i);
  1239. int deffrom = E->get()->get_argument_count() - E->get()->get_default_argument_count();
  1240. if (i >= deffrom) {
  1241. int defidx = deffrom - i;
  1242. if (defidx >= 0 && defidx < E->get()->get_default_argument_count()) {
  1243. arghint += "=" + E->get()->get_default_argument(defidx).get_construct_string();
  1244. }
  1245. }
  1246. if (i == p_argidx) {
  1247. arghint += String::chr(0xFFFF);
  1248. }
  1249. }
  1250. arghint += ")";
  1251. return; //found
  1252. }
  1253. }
  1254. #if 0
  1255. //use class directly, no code was found
  1256. if (!isfunction) {
  1257. for (const Map<StringName,Variant>::Element *E=scr->get_constants().front();E;E=E->next()) {
  1258. options.insert(E->key());
  1259. }
  1260. }
  1261. for (const Map<StringName,GDFunction>::Element *E=scr->get_member_functions().front();E;E=E->next()) {
  1262. options.insert(String(E->key())+"(");
  1263. }
  1264. for (const Set<StringName>::Element *E=scr->get_members().front();E;E=E->next()) {
  1265. options.insert(E->get());
  1266. }
  1267. #endif
  1268. }
  1269. if (scr->get_base().is_valid())
  1270. scr = scr->get_base().ptr();
  1271. else
  1272. scr = NULL;
  1273. }
  1274. }
  1275. }
  1276. } else {
  1277. //regular method
  1278. if (p_method.operator String() == "connect" || (p_method.operator String() == "emit_signal" && p_argidx == 0)) {
  1279. if (p_argidx == 0) {
  1280. List<MethodInfo> sigs;
  1281. ObjectTypeDB::get_signal_list(id.obj_type, &sigs);
  1282. for (List<MethodInfo>::Element *E = sigs.front(); E; E = E->next()) {
  1283. result.insert("\"" + E->get().name + "\"");
  1284. }
  1285. }
  1286. /*if (p_argidx==2) {
  1287. ERR_FAIL_COND(p_node->type!=GDParser::Node::TYPE_OPERATOR);
  1288. const GDParser::OperatorNode *op=static_cast<const GDParser::OperatorNode *>(p_node);
  1289. if (op->arguments.size()>)
  1290. }*/
  1291. } else {
  1292. if (p_argidx == 0 && (String(p_method) == "get_node" || String(p_method) == "has_node") && ObjectTypeDB::is_type(id.obj_type, "Node")) {
  1293. List<PropertyInfo> props;
  1294. Globals::get_singleton()->get_property_list(&props);
  1295. for (List<PropertyInfo>::Element *E = props.front(); E; E = E->next()) {
  1296. String s = E->get().name;
  1297. if (!s.begins_with("autoload/"))
  1298. continue;
  1299. // print_line("found "+s);
  1300. String name = s.get_slice("/", 1);
  1301. result.insert("\"/root/" + name + "\"");
  1302. }
  1303. }
  1304. Object *obj = id.value;
  1305. if (obj) {
  1306. List<String> options;
  1307. obj->get_argument_options(p_method, p_argidx, &options);
  1308. for (List<String>::Element *E = options.front(); E; E = E->next()) {
  1309. result.insert(E->get());
  1310. }
  1311. }
  1312. }
  1313. arghint = _get_visual_datatype(m->get_argument_info(-1), false) + " " + p_method.operator String() + String("(");
  1314. for (int i = 0; i < m->get_argument_count(); i++) {
  1315. if (i > 0)
  1316. arghint += ", ";
  1317. else
  1318. arghint += " ";
  1319. if (i == p_argidx) {
  1320. arghint += String::chr(0xFFFF);
  1321. }
  1322. String n = m->get_argument_info(i).name;
  1323. int dp = n.find(":");
  1324. if (dp != -1)
  1325. n = n.substr(0, dp);
  1326. arghint += _get_visual_datatype(m->get_argument_info(i)) + " " + n;
  1327. int deffrom = m->get_argument_count() - m->get_default_argument_count();
  1328. if (i >= deffrom) {
  1329. int defidx = i - deffrom;
  1330. if (defidx >= 0 && defidx < m->get_default_argument_count()) {
  1331. Variant v = m->get_default_argument(i);
  1332. arghint += "=" + v.get_construct_string();
  1333. }
  1334. }
  1335. if (i == p_argidx) {
  1336. arghint += String::chr(0xFFFF);
  1337. }
  1338. }
  1339. if (m->get_argument_count() > 0)
  1340. arghint += " ";
  1341. arghint += ")";
  1342. }
  1343. }
  1344. }
  1345. static void _find_call_arguments(GDCompletionContext &context, const GDParser::Node *p_node, int p_line, int p_argidx, Set<String> &result, String &arghint) {
  1346. if (!p_node || p_node->type != GDParser::Node::TYPE_OPERATOR) {
  1347. return;
  1348. }
  1349. const GDParser::OperatorNode *op = static_cast<const GDParser::OperatorNode *>(p_node);
  1350. if (op->op != GDParser::OperatorNode::OP_CALL) {
  1351. return;
  1352. }
  1353. if (op->arguments[0]->type == GDParser::Node::TYPE_BUILT_IN_FUNCTION) {
  1354. //complete built-in function
  1355. const GDParser::BuiltInFunctionNode *fn = static_cast<const GDParser::BuiltInFunctionNode *>(op->arguments[0]);
  1356. MethodInfo mi = GDFunctions::get_info(fn->function);
  1357. arghint = _get_visual_datatype(mi.return_val, false) + " " + GDFunctions::get_func_name(fn->function) + String("(");
  1358. for (int i = 0; i < mi.arguments.size(); i++) {
  1359. if (i > 0)
  1360. arghint += ", ";
  1361. else
  1362. arghint += " ";
  1363. if (i == p_argidx) {
  1364. arghint += String::chr(0xFFFF);
  1365. }
  1366. arghint += _get_visual_datatype(mi.arguments[i]) + " " + mi.arguments[i].name;
  1367. if (i == p_argidx) {
  1368. arghint += String::chr(0xFFFF);
  1369. }
  1370. }
  1371. if (mi.arguments.size() > 0)
  1372. arghint += " ";
  1373. arghint += ")";
  1374. } else if (op->arguments[0]->type == GDParser::Node::TYPE_TYPE) {
  1375. //complete constructor
  1376. const GDParser::TypeNode *tn = static_cast<const GDParser::TypeNode *>(op->arguments[0]);
  1377. List<MethodInfo> mil;
  1378. Variant::get_constructor_list(tn->vtype, &mil);
  1379. for (List<MethodInfo>::Element *E = mil.front(); E; E = E->next()) {
  1380. MethodInfo mi = E->get();
  1381. if (mi.arguments.size() == 0)
  1382. continue;
  1383. if (E->prev())
  1384. arghint += "\n";
  1385. arghint += Variant::get_type_name(tn->vtype) + " " + Variant::get_type_name(tn->vtype) + String("(");
  1386. for (int i = 0; i < mi.arguments.size(); i++) {
  1387. if (i > 0)
  1388. arghint += ", ";
  1389. else
  1390. arghint += " ";
  1391. if (i == p_argidx) {
  1392. arghint += String::chr(0xFFFF);
  1393. }
  1394. arghint += _get_visual_datatype(mi.arguments[i]) + " " + mi.arguments[i].name;
  1395. if (i == p_argidx) {
  1396. arghint += String::chr(0xFFFF);
  1397. }
  1398. }
  1399. if (mi.arguments.size() > 0)
  1400. arghint += " ";
  1401. arghint += ")";
  1402. }
  1403. } else if (op->arguments.size() >= 2 && op->arguments[1]->type == GDParser::Node::TYPE_IDENTIFIER) {
  1404. //make sure identifier exists...
  1405. const GDParser::IdentifierNode *id = static_cast<const GDParser::IdentifierNode *>(op->arguments[1]);
  1406. if (op->arguments[0]->type == GDParser::Node::TYPE_SELF) {
  1407. //self, look up
  1408. for (int i = 0; i < context._class->static_functions.size(); i++) {
  1409. if (context._class->static_functions[i]->name == id->name) {
  1410. _make_function_hint(context._class->static_functions[i], p_argidx, arghint);
  1411. return;
  1412. }
  1413. }
  1414. if (context.function && !context.function->_static) {
  1415. for (int i = 0; i < context._class->functions.size(); i++) {
  1416. if (context._class->functions[i]->name == id->name) {
  1417. _make_function_hint(context._class->functions[i], p_argidx, arghint);
  1418. return;
  1419. }
  1420. }
  1421. }
  1422. Ref<Reference> base = _get_parent_class(context);
  1423. while (true) {
  1424. Ref<GDScript> script = base;
  1425. Ref<GDNativeClass> nc = base;
  1426. if (script.is_valid()) {
  1427. for (const Map<StringName, GDFunction *>::Element *E = script->get_member_functions().front(); E; E = E->next()) {
  1428. if (E->key() == id->name) {
  1429. if (context.function && context.function->_static && !E->get()->is_static())
  1430. continue;
  1431. arghint = "func " + id->name.operator String() + String("(");
  1432. for (int i = 0; i < E->get()->get_argument_count(); i++) {
  1433. if (i > 0)
  1434. arghint += ", ";
  1435. else
  1436. arghint += " ";
  1437. if (i == p_argidx) {
  1438. arghint += String::chr(0xFFFF);
  1439. }
  1440. arghint += E->get()->get_argument_name(i);
  1441. int deffrom = E->get()->get_argument_count() - E->get()->get_default_argument_count();
  1442. if (i >= deffrom) {
  1443. int defidx = deffrom - i;
  1444. if (defidx >= 0 && defidx < E->get()->get_default_argument_count()) {
  1445. arghint += "=" + E->get()->get_default_argument(defidx).get_construct_string();
  1446. }
  1447. }
  1448. if (i == p_argidx) {
  1449. arghint += String::chr(0xFFFF);
  1450. }
  1451. }
  1452. if (E->get()->get_argument_count() > 0)
  1453. arghint += " ";
  1454. arghint += ")";
  1455. return;
  1456. }
  1457. }
  1458. base = script->get_base();
  1459. if (base.is_null())
  1460. base = script->get_native();
  1461. } else if (nc.is_valid()) {
  1462. if (context.function && !context.function->_static) {
  1463. GDCompletionIdentifier ci;
  1464. ci.type = Variant::OBJECT;
  1465. ci.obj_type = nc->get_name();
  1466. if (!context._class->owner)
  1467. ci.value = context.base;
  1468. _find_type_arguments(p_node, p_line, id->name, ci, p_argidx, result, arghint);
  1469. //guess type..
  1470. /*
  1471. List<MethodInfo> methods;
  1472. ObjectTypeDB::get_method_list(type,&methods);
  1473. for(List<MethodInfo>::Element *E=methods.front();E;E=E->next()) {
  1474. //if (E->get().arguments.size())
  1475. // result.insert(E->get().name+"(");
  1476. //else
  1477. // result.insert(E->get().name+"()");
  1478. }*/
  1479. }
  1480. break;
  1481. } else
  1482. break;
  1483. }
  1484. } else {
  1485. //indexed lookup
  1486. GDCompletionIdentifier ci;
  1487. if (_guess_expression_type(context, op->arguments[0], p_line, ci)) {
  1488. _find_type_arguments(p_node, p_line, id->name, ci, p_argidx, result, arghint);
  1489. return;
  1490. }
  1491. }
  1492. }
  1493. #if 0
  1494. bool _static=context.function->_static;
  1495. for(int i=0;i<context._class->static_functions.size();i++) {
  1496. if (context._class->static_functions[i]->arguments.size())
  1497. result.insert(context._class->static_functions[i]->name.operator String()+"(");
  1498. else
  1499. result.insert(context._class->static_functions[i]->name.operator String()+"()");
  1500. }
  1501. if (!p_static) {
  1502. for(int i=0;i<context._class->functions.size();i++) {
  1503. if (context._class->functions[i]->arguments.size())
  1504. result.insert(context._class->functions[i]->name.operator String()+"(");
  1505. else
  1506. result.insert(context._class->functions[i]->name.operator String()+"()");
  1507. }
  1508. }
  1509. Ref<Reference> base = _get_parent_class(context);
  1510. while(true) {
  1511. Ref<GDScript> script = base;
  1512. Ref<GDNativeClass> nc = base;
  1513. if (script.is_valid()) {
  1514. if (!p_static && !p_only_functions) {
  1515. for (const Set<StringName>::Element *E=script->get_members().front();E;E=E->next()) {
  1516. result.insert(E->get().operator String());
  1517. }
  1518. }
  1519. if (!p_only_functions) {
  1520. for (const Map<StringName,Variant>::Element *E=script->get_constants().front();E;E=E->next()) {
  1521. result.insert(E->key().operator String());
  1522. }
  1523. }
  1524. for (const Map<StringName,GDFunction>::Element *E=script->get_member_functions().front();E;E=E->next()) {
  1525. if (!p_static || E->get().is_static()) {
  1526. if (E->get().get_argument_count())
  1527. result.insert(E->key().operator String()+"(");
  1528. else
  1529. result.insert(E->key().operator String()+"()");
  1530. }
  1531. }
  1532. if (!p_only_functions) {
  1533. for (const Map<StringName,Ref<GDScript> >::Element *E=script->get_subclasses().front();E;E=E->next()) {
  1534. result.insert(E->key().operator String());
  1535. }
  1536. }
  1537. base=script->get_base();
  1538. if (base.is_null())
  1539. base=script->get_native();
  1540. } else if (nc.is_valid()) {
  1541. if (!p_only_functions) {
  1542. StringName type = nc->get_name();
  1543. List<String> constants;
  1544. ObjectTypeDB::get_integer_constant_list(type,&constants);
  1545. for(List<String>::Element *E=constants.front();E;E=E->next()) {
  1546. result.insert(E->get());
  1547. }
  1548. List<MethodInfo> methods;
  1549. ObjectTypeDB::get_method_list(type,&methods);
  1550. for(List<MethodInfo>::Element *E=methods.front();E;E=E->next()) {
  1551. if (E->get().arguments.size())
  1552. result.insert(E->get().name+"(");
  1553. else
  1554. result.insert(E->get().name+"()");
  1555. }
  1556. }
  1557. break;
  1558. } else
  1559. break;
  1560. }
  1561. for(int i=0;i<GDFunctions::FUNC_MAX;i++) {
  1562. result.insert(GDFunctions::get_func_name(GDFunctions::Function(i)));
  1563. }
  1564. #endif
  1565. }
  1566. Error GDScriptLanguage::complete_code(const String &p_code, const String &p_base_path, Object *p_owner, List<String> *r_options, String &r_call_hint) {
  1567. GDParser p;
  1568. Error err = p.parse(p_code, p_base_path, false, "", true);
  1569. bool isfunction = false;
  1570. Set<String> options;
  1571. GDCompletionContext context;
  1572. context._class = p.get_completion_class();
  1573. context.block = p.get_completion_block();
  1574. context.function = p.get_completion_function();
  1575. context.base = p_owner;
  1576. context.base_path = p_base_path;
  1577. switch (p.get_completion_type()) {
  1578. case GDParser::COMPLETION_NONE: {
  1579. } break;
  1580. case GDParser::COMPLETION_BUILT_IN_TYPE_CONSTANT: {
  1581. List<StringName> constants;
  1582. Variant::get_numeric_constants_for_type(p.get_completion_built_in_constant(), &constants);
  1583. for (List<StringName>::Element *E = constants.front(); E; E = E->next()) {
  1584. options.insert(E->get().operator String());
  1585. }
  1586. } break;
  1587. case GDParser::COMPLETION_FUNCTION:
  1588. isfunction = true;
  1589. case GDParser::COMPLETION_IDENTIFIER: {
  1590. _find_identifiers(context, p.get_completion_line(), isfunction, options);
  1591. } break;
  1592. case GDParser::COMPLETION_PARENT_FUNCTION: {
  1593. } break;
  1594. case GDParser::COMPLETION_METHOD:
  1595. isfunction = true;
  1596. case GDParser::COMPLETION_INDEX: {
  1597. const GDParser::Node *node = p.get_completion_node();
  1598. if (node->type != GDParser::Node::TYPE_OPERATOR)
  1599. break;
  1600. GDCompletionIdentifier t;
  1601. if (_guess_expression_type(context, static_cast<const GDParser::OperatorNode *>(node)->arguments[0], p.get_completion_line(), t)) {
  1602. if (t.type == Variant::OBJECT && t.obj_type == "GDNativeClass") {
  1603. //native enum
  1604. Ref<GDNativeClass> gdn = t.value;
  1605. if (gdn.is_valid()) {
  1606. StringName cn = gdn->get_name();
  1607. List<String> cnames;
  1608. ObjectTypeDB::get_integer_constant_list(cn, &cnames);
  1609. for (List<String>::Element *E = cnames.front(); E; E = E->next()) {
  1610. options.insert(E->get());
  1611. }
  1612. }
  1613. } else if (t.type == Variant::OBJECT && t.obj_type != StringName()) {
  1614. Ref<GDScript> on_script;
  1615. if (t.value.get_type()) {
  1616. Object *obj = t.value;
  1617. if (obj) {
  1618. GDScript *scr = obj->cast_to<GDScript>();
  1619. if (scr) {
  1620. while (scr) {
  1621. if (!isfunction) {
  1622. for (const Map<StringName, Variant>::Element *E = scr->get_constants().front(); E; E = E->next()) {
  1623. options.insert(E->key());
  1624. }
  1625. }
  1626. for (const Map<StringName, GDFunction *>::Element *E = scr->get_member_functions().front(); E; E = E->next()) {
  1627. if (E->get()->is_static())
  1628. options.insert(E->key());
  1629. }
  1630. if (scr->get_base().is_valid())
  1631. scr = scr->get_base().ptr();
  1632. else
  1633. scr = NULL;
  1634. }
  1635. } else {
  1636. on_script = obj->get_script();
  1637. }
  1638. }
  1639. }
  1640. if (!on_script.is_valid() && t.script.is_valid()) {
  1641. on_script = t.script;
  1642. }
  1643. if (on_script.is_valid()) {
  1644. GDScript *scr = on_script.ptr();
  1645. if (scr) {
  1646. while (scr) {
  1647. String code = scr->get_source_code();
  1648. if (code != "") {
  1649. //if there is code, parse it. This way is slower but updates in real-time
  1650. GDParser p;
  1651. Error err = p.parse(scr->get_source_code(), scr->get_path().get_base_dir(), true, "", false);
  1652. if (err == OK) {
  1653. //only if ok, otherwise use what is cached on the script
  1654. //GDParser::ClassNode *base = p.
  1655. const GDParser::Node *root = p.get_parse_tree();
  1656. ERR_FAIL_COND_V(root->type != GDParser::Node::TYPE_CLASS, ERR_PARSE_ERROR);
  1657. const GDParser::ClassNode *cl = static_cast<const GDParser::ClassNode *>(root);
  1658. for (int i = 0; i < cl->functions.size(); i++) {
  1659. if (cl->functions[i]->arguments.size())
  1660. options.insert(String(cl->functions[i]->name) + "(");
  1661. else
  1662. options.insert(String(cl->functions[i]->name) + "()");
  1663. }
  1664. for (int i = 0; i < cl->static_functions.size(); i++) {
  1665. if (cl->static_functions[i]->arguments.size())
  1666. options.insert(String(cl->static_functions[i]->name) + "(");
  1667. else
  1668. options.insert(String(cl->static_functions[i]->name) + "()");
  1669. }
  1670. if (!isfunction) {
  1671. for (int i = 0; i < cl->variables.size(); i++) {
  1672. options.insert(String(cl->variables[i].identifier));
  1673. }
  1674. for (int i = 0; i < cl->constant_expressions.size(); i++) {
  1675. options.insert(String(cl->constant_expressions[i].identifier));
  1676. }
  1677. }
  1678. } else {
  1679. code = ""; //well, then no code
  1680. }
  1681. }
  1682. if (code == "") {
  1683. //use class directly, no code was found
  1684. if (!isfunction) {
  1685. for (const Map<StringName, Variant>::Element *E = scr->get_constants().front(); E; E = E->next()) {
  1686. options.insert(E->key());
  1687. }
  1688. }
  1689. for (const Map<StringName, GDFunction *>::Element *E = scr->get_member_functions().front(); E; E = E->next()) {
  1690. if (E->get()->get_argument_count())
  1691. options.insert(String(E->key()) + "()");
  1692. else
  1693. options.insert(String(E->key()) + "(");
  1694. }
  1695. for (const Set<StringName>::Element *E = scr->get_members().front(); E; E = E->next()) {
  1696. options.insert(E->get());
  1697. }
  1698. }
  1699. if (scr->get_base().is_valid())
  1700. scr = scr->get_base().ptr();
  1701. else
  1702. scr = NULL;
  1703. }
  1704. }
  1705. }
  1706. if (!isfunction) {
  1707. ObjectTypeDB::get_integer_constant_list(t.obj_type, r_options);
  1708. }
  1709. List<MethodInfo> mi;
  1710. ObjectTypeDB::get_method_list(t.obj_type, &mi);
  1711. for (List<MethodInfo>::Element *E = mi.front(); E; E = E->next()) {
  1712. if (E->get().name.begins_with("_"))
  1713. continue;
  1714. if (E->get().arguments.size())
  1715. options.insert(E->get().name + "(");
  1716. else
  1717. options.insert(E->get().name + "()");
  1718. }
  1719. } else {
  1720. if (t.type == Variant::INPUT_EVENT) {
  1721. //this is hardcoded otherwise it's not obvious
  1722. Set<String> exclude;
  1723. for (int i = 0; i < InputEvent::TYPE_MAX; i++) {
  1724. InputEvent ie;
  1725. ie.type = InputEvent::Type(i);
  1726. static const char *evnames[] = {
  1727. "# Common",
  1728. "# Key",
  1729. "# MouseMotion",
  1730. "# MouseButton",
  1731. "# JoystickMotion",
  1732. "# JoystickButton",
  1733. "# ScreenTouch",
  1734. "# ScreenDrag",
  1735. "# Action"
  1736. };
  1737. r_options->push_back(evnames[i]);
  1738. Variant v = ie;
  1739. if (i == 0) {
  1740. List<MethodInfo> mi;
  1741. v.get_method_list(&mi);
  1742. for (List<MethodInfo>::Element *E = mi.front(); E; E = E->next()) {
  1743. r_options->push_back(E->get().name + "(");
  1744. }
  1745. }
  1746. List<PropertyInfo> pi;
  1747. v.get_property_list(&pi);
  1748. for (List<PropertyInfo>::Element *E = pi.front(); E; E = E->next()) {
  1749. if (i == 0)
  1750. exclude.insert(E->get().name);
  1751. else if (exclude.has(E->get().name))
  1752. continue;
  1753. r_options->push_back(E->get().name);
  1754. }
  1755. }
  1756. return OK;
  1757. } else {
  1758. if (t.value.get_type() == Variant::NIL) {
  1759. Variant::CallError ce;
  1760. t.value = Variant::construct(t.type, NULL, 0, ce);
  1761. }
  1762. if (!isfunction) {
  1763. List<PropertyInfo> pl;
  1764. t.value.get_property_list(&pl);
  1765. for (List<PropertyInfo>::Element *E = pl.front(); E; E = E->next()) {
  1766. if (E->get().name.find("/") == -1)
  1767. options.insert(E->get().name);
  1768. }
  1769. }
  1770. List<MethodInfo> mi;
  1771. t.value.get_method_list(&mi);
  1772. for (List<MethodInfo>::Element *E = mi.front(); E; E = E->next()) {
  1773. if (E->get().arguments.size())
  1774. options.insert(E->get().name + "(");
  1775. else
  1776. options.insert(E->get().name + "()");
  1777. }
  1778. }
  1779. }
  1780. }
  1781. } break;
  1782. case GDParser::COMPLETION_CALL_ARGUMENTS: {
  1783. _find_call_arguments(context, p.get_completion_node(), p.get_completion_line(), p.get_completion_argument_index(), options, r_call_hint);
  1784. } break;
  1785. case GDParser::COMPLETION_VIRTUAL_FUNC: {
  1786. GDCompletionIdentifier cid = _get_native_class(context);
  1787. if (cid.obj_type != StringName()) {
  1788. List<MethodInfo> vm;
  1789. ObjectTypeDB::get_virtual_methods(cid.obj_type, &vm);
  1790. for (List<MethodInfo>::Element *E = vm.front(); E; E = E->next()) {
  1791. MethodInfo &mi = E->get();
  1792. String m = mi.name;
  1793. if (m.find(":") != -1)
  1794. m = m.substr(0, m.find(":"));
  1795. m += "(";
  1796. if (mi.arguments.size()) {
  1797. for (int i = 0; i < mi.arguments.size(); i++) {
  1798. if (i > 0)
  1799. m += ", ";
  1800. String n = mi.arguments[i].name;
  1801. if (n.find(":") != -1)
  1802. n = n.substr(0, n.find(":"));
  1803. m += n;
  1804. }
  1805. }
  1806. m += "):";
  1807. options.insert(m);
  1808. }
  1809. }
  1810. } break;
  1811. }
  1812. for (Set<String>::Element *E = options.front(); E; E = E->next()) {
  1813. r_options->push_back(E->get());
  1814. }
  1815. return OK;
  1816. }
  1817. #else
  1818. Error GDScriptLanguage::complete_code(const String &p_code, const String &p_base_path, Object *p_owner, List<String> *r_options, String &r_call_hint) {
  1819. return OK;
  1820. }
  1821. #endif
  1822. void GDScriptLanguage::auto_indent_code(String &p_code, int p_from_line, int p_to_line) const {
  1823. Vector<String> lines = p_code.split("\n");
  1824. List<int> indent_stack;
  1825. for (int i = 0; i < lines.size(); i++) {
  1826. String l = lines[i];
  1827. int tc = 0;
  1828. for (int j = 0; j < l.length(); j++) {
  1829. if (l[j] == ' ' || l[j] == '\t') {
  1830. tc++;
  1831. } else {
  1832. break;
  1833. }
  1834. }
  1835. String st = l.substr(tc, l.length()).strip_edges();
  1836. if (st == "" || st.begins_with("#"))
  1837. continue; //ignore!
  1838. int ilevel = 0;
  1839. if (indent_stack.size()) {
  1840. ilevel = indent_stack.back()->get();
  1841. }
  1842. if (tc > ilevel) {
  1843. indent_stack.push_back(tc);
  1844. } else if (tc < ilevel) {
  1845. while (indent_stack.size() && indent_stack.back()->get() > tc) {
  1846. indent_stack.pop_back();
  1847. }
  1848. if (indent_stack.size() && indent_stack.back()->get() != tc)
  1849. indent_stack.push_back(tc); //this is not right but gets the job done
  1850. }
  1851. if (i >= p_from_line) {
  1852. l = "";
  1853. for (int j = 0; j < indent_stack.size(); j++)
  1854. l += "\t";
  1855. l += st;
  1856. } else if (i > p_to_line) {
  1857. break;
  1858. }
  1859. //print_line(itos(indent_stack.size())+","+itos(tc)+": "+l);
  1860. lines[i] = l;
  1861. }
  1862. p_code = "";
  1863. for (int i = 0; i < lines.size(); i++) {
  1864. if (i > 0)
  1865. p_code += "\n";
  1866. p_code += lines[i];
  1867. }
  1868. }