gd_editor.cpp 84 KB

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