gd_editor.cpp 85 KB

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